Docker for Mac and Kubernetes

From this article you will learn how to create a local Kubernetes cluster using Docker for Mac
28 February 2018   2957

Since version 17.12 CE Edge, Docker for Mac comes with Kubernetes. Now, to create a local Kubernetes cluster, there is no need to use additional tools like Minikube. As an experiment, let's try to deploy a small service on the cluster and see what happens.

At the time of writing, I have the latest version of Docker for Mac - 18.02.0.

DockerDocker

For the experiment, I wrote a simple short links generator on Go — brevity. The service is divided into 2 parts: an API for generating short links and their handler. PostgreSQL is used for data storage.

To deploy it and all the necessary dependencies, we need Helm — a package manager for Kubernetes, which significantly simplifies the process of deployment and further support of various services. To install it, we use Homebrew:

$ brew install kubernetes-helm

After installation, we need to initialize Helm:

$ helm init

If you have access to several Kubernetes clusters, then you will need to switch to local:

$ kubectl config use-context docker-for-desktop

The preparation is complete, now we are ready to install the necessary services. The first step is to install PostgreSQL:

$ helm install --name pg --namespace db --set postgresPassword=postgres,persistence.size=1Gi stable/postgresql

Helm allows you to override the default values and thereby configure the service for your own needs. The list of services that can be installed through Helm can be found here — hub.kubeapps.com

Now we need to make sure that the service is installed and working correctly:

$ kubectl get pods -n db
NAME                            READY     STATUS                       RESTARTS   AGE
pg-postgresql-bbd4bbb5c-njkzs   0/1       CreateContainerConfigError   0          

The service was not started because of an error. To learn more, just run the following command:

$ kubectl describe pods -n db pg-postgresql-bbd4bbb5c-njkzs | tail -n 1
Warning  Failed                  (x4 over 19s)  kubelet, docker-for-desktop  Error: lstat /Users/mgrachev/.docker/Volumes/pg-postgresql/pvc-588afa8b-0e90-11e8-8d22-025000000001: no such file or directory

Kubernetes does not see the directory, although it actually exists. It turned out that Docker for Mac does not understand the subPath option in the PostgreSQL configuration for Kubernetes. In Minikube there was such a problem too, but it was already fixed.

To solve this problem, we will need to edit Deployment for PostgreSQL:

$ kubectl edit deployment -n db pg-postgresql

And delete the following line:

subPath: postgresql-db

Now, if we look again at the list of running pods, we would see that there are no errors and the service is launched:

$ kubectl get pods -n db
NAME                             READY     STATUS    RESTARTS   AGE
pg-postgresql-5c954c46c6-6bt2t   1/1       Running   0          26s

Next, we need to create a database for brevity. First we go into the container:

$ kubectl exec -it -n db pg-postgresql-5c954c46c6-6bt2t bash

Inside the container, run psql on behalf of the postgres user and create the database:

root@pg-postgresql-5c954c46c6-6bt2t:/# gosu postgres psql
postgres=# CREATE DATABASE brevity_production;

In order for our application to be accessible from the cluster, we need the Ingress Controller. For these purposes, nginx would be fine:

$ helm install --name nginx stable/nginx-ingress

Go to the installation brevity. We clone the project and run the helm install command, but as an argument we pass the path to the pre-prepared chart (the packages in Helm are called the charts):

$ git clone https://github.com/mgrachev/brevity
$ cd brevity
$ helm install --name brevity ./helm

Let’s check that all services are running:

$ kubectl get pods
NAME                                                   READY     STATUS    RESTARTS   AGE
brevity-brevity-778799dd4-zlb6g                        1/1       Running   0          1h
nginx-nginx-ingress-controller-c8cf56768-h4txk         1/1       Running   0          1h
nginx-nginx-ingress-default-backend-864c9484bf-rfdq4   1/1       Running   0          1h

Next, we'll try to generate a short link by sending a POST request to the brevity API:

