系統環境配置
#關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
#關閉selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
#關閉swap
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
#同步服務器時間
timedatectl set-timezone Asia/Shanghai
yum install chrony -y
systemctl enable chronyd
systemctl start chronyd
chronyc sources
#containerd安裝和配置的先決條件:
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 設置必需的 sysctl 參數,這些參數在重新啟動后仍然存在。
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# 應用 sysctl 參數而無需重新啟動
sudo sysctl --system
安裝containerd
wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install containerd -y
#創建目錄並生成配置文件
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
# 替換配置文件,設置阿里雲加速倉庫,啟用systemd管理cgroup
sed -i "s#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g" /etc/containerd/config.toml
sed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.toml
sed -i "s#https://registry-1.docker.io#https://bncakk5o.mirror.aliyuncs.com#g" /etc/containerd/config.toml
#啟動containerd
systemctl daemon-reload
systemctl enable containerd
systemctl restart containerd
安裝k8s
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.22.1 kubeadm-1.22.1 kubectl-1.22.1
#設置運行時
crictl config runtime-endpoint /run/containerd/containerd.sock
#設置為開機啟動kubelet
systemctl daemon-reload
systemctl enable kubelet && systemctl restart kubelet
#初始化集群,192.168.56.108是管理節點的內網ip,請根據自己的實際網址更改;注意10.1.0.0,和10.50.0.0,可以自行設置分配地址段,請不要和現有的ip段沖突
kubeadm init \
--apiserver-advertise-address=192.168.56.108 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.1 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.50.0.0/16 \
--cri-socket=
#初始化完成后會再末尾生成提示,按照下列提示操作,將kubeadm join復制到其他node節點執行即可。
To 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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.56.108:6443 --token v5u33x.o9x2p89x2e5x659n \
--discovery-token-ca-cert-hash sha256:c704c9c42840b0bf27f1c91681cc9dabb4d0e67ba2a0ac557009b9a31961a87b
#若過了很久之后token過期了,可以使用下列命令生成新的token
kubeadm token create --print-join-command
其他node節點除了不用初始化init,其他配置均一致。
節點加入后,會處於notready狀態,是因為沒有安裝網絡插件,這里安裝calico,執行下列命令即可
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
