6 min read

Kubernetes quick start

Contents

Set up a powerful dev environment

Kubernetes is a wide and deep system to use and learn. One thing you can do to help yourself out it to set up an efficient and powerful development environment. Common tasks can be made easier with the right tools.

Except for kubectl, none of these tools are required for working with Kubernetes, but they will make your life a lot easier.

1. kubectl

The problem it solves:
I need to interact with my cluster from the terminal.

  1. Install kubectl.

    Kubectl is the official Kubernetes command-line tool. You will use it regularly to interact with your cluster, whether deploying applications, inspecting resources, or debugging issues.

  2. Get a kubeconfig file.

    Kubectl requires a kubeconfig file. This file should be provided by your cluster admin. If you use a managed cluster service like AWS EKS or Google Cloud GKE the file is typically generated by that provider's CLI tool.

    Save your kubeconfig file to $HOME/.kube/config.

  3. Test kubectl.

    Try the following commands to get the status of your cluster components, your nodes, and a list of pods in the default namespace.

    kubectl get componentstatus
    kubectl get nodes
    kubectl get pods

2. kubectx and kubens

The problem they solve:
It's annoying to switch between multiple contexts and namespaces.

  1. Install kubectx and kubens (kubens is also installed when installing kubectx).

    A kubeconfig file can have connections to multiple clusters, or even different users on the same clusters. Each of these cluster/user combinations is called a context. A common use case for contexts is working in multiple clusters for test, staging, and production.

    Switching between contexts frequently can be cumbersome, but kubectx makes it easier.

  2. List your current contexts.

    Run kubectx with no flags to list your available contexts; the current one will be highlighted. You might only see one if that's all that specified by your kubeconfig. If there are multiples, you can switch them with kubectx <context_name>.

  3. List your current namespaces.

    Run kubens with no flags to list your available namespaces; the current one will be highlighted.

    Working with namespaces is similar to working with contexts in that switching between them can also be cumbersome. kubens is similar to kubectx, only for switching between namespaces instead of contexts.

    By default, a kubectl command will only run against the default namespace. Working with resources in other namespaces requires the --namespace flag to specify a specific namespace. Kubens removes the need for this by setting your current context to a specific namespace.

  4. Create another namespace.

    kubectl create namespace test_ns
  5. Use kubens to switch namespaces.

    kubens test_ns
    kubens

    The first command switches your context to the test_ns namespace, then kubens lists the namespaces again, with kubens_test highlighted. Any commands you run now will apply to this namespace without needing the --namespace test_ns flag.

3. kube-ps1

The problem it solves:
I need to know at a glance what context and/or namespace I'm working in (a.k.a. I didn't know that I was working in production when I deleted that namespace 😑)

  1. Install kube-ps1.

    Changing contexts and namespaces is very handy, but still can lead to mistakes if you don't always remember which context and namespace you are working in. Kube-ps1 adds the current context and namespace to your command prompt, giving a quick visual reminder of where you are at.

    # before:
    ~ kubectl get pods
    # after:
    ~ (|minikube:default) kubectl get pods

4. kubectl-sudo

The problem it solves:
I need to make it harder to accidentally run destructive commands.

  1. Install kubectl-sudo.

    This plugin for kubectl adds sudo functionality just like on a Linux or macOS terminal. The idea is that you would operate normally as a user with less permissions on the cluster, with certain more powerful commands requiring a kubectl sudo to execute as more powerful user.

  2. Setting up kubectl-sudo involves creating ClusterRole and ClusterRoleBindings, the kubectl-sudo docs walk through creating these resources.

5. Local cluster

The problem it solves:
I need a cluster for testing.

If you need a small, local cluster for testing, there are many options available such as minikube, MicroK8s, and K3s.

  1. Pick one of these projects and install it.

    The installation process for minikube automatically adds the cluster to your kubeconfig and sets the context for you. You can see the context with kubens and change it as needed.

    MicroK8s installs it's own CLI utility to use to access the cluster, for example: microk8s kubectl get nodes.

    K3s requires an extra step to configure your kubeconfig after installation.

6. Lens dashboard

The problem it solves:
I need an easier way to see my cluster status.

While kubectl is great for quick commands, sometimes you might want greater visibility of your cluster and resources in a more visual format. Lens is a great tool for this, essentially acting as a Kubernetes IDE. You can see all your Pods, check the contents of different ConfigMaps, get the status of your storage or any other resource with just a few clicks. You can even configure multiple clusters and quickly switch between them.

7. Get started with your new tools

Follow our guide on getting code running on your cluster to test out your powerful new Kubernetes dev environment!

You can also read about the basics of Kubernetes to learn about the different cluster components and resources.


Questions?

We’re here to help if you have questions.