General Kubernetes Commands Documentation
Creating and Managing Pods
Create an NGINX Pod
kubectl run nginx --image=nginx
Generate POD Manifest YAML file (-o yaml). Don't create it (--dry-run)
kubectl run nginx --image=nginx --dry-run=client -o yaml
Creating and Managing Deployments
Create a Deployment
kubectl create deployment --image=nginx nginx
Generate Deployment YAML file (-o yaml). Don't create it (--dry-run)
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
Generate Deployment YAML file (-o yaml). Don’t create it (--dry-run) and save it to a file.
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
Create a Deployment from a YAML file
kubectl create -f nginx-deployment.yaml
Create a Deployment with 4 replicas directly using command
kubectl create deployment nginx --image=nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
Scale a Deployment using the kubectl scale command
kubectl scale deployment nginx --replicas=4
Creating and Managing Services
Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
Or
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
Or
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
Practise Test Commands
Create a Redis Pod with specific labels and no restart
kubectl run redis-pod --image=redis:alpine --labels="app=redis,tier=db" --restart=Never
Expose a Deployment named redis-deployment as a ClusterIP service
kubectl expose deployment redis-deployment --name=redis-service --port=6379 --target-port=6379 --selector=app=redis,tier=db --type=ClusterIP
Expose a Pod named custom-nginx as a NodePort service
kubectl expose pod custom-nginx --name=custom-nginx-service --port=8080 --target-port=8080 --type=NodePort
Create a new namespace
kubectl create namespace dev-ns
Edit a Deployment in a specific namespace
kubectl edit deployment <deployment-name> -n <namespace>
Expose a Pod named httpd as a ClusterIP service
kubectl expose pod httpd --name=httpd --port=80 --target-port=80 --type=ClusterIP
Replace a resource defined in a YAML file
kubectl replace --force -f nginx.yml
Continuously watch Pod status updates
kubectl get pods --watch
Describe a Node and grep for Taints
kubectl describe node kubemaster | grep Taint
Add a Taint to a Node
kubectl taint node node01 spray=mortein:NoExecute
Remove a Taint from a Node
kubectl taint node controlplane spray=mortein:NoExecute-
Add a label to a Node
kubectl label node node01 size=large
Metrics Server Commands
Enable the Metrics Server add-on in Minikube
minikube addons enable metric-server
Clone the Metrics Server repository and deploy
git clone https://github.com/kubernetes-sigs/metrics-server.git
kubectl create -f deploy/1.8+/
Check Node resource usage
kubectl top node
Check Pod resource usage
kubectl top pod
Logs and Debugging
View logs of a specific Pod
kubectl logs <pod-name>
View logs of a specific container within a Pod
kubectl logs <podname> <cont-name>
By using these commands, you can effectively manage your Kubernetes resources, from creating and scaling deployments to exposing services and monitoring resource usage. Each command provides a way to interact with the Kubernetes API, enabling you to automate and streamline your workflows.