1. 安裝kube三件套
apt-get update
apt install -y apt-transport-https gnupg gnupg2 gnupg1 curl lsb-release
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
2. 系統(tǒng)配置
配置主機名慢蜓,不要用localhost,例如:
hostnamectl --static set-hostname node1
關閉防火墻:
service iptables stop
systemctl stop firewalld.service
ufw disable
iptables -F
關閉swap:
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
橋接的ipv4流量轉到iptables:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 設置所需的 sysctl 參數(shù),參數(shù)在重新啟動后保持不變
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 應用 sysctl 參數(shù)而不重新啟動
sudo sysctl --system
3. 安裝containerd
apt install containerd
systemctl start containerd
mkdir -p /etc/containerd/
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
systemctl restart containerd
4. 拉取鏡像
從阿里鏡像拉去:
最好指定下版本喳篇,這里我使用1.24.2
需要注意的是即便指定了版本迅矛,一些image的版本還是會不匹配型宝,比如pause
kubeadm config images pull --kubernetes-version=v1.24.2 --image-repository=registry.aliyuncs.com/google_containers
打tag:
注意image的版本號根據(jù)自己實際pull拉下來的進行修改臭挽。
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/coredns:v1.8.6 k8s.gcr.io/coredns/coredns:v1.8.6
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/etcd:3.5.3-0 k8s.gcr.io/etcd:3.5.3-0
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.24.2 k8s.gcr.io/kube-apiserver:v1.24.2
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.24.2 k8s.gcr.io/kube-controller-manager:v1.24.2
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/kube-proxy:v1.24.2 k8s.gcr.io/kube-proxy:v1.24.2
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.24.2 k8s.gcr.io/kube-scheduler:v1.24.2
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/pause:3.7 k8s.gcr.io/pause:3.7
這里可能出現(xiàn)超時的問題:
[kubelet-check] Initial timeout of 40s passed.
Unfortunately, an error has occurred:
timed out waiting for the condition
This error is likely caused by:
- The kubelet is not running
一般是因為找不到鏡像的問題捂襟,可以執(zhí)行systemctl status containerd
查看日志。它的日志不能換行顯示不完整埋哟,所以需要先縮小再執(zhí)行笆豁,再放大查看日志。就可以找到是哪個鏡像的哪個版本找不到赤赊。
比如我pull的時候pause
的版本在阿里鏡像中默認是3.7,而k8s.gcr.io的是3.5煞赢,不斷重新拉去就導致超時了抛计。
這時候需要手動指定版本拉下image,并打tag:
ctr -n k8s.io image pull registry.aliyuncs.com/google_containers/pause:3.5
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/pause:3.5 k8s.gcr.io/pause:3.5
5. kubeadm搭建集群
kubeadm init --image-repository=registry.aliyuncs.com/google_containers \
--pod-network-cidr=192.168.0.0/16
非root用戶:(不執(zhí)行可能會引起證書錯誤)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
root用戶:
export KUBECONFIG=/etc/kubernetes/admin.conf
安裝Calico:
第二個命令照筑,需要根據(jù)自己CIDR而修改
kubectl create -f https://projectcalico.docs.tigera.io/manifests/tigera-operator.yaml
kubectl create -f https://projectcalico.docs.tigera.io/manifests/custom-resources.yaml
加入worker節(jié)點:
worker節(jié)點上執(zhí)行如下命令:(注意需要提前準備好相關必要鏡像“kubeadm config images ”那些)
kubeadm join 192.168.8.125:6443 --token ho3sx8.kq640morilxbff3f \
--discovery-token-ca-cert-hash sha256:7a4f647fe3245898528609aeb100967e4b59c55a65142e28bf7af58616095829
去除master節(jié)點污點:
kubectl taint nodes <node> node-role.kubernetes.io/master:NoSchedule-