Kubernates - practicals

To get more practical insight about internals of Kubernetes 

1. https://github.com/kelseyhightower/kubernetes-the-hard-way

2. Learn Kubernetes using Interactive Browser-Based Scenarios

Hands-on with Minikube: single node kubernates cluster

To install Minikube : 

Free course : “Kubernetes Hardway”


Then execute command:

minikube start

Now play around with Minicube with kubectl

Overview of kubectl


kubectl Cheat Sheet


5. K8S client



6. https://github.com/kubernetes-incubator

7. Ansible modules


8. Useful commands



kubectl run hw --image=karthequian/helloworld --port=80

Deployment name is : hw

kubectl get all
kubectl get pods
kubectl get pods --all-namespaces

the pod is only accessible by its internal IP address within the cluster. To make a container accessible from outside the Kubernetes virtual network, one has to expose the pod as a Kubernetes service using expose command

kubectl expose deployment hello-minikube --type=NodePort

To get YAML file at deployment

kubectl get deploy/hw -o yaml
kubectl get helloworld-service -o yaml


kubectl create -f helloworld-deployment.yml
kubectl create -f helloworld-service.yml
minikube service helloworld


kubectl get rs //replica set
kubectl scale --replica=3 deply/helloworld-deployment

With Labels

kubectl get pods --show-labels
kubectl label pod/helloworld app=newName --overwrite // to overwrite
kubectl label pod/helloworld app- // to delete

Labels can be used with deployments, services, replica sets etc. 

With Selector

To search

kubectl get pods -l label1=value1,label2=value2

kubectl get pods -l label1!=value1

kubectl get pods -l label1 in (value1, value2)

kubectl get pods -l label1 notin (value1, value2)

One can use --selector instead of -l

To delete we can use 

kubectl delete pods -l .....

Health check

One can add readinessProbe and livenessProbe in YAML file

Upgrade and roolback

kubectl create -f helloworld-black.yaml --record

--record is used to add it to roll out history

kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:blue

kubectl rollout history deployment/navbar-deployment

kubectl rollout undo deployment/navbar-deployment

to rollback to a specific version. To do this, add a `--to-revision=version`


kubectl describe pod "pod name"
kubectl describe deployment "deployment name"
kubectl logs "pod name"
kubectl exec --it "pod name" /bin/bash
kubectl exec --it "pod name" -c "container name" /bin/bash


minikube addons list
minikube addons enable "name"
minikube dashboard

kubectl edit "pod name"


an example of "log_level", and pass the value "debug" to a pod via a configmap in this example.

To create a configmap for this literal type 
kubectl create configmap logger --from-literal=log_level=debug

To see all your configmaps: `kubectl get configmaps`

To read the value in the logger configmap: `kubectl get configmap/logger -o yaml`

To edit the value, we can run `kubectl edit configmap/logger`

Application Secretes

E.g database passwords, API tokens
They cannot be part of YML file. 

kubectl create secrete
kubectl get secrete

We have similar CLI commands for cronjobs, statefulsets and namespaces

kubectl create cronjobs
kubectl edit cronjobs/hellow

kubectl create -f "yaml file for statefulsets"
kubectl get statefulsets

namespace provides multi-tenancy to k8s instance. k8s provides multiple virtual cluster on same physical cluster. 

kubectl get namespaces
kubectl create namespaces "name"
kubectl delete namespaces "name"

Auto Complete

source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.

echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.

Node IP Address

# Get ExternalIPs of all nodes
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'


# List Events sorted by timestamp
kubectl get events --sort-by=.metadata.creationTimestamp


kubectl autoscale deployment foo --min=2 --max=10                # Auto scale a deployment "foo"

Running Pods

kubectl logs my-pod                                 # dump pod logs (stdout)
kubectl port-forward my-pod 5000:6000               # Listen on port 5000 on the local machine and forward to port 6000 on my-pod
kubectl top pod POD_NAME --containers               # Show metrics for a given pod and its containers

Use Case : nginx server with load balancer 

kubectl run nginx --image = nginx: 1.10 --replicas = 5

kubectl get deployments
kubectl get pods

kubectl expose deployment nginx -type=LoadBalancer -port=80
kubectl get svc

Reference : 


Manish Panchmatia said...


Post a Comment