通过VMware Workstaion来搭建3个虚拟机
# vim /etc/hosts
192.168.50.130 CentOS-Master
192.168.50.131 CentOS-Node1
192.168.50.132 CentOS-Node2
关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
关闭swap => K8S中不支持swap分区
vim /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
*.编辑etc/fstab将swap那一行注释掉或者删除掉
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
安装Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
Docker version 18.06.1-ce, build e68fc7a
设置国内源
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
安装Kubeadm&Kubelet&Kubectl
yum install -y kubelet-1.13.3 kubeadm-1.13.3 kubectl-1.13.3
systemctl enable kubelet
部署Kubernetes Master
kubeadm init \
--apiserver-advertise-address=192.168.50.130 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.13.3 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
Master部署Pod网络插件
kubectl apply -f \
https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
运行Master安装pod
kubectl taint nodes --all node-role.kubernetes.io/master-
安装 calico 网络组件
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
安装这个可能比较慢要等一会确保安装完成后,在继续操作
查看Master是否启动成功
kubectl get node
查看pod启动情况
kubectl get pod --all-namespaces
如果成功应该如下所示
[root@master ~]# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-578894d4cd-zwdkg 1/1 Running 0 25m
kube-system calico-node-krc5l 1/1 Running 0 25m
kube-system coredns-7ff77c879f-cmz8n 1/1 Running 0 13d
kube-system coredns-7ff77c879f-wn9v5 1/1 Running 0 13d
kube-system etcd-master 1/1 Running 6 13d
kube-system kube-apiserver-master 1/1 Running 5 13d
kube-system kube-controller-manager-master 1/1 Running 6 13d
kube-system kube-flannel-ds-amd64-cfzl9 1/1 Running 4 74m
kube-system kube-proxy-2tbqb 1/1 Running 5 13d
kube-system kube-scheduler-master 1/1 Running 5 13d
在此过程中可能会遇到无法从qury.io拉取flannel镜像从而导致无法正常Running
如果不是Ready 使用下面的命令查看错误原因
kubectl describe pod kube-flannel-ds-amd64-xpd82 -n kube-system
可以通过使用国内源下载然后修改的tag的方式解决
如下:
docker pull quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64
docker tag quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
docker rmi quay-mirror.qiniu.com/coreos/flannell:v0.11.0-amd64
查看是否成功
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 13d v1.18.5
至此Master部署成功
node节点加入master
kubeadm join 192.168.50.130:6443 --token gru3cl.320u8ozfnc7qhxvm --discovery-token-ca-cert-hash \sha256:c50bb83d04f64f4a714b745f04
token是初始化节点的时候显示的,默认只有24小时有效期
如果忘记了可以使用 下面的命令查询
kubeadm token list
如果已经过了24小时可以使用一下命令重新生成或者修改
kubeadm token create
后面的has256可以通过ssh工具计算
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'