$ curl -X "POST" "http://localhost/api/v1/shortlink" --data-urlencode "url=https://github.com/mgrachev"
{"shortlink":"http://localhost/MyAyEy"}

We go to it to make sure that everything works. We can also look at the logs: 

$ kubectl logs brevity-brevity-778799dd4-zlb6g
time="2018-02-19T09:31:03Z" level=info msg="HTTP Server started at port: 80"
time="2018-02-19T09:32:35Z" level=info msg="Successful returns a short link, short: http://localhost/MyAyEy, original: https://github.com/mgrachev"
time="2018-02-19T09:32:43Z" level=info msg="Redirect to https://github.com/mgrachev, using token: MyAyEy"

As can be seen from the logs, the service successfully processed a short link and redirected to github.com/mgrachev. This completes our experiment.

In conclusion, I'm glad to see Docker for Mac with Kubernetes support. It's enough to make one click in the settings and at your disposal there will be a completely ready-to-work cluster with Kubernetes on board. On the other hand, I note that this is still an unstable version and there may be various kinds of errors. But even now, it is quite suitable for local development and testing.

Mikhail Grachev, senior ruby developer, Evrone

Ruby Meditation 21 in Kharkiv

Ruby Meditation 21 will take place in Kharkiv on Sat, April 14, 2018 at 11:00 AM EEST
02 April 2018   1841

At first in history, Ruby Meditation goes to Kharkiv! It will be hot. Join us in the delightful city Kharkiv, April 14!

Interesting talks, live discussion and awesome speakers. Friendly atmosphere, charming professionals and tasty lunch ;)

Planned talks

Tetiana Chupryna

Topic: "We're rewriting everything"

Tetiana Chupryna
Tetiana Chupryna

"Rewrite from scratch” is considered by many developers as an ultimate tool to fix problems with programs. However, what will happens if a team of a booming startup decide to put aside their code base and start with a fresh beginning? This talk is inspired by a true story. A story of success or failure, it’s up to you to decide.
 

Tetiana Chupryna

Ruby developer

Andrey Blazhey

Topic: "Peatio"

Andrey Blazhey
Andrey Blazhey

Create your platform for crypto trading. Features of the development and support of the platform. Connecting popular crypto currency.
 

Andrey Blazhey

Ruby/Node.js Developer

Victor Shepelev

Topic: "A tale of query languages. Is GraphQL The Chosen One?"

Victor Shepelev
Victor Shepelev

Should you switch to GraphQL today or avoid it as long as you can? How is GraphQL similar to guinea pig? Would it become even scarier than SOAP once was? What query languages prefer really huge data sources like OpenStreetMap? Answers to those and other questions, contrived examples, and shameless rants. Be prepared.
 

Victor Shepelev

Ruby developer

Maxim Goncharov

Topic: "Compromise Driven Development: The right way."

Maxim Goncharov
Maxim Goncharov

Every team often get some problems with implementation of selected solutions. In discussions, developers could find some compromise decision. But will this way be right?
 

Maxim Goncharov
Full-stack developer

Sergii Koba

Topic: "Ruby microservices with Docker"

Sergii Koba
Sergii Koba

Docker Compose infrastructure for Microservices. Multistage Docker images build for minimalistic Ruby images. Requests routing and interservice communication. ELK stack for logging microservices with ease.
 

Sergii Koba
Web Team Lead

Sponsors: Svitla, Aejis.

Share your positive experience and best practices on this Ruby Meditation #21. If you have any work issues you cannot solve, our community will gladly help you to find the best way out in live discussion at Lightning talks session. You may also try yourself as a speaker with a short 5-10 mins talk. Please, fill in the form.

Students. who interested in ruby and have a willingness to visit Ruby Meditation will get a discount 50% with promo code 'student'.

If you are a parent of a small baby (0-3) on maternity leave and you want to learn more about ruby development you can get a special discount for a ticket with promo code 'GrowWithYourKid'.

If you have any questions or suggestions, don't hesitate to contact us via cell phone: 099 202 63 08 or by email rubymeditation@gmail.com

Buy a ticket