树莓派安装K8S(家用实验环境部署)

605 阅读2分钟

本地系统为树莓派官方64位系统 Raspberry Pi OS Lite 64(Debian GNU/Linux 11) 使用3台树莓派4B-8G版为了构建这套家用实验环境,花了3k大洋,着实有点心痛

在master和node都需要执行的步骤

本机cgroup配置

在执行kubeadm init 时出现 missing required cgroup: memory时,可以在/boot/cmdline.txt(有的系统可能在/boot/firmware/cmdline.txt)中追加 cgroup_enable=memory cgroup_memory=1

关闭swap

网上很多教程通过编辑/etc/fstab编辑swap, 但是在树莓派系统中,并不使用fstab配置,正确的做法是

  1. 编辑/etc/dphys-swapfile
  2. 找到配置项CONF_SWAPSIZE (通过名称我们可以知道该配置项为swap大小)该值配置为0
  3. 使配置生效 sudo /etc/init.d/dphys-swapfile restart 或者 sudo reboot 重启

通过free -h命令查看swap大小

系统模块加载

# 必要的模块加载
# overlay 文件系统
# br_netfilter 网桥网络包过滤
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 开启转发和流量可观测(开机启动)
# sysctl params required by setup, params persist across reboots
# bridge-nf-call-iptables 让ip表可以看到桥接流量
# bridge-nf-call-ip6tables 让ip6表可以看到桥接流量

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



# Apply sysctl params without reboot(不用开机立刻生效)
sudo sysctl --system

CRI安装

根据官方文档配置环境doc

  1. 安装containerd doc,据观察containerd文档中的cni可以不安装,系统已经自带.
  2. 修改containerd 的cgroup driver 配置问systemd, 使用containerd config default > config.toml 修改SystemdCgroup为true 并把配置文件放到/etc/containerd/config.toml位置, 重启containerd.

安装kubeadm, kubelet and kubectl

官方文档 要求:

  1. 最低 内存:2G CPU:2核

  2. 唯一的hostname和MAC地址

  3. 关闭swap(上文已配置)

安装方式

# 安装工具
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

# 添加公钥
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

# 添加源
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

# 更新源 并安装
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

# 锁版本
sudo apt-mark hold kubelet kubeadm kubectl

在master上需要执行的步骤

  1. kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.0.104

--pod-network-cidr 指定pod网段, 10.244.0.0/16 时flannel 默认网段, 可以自行修改

--apiserver-advertise-address 宣告master节点地址

等待出现类似输出则说明安装成功

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.0.104:6443 --token f4lb9o.jj5i07q4rzhf6krf  --discovery-token-ca-cert-hash sha256:2ad164a1be1a262ef5c7e79ebaae6ad83d6a4a5b6a03304eb4b7437aed5c98e2
  1. 其中上述
To start using your cluster, you need to run the following as a regular user:

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

一定要操作,否则不能访问api-server

  1. 按照提示安装网络插件
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

这里我们安装最简单的插件flannel,进入上述链接 找到上述flannel插件

按提示执行kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

主节点操作结束

ndoe节点使用提示中

kubeadm join 192.168.0.104:6443 --token f4lb9o.jj5i07q4rzhf6krf  --discovery-token-ca-cert-hash sha256:2ad164a1be1a262ef5c7e79ebaae6ad83d6a4a5b6a03304eb4b7437aed5c98e2

把node节点加入集群

使用kubectl get nodes 等节点都变为Readay时集群安装完成

其他文章:shifengbin.github.io/