问题描述
我有一个简单的复制控制器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.
推荐答案
可以使用 statefulsets (自1.9版以来已处于beta版).引用文档:使用kind: StatefulSet
时,
This can be implemented using statefulsets which is out of beta since version 1.9. Quoting the documentation: When using kind: StatefulSet
,
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-0
,nginx-1
,nginx-2
So in the example above, you would get nginx-0
,nginx-1
,nginx-2
这篇关于在Kubernetes中,使用复制控制器时如何设置Pod的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!