搭建環境 ,下面三臺機器都是centos7.5,內核版本爲3.10.0node
1.關閉防火牆linux
systemctl stop firewalld systemctl stop firewalld systemctl disable firewalldsystemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config swapoff -a sed -i '/^.*swap.*/d' /etc/fstab
2.建立本地解析git
cat << EOF >> /etc/hosts 192.168.157.22 node01 192.168.157.23 node02 192.168.157.23 master EOF
#安裝必要的一些系統工具 yum install -y yum-utils device-mapper-persistent-data lvm2 #yum源添加軟件源信息 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #更新yum源 yum makecache fast #安裝docker yum -y install docker-ce || yum -y install docker-ce-[VERSION] #開機啓動 systemctl start docker && systemctl enable docker #查看是否安裝成功 docker version
1.yum 安裝相關軟件github
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [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 repolist yum install -y kubelet kubeadm kubectl systemctl enable kubelet
2.使用kubeadm進行master初始化docker
#生成kubeadm配置文件 kubeadm config print init-defaults > kubeadm.conf kubeadm config print-default > kubeadm.conf # 老版本能夠使用這個命令 #換kubeadm的鏡像倉庫 sed -i "s/imageRepository: .*/imageRepository: registry.aliyuncs.com\/google_containers/g" kubeadm.conf #下載k8s master 節點中的鏡像到本地 kubeadm config images pull --config kubeadm.conf #修改kubeadm.conf中advertiseAddress參數指定master的ip地址 sed -i "s/advertiseAddress: .*/advertiseAddress: 192.168.157.21/g" kubeadm.conf #我使用的是Canal網絡插件,所以須要將--pod-network-cid設置爲10.244.0.0/16 sed -i "s/podSubnet: .*/podSubnet: \"10.244.0.0\/16\"/g" kubeadm.conf #進行master初始化 kubeadm init --config kubeadm.conf
此報錯是因爲cpu核數不足,k8s最低須要2核才能夠運行shell
沒有禁用swap致使安裝異常,能夠禁用掉,或者使用 「--ignore-preflight-errors=Swap」忽略次報錯json
1.初始化成功
centos
# 保存這條命令在node上使用 kubeadm join 192.168.157.21:6443 --token g1eojw.rwjv5t18d32hjbro --discovery-token-ca-cert-hash sha256:bcf361414fb2b51387930ea0c2f902e876e52858b5ec6874281cd0b6b8f4ab4a
2.查看鏡像是否拉取完整網絡
docker image ls
3.查看服務是否健康app
kubectl get cs #查看配置信息 kubectl get configmap kube-flannel-cfg -o json -n kube-system #查看dns地址 kubectl get svc kube-dns -n kube-system
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
kubectl get pods -n kube-system
kubectl describe pod kube-flannel-ds-amd64-2dqlf -n kube-system
經過上面的報錯能夠知道容器pull失敗是因爲網絡異常
增長指定網卡「- --iface=ens33」
containers: - name: kube-flannel image: quay.io/coreos/flannel:v0.10.0-amd64 command: - /opt/bin/flanneld args: - --ip-masq - --kube-subnet-mgr - --iface=ens33
kubectl delete -f kube-flannel.yml #修改yml文件改變鏡像倉庫 sed -i 's#quay.io/coreos/flannel:v0.11.0#registry.cn-hangzhou.aliyuncs.com/rsq_kubeadm/flannel:v0.10.0#g' kube-flannel.yml #從新安裝 kubectl apply -f kube-flannel.yml
kubeadm join 192.168.157.21:6443 --token g1eojw.rwjv5t18d32hjbro --discovery-token-ca-cert-hash sha256:bcf361414fb2b51387930ea0c2f902e876e52858b5ec6874281cd0b6b8f4ab4a
若是忘記上面那條命令能夠中心生成
#從新生成token kubeadm token create #查看takenlist kubeadm token list #獲取加密 openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
kubectl get nodes -o wide