Kubernetes Commands

Learn via video courses
Topics Covered

Overview

Kubernetes commands are essential for managing containerized applications within Kubernetes clusters. The kubectl or Kubernetes command-line tool is used to perform operations on Kubernetes clusters. It is using the kubectl command-line tool that administrators and developers interact with the Kubernetes API to control various aspects of the cluster. Kubernetes commands allow users to efficiently manage cloud-native workloads and services, enhancing the productivity and reliability of their applications.

Kubectl Commands

kubectl is a command-line tool that enables users to interact with Kubernetes clusters. kubectl serves as the gateway or primary interface to interact with the Kubernetes API and control the clustebehavioriour and resources within it. From deploying applications and scaling resources to inspecting cluster information and managing pods, kubectl offers an extensive set of kubernetes commands that streamline various administrative and development tasks.

Cluster Management Commands in Kubernetes

kubectl commands allow you to retrieve vital information about the cluster, configure its settings, and maintain its overall health.

Here are some cluster management commands:

  • kubectl cluster-info: This command provides an overview of the Kubernetes cluster. It displays essential details such as the Kubernetes API server's URL, the location of the cluster's control plane components, and the Kubernetes version in use.
  • kubectl config: This command enables users to interact with different clusters by managing kubeconfig files. These files store the necessary cluster information, authentication details, and other configurations.
  • kubectl version: This command provides information about the client and server versions, as well as additional details about the environment.
  • kubectl api-resources: This command gives you the list of the API resources that are available.
  • kubectl api-versions: This command gives you the list of the API versions that are available
kubectl api-versions

DaemonSets Commands in Kubernetes

DaemonSets ensures a specified pod runs on every node in the cluster. This allows you to deploy things like background services, logging agents and more that should be available without manual intervention.

Some of thekubernete'ss basic commands to manage DaemonSets are:

  • kubectl get daemonsets: The command provides a list of all DaemonSets running in the current namespace with essential information.
  • kubectl edit daemonset: The command is used to Edit and update the definition of one or more daemonsets.
  • kubectl delete daemonset: The command is used to delete a specific daemonset.
  • kubectl create daemonset: The command is used to create a daemonset.
  • kubectl rollout daemonset: The command is used to manage the rollout of a daemonset.
  • kubectl describe ds <ds_name> -n <ns_name>: The command is used to display the detailed state of daemonsets within a namespace.

Deployments

Deployments provide a declarative way to manage the lifecycle of application pods. With kubectl commands, administrators can create, update, and monitor deployments, ensuring applications run reliably and efficiently.

Here are some deployment commands:

  • kubectl get deployment: The command is used to list one or more deployments in the current namespace.
  • kubectl describes deployment: The command is used to display the detailed state of one or more deployments.
  • kubectl edit deployment: The command is used to edit and update the definition of one or more deployments on the server.
  • kubectl create deployment: The command is used to create a new deployment.
  • kubectl delete deployment: The command is used to delete a deployment.
  • kubectl rollout status deployment: The command allows you to see the rollout status of a deployment.

Events

Events in Kubernetes provide information regarding the state and activity of resources within the cluster. They represent occurrences such as pod scheduling, resource creation, and error notifications. Some of the event commands are:

  • kubectl get events: The command gives you the list of recent events for all resources in the system.
  • kubectl get events --field-selector type=Warning: The command lists only the warnings.
  • kubectl get events --field-selector involvedObject.kind!=Pod: The commands gives you the list of all events except for pod events.
  • kubectl get events --field-selector type!=Normal: The command filters out normal events from the list of all events.

Logs

Some of the commands that help you monitor the logs of containers running within pods are:

  • kubectl logs pod_name: The commands print all the logs of a given pod.
  • kubectl logs --since: The command prints all the logs of a specific pod within the last hour.
  • kubectl logs --tail: The command gives you the last 20 lines of logs.
  • kubectl logs -f <service_name>: The command gives you the logs from a service.
  • kubectl logs -c <container_name> <pod_name>: The command prints the logs of a container in a pod.
  • kubectl logs <pod_name> pod.log: The command gives you the logs of a pod in a file called 'pod.log'.

File Manifesting

File manifesting in Kubernetes involves creating or modifying Kubernetes resources using YAML or JSON files. This approach provides a declarative and version-controlled way to define the desired state of the resources and their configurations. Some commands used to apply these manifest files and manage resources are:

  • kubectl apply -f manifest_file.yaml: The command is used to apply a configuration to an object by filename or stdin. Overrides the existing configuration.
  • kubectl create -f manifest_file.yaml: The command is used to create new objects.
  • kubectl create -f ./dir: The command is used to create objects in all the files inside a directory.
  • kubectl create -f ‘url’: The command lets you create objects from a URL.
  • kubectl delete -f manifest_file.yaml: The command lets you delete an object.

