image

In this lab we will practice creating replicaset with yaml file

Create Kubernetes cluster with 3 worker nodes.

Master: 1 node

Worker: 2 node

Hint

Solution

Create docker hub account. Docker Hub if you already have one skip this step

Open Play with Kubernetes login with your docker hub account.

Click on start

It will start a 4 hr session

create three instance

click on + ADD NEW INSTANCE three time to add three instances

image

on first instance enter below command, this node will be master node

kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16

enter below command on first node

kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml

capture output of kubeadm join XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

image

enter captured command in second and third node

kubeadm join  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

image

image

Check node status, all 3 nodes should be in ready state

image

Create a Replicaset with below settings

name: demo
image: nginx
replca: 3
labels:
  app: demo
  type: web

verify with below command

kubectl get rs
Solution Enter terminal of master node and create a new file with
vi rs.yaml
apiVersion: apps/v1

kind: ReplicaSet

metadata:

  name: demo

  labels:

    app: demo

    type: web

spec:

  replicas: 3

  selector:

    matchLabels:

      type: web

  template:

    metadata:

      labels:

        type: web

    spec:

      containers:

      - name: demo-nginx

        image: nginx

        ports:

        - containerPort: 80
kubectl apply -f rs.yaml

this command will create replicaset

How many pods are created by Replicaset?

Solution
kubectl get pods

Which nodes are pods running on?

Solution
kubectl get pods -o wide

Find details on Replicaset created in this lab

Solution
kubectl describe rs demo

this command will provide details of ReplicaSet

Use kubectl command to get Replicaset details in yaml format.

Solution
kubectl get rs demo -o yaml

Use kubectl command to get Replicaset details in json format.

Solution
kubectl get rs demo -o json

Scale replicaset to 5 replicas

verify with below command

kubectl get rs
Solution
kubectl scale  rs demo --replicas=5

How many pods are managed by Replicaset?

Solution
kubectl get pods

Confirm that pod is managed by Replicaset

Solution
kubectl describe pods demo-XXXX

Delete Replicaset we created in this lab

Solution
kubectl delete rs demo

Delete all three nodes and close session

  1. click on first node and then DELETE
  2. Repeat same for second and third node
  3. click close session

image

Congratulations you have completed this lab

Let us know if you still have any question
Youtube video for this lab