Let’s learn kubernetes by handson
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters
You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs etc
kubectl version
Linux
Windows
This should be default one
minikube start
or
minikube start --driver=docker
Command | Description | Example |
---|---|---|
kubectl config view | Show Merged kubeconfig settings | kubectl config view |
kubectl config get-contexts | display list of contexts | kubectl config get-contexts |
kubectl config current-context | display the current-context | kubectl config current-context |
Command | Description | Example |
---|---|---|
kubectl config use-context my-cluster-name | set the default context to my-cluster-name | kubectl config use-context minikube |
Specify -n <namespace> if object is not in default namespace
Command | Description | Example |
---|---|---|
kubectl get pods/po/pod | List all pods in default namespace | kubectl get pods |
kubectl get pods -A | List all pods in all namespaces | kubectl get pods -A |
kubectl get pods –show-labels | Show labels for all pods | kubectl get pods --show-labels |
Command | Description | Example |
---|---|---|
kubectl describe | Describe object | kubectl describe pod <pod name> |
kubectl get deploy/deployments | List deployments | kubectl get deploy |
kubectl get rs | List replicaset | kubectl get rs |
kubectl get nodes/no | List all nodes in cluster | kubectl get nodes |
Command | Description | Example |
---|---|---|
kubectl get all -A | List all objects in all namespaces | kubectl get all -A |
apiVersion
kind
metadata
labels:
name:
spec
Command | Description | Example |
---|---|---|
kubectl run | Create and run a particular image in a pod | kubectl run nginx --image nginx |
kubectl create | Create a resource from a file or from stdin | kubectl create deployment nginx-deploy --image=nginx |
Command | Description | Example |
---|---|---|
kubectl apply | Apply a configuration to a resource by filename or stdin | kubectl apply -f <manifest.yaml/manifest.json/folder path> |
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/redis-pod.yaml --dry-run=client
or
kubectl apply -f redis-pod.yaml --dry-run=client
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/redis-pod.yaml
or
kubectl apply -f redis-pod.yaml
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/redis-deploy.yaml --dry-run=client
or
kubectl apply -f redis-deploy.yaml --dry-run=client
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/redis-deploy.yaml
or
kubectl apply -f redis-deploy.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
labels:
app: redis
spec:
containers:
- name: redis
image: redis
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- image: redis
name: redis
Command | Description | Example |
---|---|---|
kubectl exec | Run a command in a running container | kubectl exec -it nginx -- bash |
kubectl logs | Fetch the logs of a pod | kubectl logs <pod name> |
Command | Description | Example |
---|---|---|
kubectl scale | Scale resources | kubectl scale deployment redis --replicas=2 |
Note: This won’t be updating manifest file so if you delete and create resource it will create whatever replicas mentioned in file
apiVersion: v1s
kind: Pod
metadata:
name: counter
spec:
containers:
- name: count
image: busybox
args: [/bin/sh, -c,
'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/busy-box-pod.yaml
kubectl get pods
Command | Description | Example |
---|---|---|
kubectl delete | Delete resources | kubectl delete deployment redis |
kubectl delete | Delete resources | kubectl delete -f <manifest file used to create resource> |
kubectl create deployment nginx-deployment --image nginx --dry-run=client -o yaml > nginx-deploy.yaml
kubectl [command] --help
kubectl cheatsheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
kubectl book: https://kubectl.docs.kubernetes.io/
This image was created during docker session
To verify if image present on system
docker images
For those who don’t have this image
From git bash/linux terminal
git clone https://github.com/slashpai/docker_k8s_training.git
cd docker_k8s_training/docker_apps/simple_webapp
docker build . -t simple-webapp:v1
kubectl create deployment simple-webapp --image simple-webapp:v1
kubectl get deployments
kubectl get pods
kubectl describe deploy simple-webapp
Login to minikube node
minikube ssh
docker images
Type exit
to quit from minikube
eval $(minikube docker-env)
Now any ‘docker’ command you run in this current terminal will run against the docker inside minikube cluster
If you do the following commands, it will show you the containers inside the minikube, inside minikube’s VM or Container
docker ps
docker build . -t simple-webapp:v1
kubectl get pods
minikube cache add simple-webapp:v1
You are creating a service here with below command
kubectl expose deployment simple-webapp --type=NodePort --port=4567
kubectl get services/svc
minikube service simple-webapp
Try updating the simple web app (index.erb in views folder)
Build new image use a different tag
docker build . -t simple-webapp:v2
Try setting new image for deployment
kubectl set image deploy simple-webapp simple-webapp=simple-webapp:v2
Run and verify change
minikube service simple-webapp
https://github.com/slashpai/docker_k8s_training/blob/main/kubernetes/manifests/simple-webapp-env.yml
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/simple-webapp-env.yml
kubectl expose deploy simple-webapp-env --type NodePort --port 4567
minikube service simple-webapp-env
kubectl create service nodeport simple-webapp --tcp 4567:4567 --dry-run=client -o yaml
kubectl create configmap simple-webapp-cm --from-literal APP_COLOR=blue
kubectl get configmap simple-webapp-cm
kubectl get configmap simple-webapp-cm -o yaml
kubectl apply -f https://raw.githubusercontent.com/slashpai/docker_k8s_training/main/kubernetes/manifests/simple-webapp-configmap.yml
kubectl expose deploy simple-webapp-cm --type NodePort --port 4567
minikube service simple-webapp-cm
Add-ons extend the functionality of Kubernetes.
https://kubernetes.io/docs/concepts/cluster-administration/addons/
minikube addons list
mknikube dashboard
minikube addons enable metrics-server
Wait some time till metrics pods are functional
kubectl top nodes
kubectl top pods
More details: https://minikube.sigs.k8s.io/docs/
Imperative vs Declarative method
kubectl create vs kubectl apply
kubectl create deployment nginx-deploy –image nginx
Create the objects defined in a configuration file:
kubectl create -f nginx.yaml
Process all object configuration files in the configs directory, and create or patch the live objects
You can first diff to see what changes are going to be made, and then apply
kubectl diff -f configs/
kubectl apply -f configs/
Read More: https://kubernetes.io/docs/concepts/overview/working-with-objects/object-management/
git clone https://github.com/dockersamples/example-voting-app.gits
https://github.com/dockersamples/example-voting-app#run-the-app-in-kubernetes
Kubernetes commands with Handson
Deploying custom application
Minikube features and familiarization