Full Kubernetes Kubectl Cheat Sheet

Nixon Data Full Kubernetes Kubectl Cheat Sheet

Full Kubernetes Kubectl Cheat Sheet

Categories of kubectl commands

Kubernetes commands can generally be divided into the following types:

  1. Cluster Management Commands: These commands are used to manage and interact with the Kubernetes cluster as a whole. Examples include kubectl cluster-info, kubectl config, and kubectl get nodes.
  2. Pod Management Commands: These commands are used to manage and interact with pods, which are the smallest and simplest unit in the Kubernetes object model. Examples include kubectl get pods, kubectl describe pods, and kubectl delete pods.
  3. Service Management Commands: These commands are used to manage and interact with services, which provide load balancing and service discovery capabilities. Examples include kubectl get services, kubectl describe services, and kubectl delete services.
  4. Deployment Management Commands: These commands are used to manage and interact with deployments, which represent a desired state for a set of pods. Examples include kubectl get deployments, kubectl describe deployments, and kubectl delete deployments.
  5. ConfigMap and Secret Management Commands: These commands are used to manage and interact with ConfigMaps and Secrets, which are used to store configuration data and sensitive information respectively. Examples include kubectl get configmaps, kubectl describe secrets, and kubectl delete configmaps.
  6. Volume Management Commands: These commands are used to manage and interact with persistent storage volumes and share data between containers. Examples include kubectl apply -f pvc.yaml, kubectl get pv, kubectl describe pv
  7. Job and CronJob Management Commands: These commands are used to manage and interact with Jobs and CronJobs, which are used to run batch workloads and scheduled workloads respectively. Examples include kubectl get jobs, kubectl describe cronjobs, and kubectl delete jobs
  8. Role-based Access Control Management commands: These commands are used to manage and interact with Role-based Access Control (RBAC) which enables fine-grained control over who can access and manage resources in the cluster. Examples include kubectl get roles, kubectl describe rolebindings, and kubectl delete roles

Cluster management commands in Kubernetes

Cluster management commands in Kubernetes are used to manage and interact with the Kubernetes cluster as a whole. These commands provide information about the state of the cluster, allow you to configure and manage the cluster, and provide visibility into the resources that make up the cluster. In this article, we will discuss some of the most commonly used cluster management commands in Kubernetes and how they are used.

  • kubectl get nodes:
    • This command returns a list of all nodes in the cluster, including their status and IP addresses. This command can be used to quickly check the status of the nodes in the cluster and ensure that they are running correctly.
  • kubectl get pods –all-namespaces:
    • This command returns a list of all pods in the cluster, including the namespace they belong to. This command can be used to quickly check the status of all pods in the cluster and ensure that they are running correctly.
  • kubectl describe nodes <node-name>:
    • This command provides detailed information about a specific node in the cluster, including its status, IP address, and the pods running on it. This command can be used to troubleshoot issues with a specific node and investigate why it may be in a failed state.
  • kubectl config view:
    • This command displays the current configuration settings for the kubectl command-line tool. This command can be used to check the current context and cluster configuration, and to make sure that the kubectl is set up to communicate with the correct cluster.
  • kubectl config set-context <context-name>:
    • This command is used to switch between different contexts and clusters. This command allows you to easily switch between different clusters and namespaces, making it easy to manage multiple clusters from a single kubectl command-line tool.
  • kubectl get events:
    • This command returns the events that have occurred in the cluster, including resource creation, scaling, and deletion. This command can be used to troubleshoot issues with resources in the cluster and understand what actions have been taken.
  • kubectl top:
    • This command returns the resource usage of nodes or pods. This command can be used to see the CPU, memory and storage usage of the nodes and pods. It also can be used to identify which pod or node is consuming more resources.
  • kubectl cluster-info:
    • This command returns the status and information of the Kubernetes cluster. This command can be used to check the health of the cluster, the version of Kubernetes that is running, and the URLs for the Kubernetes API server and other important components.
  • kubectl version:
    • This command returns the version of kubectl and the version of the Kubernetes cluster. This command can be used to check if the version of kubectl is compatible with the version of the cluster and identify any potential issues.
  • kubectl get componentstatuses:
    • This command returns the status of the Kubernetes control plane components. This command can be used to check the health of the control plane components and identify any potential issues.

