Introduction
For those of us who don’t want to edit resources with an editor that launches with a kubectl edit
command, there is command kubectl patch
, so let’s take advantage of it. The nice thing about the patch command is that you don’t have to handwrite it in the editor. Since resources can be changed with one liner, it is also suitable for embedding in shell scripts.
Let’s test this !
Run example deployment
root@vagrant:/home/vagrant# kubectl create deployment nginx --image=spy86/nginx:latest
deployment.apps/nginx created
root@vagrant:/home/vagrant# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-6465f7cb5d-phgmr 1/1 Running 0 8s
root@vagrant:/home/vagrant# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 10s
root@vagrant:/home/vagrant#
Modify the container image
It can be used when you want to change the image version temporarily.
root@vagrant:/home/vagrant# kubectl patch deployment nginx -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"spy86/nginx:147"}]}}}}'
deployment.apps/nginx patched
root@vagrant:/home/vagrant#
root@vagrant:/home/vagrant# kubectl describe deploy nginx | grep Image:
Image: spy86/nginx:147
Change the number of replicas
One-liner to change the number of replicas. It can be used when you want to change the number of replicas temporarily. By the way, 0 can also be specified, and it will be in a simple undeployed state.
root@vagrant:/home/vagrant# kubectl patch deployment nginx -p '{"spec":{"replicas":10}}'
deployment.apps/nginx patched
root@vagrant:/home/vagrant# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-9ddb798cf-mrjds 1/1 Running 0 3m22s
nginx-9ddb798cf-rrhvv 1/1 Running 0 101s
nginx-9ddb798cf-fpzcw 1/1 Running 0 101s
nginx-9ddb798cf-dcg8d 1/1 Running 0 101s
nginx-9ddb798cf-rb5nc 1/1 Running 0 101s
nginx-9ddb798cf-fn5s4 1/1 Running 0 101s
nginx-9ddb798cf-69k48 1/1 Running 0 101s
nginx-9ddb798cf-hhd74 1/1 Running 0 101s
nginx-9ddb798cf-n4ww9 1/1 Running 0 101s
nginx-9ddb798cf-fzrk9 1/1 Running 0 101s
Change the Service Type
First we need to create Service for deployment
root@vagrant:/home/vagrant# kubectl expose deployment nginx --type=ClusterIP --name=nginx --port=80
service/nginx exposed
root@vagrant:/home/vagrant# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 13m
nginx ClusterIP 10.152.183.209 <none> 80/TCP 6s
One-liner to change service Type to NodePort
root@vagrant:/home/vagrant# kubectl get svc nginx -o jsonpath='{.spec.type}'
ClusterIP
root@vagrant:/home/vagrant# kubectl patch service nginx -p '{"spec":{"type":"NodePort"}}'
service/nginx patched
root@vagrant:/home/vagrant# kubectl get svc nginx -o jsonpath='{.spec.type}'
NodePort
root@vagrant:/home/vagrant# kubectl get svc nginx -o jsonpath='{.spec.ports[0].nodePort}'
30222
root@vagrant:/home/vagrant#