<commit_before>---
layout: post
title: Install and configure Kubernetes on Arch Linux
tags: kubernetes k8s containers cni cri docker orchestration arch linux
---
In this post we will see how to install and configure a
[Kubernetes](https://kubernetes.io) cluster on [Arch
Linux](https://www.archlinux.org) nodes.

You can find plenty of documentation at the [official Kubernetes
site](https://kubernetes.io/docs/home/), but at the same time it can
feel a bit overwhelming, especially if you are a newcomer, so in this
post I'll try to summarize the steps needed to get your cluster and up
running on Arch Linux.

The lab setup we are going to create as part of this post will be a
highly available Kubernetes cluster. We will have three nodes for the
control-plane, which will also be un-tainted, so we can schedule pods
on them. Each node in the cluster will be running with 2 CPUs and 4 GB
of memory. If needed, we can expand the cluster afterwards by addig
more worker nodes to it.

For more details about Highly Available Kubernetes cluster, please
make sure to check this page
[here](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/).

## Requirements

First, make sure that you have Arch Linux installed. If you haven't
done that before head first to the Arch Linux [installation
guide](https://wiki.archlinux.org/index.php/Installation_guide).

Each node in the Kubernetes cluster will have 2 CPUs and 4 GB of
memory and for bootstrapping the cluster we will use the `kubeadm`
tool. First, make sure your Arch Linux nodes have the basic set of
configuration applied, e.g. [configure
hostnames](https://wiki.archlinux.org/index.php/Network_configuration#Set_the_hostname),
set [static IP
addresseses](https://wiki.archlinux.org/index.php/Network_configuration#Static_IP_address),
configure DNS servers, NTP servers, etc.

We will need `devtools` and `base-devel` packages in order to build the
`kubelet`, `kubeadm` and `kubectl` command-line tools. If you are using
a tool to install packages from [AUR](https://wiki.archlinux.org/index.php/Arch_User_Repository)
you can use that instead to install the `kubelet`, `kubeadm` and `kubectl` tools.
In this post we will build these packages and use `pacman(8)` to install them,
in order to keep things simple and straight-forward.

``` shell
sudo pacman -S devtools base-devel
```

While bootstrapping the Kubernetes cluster one of the preflight checks that
Kubernetes performs is to see whether you have an active swap. If it finds an
active swap partition it will fail and stop there. In order to pass the
preflight checks you need to disable swap on each node in the Kubernetes cluster.

``` shell
sudo swapoff -a
```

Make sure to also update `/etc/fstab`, so that swap is not mounted during boot.

If you really have no choice and must use swap, though not recommended,
you can add the following flag to your `/etc/default/kubernetes` file.

``` text
--fail-swap-on=false
```

## Container runtime

We will install and configure [Docker](https://www.docker.com) as the
[Container Runtime for
Kubernetes](https://kubernetes.io/docs/setup/production-environment/container-runtimes/).
If you have special requirements for your Docker images and containers, e.g.
you want them to reside on an LVM volume now is the time to create that volume
and have it mounted under `/var/lib/docker`.

Install Docker:

``` shell
sudo pacman -S docker
```

If you want any users to be able to interface with Docker you can
add them to the `docker` group as well, e.g.

``` shell
sudo usermod -a -G docker <username>
```

Configure Docker to use `overlay2` storage driver and `systemd` as the
`cgroup driver`. Edit `/etc/docker/daemon.json` file.

``` json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
```

Create a [systemd
drop-in](https://wiki.archlinux.org/index.php/systemd#Drop-in_files)
unit and set `TasksMax=infinity` option.

``` shell
sudo systemctl edit docker
```

Add the following content.

``` ini
[Service]
TasksMax=infinity
```

Enable and start the Docker service.

``` shell
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl start docker
```

Verify that Docker has been installed and working correctly.

``` shell
sudo docker info
```

## Installing Kubernetes packages

Now we can install the `kubectl`, `kubeadm` and `kubelet` packages.
We will use the following script to fetch the packages from AUR
and install them.

``` shell
#!/usr/bin/env sh

OLD_PWD="${OLDPWD}"
PACKAGE_DIR="${HOME}/kube-packages/$( date +%Y-%m-%d )"

mkdir -p "${PACKAGE_DIR}"

for p in kubectl-bin kubelet-bin kubeadm-bin cni-plugins-bin; do
    cd "${PACKAGE_DIR}"

    echo "> Fetching ${p} ..."
    curl -O https://aur.archlinux.org/cgit/aur.git/snapshot/${p}.tar.gz
    tar zxvf ${p}.tar.gz

    cd "${PACKAGE_DIR}/${p}"
    echo "> Building ${p} ..."
    makepkg

    echo "> Installing ${p} ..."
    sudo pacman --noconfirm -U *.tar.xz
done

cd "${OLD_PWD}"
echo "> Done"
```

Save above script somewhere (e.g. `~/install-kube-packages.sh`) and execute it.

``` shell
chmod +x install-kube-packages.sh
./install-kube-packages.sh
```

Verify that `kubectl`, `kubelet` and `kubeadm` have been installed
properly.  It is important to check that `kubectl`, `kubelet` and
`kubeadm` are all at the same version.

``` shell
$ kubelet --version
Kubernetes v1.17.4<commit_msg>Package format on Arch Linux has switched to zst<commit_after>---
layout: post
title: Install and configure Kubernetes on Arch Linux
tags: kubernetes k8s containers cni cri docker orchestration arch linux
---
In this post we will see how to install and configure a
[Kubernetes](https://kubernetes.io) cluster on [Arch
Linux](https://www.archlinux.org) nodes.

You can find plenty of documentation at the [official Kubernetes
site](https://kubernetes.io/docs/home/), but at the same time it can
feel a bit overwhelming, especially if you are a newcomer, so in this
post I'll try to summarize the steps needed to get your cluster and up
running on Arch Linux.

The lab setup we are going to create as part of this post will be a
highly available Kubernetes cluster. We will have three nodes for the
control-plane, which will also be un-tainted, so we can schedule pods
on them. Each node in the cluster will be running with 2 CPUs and 4 GB
of memory. If needed, we can expand the cluster afterwards by addig
more worker nodes to it.

For more details about Highly Available Kubernetes cluster, please
make sure to check this page
[here](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/).

## Requirements

First, make sure that you have Arch Linux installed. If you haven't
done that before head first to the Arch Linux [installation
guide](https://wiki.archlinux.org/index.php/Installation_guide).

Each node in the Kubernetes cluster will have 2 CPUs and 4 GB of
memory and for bootstrapping the cluster we will use the `kubeadm`
tool. First, make sure your Arch Linux nodes have the basic set of
configuration applied, e.g. [configure
hostnames](https://wiki.archlinux.org/index.php/Network_configuration#Set_the_hostname),
set [static IP
addresseses](https://wiki.archlinux.org/index.php/Network_configuration#Static_IP_address),
configure DNS servers, NTP servers, etc.

We will need `devtools` and `base-devel` packages in order to build the
`kubelet`, `kubeadm` and `kubectl` command-line tools. If you are using
a tool to install packages from [AUR](https://wiki.archlinux.org/index.php/Arch_User_Repository)
you can use that instead to install the `kubelet`, `kubeadm` and `kubectl` tools.
In this post we will build these packages and use `pacman(8)` to install them,
in order to keep things simple and straight-forward.

``` shell
sudo pacman -S devtools base-devel
```

While bootstrapping the Kubernetes cluster one of the preflight checks that
Kubernetes performs is to see whether you have an active swap. If it finds an
active swap partition it will fail and stop there. In order to pass the
preflight checks you need to disable swap on each node in the Kubernetes cluster.

``` shell
sudo swapoff -a
```

Make sure to also update `/etc/fstab`, so that swap is not mounted during boot.

If you really have no choice and must use swap, though not recommended,
you can add the following flag to your `/etc/default/kubernetes` file.

``` text
--fail-swap-on=false
```

## Container runtime

We will install and configure [Docker](https://www.docker.com) as the
[Container Runtime for
Kubernetes](https://kubernetes.io/docs/setup/production-environment/container-runtimes/).
If you have special requirements for your Docker images and containers, e.g.
you want them to reside on an LVM volume now is the time to create that volume
and have it mounted under `/var/lib/docker`.

Install Docker:

``` shell
sudo pacman -S docker
```

If you want any users to be able to interface with Docker you can
add them to the `docker` group as well, e.g.

``` shell
sudo usermod -a -G docker <username>
```

Configure Docker to use `overlay2` storage driver and `systemd` as the
`cgroup driver`. Edit `/etc/docker/daemon.json` file.

``` json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
```

Create a [systemd
drop-in](https://wiki.archlinux.org/index.php/systemd#Drop-in_files)
unit and set `TasksMax=infinity` option.

``` shell
sudo systemctl edit docker
```

Add the following content.

``` ini
[Service]
TasksMax=infinity
```

Enable and start the Docker service.

``` shell
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl start docker
```

Verify that Docker has been installed and working correctly.

``` shell
sudo docker info
```

## Installing Kubernetes packages

Now we can install the `kubectl`, `kubeadm` and `kubelet` packages.
We will use the following script to fetch the packages from AUR
and install them.

``` shell
#!/usr/bin/env sh

OLD_PWD="${OLDPWD}"
PACKAGE_DIR="${HOME}/kube-packages/$( date +%Y-%m-%d )"

mkdir -p "${PACKAGE_DIR}"

for p in kubectl-bin kubelet-bin kubeadm-bin cni-plugins-bin; do
    cd "${PACKAGE_DIR}"

    echo "> Fetching ${p} ..."
    curl -O https://aur.archlinux.org/cgit/aur.git/snapshot/${p}.tar.gz
    tar zxvf ${p}.tar.gz

    cd "${PACKAGE_DIR}/${p}"
    echo "> Building ${p} ..."
    makepkg

    echo "> Installing ${p} ..."
    sudo pacman --noconfirm -U *.tar.zst
done

cd "${OLD_PWD}"
echo "> Done"
```

Save above script somewhere (e.g. `~/install-kube-packages.sh`) and execute it.

``` shell
chmod +x install-kube-packages.sh
./install-kube-packages.sh
```

Verify that `kubectl`, `kubelet` and `kubeadm` have been installed
properly.  It is important to check that `kubectl`, `kubelet` and
`kubeadm` are all at the same version.

``` shell
$ kubelet --version
Kubernetes v1.17.4
{'fraction_non_alphanumeric': 0.10327272727272727, 'fraction_numerical': 0.0027272727272727275, 'mean_word_length': 4.191599811231713, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 24, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4553356', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3282, 'n_words': 1415}