如何用 Kubeadm 在 Debian 11 上安装 Kubernetes 集群
Kubernetes k8s
![](/data/attachment/album/202211/30/091928zlxbvttw58x6rztw.jpg)



> 
> 你是否在寻找一份在 Debian 11（Bullseye）上安装 Kubernetes 集群的简易指南？
> 
> 
> 


这份分步指南将向你展示如何使用 Kubeadm 工具在 Debian 11 上安装 Kubernetes 集群。


Kubernetes（k8s）集群包含主控节点和工作节点，用于运行容器化的应用程序。主控节点作为控制平面，工作节点为实际工作负载提供环境。


前置条件：


* 已安装 Debian 11
* 2 CPU / vCPU
* 2 GB RAM
* 20 GB 空闲硬盘空间
* 有管理员权限的 sudo 用户
* 稳定的网络连接


实验环境配置：


在本文中，我使用了 3 个 Debian 11 系统的节点，配置如下


* 主控节点（`k8s-master`） – 192.168.1.236
* 工作节点 1（`k8s-worker1`） – 192.168.1.237
* 工作节点 2（`k8s-worker2`） – 192.168.1.238


事不宜迟，我们直接进入安装步骤。


### 1、设置主机名和更新 /etc/hosts 文件


在主控节点和工作节点上使用 `hostnamectl` 命令来设置主机名：



```
$ sudo hostnamectl set-hostname "k8s-master"       // 在主控节点运行
$ sudo hostnamectl set-hostname "k8s-worker1"      // 在工作节点 1 运行
$ sudo hostnamectl set-hostname "k8s-worker2"      // 在工作节点 2 运行

```

在所有节点的 `/etc/hosts` 文件末尾添加下面几行内容：



```
192.168.1.236       k8s-master
192.168.1.237       k8s-worker1
192.168.1.238       k8s-worker2

```

### 2、在所有节点上关闭交换分区


我推荐关闭交换分区，以便更丝滑地使用 `kubelet`。在所有节点上执行以下命令来关闭交换分区：



```
$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

```

### 3、配置 Kubernetes 集群相关的防火墙规则


如果你的操作系统防火墙是打开的，请分别在主控节点和工作节点允许以下的端口。


在主控节点，执行：



```
$ sudo ufw allow 6443/tcp
$ sudo ufw allow 2379/tcp
$ sudo ufw allow 2380/tcp
$ sudo ufw allow 10250/tcp
$ sudo ufw allow 10251/tcp
$ sudo ufw allow 10252/tcp
$ sudo ufw allow 10255/tcp
$ sudo ufw reload

```

在工作节点，执行：



```
$ sudo ufw allow 10250/tcp
$ sudo ufw allow 30000:32767/tcp
$ sudo ufw reload

```

注意：如果你的 Debian 11 系统防火墙是关闭的，可以跳过此步骤。


### 4、在所有节点安装 Containerd 运行时


Containerd 是容器运行时的行业标准，所有节点必须安装 Containerd。


先在所有节点上配置如下的核心参数，再安装 Containerd。



```
$ cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

$ sudo modprobe overlay
$ sudo modprobe br_netfilter

$ cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

```

运行如下命令，以使上面的更改生效：



```
$ sudo sysctl --system

```

现在，在所有节点上运行如下 `apt` 命令来安装 Conatinerd。



```
$ sudo apt  update
$ sudo apt -y install containerd

```

在所有节点上运行如下命令来配置 Containerd：



```
$ containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1

```

在所有节点上设置 `cgroupdriver` 为 `systemd`，编辑 `/etc/containerd/config.toml` 文件，找到 `[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]` 部分，添加一行内容：`SystemdCgroup = true`：



```
$ sudo vi /etc/containerd/config.toml

```

![systemdCgroup-true-containerd-config-toml](/data/attachment/album/202211/30/091931d0niz8ur284i9mzr.png)


保存并退出文件。


在所有节点上重启并启用 `containerd` 服务：



```
$ sudo systemctl restart containerd
$ sudo systemctl enable containerd

```

