Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Kubernetes, how to set pods' names when using replication controllers?

Tags:

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

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.

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.

like image 983
Mohan Yang Avatar asked Oct 27 '16 00:10

Mohan Yang


People also ask

How do I change the name of my pod in Kubernetes?

It's not possible to change the name of a running pod, as the API will not accept this. The random pod names come in fact you are using Kubernetes deployments, which are the default or most commonly used ways to run pods on Kubernetes.

What are used for associating pods with replica sets?

When a ReplicaSet needs to create new Pods, it uses its Pod template. A ReplicaSet is linked to its Pods via the Pods' metadata. ownerReferences field, which specifies what resource the current object is owned by.

Which field in replication controller object is used to specify the number of pods to be created?

Pod Selector spec. selector field is a label selector. A ReplicationController manages all the pods with labels that match the selector. It does not distinguish between pods that it created or deleted and pods that another person or process created or deleted.

What is the difference between a replica set and a replication controller?

The replica set and the replication controller's key difference is that the replication controller only supports equality-based selectors whereas the replica set supports set-based selectors.


1 Answers

This can be implemented using statefulsets which is out of beta since version 1.9. Quoting the documentation: When using kind: 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.

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

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

like image 155
GaspardP Avatar answered Oct 22 '22 01:10

GaspardP