How to patch a running Kubernetes pod

How to patch a running Kubernetes pod

A running Kubernetes pod doesn’t have to be taken down to be altered. Using the patch option, adding a new container to the pod is actually quite easy.

kuberneteshero.jpg

Image: Jack Wallen

Kubernetes is one of the most powerful and flexible container cluster managers on the market. With this tool you can do almost anything you need. One such ability Kubernetes offers is the ability to patch an already-deployed pod.

Say, for example, you have a pod deployed which includes an NGINX container, but want to then later add regis and MySQL containers into the mix. Instead of taking it all down and retooling your configuration files, you could simply run a patch command to add the new containers. 

I’m going to show you how to do just that. Although this example will be fairly rudimentary, it’ll demonstrate how the patch option works with the kubectl command.

SEE: How smart tech is transforming the transportation industry (TechRepublic Premium)

What you’ll need

The only thing you’ll need to make this work is a running Kubernetes cluster. 

To find out how to deploy the cluster, check out my tutorial: How to deploy a Kubernetes cluster on Ubuntu server. Once you have that up and running, we can continue on.

How to deploy a Kubernetes service

The first thing we need to do is deploy a Kubernetes service. Create a new directory with the command:

mkdir ~/patch-demo

Change into that new directory with the command:

cd ~/patch-demo

Now, create a YAML file for a service deployment that will include NGINX with a fairly basic template and two replicas. Create this file with the command:

nano deployment-patch.yaml

In that file, paste the following contents:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata: name: patch-demo
spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: patch-demo-ctr image: nginx tolerations: - effect: NoSchedule key: dedicated value: test-team

Save and close the file. Deploy that configuration with the command:

kubectl apply -f deployment-patch.yaml

You can now see this pod is running by issuing the command:

kubectl get pods

The patch-demo pod will be listed as running (Figure A).

Figure A

Our Kubernetes patch-demo pod is up and running.

” data-credit rel=”noopener noreferrer nofollow”>patcha.jpg

patcha.jpg

Our Kubernetes patch-demo pod is up and running.

How to patch the running service

Now we’re going to create a strategic merge patch file that will add a redis container to the pod. Create a patch file with the command:

nano patch-file.yaml

In that file, paste the following:

spec: template: spec: containers: - name: patch-demo-ctr-2 image: redis

Save and close the file.

To patch the running service, issue the command:

kubectl patch deployment patch-demo --patch "$(cat patch-file.yaml)"

What happens in this instance is the deployment will terminate the old pods and create the new ones. 

You could continue this. Say, for example, you wanted to add MySQL into the mix. Create another patch file with the command:

nano patch-file-2.yaml

In that file, paste the following:

spec: template: spec: containers: - name: patch-demo-ctr-3 image: mysql

Save and close the file and re-run the patch command like so:

kubectl patch deployment patch-demo --patch "$(cat patch-file-2.yaml)"

If you issue the command:

kubectl get deployment patch-demo --output yaml

You’ll see that ngxin, redis, and mysql are all found in the pod (Figure B).

Figure B

Our Kubernetes pod now includes all three images.

” data-credit rel=”noopener noreferrer nofollow”>patchb.jpg

patchb.jpg

Our Kubernetes pod now includes all three images.

One thing you cannot do with this patching system is scale up the number of replicas for a pod. To do that, you would use the scale command like so:

kubectl scale --replicas=4 deployment patch-demo

Or you can delete the pod by scaling it down to zero like so:

kubectl scale --replicas=0 deployment patch-demo

And that’s all there is to patching a running Kubernetes service. Although this is a very basic tutorial, you’ll find that with the patch command what you can do is almost limitless.

Also see

Source of Article