From Docker-Swarm to Kubernetes – in the Easy Way!

In this blog I would like to give you a short introduction and installation guide for kubernetes.

I worked for years with Docker, Docker-Compose and Docker Swarm. I tried to switch to this ‘common standard’ kubernetes. But to be honest, I’ve always failed in the complexity of kubernetes and given up in frustration. I ask my self – why is kubernetes so complex? The short answer: it is not.

Kubernetes is in compare to Docker Swarm (the build-in scheduler of Docker) much more powerful and has a lot of additional features. But the core concept is very similar. The real problem I think is, that many people believe if they follow the installation guideline from an operation teams from Netflix or Google, this should fit there own problems in any way. But if you’re honest with yourself, you’re not dealing with tens of thousands of servers like these companies. So you don’t have to solve similar problems. If you want to run your application on a small cluster of 2 or 3 servers, than read on and learn what I found out.

Installation

In the following I will show how to install a Kubernetes cluster of 3 nodes running Linux Debian:

  • Kubernetes Master
  • Worker1
  • Worker2

I assume that you have 3 servers running in the internet. In my opinion, it makes little sense to install minikube or install a kubernetes cluster into a virtualbox running on your workstation. If you want to run your docker images during development you can use docker-compose setup a small infrastructure. The effort to install Kubernetes makes only sense if you want to run you applications productive environment.

So you need access to your servers and of course the IP addresses for the next step. In order not to annoy you with endless command line calls, you can use the following bash script for installation (or you can enter line by line):

#!/bin/sh
# Run as root
echo "#############################################"
echo " adding repositories…"
echo "#############################################"
apt-get update
apt-get install -y apt-transport-https ca-certificates ne curl gnupg2 software-properties-common

# Add docker repositry
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
add-apt-repository \
           "deb [arch=amd64] https://download.docker.com/linux/debian \
            $(lsb_release -cs) \
            stable"
# Add kubernetes repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

echo "#############################################"
echo " installing docker and kubernetes…."
echo "#############################################"
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io kubelet kubeadm kubectl

Note: To run docker and kubernetes you need a linux kernel. So it is independent which linux distribution you are using. Take the one you are most familiar with. The only thing you need is a package manager to install the components (in my case it is apt-get).

Setup the Cluster

Now you can initialize the Kubernetes cluster using the following kubeadm command:

$ kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=[NODE_IP_ADDRESS]

Replace [NODE_IP_ADDRESS] with your servers public IP address.

You will see a detailed protocol showing what happens behind the scene. If something went wrong you can easily roll back everything with the command:

$ kubeadm reset

The last output form the protocol shows you the join token we need in the next step. If you forgot to note the join token run:

$ kubeadm token create --print-join-command

Setup kubectl

To make kubectl work for your non-root user, run these commands, which are also part of the kubeadm init output:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Setup a flannel network

Next, deploy the flannel network to the kubernetes cluster using the kubectl command.

$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

The flannel network will been deployed to the Kubernetes cluster. After some seconds the cluster should be up and running. You can check the status with:

$ kubectl cluster-info

and list all nodes with:

$ kubectl get nodes

Install Worker Nodes

Now you can run the same script used to install the master node on each of your worker nodes

TBD….

Controlling Your Cluster From your Workstation

In different to docker-swarm, a kubernetes cluster can be administrated remote from your workstation. The tool ‘kubectl’ is the kubernetes command line tool used to manage your cluster via the kubernetes api server running on your manager node.

Setup kubectl

To run kubectl from your workstation you need first to install it. You will find the official install guide here. Note: you install only the kubectl tool, not a full kubernetes server as in the section before.

In order to get kubectl to talk to your cluster from your workstation, you need to copy the content from the administrator kubeconfig file (/etc/kubernetes/admin.conf) from your control-plane node to your workstation into the file

$HOME/.kube/config 

Note: The admin.conf file gives the user superuser privileges over the cluster. This file should be used sparingly. For normal users, it’s recommended to generate an unique credential to which you whitelist privileges. Kubernetes supports different authentication strategies, defined here.

The Kubernetes Dashboard

Kubernetes also comes with a web dashboard running locally on your machine! The dashboard allows you to list all the Pods, ReplicationControllers, Services, and other objects deployed in your cluster, as well as to create, modify, and delete them.

Run the following command from your workstation:

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta6/aio/deploy/recommended.yaml

By default, the dashboard will install with minimum user role privileges. To access the dashboard with full administrative permission, create a YAML file named dashboard-admin.yaml.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard

Apply the full admin privileges to dashboard service account using the dashboard-admin YAML file.

$ kubectl create -f dashboard-admin.yaml

now you can start the dashboard:

$ kubectl proxy

Kubectl will make the Dashboard available at http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

You will be asked for a token.

You can lookup the token from your workstation using the command:

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

Copy the only the token part to be used for a login.

To install the Dashboard see also here.

Deploy a Test Container

To test you new cluster run:

$ kubectl run whoami --image=emilevauge/whoami --port=80  --generator=run-pod/v1

This will create a simple service showing where it runs. With the next command you can check if your test container is up and running.

$ kubectl get pods

You can see if the pod was created.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.