Kubernetes-powered Docker CE (Community Edition)

When Solomon Hykes, CTO of Docker, announced in DockerCon 2017 last October that they will be supporting Kubernetes natively, I was pretty curious how that would work.

Obviously, after the announcement, I checked if the Edge version already supports it since Michael Frills of Docker already blogged about it. Unfortunately, during that time, it was not!

dockercon-2017-eu.001

After waiting for a few months, it has finally arrived!

Just saw the update this morning, looks like Kubernetes is now experimental in Docker CE(Community Edition). Based on the releae notes, it is now available in version 17.12.0-ce. I am running Edge so I'm not sure if this is available in the mainstream version as well. Will update this blog once I've tried.

Screenshot-2018-01-06-11.06.00

Of course, once I saw this notification, I immediately installed the update. After installation, you still need to enable Kubernetes in Preferences.

Screenshot-2018-01-06-11.39.26-1

After enabling Kubernetes, I tried to execute kubectl commands and it can't connect to the kubernetes server.

Screenshot-2018-01-06-16.12.53

So that didn't work straight out of the box! But, it's really my Mac's configuration. I saw in Docker's documentation that if you've used minikube before, you need to switch the context. Just execute this command.

kubectl config use-context docker-for-desktop

Screenshot-2018-01-06-16.17.32

Now, it works! We don't see any timeout error anymore!

Screenshot-2018-01-06-16.20.18

Let's get to the fun part. As promised, docker commands should work with Kubernetes in Docker. So let's create a new deployment using a docker stack compose file. I used this sample compose file from my repo.

demo-docker-kube-stack.yml

version: "3.3"
services:

  ms1:
    image: melvindave/spring-boot-example
    ports: 
      - "8080:8080"
    networks:
      - backend
    deploy:
      replicas: 3

  nginx:
    image: nginx
    ports:
      - "80:80"
    networks:
      - frontend      
    deploy:
      replicas: 1 

networks:
  backend:   

You can download the compose file from my repo
https://github.com/donvito/dockerstack/blob/master/demo-docker-kube-stack.yml

Deploy the Stack in our Kubernetes cluster

$ docker stack deploy --namespace docker-kube-demo --compose-file demo-docker-kube-stack.yml demo-docker-kube-stack

Screenshot-2018-01-06-16.33.30

Wait a few seconds, and you'll see that the services have started. Services in Kubernetes are similar to Services in Swarm. Pods are containers.

Screenshot-2018-01-06-16.45.57

You can also get more info for each service by using this command.

$ kubectl describe service <service name>

Screenshot-2018-01-06-16.50.28

To check containers, you can also do a "docker ps". Should be equivalent to "kubectl get pods".

Screenshot-2018-01-06-16.37.06

To be able to access the service from the host which in this case is my Mac, you need to expose the service using the NodePort type.

$ kubectl expose deployment ms1 --type=NodePort --name=ms1-service
$ kubectl expose deployment nginx --type=NodePort --name=nginx-service

Screenshot-2018-01-06-16.53.55

After executing this, you can now access both services from the browser.

Screenshot-2018-01-06-16.58.02

Cool! Now we have a stack running in a Kubernetes cluster within Docker!

Running another Stack using Swarm simultaneously

Once you've enabled Kubernetes in Docker CE, it will have Kubernetes as the default orchaestrator instead of Swarm. If you would like to use Swarm simultaneously with Kubernetes, you need to set the DOCKER_ORCHESTRATOR env variable to switch to Swarm.

Let's deploy the same stack in Swarm.

$ DOCKER_ORCHESTRATOR=swarm docker stack deploy --compose-file demo-docker-kube-stack.yml demo-docker-swarm-stack

Screenshot-2018-01-06-17.04.33

After a few seconds, services will start.

Screenshot-2018-01-06-17.06.24

Note that if you want to check services or stacks in Swarm, you always need to set the DOCKER_ORCHESTRATOR env variable before every command. If you don't pass this, it will assume, it orchestrator is kubernetes. Notice the difference below.

Screenshot-2018-01-06-17.09.16

However, "docker ps" will list all containers whether it is in Kubernetes or Swarm.

Screenshot-2018-01-06-17.10.19

Kubernetes Dashboard

Since we already have Kubernetes running, let's install the Kubernetes dashboard.

Just execute 2 commands

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

Screenshot-2018-01-06-18.57.47

Then access the dashboard using your browser
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/overview?namespace=default

Overview
Screenshot-2018-01-06-18.56.22

View Deployments, Pods, Replica Sets and Services
Screenshot-2018-01-06-19.08.17

View logs of a Pod
Screenshot-2018-01-06-19.06.44

That's it! Hope you share your experience with Kubernetes in Docker! Feel free to add your comments below.

Let's connect in LinkedIn, Twitter and github!