通过服务器域访问Kubernetes服务

时间:2018-12-24 10:38:41

标签: kubernetes

所以我的服务器上有一个正在运行的集群。服务器正在运行ubuntu 18.06我使用kubeadmkubectlkubelet来建立集群。

我的目标简而言之:我想通过执行http://myserver.com/service来获得服务。

我对将服务公开到端口8080感到迷茫。当前结构如下:

  

31001:SERVICE:8080-> 8080:POD

因此,我需要将使用http://myserver.com/service传入的请求重定向到端口为31001的kubernetes service

当前情况:我只能通过服务器IP:6443访问群集。所有的pod,服务等都已启动并运行。

所以我的问题是:如何使服务在8080端口上或多或少公开?

按照以下要求:

  

kubectl get all --all-namespaces -o wide的输出

NAMESPACE     NAME                                                     READY   STATUS    RESTARTS   AGE     IP              NODE                         NOMINATED NODE   READINESS GATES
default       pod/articleservice-deployment-6d48989664-jbzs6           1/1     Running   0          5h56m   192.168.0.4     server.address.com           <none>           <none>
default       pod/cartservice-deployment-6b844f45b8-jz45h              1/1     Running   0          5h56m   192.168.0.5     server.address.com           <none>           <none>
default       pod/catalogservice-deployment-d4bd6984c-6qlqg            1/1     Running   0          5h56m   192.168.0.6     server.address.com           <none>           <none>
default       pod/customerservice-deployment-7d6f77fdbb-p42xj          1/1     Running   0          5h56m   192.168.0.7     server.address.com           <none>           <none>
kube-system   pod/calico-node-5rl9m                                    2/2     Running   0          5h58m   999.999.99.99   server.address.com           <none>           <none>
kube-system   pod/coredns-86c58d9df4-h64fg                             1/1     Running   0          6h10m   192.168.0.2     server.address.com           <none>           <none>
kube-system   pod/coredns-86c58d9df4-pwfj4                             1/1     Running   0          6h10m   192.168.0.3     server.address.com           <none>           <none>
kube-system   pod/etcd-server.address.net                              1/1     Running   0          6h9m    999.999.99.99   server.address.com           <none>           <none>
kube-system   pod/kube-apiserver-server.address.net                    1/1     Running   0          6h10m   999.999.99.99   server.address.com           <none>           <none>
kube-system   pod/kube-controller-manager-server.address.net           1/1     Running   0          6h9m    999.999.99.99   server.address.com           <none>           <none>
kube-system   pod/kube-proxy-xb2qc                                     1/1     Running   0          6h10m   999.999.99.99   server.address.com           <none>           <none>
kube-system   pod/kube-scheduler-server.address.net                    1/1     Running   0          6h9m    999.999.99.99   server.address.com           <none>           <none>

NAMESPACE     NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                          AGE     SELECTOR
default       service/articleservice    NodePort    10.97.125.155   <none>        31001:31001/TCP,5005:32001/TCP   5h57m   app=articleservice
default       service/cartservice       NodePort    10.99.42.169    <none>        31002:31002/TCP,5005:32002/TCP   5h57m   app=cartservice
default       service/catalogservice    NodePort    10.106.101.93   <none>        31003:31003/TCP,5005:32003/TCP   5h57m   app=catalogservice
default       service/customerservice   NodePort    10.106.2.159    <none>        31004:31004/TCP,5005:32004/TCP   5h57m   app=customerservice
default       service/kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP                          6h11m   <none>
kube-system   service/calico-typha      ClusterIP   10.96.242.31    <none>        5473/TCP                         5h58m   k8s-app=calico-typha
kube-system   service/kube-dns          ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP                    6h11m   k8s-app=kube-dns

NAMESPACE     NAME                         DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                 AGE     CONTAINERS                IMAGES                                                 SELECTOR
kube-system   daemonset.apps/calico-node   1         1         1       1            1           beta.kubernetes.io/os=linux   5h58m   calico-node,install-cni   quay.io/calico/node:v3.3.2,quay.io/calico/cni:v3.3.2   k8s-app=calico-node
kube-system   daemonset.apps/kube-proxy    1         1         1       1            1           <none>                        6h11m   kube-proxy                k8s.gcr.io/kube-proxy:v1.13.1                          k8s-app=kube-proxy

NAMESPACE     NAME                                         READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS        IMAGES                        SELECTOR
default       deployment.apps/articleservice-deployment    1/1     1            1           5h56m   articleservice    elps/articleservice:1.0.7     app=articleservice
default       deployment.apps/cartservice-deployment       1/1     1            1           5h56m   cartservice       elps/cartservice:1.0.7        app=cartservice
default       deployment.apps/catalogservice-deployment    1/1     1            1           5h56m   catalogservice    elps/catalogservice:1.0.7     app=catalogservice
default       deployment.apps/customerservice-deployment   1/1     1            1           5h56m   customerservice   elps/customerservice:1.0.7    app=customerservice
kube-system   deployment.apps/calico-typha                 0/0     0            0           5h58m   calico-typha      quay.io/calico/typha:v3.3.2   k8s-app=calico-typha
kube-system   deployment.apps/coredns                      2/2     2            2           6h11m   coredns           k8s.gcr.io/coredns:1.2.6      k8s-app=kube-dns

NAMESPACE     NAME                                                    DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES                        SELECTOR
default       replicaset.apps/articleservice-deployment-6d48989664    1         1         1       5h56m   articleservice    elps/articleservice:1.0.7     app=articleservice,pod-template-hash=6d48989664
default       replicaset.apps/cartservice-deployment-6b844f45b8       1         1         1       5h56m   cartservice       elps/cartservice:1.0.7        app=cartservice,pod-template-hash=6b844f45b8
default       replicaset.apps/catalogservice-deployment-d4bd6984c     1         1         1       5h56m   catalogservice    elps/catalogservice:1.0.7     app=catalogservice,pod-template-hash=d4bd6984c
default       replicaset.apps/customerservice-deployment-7d6f77fdbb   1         1         1       5h56m   customerservice   elps/customerservice:1.0.7    app=customerservice,pod-template-hash=7d6f77fdbb
kube-system   replicaset.apps/calico-typha-5fc4874c76                 0         0         0       5h58m   calico-typha      quay.io/calico/typha:v3.3.2   k8s-app=calico-typha,pod-template-hash=5fc4874c76
kube-system   replicaset.apps/coredns-86c58d9df4                      2         2         2       6h10m   coredns           k8s.gcr.io/coredns:1.2.6      k8s-app=kube-dns,pod-template-hash=86c58d9df4

1 个答案:

答案 0 :(得分:2)

Ingress是您所需要的。它是这样工作的:

   internet
        |
   [ Ingress ]
   --|-----|--
   [ Services ]

假设您有任何k8s部署,其标签为:app: deployment-01containerPort: 8888

因此,您需要创建:

  1. 服务(例如service-01)
  2. 入口(例如ingress-01)

如果您想通过链接http://myserver.com/service到达service-01,则您的Service YAML应该如下所示:

apiVersion: v1
kind: Service
metadata:
  name: service-01
spec:
  selector:
    app: deployment-01
  ports:
  - name: web
    protocol: TCP
    port: 8080
    targetPort: 8888

和入口:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-01
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myserver.com
    http:
      paths:
      - path: /service
        backend:
          serviceName: service-01
          servicePort: 8080

就是这样!

  

注意:假设您已经部署了Ingress   控制器,它正在工作。