gpt4 book ai didi

kubernetes - 如何在 statefulset 中为 kubernetes pod 设置主机名

转载 作者:行者123 更新时间:2023-12-02 11:43:09 27 4
gpt4 key购买 nike

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

我尝试在 pod 规范下使用主机名,但从未将主机名设置为指定的主机名。但是,它设置为主机名作为 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

最佳答案

  • 根据文档:

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

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



    StatefulSet 对于需要以下一项或多项的应用程序很有值(value):
  • 稳定、唯一的网络标识符 .
  • 稳定、持久的存储。
  • 有序、优雅的部署和扩展。
  • 有序的自动滚动更新。
  • 状态集 Limitations :

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

  • Pod Identity

    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.

  • Stable Network ID

    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 Service。

    所以如 vjdhama 所述请使用 Headless Service 创建您的 Statefulset。

    您可以在文档中找到此示例:
    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 主机名应分别为:
        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,例如:
    web-0.nginx.default.svc.cluster.local

    更新:

    从 StatefulSet 公开单个 pod。您可以找到 here棘手的方式。
    使用 Statefulset 的上述优点:

    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

    会为你做这件事。

    希望这有帮助。

    关于kubernetes - 如何在 statefulset 中为 kubernetes pod 设置主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668190/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com