docker_advanced_k8s_intro

This is an old revision of the document!


Kubernetes is the new mind breaking technology of Google and it is very close to Docker. In fact you need docker to run Kubernetes as well. So you can say that Kubernetes is based on Docker. So let's get it going. We will use 3 machines:

  • 1 Master
  • 2 Nodes (not slaves or workers)

To configure our kubernetes, we have to install the following:

  • Docker (or rkt) - Container runtime.
  • Kubelet - Kubernetes Node Agent.
  • Kubeadm - The tool used to build the cluster.
  • Kubectl - The Kubernetes Client
  • CNI - Install support for CNI network (Container network Interface / Spec or Model for Kubernetes Network)

To install the packages we have to:

  1. Configure the Repos
  2. Actuall install the packages.

So let's get going :)

The repo is configured very easy using the following script:

Repo Config

apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update

Please bare in mind, this is for Ubuntu Xenial 64. If you have different distribution please adjust….the last entry :)

vagrant@node-1:~$ sudo su -
root@node-1:~# apt-get update && apt-get install -y apt-transport-https
0% [Working]
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [850 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [7,532 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [489 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [200 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [5,728 B]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse Translation-en [2,708 B]
Get:11 http://archive.ubuntu.com/ubuntu xenial/universe Translation-en [4,354 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [144 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/multiverse Translation-en [106 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1,129 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [796 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [333 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.8 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse Translation-en [8,468 B]
Get:19 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7,280 B]
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main Translation-en [4,456 B]
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8,064 B]
Get:22 http://archive.ubuntu.com/ubuntu xenial-backports/universe Translation-en [4,328 B]
Fetched 16.3 MB in 12s (1,318 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
apt-transport-https is already the newest version (1.2.32).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@node-1:~# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
OK
root@node-1:~# cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
> deb http://apt.kubernetes.io/ kubernetes-xenial main
> EOF
root@node-1:~# apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Hit:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:5 http://security.ubuntu.com/ubuntu xenial-security InRelease
Get:2 https://packages.cloud.google.com/apt kubernetes-xenial InRelease [8,993 B]
Get:6 https://packages.cloud.google.com/apt kubernetes-xenial/main amd64 Packages [35.3 kB]
Fetched 44.3 kB in 2s (20.6 kB/s)
Reading package lists... Done
root@node-1:~#

Please run this command on all 3 machines.

So let's install the packages now:

Install packages

root@k8s-master:~# apt-get install docker.io kubeadm kubectl kubelet kubernetes-cni
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  bridge-utils cgroupfs-mount conntrack containerd cri-tools ebtables pigz runc socat ubuntu-fan
Suggested packages:
  mountall aufs-tools debootstrap docker-doc rinse zfs-fuse | zfsutils
The following NEW packages will be installed:
  bridge-utils cgroupfs-mount conntrack containerd cri-tools docker.io ebtables kubeadm kubectl kubelet kubernetes-cni pigz runc socat ubuntu-fan
0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
Need to get 104 MB of archives.
After this operation, 532 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial/universe amd64 pigz amd64 2.3.1-2 [61.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/main amd64 bridge-utils amd64 1.5-9ubuntu1 [28.6 kB]
**************************************************************************************************************

Please execute that on all 3 machines

To initialize the cluster, we have to take two factors into consideration:

  1. Which will be the advertise IP ?
  2. Which will be the network which we will use for the pods.

The first question is pretty easy. Just use the network which is assigned to your master. In our case, we have 1 master and 2 noides. So we will assign the advertise IP of the master:

  • master - 192.168.50.10
  • node1 - 192.168.50.11
  • node2 - 192.168.50.12

The second question however, depends on the network which will be used for the pods. In our example I have used calico, because of the reasons listed below.Thus, our pod network by default is: 192.168.0.0/16.

So let's see how our commands

Initialize the cluster

root@k8s-master:~# kubeadm init --ignore-preflight-errors=NumCPU --apiserver-advertise-address=192.168.50.10 --pod-network-cidr=192.168.0.0/16
W0421 09:20:50.597038   21388 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.2
[preflight] Running pre-flight checks
        [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
**************************************************************************************************************************
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.50.10:6443 --token k7cnjt.c0vkn3i6sc9qp2it \
    --discovery-token-ca-cert-hash sha256:8c7874be67b9670c52a729b7a26bdefb4b55f5a49402624c0d262c0253732228
root@k8s-master:~#

After that, we have to perform a couple commands from the user, which will be responsible for the kubernetes and won't be root. (P.S. usage of root for applications is STRONGLY DISCOURAGED because of security stuff :) )

So just transfer it using the instructions above:

Execute as normal User

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

Once we have done that, we can check the cluster:

Check the cluster

ubuntu@k8s-master:~$ kubectl get nodes
NAME         STATUS     ROLES    AGE   VERSION
k8s-master   NotReady   master   62s   v1.18.2

Now, when you install If you have higher version than 1.16, you cannot use weave network anymore. In our example I have used calico 3.8.

  • docker_advanced_k8s_intro.1587461971.txt.gz
  • Last modified: 2020/04/21 09:39
  • by andonovj