Swift + Docker + Kubernetes = Awesome Deployments from macOS

Published by Shashikant Jagtap on

At the moment, Kubernetes is the hottest tool in the DevOps industry for automating, managing and scaling deployments of the web apps. However, Kubernetes rarely managed to enter in the Swift and Apple’s world. There are server-side Swift frameworks like Vapor, Kitura, Perfect etc which are doing awesome work to get Swift on the server. There are also ongoing efforts on getting the mature DevOps toolings like Docker or Terraform to provision the deployments so that companies can deploy apps on any cloud platform. The popular cloud platforms are AWS, Google Cloud, Azure, Heroku and many more. In this post, we will see how easy to create and deploy Swift web apps on the local Kubernetes cluster on macOS.

Requirements

  • macOS Mojave with Docker Desktop App (Edge Version) installed
  • Dockerized Server Side Swift app with docker-compose.yml
  • Basic knowledge of Docker and Kubernetes deployments

Getting Docker+Kubernetes on macOS

The process of getting Docker and Kubernetes on macOS become such a painless with the launch of Docker-Desktop . You just need to get a Docker-Desktop app and enable Kubernetes on it. That’s it! There are other solutions to deploy Kubernetes locally on macOS like Minikube if you are familiar with the command line but Docker-Desktop work well and easy to configure with UI. You need to get the latest Docker-Desktop app which has Kubernetes embedded. At the moment, I got this version with all the services

Enabling Kubernetes service from the Docker-Desktop is fairly easy. Just go to the Preferences->Select Kubernetes tab and enable the following for the local deployments.

 

After a few minutes, you will see that both Docker and Kubernetes service is running. Under the hood, Docker-Desktop has installed all the components of the Kubernetes and CLI tools like kubectl  and many more. We can check Kubernetes cluster information, nodes and context by running some command like

That’s it for the getting Kubernetes and Docker on macOS. Pretty Simple!

Get Dokerised and Microservised Swift App

The process of Dockerising and Microservising Swift apps is a bit hairy. We need to understand the web microservices architecture and apply it to the evolving Server Side Swift projects. However, there are some projects available on Github which has fully dockerised also some of the Server Side Swift providers gives you Dockerfiles for free which you can extend it to use micro-service based architecture with Docker-Compose. Anyway, this is a huge topic and needs a separate discussion. At this point, we need server side Swift app with a docker-compose.yml  file where all the services are defined for containerised deployments.

There is a great article to get the Dockerised Vapor app here, we will use the same concept but deploy it on local  Kubernetes cluster. I have created a Github repo with all the required source code Swift-Kubernetes. There is an awesome Kitura repository with to learn about Kitura and Kubernetes here. Similarly, you can use any Server side app with docker support for the deployment but for this demo, we will use Swift-Kubernetes

Now that, we have Dockerised and Microservised sample app. You can run docker-compose up to bring up the entire stack. After running this command, you will see three different containers running different services.

Now that, we have our stack running locally. The next step is to deploy it on the local Kubernetes cluster.

Deploy it on Local Kubernetes Cluster

Before Docker-Desktop, deploying to Kubeenetes requires heavy use of command line tools e.g we have to use Minikube or other docker based deployment options mentioned here. However, with docker-for-desktop, it’s a matter of running a single command. The only requirement is to have a dockerised app with the docker-compose.yml  file. Let’s deploy our stack to the local Kubernetes cluster. Assuming, you have cloned the Swift-Kubernetes project, we can run this command from the root of the project.

P.S: While running deploy command if you get something like Docker Swarm asking you to run docker swarm init don’t do that unless you want to deploy it on the Docker Swarm. As a Docker’s product, they prefer Docker Swarm as orchestrator but this tutorial is for Kubernetes. So you can run this command instead

With this command, we have deployed an entire stack on the local Kubernetes cluster. This includes services, nodes, pods and deployments. We can verify all the information about our deployment using the following commands

Congratulation! You have successfully deployed the Swift web app on the Kubernetes.

Get Kubernetes Dashboard

Now that, we have seen all the deployment from the command line. It would be great to see that visually with the dashboard. Kubernetes provides the web UI dashboard to display the information visually in the form web page. You can follow the instructions from this page but in a summary, we can get that using following command and creating admin user token.

You will get the dashboard served locally on the port 8001. However, you can not access it yet as we need to user account token. We also need to create a service account with details in the file called dashboard-adminuser.yaml and run the following command to get the token.

Yes. That’s right! I put the blue block to hide part of the token. I am sure nothing would be sensitive there but just in case, for safe side and not to add another problem in life, I have covered it. Once you get the token you can paste the token and access the Kubernetes Dashboard at the localhost URL

With the Kubernetes Dashboard, we can access loads of information about our deployments. You can read more about how to read Dashboard here.

You can bring the whole Kubernetes cluster down with the simple command.

This will tear down the entire stack, services, pods, deployments.

Alternatives to Docker-Desktop

If you don’t want to use Docker-Desktop, there are various other options which are not that difficult if you are command line person. Let’s discuss the couple of them.

  • Minikube

There is another command line approach to manage the Kubernetes cluster locally which is Minikube. You can install MiniKube locally using the Homebrew cask.

There is already great documentation on the Kubernetes site, how to use Minikube so there is no point t repeat it here. However, one important thing, you need to keep in the mind is context. While switching back to Minikube from docker-for-desktop. You have to make sure, context is right. You can verify that using the following command.

This command will print the current context. If it’s not set to the Minikube then, set it explicitly using the following command

Now you are set to use Minikube.

  • Kompose

The Kompose is another easy tool to get the Kubernetes Cluster up and running. You can install Kompose with homebrew

With Kompose, we can build the whole stack with a single command and get this up and running if our app has docker-compose.yml file.

Similarly, we can take the whole stack down using the kompose down command. Another great feature of Kompose is to convert existing docker compose files to the Kubernetes deployments and services files. Its one of the easy way to get going with scalable Kubernetes deployments.

Once we have all these files, we can easily create deployments, services using kubectl create command.

What Next?

At this stage, we have deployed the Swift app on the Kubernetes local cluster. There are many great things that you can do with Kubernetes e.g automating CI/CD pipelines, scaling deployments etc. The next step would deploy the cluster on the cloud-based services like AWS, Azure, Google Cloud + many more. All of them has Kubernetes support, you can’t miss it. Deploying the Swift web app with Kubernetes cluster to cloud and making it available to the world would be an awesome thing.

Conclusion

Kubernetes support in the server side Swift community is not that great at the moment, apart from IBM Kitura Kubernetes project on Github. However, there is a great opportunity for many server-side Swift frameworks like Vapor, Perfect to support Kubernetes. They are already supporting the containerised deployments of the Swift apps, we can use Kubenetes to make the Swift deployment more scalable, manageable and extendable. Let me know what you think? if you like this article then feel free to share it in the DevOps and Swift community.