me25
me25

Reputation: 557

How to delete all the resources Including service, deployment , pods, replica-set for particular deployment excluding specific one in kubernetes

Looking any kubernetes command which can help me to delete all the related resources of the deployment excluding specific one.

Example:-

Below is my two deployment echo1 & echo2 now I want to delete all the related resources which belongs to echo1 will be delete and echo2 will remain as it is.

NAME                         READY   STATUS    RESTARTS   AGE
pod/echo1-559ffc8757-th      1/1     Running   0          22s
pod/echo2-5c6c8ff4b-x56      1/1     Running   0          15s

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/echo1   ClusterIP   10.10.x.x       <none>        80/TCP    39m
service/echo2   ClusterIP   10.10.x.x       <none>        80/TCP    38m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/echo1   1/1     1            1           22s
deployment.apps/echo2   1/1     1            1           15s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/echo1-559ffc8      1         1         1       22s
replicaset.apps/echo2-5c6c8ff      1         1         1       15s

my deployement file :-

apiVersion: v1
kind: Service
metadata:
  name: echo1
spec:
  ports:
  - port: 80
    targetPort: 5678
  selector:
    app: echo1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo1
spec:
  selector:
    matchLabels:
      app: echo1
  replicas: 1
  template:
    metadata:
      labels:
        app: echo1
    spec:
      containers:
      - name: echo1
        image: hashicorp/http-echo
        args:
        - "-text=echo1"
        ports:
        - containerPort: 5678
~                             ```

Upvotes: 0

Views: 523

Answers (1)

hoque
hoque

Reputation: 6471

You can set a common label to connected resources and then delete by --selector

$ kubectl delete all --selector=<key>=<value> -n <namespace>
or 
$ kubectl delete all -l <key>=<value> -n <namespace>

In your file modify like following and apply above command

apiVersion: v1
kind: Service
metadata:
  name: echo1
  labels:
    app: echo1
spec:
  ports:
  - port: 80
    targetPort: 5678
  selector:
    app: echo1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo1
  labels:
    app: echo1
spec:
  selector:
    matchLabels:
      app: echo1
  replicas: 1
  template:
    metadata:
      labels:
        app: echo1
    spec:
      containers:
      - name: echo1
        image: hashicorp/http-echo
        args:
        - "-text=echo1"
        ports:
        - containerPort: 5678

Click here to see more examples

Upvotes: 1

Related Questions