首先准备好搭建基础设施:win10中启用hyper-v,安装了3个centOS7.9系统(最精简版本即可)
以下是详细搭建步骤:
1.yum更新
yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
2.docker安装
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io
systemctl start docker
systemctl enable docker.service
验证结果:docker version
3.修改/etc/hosts
hostnamectl set-hostname m
hostnamectl set-hostname w1
hostnamectl set-hostname w2
172.24.128.25 w2
172.24.132.130 w1
172.24.134.151 m
验证互相ping通
4.基础设置修改 ‘需要更换
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
EOF
sysctl --system
临时设置:sysctl -w net.ipv4.ip_forward=1
永久设置:vi /etc/sysctl.conf
net.ipv4.ip_forward=1
5.安装kubeadm kubelet kubectl
5.1配置yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey= http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
5.2执行yum install -y kubeadm-1.23.7-0 kubelet-1.23.7-0 kubectl-1.23.7-0
5.3设置同一个cgroup
#docker
vi /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"],
systemctl restart docker
#kubelet
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable kubelet && systemctl start kubelet
kubeadm config images list
查看所需依赖版本,下面脚本中要使用
6.执行脚本拉取镜像,sh ./kube.sh
#!/bin/bash
set -e
KUBE_VERSION=v1.23.7
KUBE_PAUSE_VERSION=3.6
ETCD_VERSION=3.5.1-0
CORE_DNS_VERSION=v1.8.6
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
coredns的镜像需要在手动处理改下repository
docker tag k8s.gcr.io/coredns:v1.8.6 k8s.gcr.io/coredns/coredns:v1.8.6
7.初始化master,只在master节点上执行
7.1kubeadm init --kubernetes-version=1.23.7 --apiserver-advertise-address=172.24.134.151
Your Kubernetes control-plane has initialized successfully!
7.2To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
7.3kubectl get pods -n kube-system
查看状态,有5个running后再进行下一步
Run “kubectl apply -f [podnetwork].yaml” with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
7.4安装网络插件calico
kubectl apply -f https://docs.projectcalico.org/v3.20/manifests/calico.yaml
可提前拉取yaml中镜像
8.集群中加入worker
kubeadm join 172.24.134.151:6443 --token squhze.dbqvqrzmr7797h57 \
--discovery-token-ca-cert-hash sha256:91080a80d02a1d999d86acf4112d1e9eab948d6c190a1ac9ec21e34bc0bd4796
kubeadm reset
踩坑补充:
1.注意不要写成kubectl reset
2.kubeadm reset
在master和所有worker节点都要执行,如果没有在worker节点执行,那么执行kubeadm join时会报错
3.kubeadm reset
重置后,再次执行kubeadm init前一定要清除掉$HOME/.kube这个目录,否则会报错
4./etc/cni/net.d可能需要删除