Published on

Kubernetes CLI Cheat Sheet: Essential Commands for Efficient Management

Authors

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!

  1. 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.

  1. Running Config Files: To apply a configuration file in Kubernetes, use the kubectl apply command followed by the f 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.

  1. 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.

  1. 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.

  1. 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.

  1. Deleting Pods: To remove a pod from your Kubernetes cluster, use the kubectl delete command followed by the pod 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.

  1. Obtaining Detailed Information about Pods: For in-depth information about a specific pod, use the kubectl describe command followed by the pod 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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

  1. 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.

  1. 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.

  1. 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.

  1. 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!