Kubectl

Kubectl is the command-line tool used to interact with a Kubernetes cluster. It allows you to create, manage, and troubleshoot resources in the cluster. Kubectl communicates with the Kubernetes API server to perform operations on the cluster.

Command Structure

When it comes to Kubectl commands, it is very similar to Docker CLI. It is used to interact with the Kubernetes cluster and perform various operations like creating, deleting, and updating resources.

For example, the command syntax is structured as follows:

kubectl [command] [TYPE] [NAME] [flags]

where command, TYPE, NAME, and flags are:

  • command: Specifies the operation that you want to perform on one or more resources, for example create, get, describe, delete.

  • TYPE: Specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms. For example, the following commands produce the same output:

kubectl get pod pod1 # singular form
kubectl get pods pod1 # plural form
kubectl get po pod1 # abbreviated form
  • NAME: Specifies the name of the resource you want to operate on. Names are case-sensitive.
  • flags: Specifies additional options for the command. Flags are optional and can be used to modify the behavior of the command.

Example of Common Kubectl Commands

# Get the list of pods in the default namespace
kubectl get pods

# Get detailed information about a pod
kubectl describe pod <pod-name>

# Create a resource from a configuration file
kubectl apply -f <file.yaml>

# Delete a resource
kubectl delete <resource-type> <resource-name>

# Get the logs of a pod
kubectl logs <pod-name>

# Execute a command in a container
kubectl exec -it <pod-name> -- <command>
ℹī¸

-- separates command options from the arguments.

In the examples above, the last command has a -- before the command. This is used to separates the options for kubectl exec from the command you intend to run inside the container.