These are some of the most commonly used cluster management commands in Kubernetes, but there are many other commands that can be used to manage and interact with a Kubernetes cluster. It is important to note that some of the commands may be specific to certain types of Kubernetes clusters, depending on the components and features that are installed.

Pod management commands in Kubernetes

Pod management commands in Kubernetes are used to manage and interact with pods, which are the smallest and simplest unit in the Kubernetes object model. Pods are used to run one or more containers and provide a way to manage the deployment and scaling of containerized applications. In this article, we will discuss some of the most commonly used pod management commands in Kubernetes and how they are used.

  • kubectl get pods:
    • This command returns a list of all pods in the current namespace, including their status and IP addresses. This command can be used to quickly check the status of all pods in the current namespace and ensure that they are running correctly.
  • kubectl describe pod <pod-name>:
    • This command provides detailed information about a specific pod, including its status, IP address, and the containers running in it. This command can be used to troubleshoot issues with a specific pod and investigate why it may be in a failed state.
  • kubectl logs <pod-name> -f:
    • This command returns the logs of a specific pod and follows the log stream. This command can be used to investigate the logs of a specific pod and understand why it may be in a failed state.
  • kubectl exec -it <pod-name> — <command> :
    • This command is used to execute a command inside a specific container in a pod. This command can be used to troubleshoot a container or to run commands inside it.
  • kubectl create -f <pod-definition-file.yaml>:
    • This command is used to create a new pod from a pod definition file in yaml format. The file should contain the pod configuration and container details.
  • kubectl delete pod <pod-name> :
    • This command is used to delete a specific pod. This command can be used to delete a pod when it is no longer needed or when it is in a failed state.
  • kubectl scale –replicas=3 -f <pod-definition-file.yaml> :
    • This command is used to scale the number of replicas of a pod. This command can be used to increase or decrease the number of replicas of a pod to meet the desired state.
  • kubectl label pods <pod-name> env=test :
    • This command is used to add labels to pods. Labels can be used to organize and manage pods, and to filter and select pods for various operations.
  1. kubectl get pods –selector=<label-name>=<label-value>:
    1. This command is used to filter and select pods based on their labels. In this example, the command would return all pods that have a label “label-name” with the value “label-value”

These are some of the most commonly used pod management commands in Kubernetes, but there are many other commands that can be used to manage and interact with pods in a Kubernetes cluster. It is important to note that some of the commands may be specific to certain types of Kubernetes clusters, depending on the components and features that are installed. It’s always a good practice to test and verify the commands in a non-production environment before applying in production. In addition, it is important to have a good understanding of the Kubernetes object model, as well as the concepts of pods and containers, in order to effectively use these commands.

Service management commands in Kubernetes

Service management commands in Kubernetes are used to manage and interact with services, which provide load balancing and service discovery capabilities for pods. Services are an abstraction layer that allows for communication between pods and external clients, and they provide a stable endpoint for clients to access the pods. In this article, we will discuss some of the most commonly used service management commands in Kubernetes and how they are used.

  • kubectl get services:
    • This command returns a list of all services in the current namespace, including their type, cluster IP, and external IP (if applicable). This command can be used to quickly check the status of all services in the current namespace and ensure that they are running correctly.
  • kubectl describe service <service-name>:
    • This command provides detailed information about a specific service, including its type, cluster IP, external IP (if applicable), and the pods and endpoints it is connected to. This command can be used to troubleshoot issues with a specific service and investigate why it may not be working correctly.
  • kubectl expose deployment <deployment-name> –type=LoadBalancer –name <service-name> :
    • This command is used to create a new service from a deployment. This command creates a new service with a load balancer type and gives it a specific name.
  • kubectl create -f <service-definition-file.yaml>:
    • This command is used to create a new service from a service definition file in yaml format. The file should contain the service configuration and the pod selectors to connect to.
  • kubectl delete service <service-name>:
    • This command is used to delete a specific service. This command can be used to delete a service when it is no longer needed or when it is not working correctly.
  • kubectl label services <service-name> env=test :
    • This command is used to add labels to services. Labels can be used to organize and manage services, and to filter and select services for various operations.
  • kubectl get services –selector=<label-name>=<label-value> :
    • This command is used to filter and select services based on their labels. In this example, the command would return all services that have a label “label-name” with the value “label-value”
  • kubectl scale service <service-name> –replicas=3 :
    • This command is used to scale the number of replicas of a service. This command can be used to increase or decrease the number of replicas of a service to meet the desired state.

