Saturday, February 27, 2021

Day 24 - Kubernetes - Pod-ReplicationController-Replicaset

 Pod-ReplicationController-Replicaset - 2/26/2021

POD
Please use PyCharm from getbrains.com
Create a POD using yaml file

1. Login to your master node
# kubernetes definition has four root level properties
kc='kc=kubectl'
# apiVersion, kind, metadata, spec

# cat pod-def.yaml

apiVersion: v1

kind: pod

metadata:
    name: myapp-pod
    labels:
      app: myapp

spec:
  containers:
    - name: nginx-container
      image: nginx
    

Create a pod with yaml file
# kc get pods
# kc create -f pod-def.yaml

pod is created
# kc get pods


==============================

kubernetes controller
- Controller are brain behind the kubernetes.
- They Monitors kubernetes objects and respond accordingly.

Replication Controller
- When your pod goes bad, we want to run new pod
- RC allow you to run multiple instances.
- Even you have a single pod you can still run RC. If this pod fails, it wil bring new
- It helps to load balance and scaling
- You can create pods and if demand increases, it adds new pod automatically.

Thre are two terms
- Replication Controller
- Replica set

RC is old and RS is new. They work on same purpose of RS is latest comes with new features.

lets create yaml file

# cat my-rs.yaml
apiVersion: v1
kind: ReplicationController
metadata:    # Metadata for RC
  name: myapp-rc
  labels:
    app: myapp
    type: front-end

spec:    # important part. It defines whats inside the object we are creating. What RS creates?
  - template:     # to define other values to reuse..
      metadata:    # this metadata is for POD
        name: myapp-pod
        labels:
    app: myapp
    type: front-end
        spec:    # POD spec file
          continers:
            -name: nginx-controller
             image: nginx

  - replicas: 3

we addded new property called replicas: 3


> kc create -f rc-def.yaml
> kc get replicationcotroller
> kc get pods    # see the pod created by RC
Pod name starts with RC name


Now, sets see ReplicaSet

apiVersion is different here.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: myapp-rs
    labels:
       app: myapp
       type: front-end
spec:
  template:
     metadata:
       name: myapp-pod
       labels:
          app:myapp
          type: frontend
    spec:
      containers:
        - name: nginx-controller
          image: nginx

replicas: 3    # here, replicas need selector
selector:        # it identifies what pod it falls under. Replica set can also manage PODs
        # that are not created as part of replicaSet creation.The pods created before replicaSet is created
        # but matches the labels specified under selector, the replicaSet will also manages those PODs.
        # the major difference between RC and RS is the selector. Its an optional on RC config file.
        # In case of RS, it need to define under matchLabels
# selector:
  matchLabels:    # matchLabel selector matches the label specified under it to the label on POD.
    type: fron-end
    

> kc create 0f repset-def.yaml
> kc get replicaset
> kc get pods

Labels and selectors
- ILets say you deploed three pods
- We like to create RS or RC to manages these pods or to ensure that there are available all the time.
- In this situation, you can use replicaSet to manages these pods but RC can not do it.
- If they are not created, RS will create for them.
- The role of RS is to monitor pods, and in case they failed, create them.
- RS is infact process that monitors pods

How RS knows what pod to monitor?
- This is where labeling comes in handy
- This is where we can provide label as a filter for replica set.
- Under selector section, you specify the matchLabels filter and provide the same lable that you use while creating the POD.

Under RS, we knew three new sections under spec.
- template
- replicas
- selector

Using RS, we can not create new PODs with matching labels that are already created.
In case one of the pod fails and need to be created, you need to include template definition part to add labels.

To update the pods, lets change the replicas to and run the command
> kc replace -f replicaset-def.yaml
> kc scale --replicas=6 -f replicaset-def.yaml
> kc scale --replicas=6 replaceset myapp-replicaset

# note, by defining file, it does not automatically so you have to complete all command

Command summary
> kc create -f replaceset-def.yaml
> kc get replicaset            # List pods
> kc delete replicaset myapp-replicaset # Deleetes PODs
> kc replae -f replicaset-definition.yaml    # update replicaset
> kc scale --replicas=6 -f replicaset-def.yaml    # without modifying file, increase the pod/replicas






This example comes from Udemy. Please purchase to get full access. One of the best course.
https://www.udemy.com/course/certified-kubernetes-administrator-with-practice-tests/l



No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...