DEV Community

Martin Pham
Martin Pham

Posted on • Updated on

Having fun with Kubernetes - Chapter 4

From previous chapter, we were talking about the infrastructure which we're gonna build. In this chapter, let’s install & setup a k8s cluster, with 2 nodes!

We will start with the Master server first

For this Lab, I’m gonna use Virtual machines to simulate servers. I’m using macOS Catalina 10.15.2, VMWare Fusion Pro 11.5.1, with 3 Ubuntu 18.04 VMs (2 CPUs, 1GB RAM, bridged network):

  • kube: master (IP: 192.168.1.33)
  • kube1: node 1 (IP: 192.168.1.34)
  • kube2: node 2 (IP: 192.168.1.35)

Before everything, note that:

  • Kubernetes won’t run if swap enabled
  • Kubernetes master-nodes communication will require some ports opened
  • Kubernetes could face some problems with SELINUX

Alright, let’s start with the Master VM.

Master component

$ sudo su
Enter fullscreen mode Exit fullscreen mode

Disable swap

$ swapoff -a
Enter fullscreen mode Exit fullscreen mode

Also don’t forget to disable swap on reboot, by editing /etc/fstab file

(Optional) Set hostname

$ hostnamectl set-hostname kube
Enter fullscreen mode Exit fullscreen mode

(Optional) Set static IP

Edit file /etc/netplan/50-cloud-init.yaml to set static IP

/etc/netplan/50-cloud-init.yaml
network:
    renderer: networkd
    ethernets:
        ens33:
            dhcp4: no
            addresses: [192.168.1.33/24]
            gateway4: 192.168.1.1
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]
    version: 2
Enter fullscreen mode Exit fullscreen mode

Update apt

$ apt update
Enter fullscreen mode Exit fullscreen mode

Install Docker

$ apt install docker.io
Enter fullscreen mode Exit fullscreen mode

Auto start Docker

$ systemctl enable docker && systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Install Kubeadm

$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add

$ apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

$ apt install kubeadm
Enter fullscreen mode Exit fullscreen mode

Init Kubeadm

$ kubeadm init --pod-network-cidr=10.244.0.0/16
Enter fullscreen mode Exit fullscreen mode

After Kubeadm inited, it will give you a command with token to run it on Node servers. It looks like this:

kubeadm join 192.168.1.33:6443 --token xxx --discovery-token-ca-cert-hash xxx
Enter fullscreen mode Exit fullscreen mode

Create k8s config place

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

Create Virtual network

We’re gonna use flannel for Virtual network

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

(Optional) Create Docker registry

For this Lab, we’re gonna setup a local insecure Docker registry to store our built images.

$ docker run -d -p 5000:5000 --name registry registry:2
Enter fullscreen mode Exit fullscreen mode

For security reasons, Docker doesn’t want to connect to an insecure registry.

To allow Docker to use insecure registry, you need to:

Edit file /etc/docker/daemon.json

/etc/docker/daemon.json
{
        "insecure-registries" : ["192.168.1.33:5000"]
}
Enter fullscreen mode Exit fullscreen mode

Or file /etc/default/docker

/etc/default/docker
DOCKER_OPTS="--insecure-registry 192.168.1.33:5000"
Enter fullscreen mode Exit fullscreen mode

Restart Docker

$ service docker restart
Enter fullscreen mode Exit fullscreen mode

(Remember 192.168.1.33 is our Master server’s IP)

Install metrics server

$ cd /etc
$ git clone https://github.com/kubernetes-incubator/metrics-server.git
$ cd metrics-server/
Enter fullscreen mode Exit fullscreen mode

Edit file /etc/metrics-server/deploy/1.8+/metrics-server-deployment.yaml

      - name: metrics-server
        image: k8s.gcr.io/metrics-server-amd64:v0.3.6
        args:
          - --cert-dir=/tmp
          - --secure-port=4443
        command:
          - /metrics-server
          - --metric-resolution=5s
          - --kubelet-insecure-tls
          - --kubelet-preferred-address-types=InternalIP
Enter fullscreen mode Exit fullscreen mode

Apply it

kubectl apply -f /etc/metrics-server/deploy/1.8+/metrics-server-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Great! you have a working Master server now. Now we're gonna setup the Node servers and connect them together, just 5 minutes ahead :)


Node component

Let’s start on Kube1 (192.168.1.34)

$ sudo su
Enter fullscreen mode Exit fullscreen mode

Disable swap

$ swapoff -a
Enter fullscreen mode Exit fullscreen mode

To disable swap on reboot, edit /etc/fstab file

(Optional) Set hostname

$ hostnamectl set-hostname kube1
Enter fullscreen mode Exit fullscreen mode

(Optional) Set static IP
Edit file /etc/netplan/50-cloud-init.yaml to set static IP

/etc/netplan/50-cloud-init.yaml
network:
    renderer: networkd
    ethernets:
        ens33:
            dhcp4: no
            addresses: [192.168.1.34/24]
            gateway4: 192.168.1.1
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]
    version: 2
Enter fullscreen mode Exit fullscreen mode

Update apt

$ apt update
Enter fullscreen mode Exit fullscreen mode

Install Docker

$ apt install docker.io
Enter fullscreen mode Exit fullscreen mode

**Auto start Docker

$ systemctl enable docker && systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Install Kubeadm

$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add

$ apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

$ apt install kubeadm
Enter fullscreen mode Exit fullscreen mode

Join this node to the master
This command was generated when you setup your Master server

$ kubeadm join xxx:6443 --token xxx --discovery-token-ca-cert-hash xxx
Enter fullscreen mode Exit fullscreen mode

(Optional) Docker registry

As I told, for some security reasons, Docker doesn’t want to connect to an insecure registry.

To allow Docker to use insecure registry, you need to:

Edit file /etc/docker/daemon.json

/etc/docker/daemon.json
{
        "insecure-registries" : ["192.168.1.33:5000"]
}
Enter fullscreen mode Exit fullscreen mode

Or edit file /etc/default/docker

/etc/default/docker
DOCKER_OPTS="--insecure-registry 192.168.1.33:5000"
Enter fullscreen mode Exit fullscreen mode

Restart Docker

$ service docker restart
Enter fullscreen mode Exit fullscreen mode

Repeat the same, for Kube2 (don’t forget the hostname and static IP)

Done! Now you have a working k8s cluster with Master and 2 Nodes, in the final chapter, we will build our infrastructure on this cluter. And trust me, it's not harder than playing with Lego blocks

Alt Text

The final chapter is here

Discussion (0)