General Kubernetes Commands Documentation
Creating and Managing Pods
Create an NGINX Pod
kubectl run nginx --image=nginxGenerate POD Manifest YAML file (-o yaml). Don't create it (--dry-run)
kubectl run nginx --image=nginx --dry-run=client -o yamlCreating and Managing Deployments
Create a Deployment
kubectl create deployment --image=nginx nginxGenerate Deployment YAML file (-o yaml). Don't create it (--dry-run)
kubectl create deployment --image=nginx nginx --dry-run=client -o yamlGenerate 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.yamlCreate a Deployment from a YAML file
kubectl create -f nginx-deployment.yamlCreate a Deployment with 4 replicas directly using command
kubectl create deployment nginx --image=nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yamlScale a Deployment using the kubectl scale command
kubectl scale deployment nginx --replicas=4Creating 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 yamlOr
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yamlCreate 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 yamlOr
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yamlPractise 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=NeverExpose 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=ClusterIPExpose a Pod named custom-nginx as a NodePort service
kubectl expose pod custom-nginx --name=custom-nginx-service --port=8080 --target-port=8080 --type=NodePortCreate a new namespace
kubectl create namespace dev-nsEdit 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=ClusterIPReplace a resource defined in a YAML file
kubectl replace --force -f nginx.ymlContinuously watch Pod status updates
kubectl get pods --watchDescribe a Node and grep for Taints
kubectl describe node kubemaster | grep TaintAdd a Taint to a Node
kubectl taint node node01 spray=mortein:NoExecuteRemove a Taint from a Node
kubectl taint node controlplane spray=mortein:NoExecute-Add a label to a Node
kubectl label node node01 size=largeMetrics Server Commands
Enable the Metrics Server add-on in Minikube
minikube addons enable metric-serverClone 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 nodeCheck Pod resource usage
kubectl top podLogs 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.