Search code examples
kubernetesyamlkubectl

Apply part of kubernetes manifest yaml using kubectl


consider the following kubernetes manifest (mymanifest.yml) :

apiVersion: v1
kind: Pod
metadata:
  name: firstpod
spec:
  containers:
  - image: nginx
    name: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: secondpod
spec:
  containers:
  - image: nginx
    name: nginx

If I do kubectl apply -f mymanifest.yml both pods are deployed. I remember someone told me that it is possible to deploy only one pod. Something like :

kubectl apply -f mymanifest.yml secondpod

But it doesn't work. Is there a way to do it?

Thx in advance


Solution

  • You can add labels to the pods

    apiVersion: v1
    kind: Pod
    metadata:
      name: firstpod
      labels:
        app: firstpod
    spec:
      containers:
      - image: nginx
        name: nginx
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: secondpod
      labels:
        app: secondpod
    spec:
      containers:
      - image: nginx
        name: nginx
    

    Use specific label to filter while applying the yaml. The filter supports =, ==, and !=

    kubectl apply -f mymanifest.yml -l app=secondpod
    

    You can also use --prune which is an alpha feature.

    # Apply the configuration in manifest.yaml that matches label app=secondpod and delete all the other resources that are
    not in the file and match label app=secondpod.
    kubectl apply --prune -f mymanifest.yml -l app=secondpod