9

I have created a replicaset with wrong container image with below configuration.

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: rs-d33393
  namespace: default
spec:
  replicas: 4
  selector:
    matchLabels:
      name: busybox-pod
  template:
    metadata:
      labels:
        name: busybox-pod
    spec:
      containers:
      - command:
        - sh
        - -c
        - echo Hello Kubernetes! && sleep 3600
        image: busyboxXXXXXXX
        name: busybox-container

Pods Information:

$ kubectl get pods
NAME              READY     STATUS             RESTARTS   AGE
rs-d33393-5hnfx   0/1       InvalidImageName   0          11m
rs-d33393-5rt5m   0/1       InvalidImageName   0          11m
rs-d33393-ngw78   0/1       InvalidImageName   0          11m
rs-d33393-vnpdh   0/1       InvalidImageName   0          11m

After this, i try to edit the image inside replicaset using kubectl edit replicasets.extensions rs-d33393 and update image as busybox.

Now, i am expecting pods to be recreated with proper image as part of replicaset.

This has not been the exact result.

Can someone please explain, why it is so?

Thanks :)

Sravan Kumar
  • 93
  • 1
  • 5

6 Answers6

13

With ReplicaSets directly you have to kill the old pod, so the new ones will be created with the right image.

If you would be using a Deployment, and you should, changing the image would force the pod to be re-created.

suren
  • 7,817
  • 1
  • 30
  • 51
8

Replicaset does not support updates. As long as required number of pods exist matching the selector labels, replicaset's jobs is done. You should use Deployment instead.

https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/

From the docs:

To update Pods to a new spec in a controlled way, use a Deployment, as ReplicaSets do not support a rolling update directly.

Shashank V
  • 10,007
  • 2
  • 25
  • 41
3

Deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods. Therefore, it is recommend to use Deployments instead of directly using ReplicaSets unless you don’t require updates at all. ( i.e. one may never need to manipulate ReplicaSet objects when using a Deployment)

Its easy to perform rolling updates and rollbacks when deployed using deployments.

$ kubectl create deployment busybox --image=busyboxxxxxxx --dry-run -o yaml > busybox.yaml

$ cat busybox.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: busybox
  name: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: busybox
    spec:
      containers:
      - image: busyboxxxxxxx
        name: busyboxxxxxxx
ubuntu@dlv-k8s-cluster-master:~$ kubectl create -f busybox.yaml --record=true
deployment.apps/busybox created

Check rollout history

ubuntu@dlv-k8s-cluster-master:~$ kubectl rollout history deployment busybox
deployment.apps/busybox
REVISION  CHANGE-CAUSE
1         kubectl create --filename=busybox.yaml --record=true

Update image on deployment

ubuntu@dlv-k8s-cluster-master:~$ kubectl set image deployment.app/busybox *=busybox --record
deployment.apps/busybox image updated


ubuntu@dlv-k8s-cluster-master:~$ kubectl rollout history deployment busybox
deployment.apps/busybox
REVISION  CHANGE-CAUSE
1         kubectl create --filename=busybox.yaml --record=true
2         kubectl set image deployment.app/busybox *=busybox --record=true

Rollback Deployment

ubuntu@dlv-k8s-cluster-master:~$ kubectl rollout undo deployment busybox
deployment.apps/busybox rolled back


ubuntu@dlv-k8s-cluster-master:~$ kubectl rollout history deployment busybox
deployment.apps/busybox
REVISION  CHANGE-CAUSE
2         kubectl set image deployment.app/busybox *=busybox --record=true
3         kubectl create --filename=busybox.yaml --record=true
DT.
  • 3,351
  • 2
  • 18
  • 32
2

You could use

k scale rs new-replica-set --replicas=0

and then

k scale rs new-replica-set --replicas=<Your number of replicas>
Adouani Riadh
  • 1,162
  • 2
  • 14
  • 37
0

Edit the replicaset(assuming its called replicaset.yaml) file with command:

kubectl edit rs replicaset
  • edit the image name in the editor
  • save the file
  • exit the editor

Now , you will need to either delete the replica sets or delete the existing pods:

kubectl delete rs new-replica-set
            
kubectl delete pod pod_1 pod_2 pod_2 pod_4

replicaset should spin up new pods with new image.

FenderBender
  • 87
  • 1
  • 9
0

edit the configuration, set the replica to 0. then use

kubectl replace -f your_definition_file.yml

then again edit the configuration, set the replica to 4, then use

kubectl replace -f your_definition_file.yml
Koedlt
  • 4,286
  • 8
  • 15
  • 33