Kubernetes Cluster
- A group or pool of nodes form a powerful machine, which is a cluster
- When you deploy programs in cluster, it intelligently distributes the work to available nodes
# cluster details
kubectl cluster-info
# troubleshoot cluster
kubectl cluster-info dump
Kubernetes Config
- Modify cluster config and switch between context of a cluster
- You can have different config for a single cluster; every single config is called context
# view current kubectl context/config
kubectl view config
# switch context/config of a cluster
kubectl config set current-context config-name
(i.e., kubectl config set current-context minikube)
# list all the available context/config
kubectl config get-contexts
Kubernetes Node
- A node can be a physical machine or virtual machine
LIST NODES
# list all nodes in a cluster
kubectl get nodes
# list all nodes in a cluster with details [Internal/External IP, etc.,]
kubectl get nodes -o wide
# get External IP of a node in a cluster
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'
[get Internal IP of a node in a cluster]
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
DELETE NODES
# remove a node from the service
kubectl drain
kubectl drain --ignore-daemonsets --force
(e.g., kubectl drain minikube --ignore-daemonsets --force)
# uncordon the drained node
kubectl uncordon
(e.g., kubectl uncordon minikube)
MANIPULATE NODES
# troubleshoot node
kubectl describe node node_name
(i.e., kubectl describe node minikube)
Kubernetes Namespace
- Namespace is intended for in use of environments
CREATE NAMESPACE
# create namespace
kubectl create namespace namespace_name
(i.e., kubectl create namespace zalenium)
LIST NAMESPACE
# list all the namespaces
kubectl get namespaces
# list all the namespaces with labels
kubectl get namespaces --show-labels
# list specific namespace with labels
kubectl get namespace namespace_name --show-labels
(i.e., kubectl get namespace zalenium --show-labels)
# list all namespaces with namespace counts
kubectl get pods --all-namespaces -o jsonpath="{..namespace}" |tr -s '[[:space:]]' '\n' |sort |uniq -c
DELETE NAMESPACE
# delete all namespaces
kubectl delete --all namespaces
# delete specific namespace
kubectl delete namespace namespace_name
(i.e.,
kubectl delete --all pods --namespace=zalenium
kubectl delete namespace zalenium
)
Kubernetes Service
- A service is a grouping of Pods running on a cluster; you can have many services in a cluster; moreover, it helps you as a load balancer, and provide zero-downtime application deployments
LIST SERVICES
# list all the services in default namespace
kubectl get service
kubectl get services
# list all the services in another namespace
kubectl get services --namespace namespace_name
(i.e., kubectl get services --namespace zalenium)
# list all the services with details [selector]
kubectl get services --namespace namespace_name -o wide
(i.e., kubectl get services --namespace zalenium -o wide)
# list all the services with selector filter
kubectl get services --all-namespaces --selector=selector_name
(i.e., kubectl get services --all-namespaces --selector=app.kubernetes.io/name=zalenium -o wide)
# list all the services from entire namespace
kubectl get services --all-namespaces
DELETE SERVICES
# delete kubernetes service
kubectl delete service service_name --namespace namespace_name
(i.e., kubectl delete service zalenium --namespace zalenium)
Kubernetes Deployment
- When you create a deployment, it creates a pod with containers in it
CREATE DEPLOYMENTS
# create deployment that deploy pods with dynamic image name
kubectl create deployment deployment_name --image=image_name
(i.e., kubectl create deployment ngnix --image=nginx)
LIST DEPLOYMENTS
# list all the deployments in the default namespace
kubectl get deployment kubectl get deployments
# list all the deployments in another namespace
kubectl get deployments --namespace namespace_name
(i.e., kubectl get deployments --namespace zalenium)
# list all the deployments with details [container name, image, selector]
kubectl get deployments --namespace namespace_name -o wide
(i.e., kubectl get deployments --namespace zalenium -o wide)
# list all the deployments with selector filter
kubectl get deployments --all-namespaces --selector=selector_name
(i.e., kubectl get deployments --all-namespaces --selector=app.kubernetes.io/name=zalenium -o wide)
# list all the deployments from entire namespace
kubectl get deployments --all-namespaces
DELETE DEPLOYMENTS
# delete deployment
kubectl delete deployment deployment_name --namespace namespace_name
(i.e., kubectl delete deployment zalenium --namespace zalenium)
MANIPULATE DEPLOYMENTS
# expose deployment - NodePort [this will create a service from the deployment/pod]
kubectl expose deployment pod_name --type=NodePort
SCALE DEPLOYMENTS
[make sure the deployment is in running state whenever you scale them]
# scale the existing deployment
kubectl scale deployment --all --replicas=4
# scale a specific deployment
kubectl scale deployment/deployment_name --all --replicas=4
(i.e., kubectl scale deployment/nginx-deployment --all --replicas=4)
# scale multiple controllers
kubectl scale deployment/deployment_name1 deployment/deployment_name2 --all --replicas=4
# scale a specific deployment locating the yaml file
kubectl scale --replicas=4 -f yaml-path
(i.e., kubectl scale --replicas=4 -f deployment.yaml)
# rule while scaling deployment
kubectl scale --current-replicas=3 --replicas=3 -f yaml-path
Kubernetes Pods
- Pod is a group of containers
- Each Pod has a unique IP
- The containers inside a pod talk to each other and share volumes
LIST PODS
# list all the pods in the default namespace
kubectl get pods
kubectl get pods --namespace default
# list all the pods in another namespace
kubectl get pods --namespace namespace_name
(i.e., kubectl get pods --namespace zalenium)
# list all the pods with details [ip addresss, concerned node]
kubectl get pods --namespace namespace_name -o wide
(i.e., kubectl get pods --namespace zalenium -o wide)
# list all the pods with selector filter
kubectl get pods --all-namespaces --selector=selector_name
(i.e., kubectl get pods --all-namespaces --selector=app.kubernetes.io/name=zalenium -o wide)
# list all the pods with status filter [Running, Pending, etc.,]
kubectl get pods --all-namespaces --field-selector=status.phase=Running
# list all the pods from entire namespace
kubectl get pods --all-namespaces
# list pods without column headers
kubectl get pods -n namespace_name --no-headers
# list pod labels
kubectl get pods -n namespace_name --no-headers -o custom-columns=:metadata.labels.app
# list pod names
kubectl get pods -n namespace_name --no-headers -o custom-columns=:metadata.name
kubectl get pods -o=name -n namespace_name | sed "s/^.\{4\}//"
kubectl get pods -n namespace_name --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
# list pod's release names
kubectl get pods -n namespace_name --no-headers -o custom-columns=:metadata.labels.release
MANIPULATE PODS
# debug/enter into a pod
kubectl exec -it pod_name bash --namespace namespace_name
(i.e., kubectl exec -it zalenium-40000-4zbtm bash --namespace zalenium)
# copy data into a pod
kubectl cp local_path namespace_name/pod_name:pod_path
(i.e., kubectl cp ./ zalenium/zalenium-40000-4zbtm bash --namespace zalenium)
# get logs of the pod
kubectl logs pod_name -n namespace_name
watch kubectl logs pod_name -n namespace_name
# troubleshoot pod
[list the pods and check for running status]
kubectl describe pod pod_name --namespace namespace_name
(i.e., kubectl describe pod zalenium-40000-4zbtm --namespace zalenium)
Kubernetes WatchDog
- Let’s you debug and control over entire kubernetes system
LIST & CHECK STATUS
# list all the details (services, deployments, pods, replicas) related to all namespaces
kubectl get all --all-namespaces
# list all the details (services, deployments, pods, replicas) specific to a namespace
kubectl get all -n namespace_name
# live status
watch kubectl get all -n namespace_name
# list all the deleted pod names too
kubectl get event --all-namespaces -o custom-columns=NAME:.metadata.name | cut -d "." -f1
# check all supported api versions
kubectl api-versions
# get details about a specific api-version
kubectl get --raw api-version
(i.e., kubectl get --raw /apis/autoscaling/v2beta2)
# default cmd to create services/deployments/pods/volumes from yaml file
kubectl create -f yaml-path
(i.e., kubectl create -f .)