Kubernetes Deployments #
Example: Create Deployment with Create Command #
# Create a deployment with Nginx container
kubectl create deployment example-deployment --image=nginx:alpine
# Create a deployment with Nginx container: Define number of pod replicas
kubectl create deployment example-deployment --image=nginx:alpine --replicas=2
# List deployments
kubectl get deployments
# List deployment details: Save output in YAML format into file
kubectl describe deployment example-deployment
# List deployment details
kubectl get deployment example-deployment -o yaml > example-deployment.yaml
# Delete the deployment
kubectl delete deployment example-deployment
Example: Create Deployment with YAML Configuration #
Create Configuration #
# Create a deployment configuration with dry-run
kubectl create deployment example-deployment --image=nginx --dry-run=client -o yaml > example-deployment.yaml
# Create a configuration for the deployment
vi example-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: example-deployment
name: example-deployment
spec:
replicas: 1
selector:
matchLabels:
app: example-deployment
strategy: {}
template:
metadata:
#creationTimestamp: null
labels:
app: example-deployment
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
resources: {}
#status: {}
-
spec.replicas:
Number of replica pods -
spec.selector:
The deployment that will manage all pods whose labels match this selector -
spec.template:
Template pod descriptor that defines the pods that will be created
Note: status:
and creationTimestamp:
can be removed.
# Create the deployment
kubectl create -f example-deployment.yaml
Scale the Deployment #
# Scale the deployment to 6 pods
kubectl scale deployment example-deployment --replicas=6
Verify the Deployment
# List deployments
kubectl get deployments
# Shell output:
NAME READY UP-TO-DATE AVAILABLE AGE
example-deployment 6/6 6 6 5m46s
Replication Controller #
-
The replication controller ensures that a specified number of pods is running all the time.
-
Replication controllers has mostly been replaced by deployments, which offer more features and functionality.
Example: Replication Controller #
Create Configuration #
The following ReplicationController deploys two replicas of pod that consists of an Nginx container:
# Create a configuration for the ReplicationController
vi replication-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-controller
labels:
app: nginx
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
# Create the ReplicationController
kubectl apply -f replication-controller.yaml
List Replication Controllers #
# List all replication controllers
kubectl get rc
# Shell output: Single container version
NAME DESIRED CURRENT READY AGE
nginx-controller 2 2 2 30s
Replication Controller Details #
# List replication controller details
kubectl describe rc nginx-controller
# Shell output:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 23s replication-controller Created pod: nginx-controller-kgdgb
Normal SuccessfulCreate 23s replication-controller Created pod: nginx-controller-vm2rt
Pod Status & Details #
# List pods: With label nginx
kubectl get pod -l app=nginx
# Shell output:
NAME READY STATUS RESTARTS AGE
nginx-controller-kgdgb 1/1 Running 0 71s
nginx-controller-vm2rt 1/1 Running 0 71s
Edit Replication Controller #
Use the kubectl edit rc
command to edit the replication controller, for example change the amount of replicas to “3”:
# Edit replication controller "nginx-controller"
kubectl edit rc/nginx-controller
# Verify the number of pods
kubectl get pod -l app=nginx
# Shell output:
NAME READY STATUS RESTARTS AGE
nginx-controller-2rdkf 1/1 Running 0 5s
nginx-controller-kgdgb 1/1 Running 0 109s
nginx-controller-vm2rt 1/1 Running 0 109s
Delete the Replication Controller #
# Delete the replication controller and it's pods
kubectl delete rc nginx-controller
kubectl delete rc nginx-alpine-controller
# Verify
kubectl get rc