image

In this lab we will practice creating pod 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 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
apiVersion: v1

kind: Pod

metadata:

  name: demo

  labels:

    app: demo

    type: web

spec:

  containers:

  - name: demo-nginx

    image: nginx

    ports:

    - containerPort: 80

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

Connect to pod and run ls -la to chec kdirectory structure

Solution
kubectl exec -it demo -- bash

this command will provide prompt inside pod

run ls -la to check files inside pod

Check pod logs using kubectl command

Solution
kubectl logs demo

this command show logs for pod and app running in pod

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

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

Which node is pod running on?

Solution
kubectl get pods -o wide

Find details on pods created in this lab

Solution
kubectl describe pod demo

this command will provide details of pod

Use kuberctl command to generate yaml for creating a pod

name: demo

image: nginx

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

Use kuberctl command to get pod details in json format

name: demo

image: nginx

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

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

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