Kubernetes: basic concepts – part 3

By | 14/07/2017

Replica Set

ReplicaSets are the next-generation Replication Controllers. The only difference between a ReplicaSet and a Replication Controller right now is the selector support. Similar to Replication Controllers, a ReplicaSet ensures that a specified number of pod “replicas” are running at any given time.

Let’s give it a try:

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: nginx-pod-rs
spec:
  replicas: 5
  selector:
    matchLabels:
     type: web
  template:
    metadata:
     labels:
      type: web
      environment: dev
    spec:
     containers:
     - name: nginx-pod-rs
       image: nginx:alpine
       ports:
       - containerPort: 80
WAUTERW-M-T3ZT:replication wim$ kubectl create -f nginx-replicationset.yaml
replicaset "nginx-pod-rs" created
WAUTERW-M-T3ZT:replication wim$ kubectl get pods
NAME                 READY     STATUS    RESTARTS   AGE
nginx-pod-rs-0k896   1/1       Running   0          22m
nginx-pod-rs-7rgjl   1/1       Running   0          22m
nginx-pod-rs-hflrp   1/1       Running   0          22m
nginx-pod-rs-lmf3v   1/1       Running   0          22m
nginx-pod-rs-m489m   1/1       Running   0          22m
WAUTERW-M-T3ZT:replication wim$ kubectl get rs
NAME           DESIRED   CURRENT   READY     AGE
nginx-pod-rs   5         5         5         23m
WAUTERW-M-T3ZT:replication wim$ kubectl describe rs nginx-pod-rs
Name:		nginx-pod-rs
Namespace:	default
Selector:	type=web
Labels:		environment=dev
		type=web
Annotations:	
Replicas:	5 current / 5 desired
Pods Status:	5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:	environment=dev
		type=web
  Containers:
   nginx-pod-rs:
    Image:		nginx:alpine
    Port:		80/TCP
    Environment:	
    Mounts:		
  Volumes:		
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason			Message
  ---------	--------	-----	----			-------------	--------	------			-------
  23m		23m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-pod-rs-hflrp
  23m		23m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-pod-rs-m489m
  23m		23m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-pod-rs-lmf3v
  23m		23m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-pod-rs-7rgjl
  23m		23m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-pod-rs-0k896

Deployments

Deployments are intended to replace Replication Controllers. They provide the same replication functions (through Replica Sets) and also the ability to rollout changes and roll them back if necessary. In fact, a Deployment owns and manages one or more Replica Sets. And a Replica Set manages the basic units in Kubernetes which are Pods.

Let’s create a simple Deployment using the same image we’ve been using.

WAUTERW-M-T3ZT:deployments wim$ kubectl create -f nginx-deployment_v1.yaml
deployment "nginx-deployment" created
WAUTERW-M-T3ZT:deployments wim$ kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   5         5         5            3           8s
WAUTERW-M-T3ZT:deployments wim$ kubectl get rs
NAME                        DESIRED   CURRENT   READY     AGE
nginx-deployment-47451619   5         5         4         12s
WAUTERW-M-T3ZT:deployments wim$ kubectl describe deployment nginx-deployment
Name:			nginx-deployment
Namespace:		default
CreationTimestamp:	Thu, 27 Jul 2017 13:45:45 +0200
Labels:			type=web
			version=v0.1
Annotations:		deployment.kubernetes.io/revision=1
Selector:		type=web
Replicas:		5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:		RollingUpdate
MinReadySeconds:	0
RollingUpdateStrategy:	1 max unavailable, 1 max surge
Pod Template:
  Labels:	type=web
		version=v0.1
  Containers:
   nginx-pod:
    Image:		nginx
    Port:		80/TCP
    Environment:	
    Mounts:		
  Volumes:		
Conditions:
  Type		Status	Reason
  ----		------	------
  Available 	True	MinimumReplicasAvailable
OldReplicaSets:	
NewReplicaSet:	nginx-deployment-47451619 (5/5 replicas created)
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason			Message
  ---------	--------	-----	----			-------------	--------	------			-------
  31s		31s		1	deployment-controller			Normal		ScalingReplicaSet	Scaled up replica set nginx-deployment-47451619 to 5
WAUTERW-M-T3ZT:deployments wim$ kubectl describe rs nginx-deployment-47451619
Name:		nginx-deployment-47451619
Namespace:	default
Selector:	pod-template-hash=47451619,type=web
Labels:		pod-template-hash=47451619
		type=web
		version=v0.1
Annotations:	deployment.kubernetes.io/desired-replicas=5
		deployment.kubernetes.io/max-replicas=6
		deployment.kubernetes.io/revision=1
