Kubernetes useful commands

Today started to learn new latest container technology Kubernetes and How this works on cloud and local systems.

MiniKube is the smaller version kubernets on your localsystem. Installation: Download virutal box and install on windows 10 system.

Open power-shell and install required tools : chocolatey and minikube

PS:C:\minikube> iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))

PS:C:\minikube> choco install virtualbox -y

PS:C:\minikube> choco install minikube -y

PS:C:\minikube> choco install minikube –version 1.10.1 -y

PS:C:\minikube> minikube start –driver=virtualbox –alsologtostderr

PS:C:\minikube> minikube status

if you facing any issue on installation then delete minikube and install again

PS:C:\minikube> minikube delete ( repeat above step to install minikube)

PS:C:\minikube> minikube dashboard

http://127.0.0.1:55467/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/pod?namespace=default

Pods

Pods are essential parts on kubernetes

PS:C:\minikube> kubectl get all (checking current running pods)

Below Sample Pod yaml format


apiVersion: v1
kind: Pod
metadata:
name: webapp

labels:
app: webapp
release: “0”
spec:
containers:

name: webapp
image: richardchesterwood/k8s-fleetman-webapp-angular:release0


Save this on first-pod.yaml on same directory

PS:C:/minikube> kubectl apply -f first-pod.yaml (apply new pod created)

PS:C:/minikube> kubectl get all (check the pod running status)

Note: Pod not visible outside kubernetes cluster without expose ports.

PS:C:\minikube> minikube ip (finding IP address for minikube)

PS:C:\minikube> kubectl describe pod webapp (view details webapp pods)

PS:C:\minikube> kubectly exec webapp — ls (list of files inside pod)

Services

Services are stable on kubernets and its connected to pods.

Pods using labels / service using selector (match both key value pair)

ClusterIP — This service access internal only

NodePort — Expose port through the node to the outside access

kubernetes recommend Node-Port number greater than 30,000

Below sample service yaml format


apiVersion: v1
kind: Service
metadata:
name: fleetman-webapp

spec:
# This defines which pods are going to be represented by this Service
# The service becomes a network endpoint for either other services
# or maybe external users to connect to (eg browser)
selector:
app: webapp
release: “0”

ports:
– name: http
port: 80
nodePort: 30080

type: NodePort


Save this file as a “webapp-service.yaml” on the same directory

PS:C:\minikube> kubectl apply -f webapp-service.yaml (apply service to cluster whenever new update on code)

PS:C:\minikube> kubectl get all (check the status)

PS:C:\minikube> minikube ip (note it down ip address)

Access from browser url: http://192.168.99.102:30080/

We can add multiple pod on same yaml file using three dot separator(—)

Release: “0” – use this method to avoid down time loading images

PS:C:\minikube> kubectl describe svc fleeman-webapp (check the selector and release connected appropriate pod)

PS:C:\minikube> kubectl get pods (shows list of pods)

PS:C:\minikube> kubectl get po –show-labels (shows pods with labels)

PS:C:\minikube>kubectl get po –show-labels -l release=0 (with release)

PS:C:\minikube> kubectl apply -f . (apply all yaml file update)

PS:C:\minikube> kubectl delete po webapp-release (delete pod)

PS:C:\minikube> kubectl delete svc webapp-release (delete service)

PS:C:\minikube> kubectl delete po — (delete all pods)

ReplicaSets

Below sample ReplicaSets format


apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: webapp
spec:
selector:
matchLabels:
app: webapp
replicas: 2
template: # template for the pods
metadata:
labels:
app: webapp
spec:
containers:
– name: webapp
image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5


PS:C:\minikube> kubectl get all (list of running pods)

PS:C:\minikube> kubectl delete po –all (delete all pods)

PS:C:\minikube>kubectl describe replicaset webapp (information about replicaset)

PS:C:\minikube> kubectl describe rs webapp ( short code of replicaset)

PS:C:\minikube> kubectl delete rs webapp (Delete replicaset)

Depolyments

Deployment can be roll back to previous stage when failure happen.

Rolling update and Zero downtime using replicaset on this yaml file.

Below Sample Deployment yaml format


apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
# minReadySeconds: 30
selector:
matchLabels:
app: webapp
replicas: 2
template: # template for the pods
metadata:
labels:
app: webapp
spec:
containers:
– name: webapp
image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5


PS:C:\minikube> kubectl rollout status deploy webapp (status for rollout)

PS:C:\minikube> kubectl rollout history deploy webapp (history of deployment)

PS:C:\minikube> kubectl rollout undo deploy webapp (revision return old version deployment)

Kubernetes can remember last 10 revision history.

Rollback feature use only in emergency stage.

Networking and Service Discovery

PS:C:\minikube> kubectl get namespace (shows namespace)

PS:C:\minikube> kubectl get pod (shows pods in the default namespace)

PS:C:\minikube> kubectl get pods -n kube-system (shows pods running on kube-system namespace)

PS:C:\minikube> kubectl get all -n kube-system (list of all services and pods running on kube-system pod)

PS:C:\minikube> kubectl describe svc kube-dns -n kube-system (service on namespace check)

Sample yaml file format mysql database connect with pods


apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
app: mysql
spec:
containers:name: mysql image: mysql:5 env: # Use secret in real life

name: MYSQL_ROOT_PASSWORD
value: password

name: MYSQL_DATABASE

value: fleetman

kind: Service
apiVersion: v1
metadata:
name: database
spec:
selector:
app: mysql
ports:

port: 3306
type: ClusterIP