Deployment management commands in Kubernetes

Deployment management commands in Kubernetes are used to manage and interact with deployments, which represent a desired state for a set of pods. Deployments provide a way to declaratively manage the replication and updates of pods, and they allow for rollbacks and other advanced features. In this article, we will discuss some of the most commonly used deployment management commands in Kubernetes and how they are used.

  • kubectl get deployments:
    • This command returns a list of all deployments in the current namespace, including their status, replicas, and available updates. This command can be used to quickly check the status of all deployments in the current namespace and ensure that they are running correctly.
  • kubectl describe deployment <deployment-name>:
    • This command provides detailed information about a specific deployment, including its status, replicas, available updates, and the pods and services it is connected to. This command can be used to troubleshoot issues with a specific deployment and investigate why it may not be working correctly.
  • kubectl create -f <deployment-definition-file.yaml>:
    • This command is used to create a new deployment from a deployment definition file in yaml format. The file should contain the deployment configuration and the pod template.
  • kubectl apply -f <deployment-definition-file.yaml> :
    • This command is used to update an existing deployment from a deployment definition file in yaml format. The file should contain the deployment configuration and the pod template.
  • kubectl delete deployment <deployment-name> :
    • This command is used to delete a specific deployment. This command can be used to delete a deployment when it is no longer needed or when it is not working correctly.
  • kubectl rollout status deployment <deployment-name> :
    • This command is used to check the status of a deployment rollout. This command can be used to check if the rollout is in progress, succeeded or failed.
  • kubectl rollout undo deployment <deployment-name> :
    • This command is used to undo the last deployment rollout. This command can be used to roll back to the previous version of the deployment.
  • kubectl scale deployment <deployment-name> –replicas=3 :
    • This command is used to scale the number of replicas of a deployment. This command can be used to increase or decrease the number of replicas of a deployment to meet the desired state.
  • kubectl set image deployment <deployment-name> <container-name>=<new-image> :
    • This command is used to update the container image of a deployment. This command can be used to update the image of a container to a new version.
  • kubectl get deployments –selector=<label-name>=<label-value> :
    • This command is used to filter and select deployments based on their labels. In this example, the command would return all deployments that have a label “label-name” with the value “label-value”.

These are some of the most commonly used deployment management commands in Kubernetes, but there are many other commands that can be used to manage and interact with deployments in a Kubernetes cluster.

ConfigMap and Secret Management Commands

Kubernetes is a powerful platform for managing containerized applications, and part of its power comes from its ability to manage configuration data and secrets. Kubernetes provides two primary resources for this purpose: ConfigMaps and Secrets.

ConfigMaps are used to store non-sensitive configuration data that can be used by pods and other resources in a Kubernetes cluster. They can be created and managed using the kubectl command-line tool.

To create a new ConfigMap, you can use the kubectl create configmap command. For example, the following command creates a ConfigMap named “my-config” with two key-value pairs:

kubectl create configmap my-config –from-literal=key1=value1 –from-literal=key2=value2

You can also create a ConfigMap from a file or a directory using the --from-file or --from-env-file options respectively. For example, the following command creates a ConfigMap from a file named “config.txt”:

kubectl create configmap my-config –from-file=config.txt

You can also use the kubectl edit or kubectl update command to modify the ConfigMap.

Once a ConfigMap has been created, it can be used in a pod or other resource by referencing it in the resource’s configuration. For example, the following pod configuration references a ConfigMap named “my-config” and sets the value of an environment variable named “CONFIG_VAR” to the value of the “key1” key in the ConfigMap:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    env:
    - name: CONFIG_VAR
      valueFrom:
        configMapKeyRef:
          name: my-config
          key: key1

