Saturday 25 July 2020

Kubernetes: Delete ReplicaSet but not the Pods managed by it

If you delete a ReplicaSet or ReplicationController, all the Pods managed by them will get deleted by default. But in some cases, you just want to delete the ReplicaSet or ReplicationController alone, and not the Pods.

 

You can do this by setting --cascade to false.

 

Let’s see it with an example.

 

Step 1: Define ReplicaSet manifest file.

 

replicaSetLabels.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: employee-service-replica-set
  labels:
    app: employee-service-replica-set
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

Step 2: Create ReplicaSet from the above definition file.

$kubectl create -f replicaSetLabels.yml 
replicaset.apps/employee-service-replica-set created

Query for replicaset and pods.

$kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
employee-service-replica-set   5         5         3       18s
Query for Pods.

$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-8gw6n   1/1     Running   0          37s
employee-service-replica-set-b4jrb   1/1     Running   0          37s
employee-service-replica-set-tg2hf   1/1     Running   0          37s
employee-service-replica-set-tmz22   1/1     Running   0          37s
employee-service-replica-set-zhkwx   1/1     Running   0          37s

Step 2: let's delete replicaset alone.

$kubectl delete rs employee-service-replica-set --cascade=false
replicaset.apps "employee-service-replica-set" deleted

Let’s query for the Pods and replicaset to confirm that Pods are not deleted.

$kubectl get rs
No resources found in default namespace.
$
$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-8gw6n   1/1     Running   0          4m8s
employee-service-replica-set-b4jrb   1/1     Running   0          4m8s
employee-service-replica-set-tg2hf   1/1     Running   0          4m8s
employee-service-replica-set-tmz22   1/1     Running   0          4m8s
employee-service-replica-set-zhkwx   1/1     Running   0          4m8s




Previous                                                    Next                                                    Home

No comments:

Post a Comment