Replicas:	5 current / 5 desired
Pods Status:	5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:	pod-template-hash=47451619
		type=web
		version=v0.1
  Containers:
   nginx-pod:
    Image:		nginx
    Port:		80/TCP
    Environment:	
    Mounts:		
  Volumes:		
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason			Message
  ---------	--------	-----	----			-------------	--------	------			-------
  1m		1m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-deployment-47451619-n0m3v
  1m		1m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-deployment-47451619-w02cr
  1m		1m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-deployment-47451619-19fvm
  1m		1m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-deployment-47451619-sr73l
  1m		1m		1	replicaset-controller			Normal		SuccessfulCreate	Created pod: nginx-deployment-47451619-24v6j
WAUTERW-M-T3ZT:deployments wim$ kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
nginx-deployment-47451619-19fvm   1/1       Running   0          1m
nginx-deployment-47451619-24v6j   1/1       Running   0          1m
nginx-deployment-47451619-n0m3v   1/1       Running   0          1m
nginx-deployment-47451619-sr73l   1/1       Running   0          1m
nginx-deployment-47451619-w02cr   1/1       Running   0          1m

You see here that in the deployment description the replication set is referenced and not the pods. The pods are described in the replication set description as can be seen from the snippet below.

Rolling updates with deployments

Make a small difference to the deployment yaml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      type: web
  template:
    metadata:
      labels:
        type: web
        version: v0.2
    spec:
      containers:
      - name: nginx-pod
        image: nginx:alpine
        imagePullPolicy: Always
        ports:
        - containerPort: 80

Apply the update

WAUTERW-M-T3ZT:deployments wim$ kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   5         6         3            4           3m
WAUTERW-M-T3ZT:deployments wim$ kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-2267353239   4         4         2         7s
nginx-deployment-47451619     2         2         2         3m
WAUTERW-M-T3ZT:deployments wim$ kubectl get pods
NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-2267353239-2xt1d   1/1       Running             0          12s
nginx-deployment-2267353239-5d9tg   0/1       ContainerCreating   0          3s
nginx-deployment-2267353239-8xmbx   1/1       Running             0          8s
nginx-deployment-2267353239-kfs50   1/1       Running             0          6s
nginx-deployment-2267353239-r2d52   1/1       Running             0          12s
nginx-deployment-47451619-19fvm     0/1       Terminating         0          3m
nginx-deployment-47451619-sr73l     1/1       Terminating         0          3m
WAUTERW-M-T3ZT:deployments wim$
WAUTERW-M-T3ZT:deployments wim$
WAUTERW-M-T3ZT:deployments wim$
WAUTERW-M-T3ZT:deployments wim$
WAUTERW-M-T3ZT:deployments wim$
WAUTERW-M-T3ZT:deployments wim$ kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-2267353239-2xt1d   1/1       Running   0          22s
nginx-deployment-2267353239-5d9tg   1/1       Running   0          13s
nginx-deployment-2267353239-8xmbx   1/1       Running   0          18s
nginx-deployment-2267353239-kfs50   1/1       Running   0          16s
nginx-deployment-2267353239-r2d52   1/1       Running   0          22s

WAUTERW-M-T3ZT:deployments wim$ kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION	CHANGE-CAUSE
1		
2		kubectl apply --filename=nginx-deployment_v2.yaml --record=true

Rollback the deployment

WAUTERW-M-T3ZT:deployments wim$ kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment" rolled back
WAUTERW-M-T3ZT:deployments wim$ kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-2267353239   3         3         3         5m
nginx-deployment-47451619     3         3         1         9m
WAUTERW-M-T3ZT:deployments wim$ kubectl get pods
NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-2267353239-2xt1d   1/1       Running             0          5m
nginx-deployment-47451619-1nqrl     1/1       Running             0          13s
nginx-deployment-47451619-3r235     1/1       Running             0          13s
nginx-deployment-47451619-5281h     1/1       Running             0          8s
nginx-deployment-47451619-7vjqw     0/1       ContainerCreating   0          6s
nginx-deployment-47451619-f1l90     0/1       ContainerCreating   0          4s
WAUTERW-M-T3ZT:deployments wim$ kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
nginx-deployment-47451619-1nqrl   1/1       Running   0          17s
nginx-deployment-47451619-3r235   1/1       Running   0          17s
nginx-deployment-47451619-5281h   1/1       Running   0          12s
nginx-deployment-47451619-7vjqw   1/1       Running   0          10s
nginx-deployment-47451619-f1l90   1/1       Running   0          8s

Leave a Reply

Your email address will not be published. Required fields are marked *