- Published on
Kubernetes CLI Cheat Sheet: Essential Commands for Efficient Management
- Authors
- Name
- Robin Haider
- @robin_haider
Kubernetes Command Cheat Sheet
Title: Kubernetes CLI Cheat Sheet: Essential Commands for Efficient Management
Introduction: As a developer or system administrator working with Kubernetes, having a handy cheat sheet of essential commands can save you time and effort. In this blog post, we present a curated list of Kubernetes CLI commands that will empower you to efficiently manage your Kubernetes clusters. From checking the Kubernetes version to restarting deployments, we've got you covered. Let's dive in!
- Checking the Kubernetes Version: To ensure you're working with the correct Kubernetes version, use the following command:
kubectl version
This command displays the client and server versions of Kubernetes, providing valuable information for troubleshooting and compatibility checks.
- Running Config Files: To apply a configuration file in Kubernetes, use the
kubectl apply
command followed by thef
flag and the path to the file:
kubectl apply -f posts.yaml
This command executes the specified configuration file, deploying the desired resources in your Kubernetes cluster.
- Retrieving Running Pods: To obtain a list of running pods in your cluster, use the following command:
kubectl get pods
This command provides a summary of all pods, displaying their names, statuses, and other relevant details.
- Running Commands Inside a Pod: Sometimes, you may need to execute commands inside a container within a pod. Use the
kubectl exec
command to accomplish this:
kubectl exec -it podName -- command
Replace podName
with the name of the pod you want to access, and command
with the specific command you wish to run. This command opens an interactive shell within the container, allowing you to perform tasks or debug as needed.
- Viewing Pod Logs: To view the logs of a pod's container, use the
kubectl logs
command:
kubectl logs podname
Replace podname
with the name of the pod whose logs you want to examine. This command provides a convenient way to troubleshoot issues and gather information about the container's activities.
- Deleting Pods: To remove a pod from your Kubernetes cluster, use the
kubectl delete
command followed by thepod
keyword and the name of the pod:
kubectl delete pod podname
This command terminates the specified pod, freeing up resources and ensuring the desired state of your cluster.
- Obtaining Detailed Information about Pods: For in-depth information about a specific pod, use the
kubectl describe
command followed by thepod
keyword and the pod's name:
kubectl describe pod podName
This command provides comprehensive details about the pod, including its current state, associated events, and configuration.
- Restarting Deployments with the Latest Image: To restart a deployment with the latest image, use the
kubectl rollout restart
command followed by the deployment's name:
kubectl rollout restart deployment dep-name
By executing this command, you ensure that the deployment's pods are updated with the latest container image, promoting seamless updates and maintaining the desired application state.
- Separating Multiple Object Configurations: When you have multiple object configurations in a single
.yaml
file, you can use--
to separate them. For example:
apiVersion: v1
kind: Pod
metadata:
name: pod1
...
---
apiVersion: v1
kind: Service
metadata:
name: service1
...
Using ---
ensures that each object is processed independently.
- Making Requests to Cluster IP Services: To make a request to a Cluster IP service, use the following URL format:
http://servicename:port
Replace servicename
with the name of the service and port
with the designated port number. This allows you to access the desired service within the cluster.
- Applying All Config Files in the Current Directory: To apply all configuration files in the current directory, you can use the following command:
kubectl apply -f .
This command applies all .yaml
files found in the current directory, allowing you to easily deploy multiple resources at once.
- Checking Services in the Default Namespace: To view services only from the default namespace, use the following command:
kubectl get services --namespace=default
This command provides a list of services specifically within the default namespace, allowing you to focus on the relevant resources.
- Viewing All Namespaces: To retrieve a list of all namespaces in your Kubernetes cluster, use the following command:
kubectl get namespaces
This command displays all namespaces, including the default namespace and any additional namespaces you have created.
- Viewing Services in a Specific Namespace: To view services within a specific namespace, such as
ingress-nginx
, use the following command:
kubectl get services -n ingress-nginx
By specifying the -n
flag followed by the desired namespace, you can narrow down the list of services to the ones within that namespace.
- Communicating with Services across Namespaces: To communicate from a client pod in the default namespace to a service in the
ingress-nginx
namespace, follow this format:
http://nameOfService.namespace.svc.cluster.local
Replace nameOfService
with the actual name of the service you want to access and namespace
with the relevant namespace. For example, to communicate with the IngressNginxController
service, the URL would be:
http://ingress-nginx-controller.ingress-nginx.svc.cluster.local
If the service is using a port other than the default port 80, specify the port number in the URL like this:
http://ingress-nginx-controller.ingress-nginx.svc.cluster.local:81
- Creating an External Name Service for Shorter URLs: If you want to create a shorter URL for your service, you can use an External Name Service. This type of service allows you to map a service to an external DNS name. For example, you can create an External Name Service with the following YAML configuration:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ExternalName
externalName: example.com
With this configuration, you can access the service using http://my-service
instead of the full DNS name.
- Port Forwarding in Kubernetes: To forward a local port to a port on a specific pod, use the following command:
kubectl port-forward podsName localPort:podPort
Replace podsName
with the name of the pod you want to forward the port from. Specify localPort
as the local port on your machine and podPort
as the port on the pod you want to forward to. For example, to forward local port 4222 to pod port 4222, you would use:
kubectl port-forward podsName 4222:4222
This command establishes a connection between your local machine and the specified pod, allowing you to access services running within the pod.
- Managing Configurations: To view the current Kubernetes configuration, including the active context, use the following command:
kubectl config view
This command displays the configuration details, including clusters, contexts, and users.
- Switching Contexts: To switch to a different context in your Kubernetes configuration, use the following command:
kubectl config use-context <context-name>
Replace <context-name>
with the name of the desired context. This command allows you to switch between different Kubernetes clusters or contexts, enabling you to work with multiple environments seamlessly.
Conclusion: With this expanded Kubernetes CLI cheat sheet, you now have a broader range of commands at your disposal for efficient cluster management. Whether you're working with services, namespaces, port forwarding, managing configurations, or switching contexts, these commands will empower you to navigate and control your Kubernetes clusters effectively. Happy coding!