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. 系统配置
配置主机名,不要用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 参数,参数在重新启动后保持不变
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 参数而不重新启动
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的版本号根据自己实际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
这里可能出现超时的问题:
[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
一般是因为找不到镜像的问题,可以执行systemctl status containerd
查看日志。它的日志不能换行显示不完整,所以需要先缩小再执行,再放大查看日志。就可以找到是哪个镜像的哪个版本找不到。
比如我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用户:(不执行可能会引起证书错误)
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:
第二个命令,需要根据自己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节点:
worker节点上执行如下命令:(注意需要提前准备好相关必要镜像“kubeadm config images ”那些)
kubeadm join 192.168.8.125:6443 --token ho3sx8.kq640morilxbff3f \
--discovery-token-ca-cert-hash sha256:7a4f647fe3245898528609aeb100967e4b59c55a65142e28bf7af58616095829
去除master节点污点:
kubectl taint nodes <node> node-role.kubernetes.io/master:NoSchedule-