在Kubernetes中,使用复制控制器时如何设置Pod的名称? [英] In Kubernetes, how to set pods' names when using replication controllers?

查看:425
本文介绍了在Kubernetes中,使用复制控制器时如何设置Pod的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的复制控制器yaml文件,如下所示:

I have a simple replication controller yaml file which looks like this:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    spec:
      containers:
      - image: library/nginx:3.2
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
    metadata:
      labels:
        app: nginx

运行此复制控制器后,我将获得3个不同的Pod,其名称为"nginx-xxx",其中"xxx"代表字母和数字的随机字符串.

And after running this replication controller, I will get 3 different pods whose names are "nginx-xxx", where "xxx" represents a random string of letters and digits.

我想要的是为复制控制器创建的Pod指定名称,以便Pod的名称可以是"nginx-01","nginx-02","nginx-03".更进一步,例如,如果pod"nginx-02"由于某种原因而关闭,并且复制控制器将自动创建另一个nginx pod,并且我希望这个新的nginx pod的名称保持为"nginx-02".

What I want is to specify names for the pods created by the replication controller, so that the pods' name can be "nginx-01", "nginx-02", "nginx-03". And further more, for say if pod "nginx-02" is down for some reason, and replication controller will automatically create another nginx pod, and I want this new nginx pod's name to remain as "nginx-02".

我想知道这是否可能吗?预先感谢.

I wonder if this is possible? Thanks in advance.

推荐答案

可以使用

This can be implemented using statefulsets which is out of beta since version 1.9. Quoting the documentation: When using kind: StatefulSet,

Pods具有唯一的身份,由序数,稳定的网络身份和稳定的存储组成.身份将固定在Pod上,而不管它在(重新)安排在哪个节点上.

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.

StatefulSet中的每个Pod都从StatefulSet的名称和Pod的序数派生其主机名.构造的主机名的模式为$(statefulset name)-$(ordinal).

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).

因此在上面的示例中,您将获得nginx-0nginx-1nginx-2

So in the example above, you would get nginx-0,nginx-1,nginx-2

这篇关于在Kubernetes中,使用复制控制器时如何设置Pod的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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