Kubernetes is a great tool for container orchestration on a server cluster. It makes it easy to deploy lots of containers in a resource-efficient way using a simple interface.
But one thing that is not easy to do with Kubernetes is to deploy it locally. Kubernetes is designed to run on an actual cluster, which means using it only on a single computer is tough.
I know. You're probably wondering why you'd want to use Kubernetes locally in the first place. The whole point of Kubernetes is to simplify the management of containers on a cluster, right? So running it on a single server might seem like building a second kitchen inside your garage. You could do it, but would it really be that useful?
Well, yes. There are actually some good reasons why you might want to run Kubernetes on a local test box. Maybe you need to test how your apps behave under Kubernetes before putting them into production. Or perhaps you just want to hone your skills, working with the kubectl
CLI interface in a safe, sandboxed environment.
Fortunately, it's possible to run Kubernetes locally for these purposes. In fact, it's so possible that there's more than one way to do it. But in this post I'll cover the most bare-metal route, which involves running Kubernetes through Docker on your local machine.
(Another way to do this is with Vagrant, but that requires running a virtual machine through a traditional hypervisor. A local Kubernetes installation through Docker is much lighter on system resources.)
(Lastly, the Red Hat recommended way is to use the Red Hat Container Development Kit, available here (free), from Red Hat Developers.)
The only prerequisite — Your development box needs to be running a modern mainstream GNU/Linux distribution such as Red Hat Enterprise Linux, which is available for $0 for development use (you can download it here).
Step 1: Install and Configure Docker and kubectl
We need two tools for this to work. The first is Docker. The procedure for installing Docker varies according to your OS, but in most cases, it can be done easily through the package manager (yum and dnf on RHEL and Fedora). Check out the Docker installation documentation for specifics.
The second tool to grab is kubectl
, the Kubernetes CLI tool. You can get this with a quick curl (wget would work fine, too), followed by commands that make the downloaded file executable, and place it in your PATH. The following commands will get the job done:
curl -O https://storage.googleapis.com/kubernetes-release/release/v1.2.2/bin/linux/amd64/kubectl chmod +x kubectl mv kubectl /usr/local/bin/kubectl
Lastly, we need to set an environment variable specifying the Kubernetes version we're using. We just downloaded version 1.2.2, so enter:
export K8S_VERSION=v1.2.2
(If you have a different version of kubectl installed, then change the preceding command accordingly.)
Step 2: Start Docker
This next step is to start a Kubernetes master (using an image from the Google Cloud Platform repository) on your local machine via Docker. This single command will do it all for you:
docker run \ --volume=/:/rootfs:ro \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:rw \ --volume=/var/lib/kubelet/:/var/lib/kubelet:rw \ --volume=/var/run:/var/run:rw \ --net=host \ --pid=host \ --privileged=true \ --name=kubelet \ -d \ gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \ /hyperkube kubelet \ --containerized \ --hostname-override="127.0.0.1" \ --address="0.0.0.0" \ --api-servers=http://localhost:8080 \ --config=/etc/kubernetes/manifests \ --cluster-dns=10.0.0.10 \ --cluster-domain=cluster.local \ --allow-privileged=true --v=2
Step 3: Create a Cluster
The final step is to use kubectl to create a cluster for us to manage, like so:
kubectl config set-cluster test-doc --server=http://localhost:8080 kubectl config set-context test-doc --cluster=test-doc kubectl config use-context test-doc
Step 4: Enjoy
That's it! With your Kubernetes master running and kubectl installed in your PATH, you can now use the latter to orchestrate your local Kubernetes cluster.
If you want to launch other apps in containers locally so that you can orchestrate them with Kubernetes, feel free to use Docker to do so. You can also check out the kubectl documentation for the complete list of commands available from the tool.
Parting Thoughts
This is a handy way to do some quick testing or experimentation with Kubernetes on a developer box. But it's also worth acknowledging the downsides of this approach so that you're aware of the limitations.
The biggest limitation is that this setup doesn't make it easy to sync your development and testing environments. If you update your app code, you'll have to restart Docker with Kubernetes and the newer app images manually. If this is a real problem for you, you might have a better experience with Vagrant. Vagrant doesn't automate the process entirely, but it at least makes it a little faster to start up a new cluster.
The other downside to this method is that it doesn't involve real networking. It does everything over localhost. In most situations, this should not be a problem. But if you need to test Kubernetes for the orchestration of apps in a cluster containing multiple nodes with different real IP addresses, this won't work well. In that case, you really need a real cluster (or a set of virtual machines, at least).
Despite these drawbacks, the steps described above are useful if you need to do some quick Kubernetes testing, or if you just want to practice using kubectl.
Hemant Jain is the founder and owner of Rapidera Technologies, a full service software development shop. He and his team focus a lot on modern software delivery techniques and tools. Prior to Rapidera he managed large scale enterprise development projects at Autodesk and Deloitte.
Last updated: January 19, 2023