Exercise Kubernetes Deployment
Table of Contents
1. Introduction
In this lab exercise, we'll cover essential Kubernetes deployment tasks. You'll create a deployment, verify its details, scale the number of pods, upgrade the application image, and finally, delete the deployment.
2. Imperative vs. Declarative
Imperative: Directly create, update, or delete resources using commands.
Declarative: Describe desired resource states using configuration files (YAML) and apply them.
3. Creating the Deployment
3.1 Declarative Method
Create a YAML specification for the deployment:
Name:
my-deployment
Container:
my-container
with imagenginx:1.14.2
Number of replicas:
3
Create a file named
deployment.yaml
:
vi deployment.yaml
Add the following content to deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:1.14.2
ports:
- containerPort: 80
(Note: This file describes the desired state of your application.)
3.2 Imperative Method
Alternatively, you can create the same deployment imperatively using the following command:
kubectl create deployment my-deployment --image=nginx --replicas=3
This command directly creates the deployment.
(Note: This command may fail since the deployment named my-deployment
already exist. Either change the name of the deployment or delete the existing deployment then recreate it with Imperative Method.) (To delete deployment refer the session ## 6. Deleting Deployment)Deleting Deployment
4. Verifying Deployment
4.1 Get Deployment Details
To list all deployments:
kubectl get deployments
To describe a specific deployment (e.g., "my-deployment"):
kubectl describe deployment my-deployment
4.2 ReplicaSet and Pods
A deployment automatically creates a ReplicaSet, which manages identical Pods.
To view ReplicaSets:
kubectl get rs
To see Pods under a specific deployment:
kubectl get pods -o wide
5. Scaling and Upgrading
5.1 Scale Pods
To scale the number of Pods in a deployment (e.g., "my-deployment") to 5:
kubectl scale deployment my-deployment --replicas=5
5.2 Rollout Status
To verify the rollout status:
kubectl rollout status deployment my-deployment
5.3 Upgrade Image Version
Update the image version in your deployment YAML file.
Apply the changes:
kubectl apply -f deployment.yaml
(Note: Image upgrades will be explained in the next exercise.)
5.4 Scale Down
To scale down to 3 replica:
kubectl scale deployment my-deployment --replicas=3
To scale down to 1 replica:
kubectl scale deployment my-deployment --replicas=1
6. Deleting Deployment
6.1 Using Imperative Command
To delete a deployment (e.g., "my-deployment"):
kubectl delete deployment my-deployment
6.2 Using Definition File
To delete the deployment using the definition file (
deployment.yaml
):kubectl delete -f deployment.yaml
7. References
Last updated