### 5、添加 Kubernetes Apt 库


执行以下命令，添加 Kubernetes Apt 库：



```
$ sudo apt install gnupg gnupg2 curl software-properties-common -y
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/cgoogle.gpg
$ sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

```

### 6、在所有节点上安装 kubelet、kubectl 和 kubeadm


在所有节点上执行以下 `apt` 命令，安装 Kubernetes 集群组件，如 `kubelet`、`kubectl` 以及 `kubeadm`。



```
$ sudo apt update
$ sudo apt install kubelet kubeadm kubectl -y
$ sudo apt-mark hold kubelet kubeadm kubectl

```

### 7、使用 Kubeadm 创建 Kubernetes 集群


现在我们可以创建 Kubernetes 集群了，在主控节点上执行以下命令：



```
$ sudo kubeadm init --control-plane-endpoint=k8s-master

```

命令输出：


![Kubernetes-Control-Plane-Initialization-Debian11](/data/attachment/album/202211/30/091931d99hajs1393vkxex.png)


出现以上内容，说明控制平面初始化成功。在输出中，有普通用户与集群交互的命令，也有把任何工作节点加入到集群的命令。


要开始与集群进行交互，请在主控节点上运行以下命令：



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

```

执行以下 `kubectl` 命令来获取节点和集群的信息：



```
$ kubectl get nodes
$ kubectl cluster-info

```

以上命令的输出：


![Nodes-Cluster-Info-Kubectl](/data/attachment/album/202211/30/091932re5fwe9q5fw5ifm9.png)


通过执行 `kubeadm join` 命令来把两个工作节点加入到集群。


注意：请从 `kubeadm init` 命令的输出中复制完整的命令。在我的例子中，命令如下：



```
$ sudo kubeadm join k8s-master:6443 --token ta622t.enl212euq7z87mgj \
   --discovery-token-ca-cert-hash sha256:2be58f54458d0e788c96b8841f811069019161f9a3dd8502a38c773e5c6ead17

```

在工作节点 1 上的输出如下：


![Worker-Node1-Join-Kunernetes-Cluster](/data/attachment/album/202211/30/091932wu38rcr87ydxduiu.png)


在工作节点 2 上的输出如下：


![Worker-Node2-Join-Kubernetes-Cluster](/data/attachment/album/202211/30/091933crf4ws9ju48myzjf.png)


在主控节点上执行以下命令，检查节点的状态：



```
$ kubectl get nodes
NAME          STATUS     ROLES           AGE     VERSION
k8s-master    NotReady   control-plane   23m     v1.25.0
k8s-worker1   NotReady   <none>          9m27s   v1.25.0
k8s-worker2   NotReady   <none>          2m19s   v1.25.0
$

```

为了使节点状态变为 `ready`，我们需要安装<ruby> 容器荚 <rt>  Pod </rt></ruby>网络插件，如 Calico 或 flannel。


### 8、安装 Calico Pod 网络插件


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



```
$ kubectl apply -f https://projectcalico.docs.tigera.io/manifests/calico.yaml

```

输出：


![Install-calico-pod-network-addon-debian11](/data/attachment/album/202211/30/091934r8kz8vkufhv0uhhv.png)


在所有节点上执行以下命令，配置防火墙允许 Calico 的端口：



```
$ sudo ufw allow 179/tcp
$ sudo ufw allow 4789/udp
$ sudo ufw allow 51820/udp
$ sudo ufw allow 51821/udp
$ sudo ufw allow 4789/udp
$ sudo ufw reload

```

执行以下命令检查下 Calico 的状态：



```
$ kubectl get pods -n kube-system

