K8s集群v1.26.1版本的简单部署实践
使用cri-dockerd+calico+kubernetes-dashboard
一、 主节点部署
环境说明
- 主节点k8s-master IP:192.168.31.41
- Node1 k8s-node1 IP:192.168.31.121
- Node2 k8s-node2 IP:192.168.31.122
均为CentOS7.9 最小化安装
(图片点击放大查看)
先创建脚本k8s_init.sh
1)、其中阿里云docker镜像加速的URL 脚本中这一行:"registry-mirrors": ["https://XXXXX.mirror.aliyuncs.com"],请自行替换
2)、脚本中/etc/hosts内容请自行替换成实际服务器IP地址
3)、脚本中hostnamectl set-hostname k8s-master 请自行替换成实际服务器的主机名
脚本内容如下
#!/bin/bash cat >> /etc/hosts << EOF 192.168.31.41 k8s-master 192.168.31.121 k8s-node1 192.168.31.122 k8s-node2 EOF hostnamectl set-hostname k8s-master systemctl restart rsyslog swapoff -a sed -ri 's/.*swap.*/#&/' /etc/fstab systemctl stop firewalld systemctl disable firewalld sed -i 's/enforcing/disabled/' /etc/selinux/config setenforce 0 cat >> /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF sysctl --system yum install ntpdate -y ntpdate ntp.aliyun.com yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加软件源信息 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3 sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo # Step 4: 更新并安装Docker-CE yum makecache fast yum -y install docker-ce # Step 4: 开启Docker服务 mkdir -p /data/docker sed -i "s#ExecStart=/usr/bin/dockerd -H fd://#ExecStart=/usr/bin/dockerd --data-root /data/docker -H fd://#g" /lib/systemd/system/docker.service cat /lib/systemd/system/docker.service | grep ExecStart systemctl daemon-reload mkdir -p /etc/docker/ touch /etc/docker/daemon.json cat > /etc/docker/daemon.json << \EOF { "bip": "10.112.0.1/24", "registry-mirrors": ["https://XXXXX.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF systemctl enable docker systemctl restart docker docker version rpm -ivh cri-dockerd-0.3.1-3.el7.x86_64.rpm sed -i "s#ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd://#ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7#g" /usr/lib/systemd/system/cri-docker.service systemctl daemon-reload systemctl enable cri-docker.service systemctl restart cri-docker.service systemctl restart docker 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 exclude=kubelet kubeadm kubectl EOF yum install -y kubelet-1.26.1 kubeadm-1.26.1 kubectl-1.26.1 --disableexcludes=kubernetes systemctl enable kubelet
二、master节点安装k8s基础环境
master节点rz上传cri-dockerd-0.3.1-3.el7.x86_64.rpm 和calico_dashboard.tar.gz
(图片点击放大查看)
sh k8s_init.sh安装k8s基础环境
(图片点击放大查看)
三、master节点进行kubeadm init初始化
kubeadm init --apiserver-advertise-address=192.168.31.41 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.26.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --cri-socket=unix:///var/run/cri-dockerd.sock --ignore-preflight-errors=all
(图片点击放大查看)
--apiserver-advertise-address=192.168.31.41为master节点IP地址
(图片点击放大查看)
初始化完成后按提示执行
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
四、Node节点使用脚本部署k8s基础环境
Node1和Node2 工作节点只需上传cri-dockerd-0.3.1-3.el7.x86_64.rpm
然后使用k8s_init.sh安装k8s基础环境,过程截图略
五、Node节点加入集群
使用主节点kukeadm init最后的提示命令
kubeadm join 192.168.31.41:6443 --token 92zebh.ntfkdtf5l5aff5pf \ --discovery-token-ca-cert-hash sha256:54f456308e46c78f65ccf6fbbd2b7b77b6bd79c9c465e169393d4035e868cfda --cri-socket=unix:///var/run/cri-dockerd.sock
(图片点击放大查看)
加入集群后kubectl get nodes可以看到node1节点
kubectl get nodes
(图片点击放大查看)
六、master节点安装calico网络插件
tar -zxvf calico_dashboard.tar.gz kubectl apply -f calico.yaml
(图片点击放大查看)
tail -f /var/log/messages可以看到在拉取calico的docker镜像
tail -f /var/log/messages
(图片点击放大查看)
kubectl get svc,pods -o wide -n kube-system
也可以看到在创建calico的pods
kubectl get svc,pods -o wide -n kube-system
(图片点击放大查看)
待calico pods为running状态时 kubectl get nodes -o wide可以看到nodes为Ready状态了
kubectl get nodes -o wide
(图片点击放大查看)
这时K8s集群创建完成
当然我这里只测试了1个节点K8s-node1
七、安装kubernetes-dashboard
kubectl apply -f kubernetes-dashboard2.7.yaml kubectl get svc,pods -o wide -n kubernetes-dashboard
(图片点击放大查看)
(图片点击放大查看)
(图片点击放大查看)
创建Token
kubectl create serviceaccount dashboard-admin -n kube-system kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin kubectl create token dashboard-admin -n kube-system
(图片点击放大查看)
(图片点击放大查看)
这时使用这个Token就可以正常登录kubernetes-dashboard
(图片点击放大查看)
以上就是K8s集群v1.26.1版本的部署过程:cri-dockerd+calico+kubernetes-dashboard
Tips
本文使用的cri-dockerd-0.3.1-3.el7.x86_64.rpm 和calico_dashboard.tar.gz 下载链接如下
链接:https://share.weiyun.com/29fFxnyV 密码:tjciyu