image

In this lab we will practice creating pod with yaml file

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

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

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

name: demo
image: nginx

Use Kubectl command

verify with below command

kubectl get pods
Solution

create a new file pod.yaml

vi pod.yaml

Press i to get in insert mode

apiVersion: v1
kind: Pod
metadata:
  name: demo
  labels:
    app: demo
    type: web
spec:
  containers:
  - name: demo-nginx
    image: nginx
    ports:
    - containerPort: 80

use escape to exit insert mode and :wq to save and exit vi

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

you may add labels

you can use any key: value pairs for labels

labels are used to select pods

provide name to container

provide image for container

kubectl apply -f pod.yaml

this command will create pod using yaml file

Task: Connect to pod and run ls -la to check directory structure

Solution
kubectl exec -it demo -- bash

this command will provide prompt inside pod

run ls -la to check files inside pod

Task: Check pod logs using kubectl command

Solution
kubectl logs demo

this command show logs for pod and app running in pod

Task: Use kubectl command to stream logs from pod and app inside pod

Solution
kubectl logs -f demo

this command will print pod logs on terminal

it will follow logs

use Ctl + c t stop logs and exit

Task: Use kubectl command to stream new logs from pod and app inside pod

get only 1 logs from history

Solution
kubectl logs -f demo --tail=1

tail command will start streaming from current logs

with 1 recent log

Task: Which node is pod running on?

Solution
kubectl get pods -o wide

Task: Find details on pods created in this lab

Solution
kubectl describe pod demo

this command will provide details of pod

Task: Use kubectl command to generate yaml for creating a pod

name: demo

image: nginx
Solution
kubectl run demo --image=nginx  --dry-run=client -o yaml

Task: Use kubectl command to get pod details in json format

name: demo

image: nginx
Solution
kubectl run demo --image=nginx  --dry-run=client -o json

Task: Delete pod you created in previous step using kubectl

verify with below command

kubectl get pods
Solution
kubectl delete pod demo

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.