Kubernetes安装部署(k8s-v1.29-2024)
2024-02-22 本文已影响0人
king_wang
部署机器
机器 | 角色 |
---|---|
192.168.110.90 | master |
192.168.110.91 | node |
192.168.110.92 | node |
准备环境
注意,所有机器都要准备
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭SeLinux
# 临时禁用selinux
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
关闭swap
# 禁用交换分区
swapoff -a
# 永久禁用,打开/etc/fstab注释掉swap那一行
sed -i 's/.*swap.*/#&/' /etc/fstab
网络配置
转发 IPv4 并让 iptables 看到桥接流量
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
# 执行 sysctl --system 使生效
安装docker
安装docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{"exec-opts":["native.cgroupdriver=systemd"],"registry-mirrors":["https://registry.cn-hangzhou.aliyuncs.com"]}
EOF
yum -y install docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl start containerd
安装k8s
安装kubelet kubeadm kubectl
设置yum源
# 此操作会覆盖 /etc/yum.repos.d/kubernetes.repo 中现存的所有配置
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
yum安装
yum install -y kubectl-1.29.2 kubeadm-1.29.2 kubelet-1.29.2
systemctl enable --now kubelet
初始化Master
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.29.2 --apiserver-advertise-address=192.168.110.90 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.1.0.0/16
特别注意:成功初始化之后,会输出node节点加入集群的命令:
kubeadm join 192.168.110.90:6443 --token euf2a7.hxgiobxonvzx73e1 --discovery-token-ca-cert-hash sha256:1063e5ccd8aed45acf0b1aab1e1acca8a70bbaef700809d9836d4ebc0d1e086d1
另外,将配置文件copy到所有机器上并声明环境变量KUBECONFIG
export KUBECONFIG=/etc/kubernetes/admin.conf
安装Calico
在master上安装Calico
# 下载operator资源清单文件
wget https://docs.projectcalico.org/manifests/tigera-operator.yaml
# 应用资源清单文件,创建operator
kubectl apply -f tigera-operator.yaml
# 下载Calico资源清单文件
wget https://docs.projectcalico.org/manifests/custom-resources.yaml
# 注意修改文件第13行,修改为kubeadm init ----pod-network-cidr对应的IP地址段
vim custom-resources.yaml
......
11 ipPools:
12 - blockSize: 26
13 cidr: 10.244.0.0/16
14 encapsulation: VXLANCrossSubnet
......
#应用资源清单文件
kubectl apply -f custom-resources.yaml
#监视calico-sysem命名空间中pod运行情况
kubectl get pods -n calico-system
# 查看kube-system命名空间中coredns状态,处于Running状态表明联网成功。
kubectl get pods -n kube-system
添加Node
使用前面kubeadm init
命令的输出结果添加node:
kubeadm join 192.168.110.90:6443 --token euf2a7.hxgiobxonvzx73e1 --discovery-token-ca-cert-hash sha256:1063e5ccd8aed45acf0b1aab1e1acca8a70bbaef700809d9836d4ebc0d1e086d1
检查k8s
# 检查节点状态
kubectl get nodes
# 检查pod状态
kubectl get pods -A -o wide