Using Minikube and Skaffold for local Kubernetes development

If you are considering to start a project powered by Kubernetes, it may seem mind-boggling where and how to start. With that said, I am writing this article to comment on the added value of empowering your development stack with Skaffold and minikube.

Minikube allows you to spin a Kubernetes cluster in your development stack without much effort. It supports multiple drivers enabling you to deploy it as a container, virtual machine or bare-metal. In this article, you will learn how to spin a cluster and to take advantage of Hyper-V, which is Windows native hypervisor.

minikube quickly sets up a local Kubernetes cluster on macOS, Linux, and Windows. We proudly focus on helping application developers and new Kubernetes users. [1]

Skaffold is a tool that allows you to do continuous integration/continuous delivery (CI/CD) for Kubernetes workloads. It will handle the whole workflow: building, pushing, and deploying workloads. You can use it to develop locally, and when you are ready to go to production, you can use this tool to ship your workloads. Isn’t that awesome?

Skaffold handles the workflow for building, pushing and deploying your application, allowing you to focus on what matters most: writing code. [2]

(image by roketpik)

Let’s setup a quick project!

For this demo, we will use pdefreitas/skaffold-react-demo workload as an example of how you can leverage these tools, and we will setup minikube to use Hyper-V driver. First, we need to set up a virtual network interface for the cluster, and you can do so by using Hyper-V Manager UI to create an external virtual switch. On the right panel, you can find an option called Virtual Switch Manager, click on it and a new window prompting the creation of it will pop up. Ensure that you create it as an external switch for this demo purposes. For this demo, we will name the switch ‘Minikube Virtual Switch’.

After this, you’re good to setup minikube and start your cluster! To do so, you will need to have Chocolatey, which is a package manager for Windows installed in your machine since minikube developers ship the Windows version of it through it. You can install minikube by running the following command in an elevated prompt, it will also install kubernetes-cli since it is an dependency of it:

choco install minikube

To spin up the cluster run the following command in an elevated prompt, it will setup an Kubernetes cluster with 2 vCPUs, 2048mb of RAM and 20gb of disk storage. You can tweak these values for your use-case needs:

minikube start --cpus=2 --memory=2048 --disk-size=20g --driver=hyperv --hyperv-virtual-switch "Minikube Virtual Switch"

Now we have to install Skaffold. On Windows you can do so by using Chocolatey too:

choco install skaffold

Don’t forget, to build the container images that you need to have Docker installed too!

For this demo, I’ve built a React application with facebook/create-react-app. Which you can get by git cloning it.

git clone https://github.com/facebook/create-react-app.git

You can run the project by running Skaffold from the root:

skaffold dev

If you do list the Kubernetes services running your Minikube cluster, you should now be able to see reactdemo-service:

kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h52m
reactdemo-service LoadBalancer 10.109.198.243 <pending> 8080:32693/TCP 56m

Since this service is of type LoadBalancer, minikube will map a NodePort (in this case number 32693) from the single node it runs. To know which IP is attributed to the minikube node, you can use the following command:

minikube ip
192.168.1.191

You should now be able to access the application through HTTP on that IP and port 32693. minikube also ships a nice feature that allows you to open a browser window with this endpoint automatically. You can do so by running the following command:

minikube service reactdemo-service

You should now be able to see the Kubernetes logo spinning in your browser! In case you change any file, the application will automatically redeploy without your intervention.

pdefreitas/skaffold-react-demo

Skaffold can also be run without automatic redeploys by using the following command:

skaffold run

If we look at the folder structure you will be able to see a simple structure of a Skaffold project:

NOTE: This is not a proper example of how to deploy/publish/orchestrate React applications for production use. If you intend to run production workloads, please research more regarding that, including hardening strategies like mentioned in my previous medium article.

Conclusion

Now you should be able to deploy your Kubernetes workloads without much hassle. Skaffold provides all sorts of functionality, including advanced CI/CD strategies, in-cluster image building by using Kanico and custom build commands if needed. To learn more about Skaffold documentation Architecture and Design section is an excellent next point for understanding in-depth this fantastic tool. If you don’t use Windows or Hyper-V no worries, minikube supports multiple drivers for your requirements!

References

[1] Skaffold
[2] Welcome! minikube

Full-time DevOps Engineer | CKA | CKAD — Helping organizations to grow a DevOps culture.