Swift + Docker + Kubernetes = Awesome Deployments from macOS

Shashikant Jagtap
Feb 18, 2019 · 8 min read
Swift + Docker + Kubernetes on macOS

Original Links: Read original links to get nicer code syntax and formatting!

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

  • Dockerized Server Side Swift app with docker-compose.yml
  • Basic knowledge of Docker and Kubernetes deployments

Getting Docker+Kubernetes on macOS

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 kubectland many more. We can check Kubernetes cluster information, nodes and context by running some command like

$ kubectl version
$ kubectl cluster-info
$ kubectl get nodes
$ kubectl config current-context

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

Get Dokerised and Microservised Swift App

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

$ git clone https://github.com/Shashikant86/Swift-Kubernetes
$ cd 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

$ docker stack deploy --compose-file docker-compose.yml vapor-swift-stack

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

$ $ docker stack deploy --orchestrator kubernetes --compose-file docker-compose.yml vapor-swift-stack

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

$ docker stack services vapor-swift-stack
$ kubectl get deployments
$ kubectl get services
$ kubectl get pods

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

Get Kubernetes Dashboard

$ kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
$ kubectl proxy

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.

$ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

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

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

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.

$ docker stack rm vapor-swift-stack

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

Alternatives to Docker-Desktop

  • 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.

$ brew cask install minikube

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.

$ kubectl config current-context

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

$ kubectl config use-context minikube

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

$ brew install kompose

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.

$ kompose up

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?

Conclusion

====================================

Like this post from XCBlog By XCTEQ ? You may also like some of our open source projects on Github or Follow us on Twitter and LinkedIn

XCBlog

P.S: All the posts from this publication are published before author Shashikant Jagtap joined Apple. Thank you so much for being a reader of XCBlog. Tech Blog about DevOps, CI/CD for  Platforms. Xcode Server, XCTest, SwiftPM, Server Side Swift, XCUITest etc.

Shashikant Jagtap

Written by

All the posts published on this channel before I joined Apple. Thanks for being a reader of XCBlog. Web: shashikantjatap.net, xcteq.co.uk

XCBlog

XCBlog

P.S: All the posts from this publication are published before author Shashikant Jagtap joined Apple. Thank you so much for being a reader of XCBlog. Tech Blog about DevOps, CI/CD for  Platforms. Xcode Server, XCTest, SwiftPM, Server Side Swift, XCUITest etc.

More From Medium

Also tagged Kubernetes

Also tagged Kubernetes

What Does Kubernetes Do, Anyway?

80

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade