Other Tutorials

Working with kubectl in Kubernetes

Introduction:

Kubectl is a command-line tool that helps us interact with our Kubernetes API server. In turn, it helps us manage our K8s cluster.

In this quick tutorial, we’ll learn and explore some commonly used kubectl commands.

Working with kubectl:

Let’s look at kubectl‘s general syntax:

kubectl [operation] [resource-type] [resource-name] [flags]

When we don’t specify the resource name, it considers all resources of the specified type.

And so, when using kubectl, there are three main things to deal with:

  • Operation: it’s what we intend to perform on one or more resources
  • Resource types: the resource type on which we want to perform the operation
  • Output options: if our operation produces some output, we can specify the expected display format using -o or –output flag

Operations:

Some of the basic operations supported by kubectl includes:

  • create: create a new resource(s)
  • apply: creates or updates a resource(s)
  • delete: for deleting resource(s)
  • get: lists one or more resources
  • explain: gives us documentation of resource(s)
  • describe: displays a detailed state of our resource(s)
  • run: starts up a pod from a given container image
  • exec: helps us execute a command against a container within a pod
  • logs: allows us to view logs for a container
  • config: helps us update our kubeconfig file

Resource Types:

K8s introduces a variety of resources against which we can perform operations. Some of the common resource types include:

  • namespaces (ns)
  • nodes (no)
  • pods (po)
  • services (svc)
  • endpoints (ep)
  • configmaps (cm)
  • deployments (deploy)
  • secrets
  • ..

We don’t need to remember all of the K8s resource names or their aliases. We can easily list them out using api-resources:

kubeclt api-resources

Output Options:

For the kubectl operations that produce a result, we can customize the output format using one of the following flags:

  • wide: outputs any additional information to the stout
  • yaml: presents the API object in YAML format
  • json: displays API object in JSON format

Example Usages:

Now that we understand the basics, let’s try out some kubectl commands:

#creates or updates the given resource specified in manifest
kubectl apply -f my-deployment.yaml

#lists all pods
kubectl get pods

#lists all services
kubectl get svc

#displays mypod information in yaml format
kubectl get pods mypod -o yaml

#prints some basic cluster information
kubectl cluster-info

#prints detailed information of the cluster nodes
kubectl get nodes -o wide

#lists all resources in the default namespace
kubectl get all

#lists all services in a specific namespace
kubectl get svc -ns my-namespace

#explains a pod resource type in detail
kubectl explain pod

#gives detailed information of our resource
kubectl describe nodes my-cluster-node1 

#dumps pod logs
kubectl logs my-pod1

Kubectl Help Documentation:

We can get help with kubectl usage with its help command:

kubectl -h

We can stream the output using less for better readability:

kubectl -h | less

If we want help with a specific command, we can also have something like:

#helps us by presenting information about describe command
kubectl describe -h | less

Conclusion:

In this quick tutorial, we learned how to interact with our K8s cluster using kubectl utility. We also covered some common useful commands.

I’ll recommend executing the example commands at Play With Kubernetes to get a better hang of it.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *