代码之家  ›  专栏  ›  技术社区  ›  user84592

Kubernetesk8s如何创建服务URL?

  •  1
  • user84592  · 技术社区  · 6 年前

    我在学习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) 
    

    我创建pod和服务如下,hellocomm部署为nodeport,helloext部署为clusterip。hellocomm和helloext都是 春季启动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
    

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

    我目前的问题是,由于helloext是作为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
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Amrit    6 年前

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

    cluster ip只能在集群内部访问,因为它是一个私有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

        2
  •  1
  •   Nathan Ogden    6 年前

    如何创建一个入口并将其指向服务以在集群外部访问它?