Kubernetes Namespaces

Namespaces allow for logical separation and organization of resources within a cluster. They provide a virtual cluster environment, enabling teams to work in isolation without interfering with each other's workloads. Some commands that facilitate the management of namespaces are:

  • kubectl create namespace: The command lets you create a new namespace.
  • kubectl get namespace: This command gives you the list of all the namespaces in a cluster.
  • kubectl describes namespace: This command is used to get the detailed state of a specific namespace.
  • kubectl delete namespace: The command allows you to delete a namespace.
  • kubectl edit namespace: The command lets you edit and update the definition of a specific namespace.

Node Commands in Kubernetes

Nodes in Kubernetes represent the underlying worker machines that run containers and handle the actual workload. Some commands used to monitor and operate nodes are:

  • kubectl taint node: The command lets you update the taints of a specific node.
  • kubectl get nodes: The command is used to get the information about all the nodes in a cluster.
  • kubectl delete node: The command is used to delete a specific node(s).
  • kubectl describe nodes | grep Allocated -A: The command gives you the information regarding resource allocation per node.
  • kubectl get pods -o wide | grep: The commands give you the list of all the pods running on a node.
  • kubectl cordon node: The command lets you mark a node as unschedulable.
  • kubectl uncordon node: The command lets you mark a node as schedulable.
  • kubectl label node: The command lets you add or update the labels of a node.

Replication Controller Commands

Replication Controllers (RC) are used to ensure the desired number of pod replicas are running in a Kubernetes cluster. They monitor the state of pods and automatically adjust the pod count to maintain the desired level of availability. Some commands used to manage and interact with replication controllers are:

  • kubectl get rc: The command gives you the list of all the replication controllers present in a namespace.
  • kubectl describe replicationcontroller rc-name: The command gives you detailed information about a specific replication controller.
  • kubectl scale rc rc-name --replicas= desired-replicas: The command is used to dynamically scale the number of replicas controlled by a Replication Controller allowing for adjustment based on application.
  • kubectl edit replicationcontroller: The command opens the Replication Controller's YAML configuration in an editor, allowing you to modify the desired configuration directly.
  • kubectl delete replicationcontroller: The command is used to remove a replication controller from a cluster.

Pods

Pods are the smallest and most basic deployable units in Kubernetes, representing a single instance of a running process. They encapsulate one or more containers with shared storage and network resources. Some commands used to manage and interact with pods are:

  • kubectl get node: The command retrieves a list of all pods in the current namespace.
  • kubectl delete pod: The command is used to delete a specific pod.
  • kubectl describe pod: The command is used to describe and retrieve detailed information about a specific pod.
  • kubectl create pod: The command is used to create a new pod.
  • kubectl exec <pod_name> -c container_name < command>: The command is used to execute a command against a container in a pod.
  • kubectl logs: The command is used to view logs generated by a specific container in a pod.
  • kubectl label pod: The command is used to add or update a label of a pod

Secrets

Secrets are used to securely store sensitive information, such as passwords, API keys, and other confidential data. Some commands used to manage secrets are:

  • kubectl create secret: This command is used to create a secret by specifying the type of secret and the data to be stored.
  • kubectl get secrets: This command gives you the list of all secrets available in the current namespace.
  • kubectl describe secrets: This command is used to retrieve detailed information about a specific secret(s).
  • kubectl delete secret: This command is used to delete a specific secret.

ReplicaSets

ReplicaSets ensure that the desired number of identical pods are running at all times. They are an evolution of the deprecated Replication Controllers, providing more advanced and flexible pod replication capabilities. Some commands used to manage replicasets are:

  • kubectl get replicasets: This command is used to retrieve the list of all the replicasets in the current namespace.
  • kubectldescribese replicasets: This command is used to retrieve detailed information about one or more replicasets.
  • kubectl scale --replicas=[x]: This command is used to dynamically scale the number of replicas controlled by a replicaset.
  • kubectl delete replicasets: This command is used to delete a specific or given set of replicasets.

Services

Services enable communication and load balancing between pods and external clients. They provide a stable endpoint to access the pods, abstracting away the complexities of managing individual pod IPs. Some commands used to interact with and manage services are:

  • kubectl get services: This command is used to get a list of all the services in the current namespace.
  • kubectl describe services: This command is used to retrieve detailed information for one or more services present in the current namespace.
  • kubectl expose: This command is used to expose a replication controller, deployment, pod, or service as a Kubernetes service.
  • kubectl edit service: The command is used to edit and update the definition of one or more services in the current namespace.
  • kubectl delete service: The command is used to delete one or more specific service(s) in the current namespace.

