Install Kubernetes In Docker

/ Comments off

This tutorial provides you the step by step procedure for installing and configuring kubernetes multinode cluster with Docker on Linux (RHEL7 / CentOS7) using Kubeadm and Kubectl. But there is also a another way of setting up Kubernetes cluster with Minikube. This post will not cover the minikube installation for now.

  • Step 2 - Kubernetes Cluster Initialization. In this step, we will initialize the kubernetes master cluster configuration. Move the shell to the master server 'k8s-master' and run the command below to set up the kubernetes master. Kubeadm init -apiserver-advertise-address=10.0.15.10 -pod-network-cidr=10.244.0.0/16.
  • Step 3 - Start and Enable Docker. Step 4 - Install Kubernetes. Step 5 - Add Software Repositories. Step 6 - Kubernetes Installation Tools. Step 7 - Kubernetes Deployment. Step 8 - Assign Unique Hostname for Each Server Node. Step 9 - Initialize Kubernetes on Master Node. Step 10 - Deploy Pod Network to Cluster.
  • Enable Kubernetes 🔗. To enable Kubernetes support and install a standalone instance of Kubernetes running as a Docker container, go to Preferences Kubernetes and then click Enable Kubernetes. By default, Kubernetes containers are hidden from commands like docker service ls, because managing them manually is not supported.

In my previous post, I showed how you can install Docker Desktop for Windows using WSL 2. In this post I show how to enable Kubernetes support, and how to deploy the Kubernetes Dashboard to give your local Kubernetes cluster a friendly GUI.

Do you need Kubernetes?

Your first question before going down this route should be 'do I need Kubernetes?'. Kubernetes has become the defacto container orchestrator for production workloads, but that doesn't mean you necessarily need it. Especially for local development.

Install Kubernetes In Docker

In my experience, the biggest wins from Docker come from the isolation it gives you from your host system. You don't need Kubernetes to benefit from that. Even if you find you start needing to orchestrate containers locally, I'd recommend Docker Compose as a simpler alternative to Kubernetes.

If you're in the .NET space, then Project Tye is another great option which should give a simpler onboarding than Kubernetes.

That said, if you're deploying to production on Kubernetes then there's considerable value to being able to run and test your code in a similar environment locally. The downside to that is it means running your own Kubernetes cluster locally… Luckily, Docker Desktop can take care of that for you!

For an introduction to Kubernetes, or for advice on deploying ASP.NET Core applications to Kubernetes, see my 12 part series.

Enabling Kubernetes on Docker Desktop for Windows

Kubernetes can run on top of Docker (currently), and Docker Desktop for Windows (or Mac) includes support for running a Kubernetes cluster for you, without you having to manually configure the cluster yourself. This is very convenient, as you'll see shortly!

There are many other ways to run Kubernetes locally, such as using kind or minikube, but if you're already using Docker Desktop, then this is probably the easiest approach.

You only need to do two things to get Kubernetes running in Docker Desktop:

  • Check Enable Kubernetes in Docker Desktop

Simple right!

After you've followed the instructions in my previous post to install WSL 2 and Docker Desktop, open up the settings screen (by clicking the cog in the top right). Navigate to the Kubernetes tab, and check Enable Kubernetes:

Click Apply & Restart, and you'll get a popup advising you that you need an internet connection. Click Install to continue.

Docker Desktop pulls all the required images in the background, and starts setting up your cluster. In the bottom left of the screen you can see the little Kubernetes symbol has appeared, but it's orange to indicating it's still starting up.

Once that turns green, you have Kubernetes up and running! Click Cancel to exit the settings.

You can now start deploying Kubernetes resources to your cluster. From the command line, you can run kubectl commands. For example kubectl get pods -n kube-system shows you some of the pods that are part of the Kubernetes system:

Docker Desktop doesn't list the pods in these containers in its UI, but they're there!

If you want to see these containers listed in the Containers/Apps section of Docker Desktop, enable the Show System Containers (Advanced) checkbox in Settings.

In fact, this is the main downside with the Kubernetes support in Docker Desktop. As I showed in my previous post, Docker Desktop gives a nice UI with convenience methods for shelling into containers etc. There isn't an equivalent out-of-the-box experience for Kubernetes.

Installing Kubernetes Dashboard to get a nice UI

It would be really nice if Docker Desktop included a GUI for Kubernetes out of the box, but we can get a similar experience using Kubernetes Dashboard. This project provides a Web UI for managing your cluster, providing an easy way to see an overview of your cluster, and even for editing and exec-ing into containers.

