如何在statefulset中为kubernetes pod设置主机名 [英] How to set hostname for kubernetes pod in statefulset

查看:2042
本文介绍了如何在statefulset中为kubernetes pod设置主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用statefulset,并且旋转了多个Pod,但它们不是彼此的副本.我想设置Pod的主机名,并将这些主机名作为env变量传递给所有Pod,以便它们可以相互通信.

I am using statefulset and I spin up multiple pods but they are not replica of each other. I want to set the hostname of the pods and passing these hostname as a env variable to all the pods so that they can communicate with each other.

我尝试在Pod规范下使用主机名,但从未将主机名设置为指定的主机名.但是,它将主机名设置为podname-0.

I tried to use hostname under pod spec but hostname is never to set to specified hostname. However, it is set to hostname as podname-0.

# Source: testrep/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet 
metadata:
  name: orbiting-butterfly-testrep
  labels:
    app.kubernetes.io/name: testrep
    helm.sh/chart: testrep-0.1.0
    app.kubernetes.io/instance: orbiting-butterfly
    app.kubernetes.io/managed-by: Tiller
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: testrep
      app.kubernetes.io/instance: orbiting-butterfly
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: testrep
        app.kubernetes.io/instance: orbiting-butterfly
    spec:
      nodeSelector:
          testol: ad3
       hostname: test1
       containers:
        - name: testrep
          image: "test/database:v1"
          imagePullPolicy: IfNotPresent
          env:
           - name: DB_HOSTS
             value: test1,test2,test3

推荐答案

  1. 根据文档:

  1. As per documentation:

StatefulSet是用于管理有状态应用程序的工作负载API对象.

StatefulSet is the workload API object used to manage stateful applications.

管理一组Pod的部署和扩展,并保证这些Pod的顺序和唯一性.

Manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering and uniqueness of these Pods.

StatefulSet对于需要以下一项或多项操作的应用程序很有价值:

StatefulSets are valuable for applications that require one or more of the following:

  • 稳定的唯一网络标识符.
  • 稳定的持久存储.
  • 有序,优雅的部署和扩展.
  • 订购的自动滚动更新.
  • Stable, unique network identifiers.
  • Stable, persistent storage.
  • Ordered, graceful deployment and scaling.
  • Ordered, automated rolling updates.

Statefulset 限制:

Statefulset Limitations:

StatefulSet当前需要无头服务来负责Pod的网络标识.您负责创建此服务.

StatefulSets currently require a Headless Service to be responsible for the network identity of the Pods. You are responsible for creating this Service.

  • Pod身份

    StatefulSet Pod具有由序号,稳定的网络身份和稳定的存储组成的唯一身份.无论将身份(安排在哪个节点上),身份都将固定在Pod上. 对于具有N个副本的StatefulSet,将为StatefulSet中的每个Pod分配一个从0到N-1的整数序数,该序数在Set上是唯一的.

    StatefulSet Pods have a unique identity that is comprised of an ordinal, a stable network identity, and stable storage. The identity sticks to the Pod, regardless of which node it’s (re)scheduled on. For a StatefulSet with N replicas, each Pod in the StatefulSet will be assigned an integer ordinal, from 0 up through N-1, that is unique over the Set.

  • 稳定的网络ID

    StatefulSet中的每个Pod都从StatefulSet的名称和Pod的序数派生其主机名. 构造的主机名的模式为$(状态集名称)-$(普通). 上面的示例将创建三个名为web-0,web-1,web-2的Pod. StatefulSet可以使用无头服务来控制其Pod的域.此服务管理的域的格式为:$(服务名称).$(名称空间).svc.cluster.local,其中"cluster.local"是群集域.创建每个Pod时,它将获得一个匹配的DNS子域,格式为:$ {podname).$ {governing service domain},其中控制服务由StatefulSet上的serviceName字段定义.

    Each Pod in a StatefulSet derives its hostname from the name of the StatefulSet and the ordinal of the Pod. The pattern for the constructed hostname is $(statefulset name)-$(ordinal). The example above will create three Pods named web-0,web-1,web-2. A StatefulSet can use a Headless Service to control the domain of its Pods. The domain managed by this Service takes the form: $(service name).$(namespace).svc.cluster.local, where "cluster.local" is the cluster domain. As each Pod is created, it gets a matching DNS subdomain, taking the form: $(podname).$(governing service domain), where the governing service is defined by the serviceName field on the StatefulSet.

  • 注意:

    您负责创建负责Pod的网络标识的Headless服务.

    You are responsible for creating the Headless Service responsible for the network identity of the pods.

    因此,如 vjdhama 所述,请使用无头服务创建Statefulset.

    So as described by vjdhama Please create your Statefulset with Headless Service.

    您可以在文档中找到此示例:

    You can find this example in the docs:

    apiVersion: v1
    kind: Service
    metadata:
     name: nginx
     labels:
       app: nginx
    spec:
      ports:
      - port: 80
        name: web
      clusterIP: None
      selector:
        app: nginx
    
    
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: web
    spec:
      selector:
        matchLabels:
          app: nginx # has to match .spec.template.metadata.labels
      serviceName: "nginx"
      replicas: 3 # by default is 1
      template:
        metadata:
          labels:
            app: nginx # has to match .spec.selector.matchLabels
        spec:
          terminationGracePeriodSeconds: 10
          containers:
          - name: nginx
            image: k8s.gcr.io/nginx-slim:0.8
            ports:
            - containerPort: 80
    

    在这种情况下,Pod DNS和Pod主机名应分别为:

    In this scenario Pod DNS and Pod Hostnames should be respectively:

        Pod DNS
        web-{0..N-1}.nginx.default.svc.cluster.local
    
        Pod Hostname
        web-{0..N-1}
    
    
        NAME                                   READY   STATUS    RESTARTS   AGE     IP               
        pod/web-0                              1/1     Running   0          5m      192.168.148.78   
        pod/web-1                              1/1     Running   0          4m53s   192.168.148.79   
        pod/web-2                              1/1     Running   0          4m51s   192.168.148.80  
    

    从Pod角度看:

       root@web-2:# nslookup nginx
       Server:      10.96.0.10
       Address: 10.96.0.10#53
    
       Name:    nginx.default.svc.cluster.local 
       Address: 192.168.148.80
       Name:    nginx.default.svc.cluster.local
       Address: 192.168.148.78
       Name:    nginx.default.svc.cluster.local
       Address: 192.168.148.79
    

    因此,您可以使用Pod DNS分别呼叫各个Pod,例如:

    So you can call each of the respective pods using the Pod DNS, like:

    web-0.nginx.default.svc.cluster.local
    

    更新:

    从StatefulSet中公开单个pod.您可以在此处棘手的方法中找到. 利用Statefulset的上述优势:

    Exposing single pod from StatefulSet. You can find here tricky way. Using described above advantages of Statefulset:

    构造的主机名的模式为$(状态集名称)-$(普通).上面的示例将创建三个名为web-0,web-1,web-2的Pod.

    The pattern for the constructed hostname is $(statefulset name)-$(ordinal). The example above will create three Pods named web-0,web-1,web-2.

    举个例子:

    apiVersion: v1
    kind: Service
    metadata:
      name: app-0
    spec:
      type: LoadBalancer
      selector:
        statefulset.kubernetes.io/pod-name: web-0
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
    

    将为您做到这一点.

    希望获得帮助.

    这篇关于如何在statefulset中为kubernetes pod设置主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