Kubernetes Deployments
Create New Deployment
- Create a new deployment with nginx docker image
In this example:
Deployment Name : nginx-app
Docker Image : nginx - Image will be downloaded from Docker Hub
Replica Count : 3
Scnerio-1 : Create Deployment using CLI
- Run following command to create the new deployment
# kubectl create deployment nginx-app --image=nginx --replicas=3
deployment.apps/nginx-app created
Scnerio-2 : Create Deployment using YAML
- Create a YAML file and define the deployment configuration
# vi nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
labels:
app: nginx-app
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- Apply the configuration defined in YAML file
# kubectl apply -f nginx-deploy.yaml
deployment.apps/nginx-deployment created
Check Deployment Status
- Check the details of newly created deployment
# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 0/3 3 0 9s
- Check detailed configuration of deployment
Setting to note here are:
Selector : app=nginx-app - This value is used find which pods are associated with this deployment.
Replicas : This show how many replicas are desired and what is the current status
StrategyType : RollingUpdate - This is default value and defines the container update strategy.
# kubectl describe deployment nginx-app
Name: nginx-app
Namespace: default
CreationTimestamp: Fri, 29 Apr 2022 23:58:49 -0400
Labels: app=nginx-app
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-app
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-app
Containers:
nginx:
Image: nginx
Port:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-app-7f6fdf9556 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m14s deployment-controller Scaled up replica set nginx-app-7f6fdf9556 to 3
- Check the details of pods associated with deployment nginx-app
# kubectl get pods -l app=nginx-app
NAME READY STATUS RESTARTS AGE
nginx-app-7f6fdf9556-bp7pf 1/1 Running 0 3m20s
nginx-app-7f6fdf9556-m8tf7 1/1 Running 0 3m20s
nginx-app-7f6fdf9556-rmj4b 1/1 Running 0 3m20s
Scaling Replicas
- Scale the replica count from 3 to 5 in the nginx-app deployment
# kubectl scale deployment nginx-app --replicas=5
deployment.apps/nginx-app scaled
- Check the details of scaled deployment
# kubectl get deployment nginx-app
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 5/5 5 5 15m
# kubectl get pods
Replica count is now changed from 3 to 5
NAME READY STATUS RESTARTS AGE
nginx-app-7f6fdf9556-46nxx 1/1 Running 0 2m16s
nginx-app-7f6fdf9556-bp7pf 1/1 Running 0 15m
nginx-app-7f6fdf9556-m8tf7 1/1 Running 0 15m
nginx-app-7f6fdf9556-nzmhd 1/1 Running 0 2m16s
nginx-app-7f6fdf9556-rmj4b 1/1 Running 0 15m
Create Services
- Create service for the nginx-app deployment
Service Type : Load Balancer
Exposing service : Port 8080 (LoadBalancer) to port 80 (nginx container)
No we can use LoadBalancer IP:8080 to connect to nginx containers
# kubectl expose deployment nginx-app --type=LoadBalancer --port=8080 --target-port=80
service/nginx-app exposed
- Check the details of service
# kubectl get services nginx-app
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-app LoadBalancer 10.99.180.191 8080:32290/TCP 111s
# kubectl describe service nginx-app
Name: nginx-app
Namespace: default
Labels: app=nginx-app
Annotations:
Selector: app=nginx-app
Type: LoadBalancer
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.99.180.191
IPs: 10.99.180.191
Port: 8080/TCP
TargetPort: 80/TCP
NodePort: 32290/TCP
Endpoints: 172.17.0.3:80,172.17.0.4:80,172.17.0.5:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events:
Accessing NGINX Container Web Services
Note: When using minikube, kubernetes IPs are internal and not accessible from outside. But we can login to the kubernetes cluster server and access nginx web pages from there as a test.
- Login to minikube cluster server
# minikube ssh
- Access nginx webpage
# curl http://10.99.180.191:8080