1. 主机分别准备docker & kubernetes环境(3台分别执行)
# docker sudo apt-get update && sudo apt-get install -y apt-transport-https sudo apt install -y docker.io sudo systemctl start docker sudo systemctl enable docker # kubernetes sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add sudo echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni
备注:
如果上述源连不上,可以用ustc的:
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
2. 初始化集群master(k8s1)
这里有个神坑,master的CPU核心必须在两个或以上。
集群初始化,其中第一个IP是你的内网IP,第二个是你希望Pod所使用的IP地址范围,牢记最后的输出,后面会用
kubeadm init --apiserver-advertise-address=10.4.96.3 --pod-network-cidr=192.168.0.0/16 ... You can now join any number of machines by running the following on each node as root: kubeadm join 10.3.96.3:6443 --token w1zh7w.l6chof87e113m8u7 --discovery-token-ca-cert-hash sha256:5c010cce4123abcf6c48fd98f8559b33c1efc80742270d7493035a92adf53602
备注:这一步会下载很多镜像,你懂的原因,国内是下载不了的。可以参考《国内免??预下载kuberntes镜像》
执行本地配置
# 执行一下本地配置 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
配置网络模型flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
或者weavenet
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
或者calico (需要init时--pod-network-cidr=192.168.0.0/16)
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
3. 配置集群slave(k8s2, k8s3)
s2和s3分别执行,就是master输出的那一行
kubeadm join 10.3.96.3:6443 --token w1zh7w.l6chof87e113m8u7 --discovery-token-ca-cert-hash sha256:5c010cce4123abcf6c48fd98f8559b33c1efc80742270d7493035a92adf53602
4.验证
回到k8s1查看节点
kubectl get nodes NAME STATUS ROLES AGE VERSION k8s1 Ready master 2m v1.11.1 k8s2 Ready <none> 40s v1.11.1 k8s3 Ready <none> 28s v1.11.1
成功!
参考文献: