本次使用kubeadm方式,部署1.23.17版本
安装包百度云盘地址:
链接:https://pan.baidu.com/s/1UrIotP253DoyDIYB7G1C0Q
提取码:8q6a
集群所需虚拟机环境
主机名称 | IP | 资源 |
harbor | 10.0.0.230 | 1c2g |
master | 10.0.0.231 | 2c4g |
worker1 | 10.0.0.232 | 2c4g |
worker2 | 10.0.0.233 | 2c4g |
- cat >> /etc/hosts <
- 10.0.0.230 harbor harbor.test.com
- 10.0.0.231 master
- 10.0.0.232 worker1
- 10.0.0.233 worker2
- EOF
yum -y install lrasz
然后将安装包拖进去获取rz -E 上传就好了
- tar xf docker.tar.gz
- ./install-docker.sh install
- [root@master ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
- [root@master ~]# sysctl -p
- net.ipv4.ip_forward = 1
- vim /etc/docker/daemon.json
-
- {
- "registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"],
- "insecure-registries": ["harbor.test.com"],
- "exec-opts": ["native.cgroupdriver=systemd"]
- }
- "insecure-registries": ["harbor.test.com"], #允许拉取自建仓库harbor仓库的镜像;
-
- "exec-opts": ["native.cgroupdriver=systemd"] #kubeadm去寻找的cgroup默认是systemd,而docker不配置的话,默认是cgroupfs,不配置这个,部署k8s时会报错;
systemctl restart docker
- [root@master ~]# free -h
- total used free shared buff/cache available
- Mem: 3.8G 275M 2.7G 19M 896M 3.3G
- Swap: 2.0G 0B 2.0G
[root@master ~]# sed -ni '/^[^#]*swap/s@^@#@p' /etc/fstab
- cat > /etc/modules-load.d/k8s.conf << EOF
- br_netfilter
- EOF
-
- cat > /etc/sysctl.d/k8s.conf << EOF
- net.bridge.bridge-nf-call-ip6tables=1
- net.bridge.bridge-nf-call-iptables=1
- net.ipv4.ip_forward=1
- EOF
-
- sysctl --system
- systemctl stop firewalld
- systemctl disable firewalld
- vi /etc/selinux/config
-
- 改成:SELINUX=disabled
tar xf kubeadm-kubelet-kubectl.tar.gz
yum -y localinstall kubeadm-kubelet-kubectl/*.rpm
systemctl enable --now kubelet.service
systemctl status kubelet.service
kubeadm init --kubernetes-version=v1.23.17 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.100.0.0/16 --service-cidr=10.200.0.0/16 --service-dns-domain=test.com
参数解析
- --pod-network-cidr=10.100.0.0/16 #pod的网段
-
- --service-cidr=10.200.0.0/16 #service资源的网段
-
- --service-dns-domain=test.com #service集群的dns解析名称
- 先执行:kubeadm reset -f
- 在执行: kubeadm init --kubernetes-version=v1.23.17 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.100.0.0/16 --service-cidr=10.200.0.0/16 --service-dns-domain=test.com
发现硬盘变为只读硬盘
- mount -o remount,rw /sys/fs/cgroup
- mount -o remount,rw /
重新初始化即可
- mkdir -p $HOME/.kube
- sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- sudo chown $(id -u):$(id -g) $HOME/.kube/config
- [root@master pod]# kubectl get cs
- Warning: v1 ComponentStatus is deprecated in v1.19+
- NAME STATUS MESSAGE ERROR
- controller-manager Healthy ok
- scheduler Healthy ok
- etcd-0 Healthy {"health":"true","reason":""}
- [root@master pod]# kubectl get nodes
- NAME STATUS ROLES AGE VERSION
- master Ready control-plane,master 31h v1.23.17
kubeadm join 10.0.0.231:6443 --token 0he81g.kbo3vnqqmep9m33p --discovery-token-ca-cert-hash sha256:f865442d8659854db32f692d937c2d8b17e2a07942e8bd7d7bbb8a1d831b3fd1
token失效报错如下:
master节点重新生成
kubeadm token create --print-join-command
然后在worker节点上分别执行生成的命令即可
worker节点加入集群后,在master节点上查看,此时发现,状态显示“NotReady,是因为未有CNI网络组件
- [root@master ~]# kubectl get nodes
- NAME STATUS ROLES AGE VERSION
- k8s231 NotReady control-plane,master 13m v1.23.17
- k8s232 NotReady <none> 40s v1.23.17
- k8s233 NotReady <none> 18s v1.23.17
-
-
-
- net-conf.json: |
- {
- "Network": "10.100.0.0/16",
- "Backend": {
- "Type": "vxlan"
- }
- }
kubectl apply -f kube-flannel.yml
[root@master pod]# kubectl get pods -A -o wide | grep flannel
这里,如果未更改配置文件中的镜像下载地址,【running】那里会显示【imagebackoff】,这是因为国内把docker给禁了,无法直接从docker官网拉镜像了,可以先把镜像上传到harbor,将镜像下载地址变更为harbor的就好了(进入文件,输入/,然后输入image,将镜像地址变更为harbor地址)
这里是我自己的配置的地址,这个要根据自己配置的来
- image: harbor.test.com/k8s/flannel/flannel:v0.22.2
- image: harbor.test.com/k8s/flannel/flannel-cni-plugin:v1.2.0
- image: harbor.test.com/k8s/flannel/flannel:v0.22.2
配置完成后
kubectl delete -f kube-flannel.yml
kubectl apply -f kube-flannel.yml
- [root@master pod]# kubectl get nodes
- NAME STATUS ROLES AGE VERSION
- master Ready control-plane,master 31h v1.23.17
- worker1 Ready <none> 30h v1.23.17
- worker2 Ready <none> 30h v1.23.17
至此,k8s集群搭建完毕