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!
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.
Of course, once I saw this notification, I immediately installed the update. After installation, you still need to enable Kubernetes in Preferences.
After enabling Kubernetes, I tried to execute kubectl commands and it can't connect to the kubernetes server.
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
Now, it works! We don't see any timeout error anymore!
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.
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
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
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.
You can also get more info for each service by using this command.
$ kubectl describe service <service name>
To check containers, you can also do a "docker ps". Should be equivalent to "kubectl get pods".
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
After executing this, you can now access both services from the browser.
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
After a few seconds, services will start.
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.
However, "docker ps" will list all containers whether it is in Kubernetes or Swarm.
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
Then access the dashboard using your browser
View Deployments, Pods, Replica Sets and Services
View logs of a Pod
That's it! Hope you share your experience with Kubernetes in Docker! Feel free to add your comments below.