Skip to content

Kubectl Commands for Application Management

If you are using Kubernetes then you are likely familiar with the kubectl command. In this post, I would like to cover some of the application management commands it provides and you will encounter. Read on to learn more!

Delete | Describe | Edit | Get | Logs

Let’s start with the easy stuff:

The get operation caught me off guard initially. While it will “get” the object specified, it does not show the objects configuration/output, but instead shows what objects exist. For example:

When I was getting started with k8s, I expected the above command to show me the configuration/output of the grafana deployment. As you can see from the above example this is not the case. Looking back on it, the behavior makes sense because if you leave out which object you want then you get all objects found:

So how do you get the actual configuration for a given object? By specifying the args -o yaml

Also worth mentioning, the describe command can be invaluable when validating configuration and checking for events that may be impacting your application. Finally, note for pods that you can use the logs operation to get (by default) all logs from the pod:

kubectl logs pod grafana [--tail=30|-f]

Apply | Create | Delete | Patch

The kubectl operations specified above assumes you have already deployed something into the k8s namespace you are in. If you wish to perform operations to add/remove configurations within the namespace then you can use something like:

kubectl <apply|create|delete|patch> -f <file>.yaml

You will notice a few things about these operations:

  • No update — use apply or create instead
  • update does not delete
  • You cannot undo a patch — in general, use apply or create instead
  • Why have apply and create?

K8s supports both imperative (create) and declarative (apply) management of objects. In general, you probably want apply as this creates/updates. For more information, see the documentation.

One final thing to note is that you can dynamically pass in configuration. This will become critical as you start integrating k8s into your CI/CD system. For example:

envsubst > .yaml | kubectl apply -f -

Port-forward | Proxy

Now that you know how to create and manipulate objects, the next command to understand is port-forward:

kubectl port-forward <podName> <localPort>:<sourcePort>

A couple things to note about this command:

  • You can port-forward to a pod, not a service
  • If localPort and sourcePort are the same you can just specify one port (kubectl port-forward <podName> <port>)

In order to access non-pods (e.g. services), you can use the proxy command:

kubectl proxy

Then you can access resources via the proper URI:

http://localhost:8001/api/v1/namespaces/<namespace>/services/<serviceName>:<port>/proxy/<path>

You do this over the Internet as well assuming the k8s API is exposed. In addition, you can specify credentials to be used when accessing:

Exec

Since we are talking about connecting to applications, the exec operation can be very handy. It allows you to run commands against an application. For example:

You can also run a shell to remote into the pod:

Note: If you run a command that is not in the PATH then you will get an error. For example, if the system has sh but not bash. If this occurs, update your command accordingly. For CI/CD systems you cannot pass the -t option. I suggest parameterizing so you can run locally or via CI.

Rollout

As you get to operationalizing your applications, you will eventually encounter other cool kubectl commands. For example, you can check the status of a rollout:

kubectl rollout status <deployment|daemonsets|statefulsets>

This can be useful in CI/CD systems to confirm rollouts are working as expected. This command can also be used to rollback when issues arise:

kubectl rollout undo <deployment|daemonsets|statefulsets> <name> [--to-revision=2]

In fact, you can pause and resume a rollout as well as see the rollout history:

Summary

As you can see, the kubectl command is very powerful. This post covered some of the commands you will likely encounter, but only scratches the service of what is possible (e.g. if you are a cluster administrator, you will be interested in the cluster management options not covered in this post). For a complete list of all available commands see the documentation.

© 2018, Steve Flanders. All rights reserved.

Published inCloud NativeSystem Administration

Be First to Comment

    Leave a Reply

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