Edit a kubernetes (k8s) deployment
Sometimes you need to edit a kubernetes deployment quickly in real time, w/o applying a file, could be a quick one off, or an emergency
So when you create a real world deployment, you eventually will have to modify it, whether that means you're just updating the image artifact with a new deploy. Or maybe you need to tweak the number of replicas, add cpu/memory limits, etc. So you can either update the yaml
file you ran kubectl apply -f <XYZ.yaml>
on, or you can just run kubectl edit deployment <mydeployment>
and it will pop open your default editor for you to be able to modify the deployment in real time.
In our case we're going to modify the hello-minikube
deployment we have. Run the following command, and it will open up your default editor, on linux this will typically be vi/vim
PS ...\kubernetes\kubernetesTraining> kubectl edit deployment hello-minikube
Here is the output when you're inside your editor.
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"hello-minikube"},"name":"hello-minikube","namespace":"default"},"spec":{"progressDeadlineSeconds":600,"replicas":3,"revisionHistoryLimit":10,"selector":{"matchLabels":{"app":"hello-minikube"}},"strategy":{"rollingUpdate":{"maxSurge":"25%","maxUnavailable":"25%"},"type":"RollingUpdate"},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"hello-minikube"}},"spec":{"containers":[{"image":"k8s.gcr.io/echoserver:1.10","imagePullPolicy":"IfNotPresent","name":"echoserver","resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File"}],"dnsPolicy":"ClusterFirst","restartPolicy":"Always","schedulerName":"default-scheduler","securityContext":{},"terminationGracePeriodSeconds":30}}}}
creationTimestamp: "2020-02-23T14:33:11Z"
generation: 6
labels:
app: hello-minikube
name: hello-minikube
namespace: default
resourceVersion: "235503"
selfLink: /apis/apps/v1/namespaces/default/deployments/hello-minikube
uid: 08a2d4f6-5346-4d13-9a44-3b7a0fb7dc44
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: hello-minikube
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: hello-minikube
spec:
containers:
- image: k8s.gcr.io/echoserver:1.10
imagePullPolicy: IfNotPresent
name: echoserver
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 3
conditions:
- lastTransitionTime: "2020-02-23T14:33:11Z"
lastUpdateTime: "2020-02-23T14:48:52Z"
message: ReplicaSet "hello-minikube-797f975945" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
- lastTransitionTime: "2020-02-23T15:16:44Z"
lastUpdateTime: "2020-02-23T15:16:44Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 6
readyReplicas: 3
replicas: 3
updatedReplicas: 3
Lets go in and change the number of replicas from 3
-> 1
find the line
replicas: 3
and change the 3 to a 1, then save and exit the file, and you'll get a prompt that the deployment has been edited.
PS ...\kubernetes\kubernetesTraining> kubectl edit deployment hello-minikube deployment.apps/hello-minikube edited
Now lets verify that our changes to the replica count were committed properly.
PS ...\kubernetes\kubernetesTraining> kubectl get deployment hello-minikube -o yaml | grep -i replicas {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"hello-minikube"},"name":"hello-minikube","namespace":"default"},"spec":{"progressDeadlineSeconds":600,"replicas":3,"revisionHistoryLimit":10,"selector":{"matchLabels":{"app":"hello-minikube"}},"strategy":{"rollingUpdate":{"maxSurge":"25%","maxUnavailable":"25%"},"type":"RollingUpdate"},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"hello-minikube"}},"spec":{"containers":[{"image":"k8s.gcr.io/echoserver:1.10","imagePullPolicy":"IfNotPresent","name":"echoserver","resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File"}],"dnsPolicy":"ClusterFirst","restartPolicy":"Always","schedulerName":"default-scheduler","securityContext":{},"terminationGracePeriodSeconds":30}}}}
replicas: 1
availableReplicas: 1
message: ReplicaSet "hello-minikube-797f975945" has successfully progressed.
reason: NewReplicaSetAvailable
reason: MinimumReplicasAvailable
readyReplicas: 1
replicas: 1
updatedReplicas: 1
We can see by greping the current deployment as yaml
and grepping for replica
that the replicas did in fact update from 3 to 1.
You can use this syntax and style for any kubernetes (k8s) changes you want to do in real time.