```

![Calico-Pods-Status-Kuberenetes-Debian11](/data/attachment/album/202211/30/091935kyytijujjnpi14pq.png)


完美！现在再检查下节点状态：


![Nodes-status-after-calico-Installation](/data/attachment/album/202211/30/091935m68ka1g2at62ms8s.png)


非常棒！上面的输出说明主控节点和工作节点的状态都是 `ready`。现在这个集群可以正常工作了。


### 9、检查 Kubernetes 集群安装是否正确


我们尝试通过 `deployment` 命令来部署基于 Nginx 的应用程序，来验证 Kubernetes 集群的安装是否正确。执行以下命令：



```
$ kubectl create deployment nginx-app --image=nginx --replicas 2
$ kubectl expose deployment nginx-app --name=nginx-web-svc --type NodePort --port 80 --target-port 80
$ kubectl describe svc nginx-web-svc

```

以上命令的输出：


![Nginx-Based-App-Kubernetes-Cluster-Debian11](/data/attachment/album/202211/30/091936jz9b9ndbbd77bx6p.png)


使用以下的 `curl` 命令通过节点端口 30036 来访问基于 nginx 的应用程序。


注意：在 `curl` 命令中，可以使用两个工作节点任一的主机名。



```
$ curl http://k8s-worker1:30036

