Kubernetes with Minikube and switching Docker environments
The popularity of Kubernetes as a container orchestration service has been growing at an amazing rate. Developers are flocking to the platform and taking advantage of the increase in the speed of their development deployments.
Minikube is a tool that helps you run Kubernetes locally. It runs as a single-node Kubernetes cluster on your machine so you can use it to run your software which will use the same/or similar configuration to run in production.
With Kubernetes you run Docker containers within your pods. These containers are started by running Docker images. These images can be pulled from public repositories. But what happens when you are busy developing your software locally and need quicker iterations and not have to push the image changes to a public repository so that it can be pulled from that public repository just to start up your container on a Kubernetes pod running locally.
Because Minikube runs in a VM on your machine, it has Docker and a local repository installed. So all you really need to do is build your docker images locally and tag them inside of the Minikube VM.
Minikube has no way to access your local Docker environment or repository. Luckily there is an easy way to switch context between your Minikube and local environments.
You can find out more information about installing and starting Minikube here.
To switch your Docker context to using Minikube you run the following command from your terminal:
Now all your Docker commands will be executed in the Minikube VM. Now all you need to do is build your image and tag it to be available when deploying to your Minikube Kubernetes instance.
Now you can deploy the image to your local Minikube environment.
The important thing here is that you set the imagePullPolicy: Never
; This assumes that the image is available locally and will make no attempt to pull the image from a remote location.
Your pod should be running happily. When you make a code change and are ready to test out the changes in the container, you can just rebuild the image using the Docker build with no cache option. Then you can either just delete the deployment and apply it again; or just delete the pod so it will create a new one for you; or you can scale the deployment to 0 and then back up to 1, or your desired amount of replicas.
Once you are done with your development and you would like to switch back to your original context, you can do this by running the following:
You local Docker context would now be restored and you can happily run your local docker images again.
BONUS
Minikube can be quite resource intensive on your machine. You can tweak the resource usage settings in VirtualBox, or you can use some simple commands to tweak your environment. Sometimes you might want to dump everything from your Minikube environment and start over. Resetting your Minikube environment and changing resources can be done with the following commands:
More Resources
Want to learn more about Kubernetes. Please check out this great book that I helped review:
https://www.packtpub.com/product/the-kubernetes-workshop/9781838820756