k8s集群部署+docker

发布时间:
更新时间:
🕒 阅读时间:3 min read 👀 阅读量:Loading...

所有节点安装Docker、kubeadm、kubectl

1 .卸载旧环境

Terminal window
kubeadm reset -f
yum remove -y kubeadm kubelet kubectl
yum remove -y docker* containerd*
rm -rf /etc/kubernetes /var/lib/kubelet /var/lib/etcd /etc/docker /etc/containerd
rm -rf /etc/yum.repos.d/kubernetes.repo

2. 安装前准备

Terminal window
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
# 关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 根据规划设置主机名
hostnamectl set-hostname <hostname>
# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.1.146 k8smaster
192.168.1.145 k8snode1
192.168.1.144 k8snode2
EOF
# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 时间同步
yum install ntpdate -y
ntpdate time.windows.com

3. 安装Docker

Terminal window
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-23.0.6 docker-ce-cli-23.0.6 containerd.io
# 启动Docker
systemctl enable --now docker

4. 配置Docker Group(必须为systemd)

Terminal window
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://docker.1ms.run"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF

5. 重启Docker

Terminal window
systemctl daemon-reload
systemctl restart docker

6. 添加kubernetes阿里云镜像(最后支持 Docker 的 1.23 版本)

Terminal window
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

7. 安装 K8s 1.23.17(最后支持 Docker 的版本)

Terminal window
yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17 --disableexcludes=kubernetes
# 启动kubelet
systemctl enable --now kubelet

8. 部署Kubernetes Maste

Terminal window
kubeadm init \
--apiserver-advertise-address=192.168.1.146 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.17 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

执行成功后按照提示执行

image-20260430112400075

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

9. 加入Kubernetes Node

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

Terminal window
kubeadm join 192.168.1.146:6443 --token y4k6va.wtjx112a7lh6ye92 \
--discovery-token-ca-cert-hash sha256:065147d0d6a0122134700ee00150b5490a84f74fd9c786fc7a36d236d8a89ec6

默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:

Terminal window
kubeadm token create --print-join-command

10. 部署CNI网络插件

Terminal window
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 默认镜像地址无法访问,sed命令修改为docker hub镜像仓库。
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
kube-flannel-ds-amd64-2pc95 1/1 Running 0 72s

image-20260430114529589

11. 测试kubernetes集群

Terminal window
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
# 可以看到nginx的端口
kubectl get pod,svc

访问地址:http://NodeIP:Port

k8s集群部署+docker

作者: Mindspark

本文链接: https://oxai.net.cn/posts/f746ae9e

本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。

留言评论

2000年1月1日星期六
00:00:00