0
0
Kubernetesdevops~5 mins

Deployment as higher-level abstraction in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing individual containers can be complex and error-prone. A Deployment in Kubernetes helps by managing groups of containers, making updates and scaling easier and safer.
When you want to run multiple copies of an application to handle more users.
When you need to update your application without downtime by rolling out changes gradually.
When you want Kubernetes to automatically restart containers if they fail.
When you want to easily roll back to a previous version if an update causes problems.
When you want to manage the desired state of your application automatically.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
  labels:
    app: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80

This file defines a Deployment named example-deployment that runs 3 copies (replicas) of an Nginx container.

The selector and template.metadata.labels ensure the Deployment manages the right Pods.

The spec.template.spec.containers section defines the container image and port.

Commands
This command creates the Deployment in Kubernetes, telling it to start 3 copies of the Nginx container as defined in the file.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/example-deployment created
This command lists all Deployments in the current namespace to verify that the Deployment was created successfully.
Terminal
kubectl get deployments
Expected OutputExpected
NAME READY UP-TO-DATE AVAILABLE AGE example-deployment 3/3 3 3 10s
This command lists all Pods managed by the Deployment using the label selector to confirm the Pods are running.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-6f8d7f9d5f-abc12 1/1 Running 0 15s example-deployment-6f8d7f9d5f-def34 1/1 Running 0 15s example-deployment-6f8d7f9d5f-ghi56 1/1 Running 0 15s
-l - Filter resources by label selector
This command checks the status of the Deployment rollout to ensure all Pods are updated and running as expected.
Terminal
kubectl rollout status deployment/example-deployment
Expected OutputExpected
deployment "example-deployment" successfully rolled out
Key Concept

If you remember nothing else from this pattern, remember: a Deployment manages multiple copies of your app and handles updates safely.

Common Mistakes
Not matching the labels in the Deployment selector and Pod template.
Kubernetes won't know which Pods belong to the Deployment, so it won't manage them properly.
Ensure the labels under spec.selector.matchLabels exactly match the labels under spec.template.metadata.labels.
Applying a Deployment file without specifying the correct namespace when needed.
The Deployment might be created in the default namespace, causing confusion or conflicts.
Use the --namespace flag or set the namespace in the metadata section of the YAML file.
Changing the container image in the Deployment file but not applying the updated file.
The running Pods won't update to the new image, so changes won't take effect.
Run kubectl apply -f deployment.yaml again after changes to update the Deployment.
Summary
Create a Deployment YAML file to define how many copies of your app to run and which container image to use.
Use kubectl apply to create or update the Deployment in Kubernetes.
Verify the Deployment and its Pods are running with kubectl get commands and check rollout status for updates.