Running Kubernetes, Docker and a private Registry locally

Posted by: Seth Lakowske

Published:

This guide will walk you through the steps to setting up an environment to run Kubernets (k8s) on your laptop or workstation. You'll need to install Docker if you haven't already and Kubernetes client (kubectl).

Requirements

Run Kubernetes using Docker

Once you have Docker Toolbox installed, you can leverage Docker to run Kubernetes with hyperkube. First, setup the Kubernetes version environment variable.
export K8S_VERSION=$(curl -sS https://storage.googleapis.com/kubernetes-release/release/stable.txt)
Now run the Kubernetes Hyperkube image. This image will run additional Kubernetes images used to orchestrate your cluster. I've added --restart=always so that Kubernetes will be relaunched on reboots, or if it dies. You may tweak these starting parameters at some point(i.e. --cluster-dns), but they ought to provide working defaults for now.
export ARCH=amd64
docker run -d \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:rw \
--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 \
--restart=always \
gcr.io/google_containers/hyperkube-${ARCH}:${K8S_VERSION} \
/hyperkube kubelet \
--containerized \
--hostname-override=127.0.0.1 \
--api-servers=http://localhost:8080 \
--config=/etc/kubernetes/manifests \
--cluster-dns=8.8.8.8 \
--cluster-domain=cluster.local \
--allow-privileged --v=2
Verify your setup is running. You should see a number of kubernetes containers in the list.
docker ps
After you verify the docker containers exist, you should verify you can connect to the Kubernetes cluster.
kubectl get pods --all-namespaces
Note for macOS users: If that doesn't work, you may need to port forward to the virtual machine vm.
docker-machine ssh `docker-machine active` -N -L 8080:localhost:8080

Run a private local registry

Start a private local registry that Docker always restarts if the container dies.
docker run -d -p 5000:5000 --restart=always --name registry registry:2
Build an image and tag it.
docker build -t hello-node .
docker tag hello-node localhost:5000/hello-node
Push the image to your local registry.
docker push localhost:5000/hello-node
Now run a deployment in Kubernetes using the image located on your local registry.
kubectl run hello-node --image=localhost:5000/hello-node --port=8888

Conclusion

You should now have a deployment of your image, in my case hello-node, running using your private local registry, Kubernetes and Docker. You can verify by running

kubectl get deployments

You may also connect to your pod via port forwarding to verify connectivity. For example, I forward traffic from 8090 on my localhost to port 8080 on a jenkins pod. Now when I point my browser to localhost:8090, I talk to the jenkins web service.

kubectl port-forward jenkins-3828317938-il1gc 8090:8080