Introduction
Kubernetes is a powerful tool for managing applications inside containers. If you're new to Kubernetes or want to practice, using tools like Kind (Kubernetes in Docker) can make it easier to set up and learn. In this blog, we’ll go over the basic Kubernetes commands you need to know when using Kind. These commands will help you set up clusters, deploy applications, and manage your containers. Let’s dive in
1. Cluster Management
Get cluster information
This command provides details about the Kubernetes cluster.
Command: kubectl cluster-info
List all nodes in the cluster
This shows all the nodes present in your Kubernetes cluster.
Command: kubectl get nodes
2. Node Management
Describe a specific node
This command gives detailed information about a specific node in the cluster.
Command: kubectl describe node <node-name>
Drain a node for maintenance
This safely evicts all pods from the node, marking it as unschedulable.
Command: kubectl drain <node-name>
Uncordon a node after maintenance
This allows scheduling pods again on the node after maintenance.
Command: kubectl uncordon <node-name>
Label a node
This adds a label to a node, which can help in categorizing or grouping nodes.
Command: kubectl label node <node-name> <key>=<value>
Remove a label from a node
This removes a label from a node.
Command: kubectl label node <node-name> <label-key>-
3. Namespace Management
Describe a specific namespace
This provides information about a particular namespace.
Command: kubectl describe namespace <namespace-name>
Create a new namespace
This command creates a new namespace within the cluster.
Command: kubectl create ns <namespace-name>
List all namespaces
This shows all namespaces available in the cluster.
Command: kubectl get namespaces
Switch to a different namespace
This changes the current context to another namespace.
Command: kubectl config set-context --current --namespace=<namespace-name>
Delete a namespace
This deletes a specific namespace from the cluster.
Command: kubectl delete namespace <namespace-name>
4. Resource Management
Create or update a resource from a YAML file
This applies the resource definition from a specified YAML file.
Command: kubectl apply -f <resource-definition.yaml>
Create a resource imperatively
This creates a resource directly from the command line (no YAML file).
Command: kubectl create <resource-type>
Create a resource using a URL
This applies a resource definition from a URL.
Command: kubectl apply -f url-to-resource-definition.yaml
5. Viewing and Finding Resources
List all resources of a specific type
This displays resources like pods, services, etc., in the cluster.
Command: kubectl get <resource-type>
List resources with additional details
This shows resources along with extra details like IP addresses and node information.
Command: kubectl get <resource-type> -o wide
Describe a specific resource
This provides detailed information about a specific resource.
Command: kubectl describe <resource-type> <resource-name>
List resources with a specific label
This filters resources by a specific label key-value pair.
Command: kubectl get <resource-type> -l <label-key>=<label-value>
6. Deleting Resources
Delete a specific resource
This removes a resource from the cluster.
Command: kubectl delete <resource-type> <resource-name>
Delete multiple resources
This deletes several resources in one command.
Command: kubectl delete <resource-type1> <resource-name1> <resource-type2> <resource-name2>
Delete all resources of a specific type
This command deletes all resources of a specified type.
Command: kubectl delete <resource-type> --all
7. Pod Management
List all pods in the cluster
This command shows all the pods running in the cluster.
Command: kubectl get pods
Describe a specific pod
This provides detailed information about a specific pod.
Command: kubectl describe pod <pod-name>
Get logs from a pod
This shows the logs of a specific pod.
Command: kubectl logs <pod-name>
Stream logs from a pod
This allows you to follow the logs in real-time.
Command: kubectl logs -f <pod-name>
Exec into a pod
This lets you run a command inside a pod, useful for troubleshooting.
Command: kubectl exec -it <pod-name> -- <command>
8. Deployment Management
Create a deployment
This command creates a deployment with a specified image.
Command: kubectl create deployment <deployment-name> --image=<image-name>
List all deployments
This shows all deployments in the cluster.
Command: kubectl get deployments
Update a deployment’s image
This updates the container image in a deployment.
Command: kubectl set image deployment/<deployment-name> <container-name>=<new-image-name>
Check deployment rollout status
This shows the current rollout status of a deployment.
Command: kubectl rollout status deployment/<deployment-name>
9. Scaling Resources
Scale a deployment
This scales the number of replicas in a deployment.
Command: kubectl scale deployment <deployment-name> --replicas=<replica-count>
Scale a statefulset
This scales the number of replicas in a statefulset.
Command: kubectl scale statefulset <statefulset-name> --replicas=<replica-count>
10. Service Management
Create a service
This command creates a new service in the cluster.
Command: kubectl expose deployment --port= --target-port= --type=<service-type>
This exposes a deployment as a service, making it accessible.
Command: kubectl expose deployment --port= --target-port= --type=<service-type>
Delete a service
This deletes a specific service from the cluster.
Command: kubectl delete service <service-name>
11. Networking
Port forward to a pod
This forwards a port from your local machine to a pod.
Command: kubectl port-forward <pod-name> <local-port>:<pod-port>
Expose a deployment as a NodePort service
This makes a deployment accessible outside the cluster via a NodePort service.
Command: kubectl expose deployment <deployment-name> --type=NodePort --port=<port>
12. Storage
Create a PersistentVolume
This command creates a persistent volume in the cluster.
Command: kubectl create -f <persistent-volume-definition.yaml>
List all PersistentVolumes
This lists all persistent volumes in the cluster.
Command: kubectl get pv
13. Monitoring and Troubleshooting
Check cluster events
This shows all events in the cluster, useful for troubleshooting.
Command: kubectl get events
Get resource utilization of nodes
This shows the current resource usage (CPU, memory) for each node.
Command: kubectl top nodes
14. Kind Cluster Management
Create a Cluster
This command creates a new cluster with the specified name.
Command: kind create cluster --name my-cluster
Get Clusters
This command lists all clusters that have been created using Kind.
Command: kind get clusters
Get Nodes of a Cluster
This command lists all the nodes within the specified cluster.
Command: kind get nodes --name my-cluster
Get Kubeconfig
This command retrieves the kubeconfig file for the specified cluster, which is used to connect to the cluster.
Command: kind get kubeconfig --name my-cluster > ~/my-cluster-kubeconfig
Set Kubeconfig for kubectl
Use this command to set the Kubeconfig for kubectl to interact with your Kind cluster.
Command: kubectl --kubeconfig ~/my-cluster-kubeconfig ...
Export Logs
This command exports logs from the specified Kind cluster.
Command: kind export logs --name my-cluster
Load Docker Image into the Cluster
This command loads a Docker image into the Kind cluster.
Command: kind load image-archive <image-archive-path>
Get Cluster IP Address
This command retrieves the IP address of the cluster's control plane node.
Command: docker container inspect my-cluster-control-plane --format '{{ .NetworkSettings.Networks.kind.IPAddress }}'
Delete a Cluster
This command deletes the specified cluster from Kind.
Command: kind delete cluster --name my-cluster
Check Kind Version
This command shows the installed version of Kind.
Command: kind version
Thanks for going through these essential Kubernetes and Kind commands. These are key tools for managing and scaling your clusters effectively. If you have any questions or would like to see more examples, feel free to drop a comment. Stay tuned for more posts on Kubernetes and DevOps Keep learning and improving your skills