Secrets are used to store sensitive configuration data, such as passwords, tokens, and encryption keys, that should be kept confidential. They can be created and managed using the kubectl command-line tool.

To create a new Secret, you can use the kubectl create secret command. For example, the following command creates a Secret named “my-secret” with two key-value pairs:

kubectl create secret generic my-secret --from-literal=key1=value1 --from-literal=key2=value2

You can also create a Secret from a file or a directory using the --from-file or --from-env-file options respectively. For example, the following command creates a Secret from a file named “secret.txt”:

kubectl create secret generic my-secret --from-file=secret.txt

Once a Secret has been created, it can be used in a pod or other resource by referencing it in the resource’s configuration. For example, the following pod configuration references a Secret named “my-secret” and sets the value of an environment variable named “SECRET_VAR” to the value of the “key1” key in the Secret:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  -name: my-container
image: my-image
env:
name: SECRET_VAR
valueFrom:
secretKeyRef:
name: my-secret
key: key1

In addition to creating and modifying ConfigMaps and Secrets, the kubectl command-line tool also provides other commands for managing these resources.

To view the details of a ConfigMap or Secret, you can use the kubectl describe command. For example, the following command shows the details of a ConfigMap named “my-config”:

kubectl describe configmap my-config

To list all ConfigMaps or Secrets in a namespace, you can use the kubectl get command. For example, the following command lists all ConfigMaps in the “default” namespace:

kubectl get configmaps -n default

To delete a ConfigMap or Secret, you can use the kubectl delete command. For example, the following command deletes a ConfigMap named “my-config”:

kubectl delete configmap my-config

You can also use the kubectl patch command to update the fields of a ConfigMap or Secret.

In addition to the basic commands above, there are other advanced options and features available for managing ConfigMaps and Secrets in Kubernetes, such as using ConfigMaps to configure Kubernetes services or using Kubernetes Secrets to store sensitive data in a GitOps workflow.

It’s important to note that while Kubernetes Secrets and ConfigMaps provide a way to manage sensitive information and configuration, it is still important to ensure that your configuration and secrets are stored securely and access is restricted to authorized personnel.

In summary, ConfigMaps and Secrets are powerful resources in Kubernetes that allow you to store and manage configuration data and secrets in a secure and organized manner. The kubectl command-line tool provides a range of commands for creating, modifying, and managing ConfigMaps and Secrets, and there are also advanced options and features available for more specific use cases.

Volume Management Commands

In Kubernetes, a volume is a way to store data that can be accessed by pods. Volumes can be used to persist data across pod restarts, share data between multiple pods, or provide a way to access external storage systems. Kubernetes provides a number of different types of volumes, including hostPath, emptyDir, configMap, secret, and more.

The kubectl command-line tool provides a number of commands for managing volumes in a Kubernetes cluster.

To create a new volume, you can use the kubectl create command. For example, the following command creates a new emptyDir volume named “my-volume”:

kubectl create -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: my-volume
      mountPath: /data
  volumes:
  - name: my-volume
    emptyDir: {}
EOF

You can also create a volume from a configMap or secret using the configMap or secret volume types respectively.

To view the details of a volume, you can use the kubectl describe command. For example, the following command shows the details of a volume named “my-volume”:

kubectl describe pod my-pod

To update a volume, you can use the kubectl apply command. For example, the following command updates the size of an existing emptyDir volume named “my-volume”:

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: my-volume
      mountPath: /data
  volumes:
  - name: my-volume
    emptyDir:
      medium: Memory
EOF

To delete a volume, you can use the kubectl delete command. For example, the following command deletes a volume named “my-volume”:

kubectl delete -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: my-volume
      mountPath: /data
  volumes:
  - name: my-volume
    emptyDir: {}
EOF

In addition to the basic commands above, there are other advanced options and features available for managing volumes in Kubernetes, such as using Persistent Volumes and Persistent Volume Claims to manage storage resources, using StatefulSets to manage stateful applications, and using VolumeSnapshot to take a snapshot of a volume.

