参考:https://kubernetes.io/zh-cn/docs/setup/
准备工作
{x} 系统:Debian 11
关闭Swap
参考:https://www.ghl.name/archives/debian-ubuntu-delete-swap-partition-safely.html
# 将swap所在行删掉
vim /etc/fstab
vim /etc/initramfs-tools/conf.d/resume
update-initramfs -u
update-grub
reboot
安装Docker
官方文档:https://docs.docker.com/engine/install/debian/
配置容器运行时
containerd config default > /etc/containerd/config.toml
vim /etc/containerd/config.toml
配置 systemd cgroup 驱动
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
配置重载沙箱(pause)镜像
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
使用kubeadm安装Kubernetes
安装 kubeadm、kubelet 和 kubectl
sudo apt-get update
# apt-transport-https 可能是一个虚拟包(dummy package);如果是的话,你可以跳过安装这个包
sudo apt-get install -y apt-transport-https ca-certificates curl
# 在低于 Debian 12 和 Ubuntu 22.04 的发行版本中,/etc/apt/keyrings 默认不存在
sudo mkdir -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 这会覆盖 /etc/apt/sources.list.d/kubernetes.list 中的所有现存配置
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
# 更新 apt 包索引,安装 kubelet、kubeadm 和 kubectl,并锁定其版本:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
配置自动补全
sudo apt install -y bash-completion
source <(kubectl completion bash)
kubectl completion bash >/etc/bash_completion.d/kubectl
source <(crictl completion bash)
crictl completion bash >/etc/bash_completion.d/crictl
vim ~/.bashrc
# 移除重复命令
export HISTCONTROL=ignoreboth:erasedups
export KUBECONFIG=/etc/kubernetes/admin.conf
# 别名
alias k=kubectl
complete -o default -F __start_kubectl k
使用 kubeadm 创建集群
kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.1.0.0/16
要使非 root 用户可以运行 kubectl,请运行以下命令, 它们也是 kubeadm init 输出的一部分:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
或者,如果你是 root 用户,则可以运行:
# 写入 ~/.bashrc 更方便
export KUBECONFIG=/etc/kubernetes/admin.conf
配置crictl
vim /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
安装 Pod 网络附加组件
wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
# 修改podCIDR
vim kube-flannel.yml
kubectl apply -f kube-flannel.yml
安装Dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
# 如果仅有一个master节点,dashboard无法部署,需要删除污点
kubectl edit nodes <MasterNode名称>