image###

Task: 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

imageon 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

you may also use kubeadm token list to find token

use this command on second and third node kubeadm join <IP address of master/first node>:6443 –token –discovery-token-unsafe-skip-ca-verification

image

enter captured command in second and third node

kubeadm join  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
imageimageCheck node status, all 3 nodes should be in ready state image

Task: Create a pod using Kubectl and expose it

name: web

image: nginx

port: 80

verify that a ClusterIP service has been created with below command

kubectl get svc web
kubectl get ep
kubectl get all
Solution
kubectl run web --image=nginx --port=80 --expose

Task: Create a pod using Kubectl and access the service created in previous step

name: jumphost

image: busybox
Solution

Get IP address of service by running below command

kubectl get svc web

Create a new pod and connect to its shell

kubectl run jumphost --image=busybox -it  -- sh

Use IP address to access the service in new pod

wget -O- <IP address>:80

Task: Create a ClusterIP service using yaml

name: web1-svc

selector: app=web

verify that a ClusterIP service has been created with below command

kubectl get svc web1-svc
kubectl get ep
kubectl get all
Solution

create a new file svc.yaml

vi svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: web1-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: ClusterIP # this is optional as ClusterIP is default type

use escape key and :wq to save and exit vi.

apiVersion, Kind, metadata.name and spec are required field.

kubectl apply -f svc.yaml

this command will create service using yaml file

Task: Use jumphost pod and access the service created in previous step

name: jumphost

image: busybox
Solution

Get IP address of service by running below command

kubectl get svc web1-svc

Connect to jumphost pod shell

kubectl exec busybox -it  -- sh

Use IP address to access the service in new pod

wget -O- <IP address>:80

Task: Create NodePort Service for pod web

verify with below command

kubectl get svc
kubectl get ep
kubectl get all
Solution
 kubectl expose pod web --port 80 --name=nginx-svc --type=NodePort --target-port=80

Task: Find port used by NodePort service in Kubernetes to expose container

Solution
kubectl get all

Task: Access pod port exposed in previous step using browser

Solution

Task: Create NodePort service using Kubectl command

name: nginx-svc

container port: 80

target port: 80

nodeport: 30001

verify with below command

kubectl get svc
Solution
kubectl create service nodeport nginx-svc1 --tcp=80:80 --node-port=30001

Task: Try accessing nodeport using URL with port 30001

It will not work as nodeport service require label to match container

Add label to get nodeport service working

verify with below command

kubectl describe svc nginx-svc1
Solution
 kubectl label pod web app=nginx-svc1

Task: Access container port using browser

Solution

Task: Create NodePort service using yaml

name: nginx-svc2

container port: 80

target port: 80

nodeport: 30002

selector: run=web

verify with below command

kubectl get svc
Solution
vi svc1.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc2
  namespace: default
spec:
  ports:
  - nodePort: 30002
    port: 80
    protocol: TCP #optional as TCP is default
    targetPort: 80
  selector:
    run: web
  type: NodePort

use escape key and :wq to save and exit vi.

apiVersion, Kind, metadata.name and spec are required field.

kubectl apply -f svc1.yaml

this command will create nodeport service using yaml file

Task: Access container port using browser

Solution

Task: Change service web1-svc to NodePort

verify with below command

kubectl get svc
Solution

This can be done in two ways

kubectl edit svc web1-svc

and change service type from ClusterIP to NodePort

or

kubectl patch svc web1-svc -p '{"spec":{"type":"NodePort"}}'

use escape key and :wq to save and exit vi.

apiVersion, Kind, metadata.name and spec are required field.

kubectl apply -f svc1.yaml

this command will create nodeport service using yaml file

Task: Access container port using browser

Solution

Task: Create a deployment using Kubectl

name: http

image: nginx

port: 80

replica: 3
kubectl get all
Solution
kubectl create deployment http --image=nginx --port=80 --replicas=3

Task: Create a service type ClusterIP to connect pods in deployment http

name: http

type: ClusterIp

ports: 8080:80
Solution

Get IP address of service by running below command

kubectl create svc  clusterip http --tcp=8080:80

This command will create service type ClusterIP

Task: Create a pod using Kubectl and access the service created in previous step

name: jumphost

image: busybox
Solution

Get IP address of service by running below command

kubectl get svc web

Create a new pod and connect to its shell

kubectl exec jumphost -it  -- sh

Use IP address to access the service in new pod

wget -O- <IP address>:8080

Task: Modify service nginx-svc to point to pods in deployment https

Solution

Get IP address of service by running below command

kubectl edit svc nginx-svc

change selector from run: web to app: http

Task: Delete pod web and verify access to service nginx-svc

Solution
kubectl delete pods web

Task: Modify service nginx-svc1 to point to pods in deployment http

Solution

This can be done in two ways

kubectl edit svc nginx-svc1

use I for insert mode and escape key and :wq to save and exit change selector from run: web to app: http

or

kubectl patch svc nginx-svc1 -p '{"spec":{"selector":{"app":"http"}}}'

Task: Delete pod web and verify access to service nginx-svc

Solution

Task: Delete all open nodes/instances and close session

  1. Select the node and click on DELETE
  2. Repeat same for any other open nodes
  3. click close session

cleanup}}


Click on ‘Submit Feedback’ on the bottom left of the page to submit any questions/feedback.