To install the Kubernetes Dashboard, open a PowerShell terminal, and run the following:

This command downloads a file from the internet and runs it on your computer. You should always be wary of doing that, inspect the file first and make sure you're happy using it!

This command downloads the deployment manifest and applies it to your cluster, you should see something like the following:

If you take a look in Docker Desktop, you'll see some containers are running, with long names. These are the containers running as part of the Kubernetes Dashboard pods

At this point, we could technically start the Kubernetes Dashboard, but you'd be presented with the following login screen:

Rather than faff with tokens, we're going to enable a setting that lets you bypass this authentication page entirely.

Obviously don't do this in production!

Install kubernetes in docker tutorial

Disabling the login prompt in Kubernetes Dashboard

The Kubernetes dashboard allows you to bypass the login page if you edit the default deployment and add an extra argument --enable-skip-login.

There's two main ways you can do this:

  • Use kubectl edit to open an editor to edit the YAML manifest.
  • Use kubectl patch to patch the YAML manifest directly

The former is 'easier', while the latter lets you script it (I'll provide the script shortly).

The goal is to update this section of the kubernetes-dashboard deployment (in the kubernetes-dashboard namespace):

to add the --enable-skip-login argument, as shown here:

If you want to make this change manually, run the following, which opens notepad.exe

make the changes, and close the editor, and Kubernetes will update the deployment.

The above snippet shows the structure of the YAML, but there are a lot more values in the deployment. Make sure you don't accidentally delete any, and take care with the whitespace!

Alternatively (my preference), just run the following command. This does the same thing, adding the extra argument, but avoids having to mess with notepad:

Now we're at a point to give the dashboard a try.

Viewing the Kubernetes Dashboard

To view the dashboard, run kubectl proxy in a terminal:

As indicated, you can now call services in the cluster using port 8001. The URL you need for the Kubernetes dashboard is:

Doesn't exactly roll of the tongue, does it? Be sure to bookmark it 🙂

If you now access this page, you'll see the login page as before. But now there's an all-important 'skip' button which lets you avoid messing with tokens!

When the dashboard first loads up you're presented with an empty UI—not entirely surprising as you're not running any applications yet!

This is the view for the default namespace, which is where your services will be deployed by default. You can view other namespaces by selecting them from the dropdown at the top left of the page. In the following screenshot I've selected the kubernetes-dashboard namespace, so you can see there's two deployments:

This is all pretty nice, and will be good enough for most people. However, most screenshots of the dashboard show a view that includes nice graphs of CPU and memory usage:

If you want to get these graphs you'll need to install the Kubernetes Metrics Server.

Installing Kubernetes Metrics Server

From the project's GitHub page:

Metrics Server collects resource metrics from Kubelets and exposes them in Kubernetes apiserver through Metrics API…

The Kubernetes Dashboard already comes with the prerequisites for working with metrics server, (did you notice the 'metrics-scraper' pod in your cluster?) so you just need to install the Metrics Server itself.

Run the following command to download the Metrics Server manifests and install them in your cluster:

Cached

Again, be sure to check you're happy with the script before you download it to to your machine!

This installs a variety of resources in your cluster, but again, we need to patch one of the deployments. We need to add the --kubelet-insecure-tls argument to the metrics-server deployment, otherwise you'll see an error saying something like unable to fetch metrics from node docker-desktop. The following command patches the deployment:

With that, you'll now get nice graphs on your pods! If you're not already, run kubectl proxy and view the pods in the kubernetes-dashboard namespace, we now have nice graphs for everything!

Our dashboard setup is now complete. For completeness, I'll show how to easily delete all the dashboard and metrics resources if you don't need them anymore.

Deleting the dashboard + metrics server

Deleting the resources is very similar to creating them, but instead of using kubectl apply we'll use kubectl delete:

First delete the metrics server:

and then delete the dashboard:

After that, everything should be back as it was before you started.

Bonus: stopping WSL eating all your memory

While I was playing with the Kubernetes support, I ran into a common issue—WSL was eating all my memory! Luckily, there's an easy fix by creating a .wslconfig file in your user profile folder, as described in the docs.

The following script creates the file, and limits the memory used by WSL to 8GB

That solved the issue I was seeing.

Summary

In this post I showed how to enable Kubernetes for Docker Desktop, how to install the Kubernetes Dashboard, and how to install Metrics Server to provide CPU and memory usage graphs in the dashboard. The following script installs and patches both the dashboard and metrics server, and then starts the kubectl proxy so you can access the dashboard:

After you start the proxy, you can access the dashboard at the following link: http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.

Estimated reading time: 6 minutes

Install Kubernetes In Docker 8

Prerequisites

  • Download and install Docker Desktop as described in Orientation and setup.
  • Work through containerizing an application in Part 2.
  • Make sure that Kubernetes is enabled on your Docker Desktop:
    • Mac: Click the Docker icon in your menu bar, navigate to Preferences and make sure there’s a green light beside ‘Kubernetes’.
    • Windows: Click the Docker icon in the system tray and navigate to Settings and make sure there’s a green light beside ‘Kubernetes’.

    If Kubernetes isn’t running, follow the instructions in Orchestration of this tutorial to finish setting it up.

Introduction

Now that we’ve demonstrated that the individual components of our application run as stand-alone containers, it’s time to arrange for them to be managed by an orchestrator like Kubernetes. Kubernetes provides many tools for scaling, networking, securing and maintaining your containerized applications, above and beyond the abilities of containers themselves.

In order to validate that our containerized application works well on Kubernetes, we’ll use Docker Desktop’s built in Kubernetes environment right on our development machine to deploy our application, before handing it off to run on a full Kubernetes cluster in production. The Kubernetes environment created by Docker Desktop is fully featured, meaning it has all the Kubernetes features your app will enjoy on a real cluster, accessible from the convenience of your development machine.

Describing apps using Kubernetes YAML

All containers in Kubernetes are scheduled as pods, which are groups of co-located containers that share some resources. Furthermore, in a realistic application we almost never create individual pods; instead, most of our workloads are scheduled as deployments, which are scalable groups of pods maintained automatically by Kubernetes. Lastly, all Kubernetes objects can and should be described in manifests called Kubernetes YAML files. These YAML files describe all the components and configurations of your Kubernetes app, and can be used to easily create and destroy your app in any Kubernetes environment.

  1. You already wrote a very basic Kubernetes YAML file in the Orchestration overview part of this tutorial. Now, let’s write a slightly more sophisticated YAML file to run and manage our bulletin board. Place the following in a file called bb.yaml:

    In this Kubernetes YAML file, we have two objects, separated by the ---:

    • A Deployment, describing a scalable group of identical pods. In this case, you’ll get just one replica, or copy of your pod, and that pod (which is described under the template: key) has just one container in it, based off of your bulletinboard:1.0 image from the previous step in this tutorial.
    • A NodePort service, which will route traffic from port 30001 on your host to port 8080 inside the pods it routes to, allowing you to reach your bulletin board from the network.

    Also, notice that while Kubernetes YAML can appear long and complicated at first, it almost always follows the same pattern:

    • The apiVersion, which indicates the Kubernetes API that parses this object
    • The kind indicating what sort of object this is
    • Some metadata applying things like names to your objects
    • The spec specifying all the parameters and configurations of your object.

Deploy and check your application

  1. In a terminal, navigate to where you created bb.yaml and deploy your application to Kubernetes:

    you should see output that looks like the following, indicating your Kubernetes objects were created successfully:

  2. Make sure everything worked by listing your deployments:

    if all is well, your deployment should be listed as follows:

    This indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services:

    In addition to the default kubernetes service, we see our bb-entrypoint service, accepting traffic on port 30001/TCP.

  3. Open a browser and visit your bulletin board at localhost:30001; you should see your bulletin board, the same as when we ran it as a stand-alone container in Part 2 of the Quickstart tutorial.

  4. Once satisfied, tear down your application:

Conclusion

At this point, we have successfully used Docker Desktop to deploy our application to a fully-featured Kubernetes environment on our development machine. We haven’t done much with Kubernetes yet, but the door is now open; you can begin adding other components to your app and taking advantage of all the features and power of Kubernetes, right on your own machine.

In addition to deploying to Kubernetes, we have also described our application as a Kubernetes YAML file. This simple text file contains everything we need to create our application in a running state. We can check it into version control and share it with our colleagues, allowing us to distribute our applications to other clusters (like the testing and production clusters that probably come after our development environments) easily.

Kubernetes references

Kubernetes - Autoscaling - Tutorialspoint

Further documentation for all new Kubernetes objects used in this article are available here:

Docs.docker.com › Desktop › KubernetesDeploy On Kubernetes Docker Documentation

kubernetes, pods, deployments, kubernetes services