controllers

  • replication controller

    • the old one
  • replica set

    • recommended way to setup applications

same purpose but not the same system


replication controller:

---
apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: nginx
      labels:
        apps: nginx
        type: front-end
    spec:
      containers:
        - name: nginx
          image: nginx

  replicas: 3

ReplicationController is a nested setup

  • first metadata is for the replication controller > parent
  • the next metadata is for the POD > child
> kubectl get replicationcontrollers

NAME       DESIRED   CURRENT   READY   AGE
myapp-rc   3         3         3       67s

replica set:

---
apiVersion: apps/v1 
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end
  
spec:
  template:
    metadata:
      name: nginx
      labels:
        apps: nginx
        type: front-end
    spec:
      containers:
        - name: nginx
          image: nginx
  replicas: 3
  selector: 
    matchLabels:
      type: front-end

ReplicaSets ensure that defined number of pods is always running

object spec:

  • spec:
    • template: > for pods - make of metadata infos
    • selector: > make using of matchLabels

One major difference is that replica sets needs to define a selector for the replicas. The selector can also manage other pods which are not in this definition file. Cause of this a labeling is required and using matchLabels. You can label and select multi labels that must match.
# show replicasets
> kubectl get replicaset
> kubectl get rs

Replicaset can be used to monitor existence pods - if there a are already created - if not the rs will it create. This is the role of a rs. How did they know what to do? Labels are the key - using matchLabel will catch the provided label given to a pod. To create a new pod if one dies the template section is required from there the rs will get his infos. Replicaset can be updated on multiply ways.

  1. update replicas value in the yml file and apply using replace
> kubectl replace -f replicaset-definition.yml
  1. using kubectl
> kubectl scale --replicas=7 -f replicaset-definition.yml
> kubectl scale --replicas=3 replicaset myapp-replicaset

within the second method the yaml definition file will not be updated we can use it to automate scale pods depending on load

inspect replicaset:

> kubectl describe replicasets myapp-replicaset1

Name:         myapp-replicaset
Namespace:    default
Selector:     app=myapp
Labels:       app=myapp
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  16m    replicaset-controller  Created pod: myapp-replicaset1-6jrqc
  Normal  SuccessfulCreate  16m    replicaset-controller  Created pod: myapp-replicaset1-h665k
  Normal  SuccessfulCreate  16m    replicaset-controller  Created pod: myapp-replicaset1-lnhk2
  Normal  SuccessfulCreate  3m23s  replicaset-controller  Created pod: myapp-replicaset1-b7sxh
  Normal  SuccessfulCreate  2m31s  replicaset-controller  Created pod: myapp-replicaset1-d7r5h
  Normal  SuccessfulCreate  119s   replicaset-controller  Created pod: myapp-replicaset1-lhsk7
  Normal  SuccessfulCreate  119s   replicaset-controller  Created pod: myapp-replicaset1-f8djj

If we create a new pod outside from this replicaset including the same label - this pod will be terminated cause it is one to much.

edit temporaly in memory created replicaset

> kubectl edit replicaset myapp-replicaset

changes takes effect after closing the file

we can also scale with a command

> kubectl scale replicaset myapp-replicaset --replicas=2
> kubectl scale rs myapp-replicaset --replicas=2

make usage of the internal help

> kubectl explain replicaset | grep VERSION