Having fun with Kubernetes - Chapter 4
Tags: kubernetes, docker, php, linux
Published: 2019-12-23 16:19:44

From [previous chapter](https://www.martinpham.com/2019/12/08/having-fun-with-kubernetes-3/), 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
```


**Disable swap**
```
$ swapoff -a
```

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

**(Optional) Set hostname**
```
$ hostnamectl set-hostname kube
```

**(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
```

**Update apt**
```
$ apt update
```

**Install Docker**
```
$ apt install docker.io
```

**Auto start Docker**
```
$ systemctl enable docker && systemctl start docker
```

**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
```

**Init Kubeadm**
```
$ kubeadm init --pod-network-cidr=10.244.0.0/16
```

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
```


**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
```


**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
```

**(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
```

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"]
}
```
Or file `/etc/default/docker`
```
/etc/default/docker
DOCKER_OPTS="--insecure-registry 192.168.1.33:5000"
```

Restart Docker
```
$ service docker restart
```

(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/
```

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
```
Apply it
```
kubectl apply -f /etc/metrics-server/deploy/1.8+/metrics-server-deployment.yaml
```
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
```

**Disable swap**
```
$ swapoff -a
```

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

**(Optional) Set hostname**
```
$ hostnamectl set-hostname kube1
```

**(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
```
**Update apt**
```
$ apt update
```

**Install Docker**
```
$ apt install docker.io
```


**Auto start Docker
```
$ systemctl enable docker && systemctl start docker
```

**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
```


**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
```

**(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"]
}
```
Or edit file `/etc/default/docker`
```
/etc/default/docker
DOCKER_OPTS="--insecure-registry 192.168.1.33:5000"
```

Restart Docker
```
$ service docker restart
```

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](https://thepracticaldev.s3.amazonaws.com/i/6687oq714zg04y0qar1m.jpg)

The final chapter is [here](https://www.martinpham.com/2019/12/14/having-fun-with-kubernetes-final/)