Kubernetes Service Accounts

Kubernetes Service Accounts are a secure way for pods to authenticate themselves within the cluster and access other resources. They are automatically created for every namespace and can be associated with specific permissions through Role-Based Access Control (RBAC). Some commands used to interact with and manage Service Accounts are:

  • kubectl get serviceaccounts: This command is used to retrieve the list of all the service accounts available in the current namespace.
  • kubectl describe serviceaccount: This command is used to retrieve detailed information concerning one or more specific service accounts in the current namespace.
  • kubectl replace serviceaccount: This command is used to replace a service account.
  • kubectl delete serviceaccount: This command is used to delete a specific service account in the current namespace.

Stateful Sets

Stateful Sets are specialized controllers in Kubernetes designed for managing stateful applications, where each pod has a unique identity and stable network identity. Unlike regular controllers like ReplicaSets or Deployments, Stateful Sets provide stable, persistent storage and ordered predictable pod creation and deletion. Some commands used to interact with and manage Stateful Sets are:

  • kubectl get statefulset: This command is used to retrieve the list of all the Stateful Sets available in the current namespace.
  • kubectl delete statefulset: This command is used to delete the stateful set only and not the pod.

Common Options in Kubernetes

In Kubectl you can specify optional flags with commands that can be utilized to modify the behavbehavioroutput of commands.

Here are some of the most common and useful ones:

  • -o, --output: This option enables administrators to specify the desired output format for the command.
  • -n, --namespace: This command is used to list all the Pods in a specific Namespace you would do this command.
  • -i, --selector: This option is used to filter resources based on labels. It allows you to list, describe, or delete resources that match the specified label selector.
  • -f, --filename: This option allows you to provide a YAML or JSON file containing resource definitions to be created or modified.
  • -w, --watch: This option enables you to watch for changes to resources in real time. The command remains active and continuously updates the output as changes occur.
  • -h, --help: This option displays a helpful message with usage instructions and a list of available options for a specific kubectl command that is used.

FAQs

Q: How do I list all the available kubectl commands?

A: To see a list of all available kubectl kubernetes commands, you can run kubectl without any arguments or options.

Q: What is the difference between a ReplicaSet and a Deployment?**

A: Both ReplicaSets and Deployments manage pod replicas, but Deployments offer additional features like rolling updates and version management. ReplicaSets are lower-level controllers, while Deployments are higher-level abstractions built on top of ReplicaSets.

Q: How can I update the image of a running pod?

A: To update the image of a running pod, you can use the kubectl set image command or edit the pod directly using the kubectl edit pod.

Q: How can I access logs from a specific container within a pod with multiple containers?

A: To access logs from a specific container within a multi-container pod, use the -c flag with the kubectl logs command followed by the container name.

Q: Can I execute a shell command inside a pod?

A: Yes, you can execute shell commands inside a pod using the kubectl exec command. Use the -it flags for an interactive shell session.

Q: How do I enable auto-scaling for a Kubernetes deployment?

A: Auto-scaling can be enabled for a Kubernetes deployment by creating a Horizontal Pod Autoscaler (HPA) resource. The HPA adjusts the number of replicas based on CPU utilization or custom metrics.

Q: Can I roll back deployment to a previous version?

A: Yes, you can roll back deployment to a previous version using the kubectl rollout undo command. It reverts to the previous revision or a specified revision.

Q: How do I delete all resources in a specific namespace?

A: To delete all resources in a specific namespace, you can use the kubectl delete command with the --all flag and specify the resource types.

Q: How do I install kubectl on my local machine?

A: The installation process for kubectl varies depending on your operating system. For Linux, you can use package managers like apt or yum. For macOS, you can use package managers like Homebrew. For Windows, you can download the kubectl binary and add it to your system's PATH. Refer to the Kubernetes documentation for detailed installation instructions.

Conclusion

  • Kubernetes commands, executed through kubectl, is an interface for interacting with and controlling Kubernetes resources.
  • kubectl get, describe, and delete commands allow you to list, inspect, and remove resources like pods, services, and deployments.
  • kubectl apply facilitates declarative resource management, while kubectl edit enables direct YAML configuration modification.
  • Pods can be managed using kubectl logs, exec, and delete commands for log retrieval, executing commands, and pod termination.
  • Stateful Sets and ReplicaSets can be managed using kubectl get, describe, scale, and delete commands, ensuring stable pod replication.