= 5 things you should be doing with Kubectl

Kubernetes is software to help you run lots of containers in an organized way.
Aside from providing tools to manage (or https://opensource.com/article/20/11/orchestration-vs-automation[orchestrate]) the containers you run, Kubernetes also helps those containers scale out as needed.
With Kubernetes as your central control panel (or _control plane_), you need a way to manage Kubernetes, and the tool for that job is kubectl.
The `kubectl` command lets you control, maintain, analyze, and troubleshoot Kubernetes clusters.
As with many tools using the `ctl` (short for "control") suffix, such as systemctl and sysctl, kubectl is has purview over a broad array of functions and tasks, so you end up using it a lot if you're running Kubernetes.
It's a big command, with lots of options, so here are five common tasks that kubectl makes easy.

== 1. List and describe resources

Containers, by design, tend to multiply.
Under certain conditions, they can multiply rapidly.
This can get overwhelming if the only way you have to see running containers is `podman ps` or `docker ps`.
With `kubectl get` and `kubectl describe`, you can get information about what pods are running and the containers they're handling.
What's more is that you can get just the information you need by using options like `--namespace` or `name` or `--selector`.

The `get` subcommand is useful for a lot more than just pods and containers, too.
It has information about nodes, namespaces, deployments, services, and replicas.

== 2. Create resources

If you've only ever created deployments through a web UI like the one provided by OpenShift, OKD, or Kubernetes, but you're looking to take control of your cluster from your Linux terminal instead, then get ready to use `kubectl create`.
The `kubectl create` command doesn't just instantiate a new app deployment, though.
There are lots of other components available in Kubernetes that you can create, such as services, quotas, and https://opensource.com/article/20/11/kubernetes-jobs-cronjobs[cronjobs].

A cronjob in Kubernetes can create a transient pod meant to perform some task on a schedule of your choosing.
They're not difficult to set up.
Here's a cronjob to have a Busybox image echo "hello world" every minute:

[source,bash]
----
$ kubectl create cronjob \
hello-world \
--image=busybox \
--schedule="*/1 * * * *" -- echo "hello world"
----

== 3. Edit files

You may have an understanding that objects in Kubernetes have accompanying configuration files, but rummaging through your filesystem to find the appropriate file can be troublesome.
With `kubectl` edit, you can keep your mind on the objects and not on the files that define them.
You can have `kubectl` find and open the file for you (it respects the `KUBE_EDITOR` environment variable, so you can set your editor to whatever you prefer):

[source,bash]
----
$ KUBE_EDITOR=emacs \
kubectl edit cronjob/hello-world
----

== 4. Trade files between containers

Newcomers to containers are often baffled by the concept of a shared system that they can't apparently access.
They may learn about `exec` options in their container engine or in kubectl itself, but containers still can seem impervious when they can't just grab a file from a container, or place a file into the container.
Using the `kubectl cp` command, you can treat containers as if they were remote servers, making copying files to and from containers no more complex than an SSH command.

[source,bash]
----
$ kubectl cp foo my-pod:/tmp
----

== 5. Apply changes

Making changes to Kubernetes objects can be done any time with the `kubectl apply` command.
All you have to do is point the command to a configuration file:

[source,bash]
----
$ kubectl apply -f ./mypod.json
----

Akin to running an Ansible playbook or a Bash script, `apply` makes it easy to "import" settings quickly into a running Kubernetes instance.

== Use kubectl

Kubectl is a powerful tool, and because it's a terminal command it can be scripted and used in many ways a web UI cannot.
Learning kubectl is a great way to further your understanding of Kubernetes, containers, pods, and all the technologies that surround these important cloud innovations.
Download our kubectl cheat sheet for a quick reference, complete with sample commands, to help you as you learn, and remind you of the details once you're a pro.