image

In this lab we will practice creating pod with kubectl commands

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 pod with name web and image nginx

name: web

image: nginx

Use Kubectl command

verify with below command

kubectl get pods
Solution

this command will create pod with image nginx and name web

kubectl run web --image=nginx

Delete pod you created in previous step using kubectl

verify with below command

kubectl get pods
Solution
kubectl delete pod web

this command will delete pod with name web

Create a pod using Kubectl and expose port

name: web

image: nginx

port: 80

verify with below command

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

Create a pod using Kubectl and expose port, port should be accessible from outside kubernetes cluster

name: web

image: nginx

port: 80

verify with below command

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

Find port used by nodeport service in Kubernetes to expose contianer

Solution
kubectl get all

Access container port using browser

Solution

Create nodeport service using kubectl command

name: nginx-svc

container port: 80

target port: 80

nodeport: 3001

verify with below command

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

Try accessing nodeport using URL with port 30001

It will not work as nodeport service requrie 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

Access container port using browser

Solution

Use kuberctl command to generate yaml for creating a pod

name: web

image: nginx

Solution
kubectl run web --image=nginx  --dry-run=client -o yaml

Which node is pod running on?

Solution
kubectl get pods -o wide

Delete pod you created in previous step using kubectl

verify with below command

kubectl get pods
Solution
kubectl delete pod web

this command will delete pod with name web

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