使用 K3s 在树莓派上运行 Kubernetes 集群
Kubernetes k3s 树莓派

> 
> 跟随接下来的介绍，自己搭建一个三节点的 Kubernetes 集群。
> 
> 
> 


![](/data/attachment/album/202003/29/121559p44e3dfrerf3zver.jpg)


我对在树莓派上搭建 [Kubernetes](https://opensource.com/resources/what-is-kubernetes) 集群已经感兴趣很长时间了，只要照着网上的教程，我可以在由三个树莓派组成的集群中搭建一套 Kubernetes 并正常运行。但在这种情况下，主节点上的内存和 CPU 资源捉襟见肘，执行 Kubernetes 任务的时候往往性能不佳，想要升级 Kubernetes 就更不可能了。


这个时候，我非常激动地发现了 [K3s](https://k3s.io/) 这个项目。K3s 被誉为在可用于资源受限环境下的轻量级 Kubernetes，它还针对 ARM 处理器做出了优化，这让 Kubernetes 集群可以在树莓派上运行得更好。在下文中，我们将会使用 K3s 来创建一个 Kubernetes 集群。






### 准备


要按照本文介绍的方式创建 Kubernetes 集群，首先需要准备：


* 至少一个树莓派（包括 SD 卡和电源）
* 网线
* 将所有树莓派连接到一起的交换机或路由器


我们会通过在线安装的方式安装 K3s，因此还需要可以连接到互联网。


### 集群概览


在这个集群里，我们会使用三个树莓派。其中一个树莓派作为主节点，我们将它命名为 `kmaster`，并为其分配一个静态 IP `192.168.0.50`（注：假设使用的私有网段是 192.168.0.0/24），而另外两个树莓派作为工作节点，分别命名为 `knode1` 和 `knode2`，也分别分配 `192.168.0.51` 和 `192.168.0.52` 两个 IP 地址。


当然，如果你实际的网络布局和上面不同，只要将文中所提及到的 IP 替换成你实际可用的 IP 就可以了。


为了不需要通过 IP 来引用某一个节点，我们将每个节点的主机名记录到 PC 的 `/etc/hosts` 文件当中：



```
echo -e "192.168.0.50\tkmaster" | sudo tee -a /etc/hosts
echo -e "192.168.0.51\tknode1" | sudo tee -a /etc/hosts
echo -e "192.168.0.52\tknode2" | sudo tee -a /etc/hosts
```

### 部署主节点


我们首先部署主节点。最开始的步骤当然是使用镜像安装最新的 Raspbian，这个步骤可以参考我的[另一篇文章](https://carpie.net/articles/headless-pi-with-static-ip-wired-edition)，在这里就不展开介绍了。在安装完成之后，启动 SSH 服务，将主机名设置为 `kmaster`，然后分配静态 IP `192.168.0.50`。


在主节点上安装 Raspbian 完成后，启动树莓派并通过 `ssh` 连接上去：



```
ssh pi@kmaster
```

在主节点上执行以下命令安装 K3s：



```
curl -sfL https://get.k3s.io | sh -
```

等到命令跑完以后，一个单节点集群就已经运行起来了。让我们检查一下，还在这个树莓派上执行：



```
sudo kubectl get nodes
```

就会看到这样的输出：



```
NAME     STATUS   ROLES    AGE    VERSION
kmaster  Ready    master   2m13s  v1.14.3-k3s.1
```

### 获取<ruby> 连接令牌 <rt>  join token </rt></ruby>


之后我们需要部署工作节点。在工作节点上安装 K3s 的时候，会需要用到连接令牌，它放置在主节点的文件系统上。首先把连接令牌保存出来以便后续使用：



```
sudo cat /var/lib/rancher/k3s/server/node-token
```

### 部署工作节点


通过 SD 卡在每个作为工作节点的树莓派上安装 Raspbian。在这里，我们把其中一个树莓派的主机名设置为 `knode1`，为其分配 IP 地址 `192.168.0.51`，另一个树莓派的主机名设置为 `knode2`，分配 IP 地址 `192.168.0.52`。接下来就可以安装 K3s 了。


启动主机名为 `knode1` 的树莓派，通过 `ssh` 连接上去：



```
ssh pi@knode1
```

在这个树莓派上，安装 K3s 的过程和之前差不多，但需要另外加上一些参数，表示它是一个工作节点，需要连接到一个已有的集群上：



```
curl -sfL http://get.k3s.io | K3S_URL=https://192.168.0.50:6443 \
K3S_TOKEN=刚才保存下来的连接令牌 sh -
```

`K3S_TOKEN` 的值需要替换成刚才保存下来的实际的连接令牌。完成之后，在主机名为 `knode2` 的树莓派上重复这个安装过程。


### 通过 PC 访问集群


现在如果我们想要查看或者更改集群，都必须 `ssh` 到集群的主节点才能使用 `kubectl`，这是比较麻烦的。因此我们会将 `kubectl` 放到 PC 上使用。首先，在主节点上获取一些必要的配置信息，`ssh` 到 `kmaster` 上执行：



```
sudo cat /etc/rancher/k3s/k3s.yaml
```

复制上面命令的输出，然后在你的 PC 上创建一个目录用来放置配置文件：



```
mkdir ~/.kube
```

将复制好的内容写入到 `~/.kube/config` 文件中，然后编辑该文件，将



```
server: https://localhost:6443
```

改为



```
server: https://kmaster:6443
```

出于安全考虑，只对自己保留这个配置文件的读写权限：



```
chmod 600 ~/.kube/config
```

如果 PC 上还没有安装 `kubectl` 的话，就可以开始安装了。Kubernetes 官方网站上有各种平台安装 `kubectl` 的[方法说明](https://kubernetes.io/docs/tasks/tools/install-kubectl/)，我使用的是 Ubuntu 的衍生版 Linux Mint，所以我的安装方法是这样的：



```
sudo apt update && sudo apt install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt update && sudo apt install kubectl
```

上面几个命令的作用是添加了一个包含 Kubernetes 的 Debian 软件库，获取 GPG 密钥以确保安全，然后更新软件包列表并安装 `kubectl`。如果 `kubectl` 有更新，我们将会通过<ruby> 标准软件更新机制 <rt>  standard software update mechanism </rt></ruby>收到通知。


现在在 PC 上就可以查看 Kubernetes 集群了：



```
kubectl get nodes
```

输出大概会是这样：



```
NAME     STATUS  ROLES   AGE   VERSION
kmaster  Ready   master  12m   v1.14.3-k3s.1
knode1   Ready   worker  103s  v1.14.3-k3s.1
knode1   Ready   worker  103s  v1.14.3-k3s.1
```

至此，我们已经搭建了一个三节点的 Kubernetes 集群。


### K3s 的彩蛋


如果执行 `kubectl get pods --all-namespaces`，就会看到其它服务的一些 Pod，比如 [Traefik](https://traefik.io/)。Traefik 在这里起到是反向代理和负载均衡器的作用，它可以让流量从单个入口进入集群后引导到集群中的各个服务。Kubernetes 支持这种机制，但 Kubernetes 本身不提供这个功能，因此 Traefik 是一个不错的选择，K3s 安装后立即可用的优点也得益于此。


在后续的文章中，我们会继续探讨 Traefik 在 Kubernetes ingress 中的应用，以及在集群中部署其它组件。敬请关注。




---


via: <https://opensource.com/article/20/3/kubernetes-raspberry-pi-k3s>


作者：[Lee Carpenter](https://opensource.com/users/carpie) 选题：[lujun9972](https://github.com/lujun9972) 译者：[HankChow](https://github.com/HankChow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
For a long time, I've been interested in building a [Kubernetes](https://opensource.com/resources/what-is-kubernetes) cluster out of a stack of inexpensive Raspberry Pis. Following along with various tutorials on the web, I was able to get Kubernetes installed and working in a three Pi cluster. However, the RAM and CPU requirements on the master [node](https://www.kubernetesbyexample.com/en/concept/nodes) overwhelmed my Pi. This caused poor performance when doing various Kubernetes tasks. It also made an in-place upgrade of Kubernetes impossible.

As a result, I was very excited to see the [k3s project](https://k3s.io/). K3s is billed as a lightweight Kubernetes for use in resource-constrained environments. It is also optimized for ARM processors. This makes running a Raspberry Pi-based Kubernetes cluster much more feasible. In fact, we are going to create one in this article.

## Materials needed

To create the Kubernetes cluster described in this article, we are going to need:

- At least one Raspberry Pi (with SD card and power adapter)
- Ethernet cables
- A switch or router to connect all our Pis together

We will be installing k3s from the internet, so they will need to be able to access the internet through the router.

## An overview of our cluster


For this cluster, we are going to use three Raspberry Pis. The first we'll name **kmaster** and assign a static IP of 192.168.0.50 (since our local network is 192.168.0.0/24). The first worker node (the second Pi), we'll name **knode1** and assign an IP of 192.168.0.51. The final worker node we'll name **knode2** and assign an IP of 192.168.0.52.

Obviously, if you have a different network layout, you may use any network/IPs you have available. Just substitute your own values anywhere IPs are used in this article.

So that we don't have to keep referring to each node by IP, let's add their host names to our **/etc/hosts** file on our PC.

```
echo -e "192.168.0.50\tkmaster" | sudo tee -a /etc/hosts
echo -e "192.168.0.51\tknode1" | sudo tee -a /etc/hosts
echo -e "192.168.0.52\tknode2" | sudo tee -a /etc/hosts
```


## Installing the master node

Now we're ready to install the master node. The first step is to install the latest Raspbian image. I am not going to explain that here, but I have a [detailed article](https://carpie.net/articles/headless-pi-with-static-ip-wired-edition) on how to do this if you need it. So please go install Raspbian, enable the SSH server, set the hostname to **kmaster**, and assign a static IP of 192.168.0.50.

Now that Raspbian is installed on the master node, let's boot our master Pi and **ssh** into it:

`ssh pi@kmaster`


Now we're ready to install **k3s**. On the master Pi, run:

`curl -sfL https://get.k3s.io | sh -`


When the command finishes, we already have a single node cluster set up and running! Let's check it out. Still on the Pi, run:

`sudo kubectl get nodes`


You should see something similar to:

```
NAME STATUS ROLES AGE VERSION
kmaster Ready master 2m13s v1.14.3-k3s.1
```


## Extracting the join token

We want to add a couple of worker nodes. When installing **k3s** on those nodes we will need a join token. The join token exists on the master node's filesystem. Let's copy that and save it somewhere we can get to it later:

`sudo cat /var/lib/rancher/k3s/server/node-token`


## Installing the worker nodes

Grab some SD cards for the two worker nodes and install Raspbian on each. For one, set the hostname to **knode1** and assign an IP of 192.168.0.51. For the other, set the hostname to **knode2** and assign an IP of 192.168.0.52. Now, let's install **k3s**.

Boot your first worker node and **ssh** into it:

`ssh pi@knode1`


On the Pi, we'll install **k3s** as before, but we will give the installer extra parameters to let it know that we are installing a worker node and that we'd like to join the existing cluster:

```
curl -sfL http://get.k3s.io | K3S_URL=https://192.168.0.50:6443 \
K3S_TOKEN=join_token_we_copied_earlier sh -
```


Replace **join_token_we_copied_earlier** with the token from the "Extracting the join token" section. Repeat these steps for **knode2**.

## Access the cluster from our PC

It'd be annoying to have to **ssh** to the master node to run **kubectl** anytime we wanted to inspect or modify our cluster. So, we want to put **kubectl** on our PC. But first, let's get the configuration information we need from our master node. **Ssh** into **kmaster** and run:

`sudo cat /etc/rancher/k3s/k3s.yaml`


Copy this configuration information and return to your PC. Make a directory for the config:

`mkdir ~/.kube`


Save the copied configuration as **~/.kube/config**. Now edit the file and change the line:

`server: https://localhost:6443`


to be:

`server: https://kmaster:6443`


For security purpose, limit the file's read/write permissions to just yourself:

`chmod 600 ~/.kube/config`


Now let's install **kubectl** on our PC (if you don't already have it). The Kubernetes site has [instructions](https://kubernetes.io/docs/tasks/tools/install-kubectl/) for doing this for various platforms. Since I'm running Linux Mint, an Ubuntu derivative, I'll show the Ubuntu instructions here:

```
sudo apt update && sudo apt install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | \
sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt update && sudo apt install kubectl
```


If you're not familiar, the above commands add a Debian repository for Kubernetes, grab its GPG key for security, and then update the list of packages and install **kubectl**. Now, we'll get notifications of any updates for **kubectl** through the standard software update mechanism.

Now we can check out our cluster from our PC! Run:

`kubectl get nodes`


You should see something like:

```
NAME STATUS ROLES AGE VERSION
kmaster Ready master 12m v1.14.3-k3s.1
knode1 Ready worker 103s v1.14.3-k3s.1
knode1 Ready worker 103s v1.14.3-k3s.1
```


Congratulations! You have a working 3-node Kubernetes cluster!

## The k3s bonus

If you run **kubectl get pods --all-namespaces**, you will see some extra pods for [Traefik](https://traefik.io/). Traefik is a reverse proxy and load balancer that we can use to direct traffic into our cluster from a single entry point. Kubernetes allows for this but doesn't provide such a service directly. Having Traefik installed by default is a nice touch by Rancher Labs. This makes a default **k3s** install fully complete and immediately usable!

We're going to explore using Traefik through Kubernetes **ingress** rules and deploy all kinds of goodies to our cluster in future articles. Stay tuned!

## 10 Comments