It’s important to note that while Kubernetes Volumes provide a way to manage data, it is still important to ensure that your data is stored securely and access is restricted to authorized personnel.

Job and CronJob Management Commands

In Kubernetes, a Job is a way to run a one-time or finite task, while a CronJob is a way to run a task on a scheduled basis. Both Job and CronJob are used to run batch jobs, which are long-running, non-interactive tasks that are typically used for things like data processing, backups, and other background tasks.

The kubectl command-line tool provides a number of commands for managing Jobs and CronJobs in a Kubernetes cluster.

To create a new Job, you can use the kubectl create command. For example, the following command creates a new Job named “my-job” that runs the “my-image” container:

kubectl create -f - <<EOF
apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  template:
    spec:
      containers:
      - name: my-container
        image: my-image
      restartPolicy: Never
  backoffLimit: 4
EOF

To view the details of a Job, you can use the kubectl describe command. For example, the following command shows the details of a Job named “my-job”:

kubectl describe job my-job

To delete a Job, you can use the kubectl delete command. For example, the following command deletes a Job named “my-job”:

kubectl delete job my-job

A CronJob is similar to a Job, but it runs on a schedule, specified using a cron expression. To create a new CronJob, you can use the kubectl create command, and specify the schedule using the schedule field. For example, the following command creates a new CronJob named “my-cronjob” that runs the “my-image” container every day at 4am:

kubectl create -f - <<EOF
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "0 4 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-container
            image: my-image
          restartPolicy: Never
  backoffLimit: 4
EOF

To view the details of a CronJob, you can use the kubectl describe command, and to delete a CronJob, you can use the kubectl delete command.

In addition to the basic commands above, there are other advanced options and features available for managing Jobs and CronJobs in Kubernetes, such as using parallelism and completions to control the number of pods that run in parallel and the number of successful completions required, using activeDeadlineSeconds to limit the runtime of a job, and using concurrencyPolicy to control how the CronJob behaves when multiple runs overlap.

It’s important to note that while Jobs and CronJobs provide a way to run batch jobs, it is still important to ensure that your jobs are configured properly and that the resources consumed by the jobs are appropriate for your cluster.

Role-based Access Control Management commands

Role-based Access Control (RBAC) is a method of controlling access to resources in a Kubernetes cluster based on the roles of individual users or service accounts. RBAC allows you to specify which users or service accounts have access to which resources, and what actions they are allowed to perform on those resources.

The kubectl command-line tool provides a number of commands for managing RBAC in a Kubernetes cluster.

To create a new Role, you can use the kubectl create command. For example, the following command creates a new Role named “my-role” that allows read access to all pods in the “default” namespace:

kubectl create -f - <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: my-role
  namespace: default
rules:
- apiGroups: ["", "extensions", "apps"]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
EOF

To create a new RoleBinding, which binds a Role to a user or service account, you can use the kubectl create command. For example, the following command creates a new RoleBinding named “my-rolebinding” that binds the “my-role” Role to the “my-user” user:

kubectl create -f - <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: my-rolebinding
  namespace: default
subjects:
- kind: User
  name: my-user
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: my-role
  apiGroup: rbac.authorization.k8s.io
EOF

You can also use the kubectl create clusterrole and kubectl create clusterrolebinding command to create ClusterRole and ClusterRoleBinding respectively, which provide a way to grant permissions at the cluster level, rather than just within a single namespace.

To view the details of a Role or RoleBinding, you can use the kubectl describe command. For example, the following command shows the details of a Role named “my-role”:

kubectl describe role my-role

To delete a Role or RoleBinding, you can use the kubectl delete command. For example, the following command deletes a Role named “my-role”:

kubectl delete role my-role

In addition to the basic commands above, there are other advanced options and features available for managing RBAC in Kubernetes, such as using Role Aggregation to inherit permissions from multiple roles and using RBAC to control access to Kubernetes APIs.

It’s important to note that while RBAC provides a way to manage access to resources in a Kubernetes cluster, it is still important to ensure that your RBAC rules are configured properly.