image

In this lab we will practice creating pod with kubectl commands

In case you prefer a video, check below our YouTube video for this lab

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

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

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

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

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

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

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

Solution
kubectl get all

Task: Access container port using browser

Solution

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

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: Use kubectl command to generate yaml for creating a pod

name: web

image: nginx

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

Task: Which node is pod running on?

Solution
kubectl get pods -o wide

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

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.