Change an Existing Configuration #
Create Example Deployment #
# Create a manifest for the deployment
vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
# Create the deployment
kubectl apply -f nginx-deployment.yaml
Adopt the Deployment Configuration: Non-Disruptive #
Kubectl Apply #
Change the deployment configuration and add for example a port:
# Adopt the manifest
vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports: # Add a port
         - containerPort: 80
           protocol: TCP
        resources: {}
status: {}
# Update the existing resource from the adopted configuration
kubectl apply -f nginx-deployment.yaml
# Verify the port:
kubectl describe deployment nginx | grep Port
# Shell output:
    Port:         80/TCP
    Host Port:    0/TC
Note: kubectl apply instead of kubectl create is generally recommended when managing resources declaratively in Kubernetes. kubectl apply automatically adds the kubectl.kubernetes.io/last-applied-configuration annotation to the resource, which stores the last applied configuration, to track changes.
Kubectl Edit #
This opens the current resource configuration in a text editor, allowing you to make changes directly.
Change the deployment configuration and add for example a port:
# Open the deployment configuration
kubectl edit deployment nginx
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"creationTimestamp":null,"labels":{"app":"nginx"},"name":"nginx","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"nginx"}},"strategy":{},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx","name":"nginx","resources":{}}]}}},"status":{}}
  creationTimestamp: "2024-11-04T18:24:08Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "12855"
  uid: a14736df-9f28-4ffa-ab4b-b75268e28e77
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
        ports: # Add a port
         - containerPort: 80
           protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2024-11-04T18:24:10Z"
    lastUpdateTime: "2024-11-04T18:24:10Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2024-11-04T18:24:08Z"
    lastUpdateTime: "2024-11-04T18:24:10Z"
    message: ReplicaSet "nginx-7854ff8877" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
- Save the configuration
# Shell output:
deployment.apps/nginx edited
# Verify the port:
kubectl describe deployment nginx | grep Port
# Shell output:
    Port:         80/TCP
    Host Port:    0/TC
 
Kubectl Patch #
Useful for partial configuration updates.
Change the deployment configuration and add for example a port:
# Example: JSON Patch
kubectl patch deployment nginx --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports", "value": [{"containerPort": 80, "protocol": "TCP"}]}]'
# Example: YAML Merge Patch
kubectl patch deployment nginx --type='merge' -p='{"spec": {"template": {"spec": {"containers": [{"name": "nginx", "image": "nginx", "ports": [{"containerPort": 80, "protocol": "TCP"}]}]}}}}'
# Verify the port:
kubectl describe deployment nginx | grep Port
# Shell output:
    Port:         80/TCP
    Host Port:    0/TC
 
Adopt the Deployment Configuration: Disruptive #
Kubectl Replace #
Change the deployment configuration and add for example a port:
# Adopt the manifest
vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports: # Add a port
         - containerPort: 80
           protocol: TCP
        resources: {}
status: {}
# List pods
kubectl get pods
# Shell output:
NAME                     READY   STATUS    RESTARTS   AGE
nginx-8678697d5c-tcs77   1/1     Running   0          86s
# Terminate and recreate the resources
kubectl replace -f nginx-deployment.yaml
# Shell output:
deployment.apps/nginx replaced
# List pods
kubectl get pods
# Shell output:
NAME                     READY   STATUS        RESTARTS   AGE
nginx-797d4647d-4jg4c    1/1     Running       0          1s
nginx-8678697d5c-tcs77   1/1     Terminating   0          97s
# Shell output:
NAME                    READY   STATUS    RESTARTS   AGE
nginx-797d4647d-4jg4c   1/1     Running   0          8s
Delete the Deployment #
# Delete the deployment
kubectl delete deployment nginx
 
Kubernetes Update Rollouts and Rollbacks with Set Image Command #
Create Example Deployment #
# Create a deployment with Nginx container
kubectl create deployment example-deployment --image=nginx:1.26.2
# Verify the pod
kubectl get pod
# Shell output:
NAME                                 READY   STATUS    RESTARTS   AGE
example-deployment-bb86db598-r52hg   1/1     Running   0          33s
Update Rollout #
# Update the deployment to a new image version
kubectl set image deployment example-deployment nginx=nginx:1.27.2
# Shell output:
deployment.apps/example-deployment image updated
# Annotate the deployment with the change cause
kubectl annotate deployment example-deployment kubernetes.io/change-cause="Updated image to nginx:1.27.2"
# Shell output:
deployment.apps/example-deployment annotated
Verify Deployment Image #
# Verify the pod
kubectl get pods
# Shell output:
NAME                                  READY   STATUS    RESTARTS   AGE
example-deployment-7bfd79855f-txcqw   1/1     Running   0          3m9s
# List deployment details
kubectl describe deployment example-deployment
# Shell output:
...
Pod Template:
  Labels:  app=example-deployment
  Containers:
   nginx:
    Image:        nginx:1.27.2
...
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  2m58s  deployment-controller  Scaled up replica set example-deployment-bb86db598 to 1
  Normal  ScalingReplicaSet  116s   deployment-controller  Scaled up replica set example-deployment-7bfd79855f to 1
  Normal  ScalingReplicaSet  115s   deployment-controller  Scaled down replica set example-deployment-bb86db598 to 0 from 1
Rollback: Most Recent Previous Version #
# Roll back to the most recent previous version
kubectl rollout undo deployments example-deployment
# Shell output:
deployment.apps/example-deployment rolled back
# Annotate the deployment with the change cause
kubectl annotate deployment example-deployment kubernetes.io/change-cause="Rollback to image nginx:1.26.2"
# Shell output:
deployment.apps/example-deployment annotated
View Rollout History #
# List rollout history
kubectl rollout history deployment example-deployment
# Shell output:
REVISION  CHANGE-CAUSE
2         Updated image to nginx:1.27.2
3         Rollback to image nginx:1.26.2
Rollback to a Specific Revision #
# Rollback to a Specific Revision: Revision 2
kubectl rollout undo deployment example-deployment --to-revision=2
# Shell output:
deployment.apps/example-deployment rolled back
# Annotate the deployment with the change cause
kubectl annotate deployment example-deployment kubernetes.io/change-cause="Rollback to image nginx:1.27.2"
# Shell output:
deployment.apps/example-deployment annotated
View Rollout History:
# List rollout history
kubectl rollout history deployment example-deployment
# Shell output:
REVISION  CHANGE-CAUSE
3         Rollback to image nginx:1.26.2
4         Rollback to image nginx:1.27.2