```

![Access-Nginx-Based-App-via-NodePort-Kubernetes-Debian11](/data/attachment/album/202211/30/091936awvwne1fzee31e55.png)


以上的输出说明我们可以正常访问基于 nginx 的应用程序了。


以上为全部内容。希望本文对你有用，参照本文可以在 Debian 11 上正常安装 Kubernetes 集群。如有任何问题，请在下面评论区告诉我。




---


via: <https://www.linuxtechi.com/install-kubernetes-cluster-on-debian/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed) 译者：[lxbwolf](https://github.com/lxbwolf) 校对：[wxy](https://github.com/wxy)


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


OK
The step-by-step guide on this page will explain you how to install Kubernetes cluster on Debian 12/11 with Kubeadm utility.

Kubernetes has become the go-to container orchestration platform for managing containerized applications efficiently.Kubernetes (k8s) cluster contains master and worker nodes which are used to run containerized applications. Master node works as control plan and worker nodes offers environment for actual application workload.

#### Prerequisites

- Minimal Installed Debian 12 /11
- 2 CPU / vCPU
- 2 GB RAM
- 20 GB free disk space
- Sudo User with Admin rights
- Stable Internet Connectivity
- Ensure that each node can communicate with the others via a reliable network connection.

#### Lab Setup

For the demonstration, I am using three Debian 12 systems.

- Master Node (k8s-master) – 192.168.1.23
- Worker Node 1 (k8s-worker01) – 192.168.1.24
- Worker Node 2 (k8s-worker02) – 192.168.1.25

Without any further delay, let’s jump into Kubernetes Cluster installation steps.

## 1) Set Host Name and Update Hosts File

Login to each node (master & woker nodes) and set their hostname using hostnamectl command.

$ sudo hostnamectl set-hostname "k8s-master.linuxtechi.local" // Run on master node $ sudo hostnamectl set-hostname "k8s-worker01.linuxtechi.local" // Run on 1st worker node $ sudo hostnamectl set-hostname "k8s-worker02.linuxtechi.local" // Run on 2nd worker node

Also add the following entries in /etc/hosts file on all the nodes,

192.168.1.23 k8s-master.linuxtechi.local k8s-master 192.168.1.24 k8s-worker01.linuxtechi.local k8s-worker01 192.168.1.25 k8s-worker02.linuxtechi.local k8s-worker02

## 2) Disable Swap on All Nodes

For kubelet to work smoothly, it is recommended to disable swap. Run following commands on master and worker nodes to turn off swap.

$ sudo swapoff -a $ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

## 3) Add Firewall Rules for Kubernetes Cluster

In case, OS firewall is enabled on your Debian systems then allow following ports on master and worker nodes respectively.

On Master node, run

$ sudo ufw allow 6443/tcp $ sudo ufw allow 2379/tcp $ sudo ufw allow 2380/tcp $ sudo ufw allow 10250/tcp $ sudo ufw allow 10251/tcp $ sudo ufw allow 10252/tcp $ sudo ufw allow 10255/tcp $ sudo ufw reload

On Worker Nodes,

$ sudo ufw allow 10250/tcp $ sudo ufw allow 30000:32767/tcp $ sudo ufw reload

Note: If firewall is disabled on your Debian 12/11 systems, then you can skip this step.

## 4) Install Containerd Run time on All Nodes

Containerd is the industry standard container run time and supported by Kubernetes. So, install containerd on all master and worker nodes.

Before installing containerd, set the following kernel parameters on all the nodes.

$ cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf overlay br_netfilter EOF $ sudo modprobe overlay $ sudo modprobe br_netfilter $ cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF

To make above changes into the effect, run

$ sudo sysctl --system

Now, install conatinerd by running following [apt command](https://www.linuxtechi.com/apt-command-ubuntu-debian-linux/) on all the nodes.

$ sudo apt update $ sudo apt -y install containerd

Next, configure containerd so that it works with Kubernetes, run beneath command on all the nodes

$ containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1

Set cgroupdriver to systemd on all the nodes,

Edit the file ‘/etc/containerd/config.toml’ and look for the section ‘[plugins.”io.containerd.grpc.v1.cri”.containerd.runtimes.runc.options]’ and change ‘SystemdCgroup = false’ to ‘SystemdCgroup = true‘

$ sudo vi /etc/containerd/config.toml

Save and exit the file.

Restart and enable containerd service on all the nodes,

$ sudo systemctl restart containerd $ sudo systemctl enable containerd

## 5) Add Kubernetes Apt Repository

In Debian 12/11, Kubernetes related packages are not available in the default package repositories. We have to add additional Kubernetes apt repository on all the nodes, run

$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list $ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

## 6) Install Kubernetes Tools

Next, install the Kubernetes tools, including kubeadm, kubelet, and kubectl on all the nodes.

$ sudo apt update $ sudo apt install kubelet kubeadm kubectl -y $ sudo apt-mark hold kubelet kubeadm kubectl

## 7) Install Kubernetes Cluster with Kubeadm

kubelet doesn’t appreciate the command-line options anymore (these are deprecated). Instead, I suggest to create a configuration file, say ‘kubelet.yaml’ with following content.

$ vi kubelet.yaml apiVersion: kubeadm.k8s.io/v1beta3 kind: InitConfiguration --- apiVersion: kubeadm.k8s.io/v1beta3 kind: ClusterConfiguration kubernetesVersion: "1.28.0" # Replace with your desired version controlPlaneEndpoint: "k8s-master" --- apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration

Now, we are all set to initialize Kubernetes cluster, run following command only from master node,

$ sudo kubeadm init --config kubelet.yaml

Output,

Above output confirms that control plane has been initialized successfully. In the output, we have commands for regular user for interacting with the cluster and also the command to join any worker node to this cluster.

To start interacting with cluster, run following commands on master node,

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

Run following kubectl command to get nodes and cluster information,

$ kubectl get nodes $ kubectl cluster-info

Output of above commands,

On your worker nodes, join them to the cluster by running the command that was displayed when you initialized the master node. It will look something like this ‘Kubeadm join’

Note: Copy the exact command from the output of ‘kubeadm init’ command. In my case, following is the command

$ sudo kubeadm join k8s-master:6443 --token 21nm87.x1lgd4jf0lqiiiau \ --discovery-token-ca-cert-hash sha256:28b503f1f2a2592678724c482776f04b445c5f99d76915552f14e68a24b78009

Output from Worker Node 1,

Output from Worker Nod 2 ,

Check the nodes status by running following command from master node,

$ kubectl get nodes

To make nodes status ready, we must install POD network addons like Calico or flannel.

## 8) Setup Pod Network Using Calico


On the master node, run beneath command to install calico,

$ kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml

Output,

Allow Calico ports in OS firewall, run beneath ufw commands on all the nodes,

$ sudo ufw allow 179/tcp $ sudo ufw allow 4789/udp $ sudo ufw allow 51820/udp $ sudo ufw allow 51821/udp $ sudo ufw reload

Verify the status of Calico pods, run

$ kubectl get pods -n kube-system

Perfect, now check nodes status again,

Great, above confirms that master and worker nodes are in ready staate. Now, we can say our cluster ready handle workload.

## 9) Test Kubernetes Cluster Installation

In order validate and test Kubernetes cluster installation, let’s try to deploy nginx based application via deployment. Run beneath commands,

$ kubectl create deployment nginx-app --image=nginx --replicas 2 $ kubectl expose deployment nginx-app --name=nginx-web-svc --type NodePort --port 80 --target-port 80 $ kubectl describe svc nginx-web-svc

Output of above commands,

Try to access the nginx based application using following [curl command](https://www.linuxtechi.com/linux-curl-command-examples-part-1/) along with the nodeport 32283.

Note : In the curl command we can use either of worker node’s hostname.

$ curl http://k8s-worker01:32283

Great, output above confirms that we are able to access our nginx based application.

That’s all from this guide, I hope you have found it informative and able to install [Kubernetes ](https://kubernetes.io/)cluster on Debian 12 smoothly. Kindly do post your queries and feedback in below comments section.

Lee KirklandThank you so much! I tried to get k3s running on Ubuntu and couldn’t figure out why it wasn’t taking, I guess something was misconfigured. Following your guide I got Kubernetes running on my two nodes.

AmauriFantastic tutorial, worked perfectly on Debian 12 !

Thanks !

DylanHello,

When making this command:

sudo kubeadm init –control-plane-endpoint=k8s-master

I get this error on my k8s-master:

[init] Using Kubernetes version: v1.27.3

[preflight] Running pre-flight checks

error execution phase preflight: [preflight] Some fatal errors occurred:

[ERROR CRI]: container runtime is not running: output: time=”2023-06-28T01:39:44+02:00″ level=fatal msg=”validate service connection: CRI v1 runtime API is not implemented for endpoint \”unix:///var/run/containerd/containerd.sock\”: rpc error: code = Unimplemented desc = unknown service runtime.v1.RuntimeService”

, error: exit status 1

[preflight] If you know what you are doing, you can make a check non-fatal with `–ignore-preflight-errors=…`

To see the stack trace of this error execute with –v=5 or higher

The containerd service is active… (Debian 11)

EricCheck your containerd version: dpkg -l | grep -i containerd.

You need a minimum version :1.5.

You can use containerd.io on docker repository, if you want.

`curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -`

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

apt update

apt install -y containerd.io

SiddharthThank you for this great post!

Note – I was successfully able to use most of this post to deploy k8s on my Debian 12 too.

VincentThank you for this great post. Following the official documentation always asks you to keep a lot of open tabs and check several commands. It’s nice to have a place with clear instructions.

I would suggest a few changes:

* kubernetes’ has a new repository. The installation method is slightly different:

`````

$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

$ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

$ sudo apt-get update

$ sudo apt install kubelet kubeadm kubectl -y

$ sudo apt-mark hold kubelet kubeadm kubectl

```

* kubelet doesnt appreciate the command-line options anymore (these are deprecated). Instead, I suggest to create a configuration file, say `kubelet.yaml` and put in it:

`````

apiVersion: kubeadm.k8s.io/v1beta3

kind: InitConfiguration

---

apiVersion: kubeadm.k8s.io/v1beta3

kind: ClusterConfiguration

kubernetesVersion: "1.28.0" # Replace with your desired version

controlPlaneEndpoint: "k8s-master"

---

apiVersion: kubelet.config.k8s.io/v1beta1

kind: KubeletConfiguration

```

This is linked to the version you use though.

And then, to init: `sudo kubeadm init –config kubelet.yaml`

Again, thank you,

Pradeep KumarThank You Vincent !! for your valuable feedback and suggestion. We have updated the post as per your suggestion.

EliséeHello Kumar !

Thx you for this great post !

Can you help with add windows node in the linux kubernetes cluster like this one ? i have problem with calico part of windows and preparation of linux kubernetes cluster.

Thx you for your help.