Continuous Kubernetes deployments with Skaffold on Docker for Mac

Background

My previous post was about doing continuous deployment of Kubernetes application using Skaffold to a Minikube cluster. In the recent version of Docker for Desktop (Mac & Windows), there is built in support for Kubernetes. We can decide to deploy multi container applications to a single node local cluster and choose the type of Orchestrator. Currently Docker for Mac supports Docker Swarm and Kubernetes. In this post, I will demonstrate how we can use Skaffold to deploy to a Kubernetes cluster instead of Minikube.
We will perform following steps during the course of this post
  • Enable Kubernetes support for Docker for Mac
  • Set context to work with Docker for Mac
  • Use Skaffold with Docker for Mac

Enable Kubernetes support for Docker for Mac

Lets first enable the support for Kubernetes orchestrator for our installation of Docker for Mac. Navigate to the preferences section of Docker. Select the Kubernetes pane, enable Kubernetes support, select default Orchestrator ad Kubernetes. Finally apply the setting as shown with steps 1 to 4 below. In my case I had these settings already enabled. If you are enabling the settings for the first time it can take a while for the Kubernetes to start the single node cluster.
Kubernetes for Docker
Once everything is setup we should get the Docker is running and Kubernetes is running in green status.

Set context to work with Docker for Mac

We will be using the kubectl command line tool to deploy the artifacts to Kubernetes cluster. kubectl can work with multiple clusters at the same time. We need to ensure that the right context is set for the kubectl. Lets first get the list of all the clusters we have at our disposal. This is done by executing the command
kubectl config get-contexts
kubectl get context
We can see two clusters are currently configured. The minikube cluster is the current context as shown by the * sign in front of the cluster name. We need the context to be set to the docker-for-mac context. This can be done by executing the command
kubectl config use-context docker-for-desktop
kubectl use context
We are now ready to deploy the application to this cluster.

Use Skaffold with Docker for Mac

Once the right context is set, there is no change to be done on the skaffold side. We execute the same command skaffold dev which we did during the earlier post. Skaffold does its magic
skaffold dev - part1
The proper context has been selected by skaffold.
skaffold dev - part2
The build speed is just over 1 second to build the required Docker images. Same way the deploy is done in less than 2 seconds. I have not seen these kind of speeds while using docker compose in the past. With this, now skaffold is continuously monitoring for changes to the source code. Let test it by making some changes in the code.

As you can see from the above screen capture, the updates are really fast. It doesn't even take 1 second to update the deployment. I commented and uncommented the same set of lines that we had used during the previous post.

Conclusion

As can be seen from this demo, we can seamlessly move from minikube cluster to the docker for mac cluster while working with skaffold. It does not require any configuration change to the skaffold.yaml file or to the Kubernetes manifest files. Just  by setting the proper context for kubectl we can have the same productivity gains that we saw during the last post. I hope that developers working with Docker and Kubernetes find this tool useful.
Until next time, Code with Passion and Strive for Excellence.
Share:
spacer

4 comments:

  1. Thanks for finally writing about >"Continuous Kubernetes deployments with Skaffold on Docker for Mac" <Liked it!

    ReplyDelete
  2. Hi there, just became alert to your blog through Google, and found that it's truly informative.
    I am going to watch out for brussels. I'll be grateful if you continue
    this in future. A lot of people will be benefited from your writing.
    Cheers!

    ReplyDelete
  3. whoah this blog is wonderful i love reading your articles.
    Keep up the good work! You understand, many persons are
    looking around for this information, you can help them greatly.

    ReplyDelete
  4. Heya i'm for the first time here. I found this board and I find It truly useful & it helped me out a lot.
    I hope to give something back and help others like
    you aided me.

    ReplyDelete