Beginners guide to Kubernetes: Kubectl

October 31, 2022
Beginners guide to Kubernetes Kubectl

If you are a developer and just starting with Kubernetes, you may have seen kubectl commands and wondered: Is it a tool just for DevOps? Should I be using it, too? How can it be useful for me? Will it take too much time for me to learn to use it? In this beginner’s guide to Kubernetes: Kubectl, we want to help you understand Kubectl and give you an outline of the basic commands you will likely need.

How to get started with Kubernetes

In case you have just started looking into Kubernetes, here you have a quick roundup of what it offers, how it works, and how to get it running. If you have this already figured out, you can skip to the next section on how to use Kubectl.

What is Kubernetes and why would you use it

Kubernetes, or simply K8s, is an open-source orchestration tool that helps you manage your cloud-native applications. One of their main appeals is that they run in the same isolated environment–their containers–, from development to production, and hence on every possible system that supports a container runtime. Kubernetes provides this runtime and adds a series of interesting features, such as automatic horizontal scaling and easy rollouts and rollbacks with zero downtime. In addition, it relies on a huge and very active community that guarantees the quality of the project. Because of all this, and much more, Kubernetes has become the base of cloud computing and all major cloud vendors support it.

Kubernetes architecture

Before diving into the hands-on part, it is important to understand the very basics of K8s architecture.

In your cloud, the workload is shared among a cluster of nodes. This cluster is composed of one or more working nodes and one master node. The working nodes are the ones that actually run the applications. Apps run inside pods, which are the smallest deployable unit in Kubernetes. K8s can launch and kill multiple pods running the same application to adjust to demand. They are registered as a service so that clients can access them via a persistent IP address and port.

The master node manages the whole cluster – it monitors the worker nodes and schedules the pods. The master also exposes the APIs for different operations through its API server. This component of the master node is the entry point to the cluster, either via a UI or a CLI such as Kubectl.

How to get started with Kubernetes

A good starting point is to install minikube on your computer. This tool sets up a local K8s cluster on your computer so that you can test it and get comfortable with Kubernetes before going all out.

Conversely, if you want to start using the cloud right away, the best place to do it is Napptive. It manages Kubernetes for you so that you can start running your applications without having to learn K8s beforehand. You could still access it with Kubectl in case you need it, as you will see in the last section of this article.

Join Napptive

Napptive enables developer self-service. We encourage you to try our playground and experience accelerated cloud-native development. It’s completely free, all you need to do is simply sign up and get started!

Beginnners guide to Kubernetes: Kubectl

Kubectl is a powerful CLI for communicating with a Kubernetes cluster’s control plane, using the Kubernetes API. The syntax of the kubectl command is as follows:

<strong>kubectl [command] [TYPE] [NAME] [flags]</strong>Code language: HTML, XML (xml)

where command, TYPE, NAME, and flags are:

  • command: specifies the operation that you want to perform. Usual operations are: create, describe, get, apply, and delete.
  • TYPE: specifies the resource type, for example, pod. You can use the singular, plural, or abbreviated forms. For example, pod, pods, and po refer to the same resource type.
  • NAME: specifies the resource name. Unlike types, names are case-sensitive. If the name is omitted, details for all the resources are displayed. For example, kubectl get pods.
  • flags: specifies optional flags. They work as modifiers and override any default values or environmental variables. For example, you can use the -s or –server flags to specify the address and port of the Kubernetes API server.

If you need help, run kubectl help.

You can also check all the details on the kubectl reference page of the Kubernetes documentation.

Mini-cheatsheet of kubectl commands

Some of the most used commands are the following:

kubectl get nodesCode language: JavaScript (javascript)

Nodes are virtual or physical machines that act as working machines in Kubernetes. This command grabs each node’s name, status (running, ready, inactive), roles (master, worker, controlplane, etcd), age, and Kubernetes version.

kubectl get pods --field-selector=status.phase=RunningCode language: JavaScript (javascript)

This command lets you track which pods are running.

kubectl cluster-info

This command displays endpoint information (name, resource type, etc.) for your master node and the services running atop your cluster’s infrastructure.

kubectl logs -f <service_name>Code language: HTML, XML (xml)

Use this to display human-readable details about how the given service is running, which notable events occurred, and at what time.

kubectl apply -f config.yamlCode language: CSS (css)

You can use this to create a deployment specified by the configuration file in YAML format that is passed as an argument after the -f flag.

kubectl set image deployment <deployment_name> <container_name>=<image>Code language: HTML, XML (xml)

This command initiates a rolling update of the <container_name> containers of the <deployment_name> deployment by changing the image.

kubectl rollout undo deployment <deployment_name> Code language: HTML, XML (xml)

Conversely, you can undo the rollout to the previous version of the deployment. Add the flag –to-revision=<number> to rollback to a specific version.

You can find a much more comprehensive cheatsheet on the Kubernetes reference’s kubectl page.

How to use Kubectl with Napptive

The Napptive Playground offers compatibility by default with the standard Kubernetes API and thus is compatible with kubectl without any changes. Just keep in mind that the kubeconfig file, which contains all the information required to interact with the standard Kubernetes API, will be available on ~/.napptive/<installation_name>/napptive-kubeconfig. You will have to specify the route to your kubeconfig using the –kubeconfig flag, such as:

kubectl --kubeconfig ~/.napptive/default/napptive-kubeconfig get podsCode language: JavaScript (javascript)

For more details, you can check the kubectl page in the Napptive Playground Documentation.

If you want to propel your development, why not try our playground? It’s free, simply sign up and get started!

More like this

The Evolution of Platform Engineering: Past, Present, and Future

The Evolution of Platform Engineering: Past, Present, and Future

In this post, we delve into the history of platform engineering, examining how it has evolved and what the future might hold. We will explore the transition from traditional IT infrastructure to modern platform engineering practices, and predict future trends and...

How to Quantify the ROI of Platform Engineering

How to Quantify the ROI of Platform Engineering

Measuring the Impact Platform Engineering is becoming the new “it” thing in software development, and it’s no wonder why. For starters, if we use an IDP (Internal Developer Platform) we can end up having fewer silos, better reusing components, and reaching an improved...

Host your own dashboard with Metabase

Host your own dashboard with Metabase

Data analytics platform on Napptive Data is one of the most precious assets of the twenty-first century, driving innovation, informing decisions, and shaping the future of technology and business. But extracting value out of an organization's data requires strategic...