Marco Mei
Marco Mei

Reputation: 137

how to export the all deployment in kubernetes with yaml format except the deployment in particular namespace

with the command kubectl get deploy -A -o yaml I can get the all deployments definition, includes the namespaces kube-system and monitoring which are not my target services.

I tried to use the command kubectl get deploy -A |grep -v kube-system |grep -v monitoring and got the target deployments like:

$ kubectl  get deploy -A |grep -v kube-system |grep -v monitoring
NAMESPACE     NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
default       nginx-deploy                             2/2     2            2           103d
default       test                                     1/1     1            1           48d
mai           mai-1                                    10/10   10           10          85d
mai           mai-w                                    1/1     1            1           20d
mai           test-deploy-1                            1/1     1            1           68d
q-test        nginx-deployment-demo-01                 5/5     5            5           64d

In fact I need to get the all deployments nginx-deploy,test,mai-1,mai-w,test-deploy-1,nginx-deployment-demo-01 as above. So I tried below command: kubectl get deploy -A |grep -v kube-system |grep -v monitoring -oyaml, as expected it failed to get the target deployments definition.

Any one could help to fix it?

Upvotes: 0

Views: 1932

Answers (1)

P....
P....

Reputation: 18371

This should do the task in you are on bash:

for ns in $(kubectl get ns -o name  --no-headers |grep -v kube-system); do
    kubectl get deployment -n ${ns##*/}"  -o yaml;
done

In the above snippet, first, the list of namespaces is captured and filtered and then looped.

Upvotes: 1

Related Questions