Kubernetes?k8s 如何制作服务 url?

use*_*592 5 kubernetes kubernetes-ingress

我正在学习k8s。我的问题是如何让k8s像minikube命令“minikube get service xxx --url”那样获取服务url呢?为什么我问是因为当 pod 关闭并再次启动/创建/启动时,不需要通过访问服务 url 来更改 url。当我将 pod 部署为 NodePort 时,我可以使用主机 IP 和端口访问 pod,但是如果重新启动/再次创建它,端口会发生变化。

我的情况如下图所示:我有

one master(172.16.100.91) and 
one node(hostname node3, 172.16.100.96) 
Run Code Online (Sandbox Code Playgroud)

我创建如下 pod 和服务,hellocomm 部署为 NodePort,helloext 部署为 ClusterIP。hellocomm 和 helloext 都是 spring boot hello world 应用程序。

docker build -t jshenmaster2/hellocomm:0.0.2 .
kubectl run hellocomm --image=jshenmaster2/hellocomm:0.0.2 --port=8080
kubectl expose deployment hellocomm --type NodePort

docker build -t jshenmaster2/helloext:0.0.1 .
kubectl run helloext --image=jshenmaster2/helloext:0.0.1 --port=8080
kubectl expose deployment helloext --type ClusterIP


[root@master2 shell]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
hellocomm    NodePort    10.108.175.143   <none>        8080:31666/TCP   8s        run=hellocomm
helloext     ClusterIP   10.102.5.44      <none>        8080/TCP         2m        run=helloext

[root@master2 hello]# kubectl get pods -o wide
NAME                         READY     STATUS    RESTARTS   AGE       IP              NODE
hellocomm-54584f59c5-7nxp4   1/1       Running   0          18m       192.168.136.2   node3
helloext-c455859cc-5zz4s     1/1       Running   0          21m       192.168.136.1   node3
Run Code Online (Sandbox Code Playgroud)

在上面,我的pod部署在node3(172.16.100.96),所以我可以通过172.16.100.96:31666/hello访问hellocomm,在这种情况下,很容易看到当node3宕机时,​​一个新的pod被创建/启动,端口也会发生变化。所以我的客户失去了连接。我不想要这个解决方案。

我目前的问题是 helloext 部署为 ClusteriP,它也是如上所示的服务。这是否意味着 ClusterIP 10.102.5.44 和端口 8080 将是服务 URL,http: //10.102.5.44:8080/hello ?

我是否需要再次通过 yaml 文件创建服务?与通过 yaml 文件针对命令创建的服务有什么区别?如果我必须通过 yaml 创建服务,如何编写以下 yaml 文件?

下面是我需要填写的yaml定义模板,如何填写?

apiVersion: v1
kind: Service
matadata:
  name: string         helloext      
  namespace: string    default
  labels:              
    - name: string     helloext
  annotations:
    - name: string     hello world   
spec:
  selector: []            ?
  type: string            ?
  clusterIP: string       anything I could give?  
  sessionAffinity: string  ? (yes or no) 
  ports:
  - name: string          helloext  
    protocol: string      tcp  
    port: int             8081? (port used by host machine)
    targetPort: int       8080? (spring boot uses 8080)
    nodePort: int         ? 
  status:                 since I am not using loadBalancer in deploymennt, I could forget this.  
    loadBalancer:
      ingress:
        ip: string
        hostname: string
Run Code Online (Sandbox Code Playgroud)

Amr*_*era 4

NodePort,顾名思义,直接在节点上(实际上是在集群中的所有节点上)打开一个端口,以便您可以访问您的服务。默认情况下它是随机的 - 这就是为什么当一个 Pod 死亡时,它会为你生成一个新的。但是,您也可以指定一个端口(此处为第 3 段) - 即使在重新创建 Pod 后您也可以访问同一端口。

clusterIP 只能在集群内部访问,因为它是私有 IP。这意味着,在默认情况下,您可以从集群内的另一个容器/节点访问此服务。您可以exec/ssh进入任何正在运行的容器/节点并尝试一下。

Yaml 文件可以进行版本控制、文档化、模板化(Helm)等。

检查https://kubernetes.io/docs/reference/ generated/kubernetes-api/v1.10/#servicespec-v1-core 了解每个字段的详细信息。

编辑:有关此处服务的更多详细信息:https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0