提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Debian12 部署 k8s 1.27.4 集群部署手记


一、环境配置

1.关闭swap

sudo swapoff -a
sudo sed -i 's/.*swap.*/#&/' /etc/fstab

2.关闭防火墙

sudo swapoff -a
sudo sed -i 's/.*swap.*/#&/' /etc/fstab

3.sysctl 参数

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

EOF

# 不重启应用配置
sysctl --system

4.配置内核参数

modprobe br_netfilter

# 注意
modprobe : command not found
# 解决方法
vim ~/.bashrc
添加行
export PATH=/usr/sbin:$PATH
# 不重启应用配置
source ~/.bashrc

5.修改主键名称和host文件

hostnamectl set-hostname k8smaster && hostname
hostnamectl set-hostname k8snode1 && hostname
hostnamectl set-hostname k8snode2 && hostname

vim /etc/hosts
192.168.124.134 k8smaster
192.168.124.135 k8snode1
192.168.124.141 k8snode2

注意:环境配置问题可能会导致 kubeadm init 失败


二、containerd 容器部署

1.添加镜像源

# 下载证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 配置容器镜像源
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新源
apt-get upgrade && apt-get update

2.安装

# 安装容器
apt-get install containerd

# 初始化配置
containerd config default > /etc/containerd/config.toml

# sandbox_image = "registry.k8s.io/pause:3.6" 更新配置如下
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6"

# SystemdCgroup = false 更新配置如下
SystemdCgroup = true

3.服务管理

# 重新加载配置文件
systemctl daemon-reload 

 # 设置开机自启并立即启动
systemctl enable --now containerd

# 停止服务
systemctl stop containerd.service

# 重启服务
systemctl restart containerd.service

# 查看服务状态
systemctl status containerd.service 

4.常用代码

# 查看组件版本信息
containerd -version
ctr version
crictl version

# 容器拉取镜像
ctr  pull nginx:alpine

三、kubelet 部署

1.添加镜像源

# 下载证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

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

# 更新源
apt-get upgrade && apt-get update

# 安装最新版本
apt-get install -y kubelet kubeadm kubectl 

# 安装指定版本
apt install kubelet=1.27.4-00
apt install kubeadm=1.27.4-00
apt install kubectl=1.27.4-00

2.服务管理

# 服务重启
systemctl restart --now kubelet

# 查询状态
systemctl status kubelet

# 查看服务日志信息
journalctl -xeu kubelet
journalctl -xe

至此,基础配置已经完成,可以克隆备份服务器,或者做镜像


四、集群部署

1.master 初始化

# master 初始化
kubeadm init \
--kubernetes-version v1.27.4 \
--image-repository registry.aliyuncs.com/google_containers \
--apiserver-advertise-address 192.168.124.134 \
--service-cidr 10.245.0.0/12 \
--pod-network-cidr 10.244.0.0/16

2.添加node节点

# 添加node节点
kubeadm join 192.168.124.134:6443 --token yduazy.2httobrp3wi62nxm --discovery-token-ca-cert-hash sha256:7fb2eb05820da8184effc08f151a86dcdcc253e255ce859765629862da873f6a

3.常用代码

# 查询token
kubeadm token list

# 生成token,用于节点添加
kubeadm token create --print-join-command

# 查看节点信息
kubectl get node

五、flannel 网络插件部署

1.插件部署

# 下载网络插件配置
curl -fsSL -O https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

# 删除配置,删除后可以重新部署
kubectl delete -f kube-flannel.yml

# 部署网络插件
kubectl apply -f kube-flannel.yml

flannel无法拉取镜像(ImagePullBackOff)
手工拉取网络插件镜像,配置中的镜像没办法拉取是可以手工尝试拉取
ctr images pull docker.io/library/redis:latest
ctr images pull docker.io/flannel/flannel:v0.22.2
ctr images pull docker.io/flannel/flannel-cni-plugin:v1.2.0

2.常用代码

# 查看所有pods状态
kubectl get pods -A

# 查看pod日志
kubectl logs kube-flannel-ds-5dm4j -n kube-flannel

# flannel拉取镜像失败(ImagePullBackOff)
# 手工拉取网络插件镜像,配置中的镜像没办法拉取是可以手工尝试拉取
ctr images pull docker.io/flannel/flannel:v0.22.2
ctr images pull docker.io/flannel/flannel-cni-plugin:v1.2.0

# 错误信息,可能是由于防火墙或者kubeadm init和yml中cidr配置不一致,访问不可达导致 
Error registering network: failed to acquire lease: node "nodeName" pod cidr not assigned

六、dashboard 部署

1.部署

# 下载配置文件
curl -O https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml

# 修改配置文件
spec:
  type: NodePort          #添加行
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30080     #添加行

# 仪表盘部署
kubectl apply -f recommended.yaml

# 移除仪表盘,移除后可以再次部署
kubectl delete -f recommended.yaml

# 创建账号
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard

# 绑定权限
kubectl create clusterrolebinding dashboard-admin-rb --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin

# 生成token
kubectl -n kubernetes-dashboard create token dashboard-admin --duration 3153600000s

2.常用代码

# 查看状态
kubectl get pod,svc -n kubernetes-dashboard

# 查看容器错误信息
kubectl describe pod kubernetes-dashboard-6967859bff-wgxxz --namespace=kubernetes-dashboard

# 查看仪表盘所在节点、端口
kubectl get pod --namespace=kubernetes-dashboard -o wide | grep dashboard

# 复制token并登录dashboard ui
# 通过节点ip和recommended.yaml中的nodePort端口登陆
https://172.31.100.116:30080/#/login
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