使用3臺集群部署kubernetes集群,角色分別如下:
Master 10.0.30.121 Centos7.7 kubenetes1.17.5 docker19.03
Node1 10.0.30.120 Centos7.7 kubenetes1.17.5 docker19.03
Node2 10.0.30.122 Centos7.7 kubenetes1.17.5 docker19.03
1.修改Linux系統(tǒng)相關(guān)內(nèi)核參數(shù)
加載內(nèi)核模塊確保內(nèi)核加載了如下模塊
modprobe
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
br_netfilter
修改內(nèi)核參數(shù) vim /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1 #開啟內(nèi)核轉(zhuǎn)發(fā)功能
vm.swappiness = 0 #永久禁用swap分區(qū),臨時禁用swapoff -a
sysctl -p
2.在各節(jié)點上配置docker、kubenetes的yum源,這里使用aliyun的yum源。
添加docker yum倉庫
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
添加kubernetes的yum倉庫
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
setenforce 0
更新yum倉庫
yum makecache fast
3.在所有節(jié)點上安裝docker-ce及kubernetes程序,默認安裝的都是最新版本的docker和kubernetes組件,安裝時可以指定版本號進行安裝
yum -y install docker-ce kubelet-1.17.5 kubeadm-1.17.5 kubectl-1.17.5
systemctl enable docker && systemctl start docker
4.修改kubelet配置文件添加如下兩行
/etc/sysconfig/kubenet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
指定kube-proxy使用ipvs規(guī)則,如果系統(tǒng)內(nèi)核沒有加載ipvs模塊這自動降級使用iptables規(guī)則
KUBE_PROXY_MODE=ipvs
5.因無法訪問k8s鏡像站點,這里使用的方式是臨時開一臺香港區(qū)阿里云或者騰訊云的服務器,將鏡像拉取重新tag推送到個人的鏡像倉庫中,初始化kubernetes集群時使用--image-repository選項指定鏡像倉庫或者使用shell腳本提前將個人倉庫中的鏡像拉取到部署的節(jié)點機器上并重新tag。

6.初始化kebernetes集群,在master節(jié)點上操作
systemctl enable kubelet && systemctl start kubelet
kubeadm init --kubernetes-version=v1.17.5 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.0.30.121 --ignore-preflight-errors=Swap
出現(xiàn)如下信息集群初始化成功
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 10.0.30.121:6443 --token rrl9v5.380bgy7wm670xiy9
--discovery-token-ca-cert-hash sha256:d10dcc83bc3ee746626d4c1565145c6dbfa33b545aabe6306f62c514d8a27dfc
7.將node節(jié)點加入到集群中,在node1和node2節(jié)點上執(zhí)行
kubeadm join 10.0.30.121:6443 --token rrl9v5.380bgy7wm670xiy9
--discovery-token-ca-cert-hash sha256:d10dcc83bc3ee746626d4c1565145c6dbfa33b545aabe6306f62c514d8a27dfc
8.部署網(wǎng)絡組件
在master節(jié)點上查看集群的node節(jié)點信息,發(fā)現(xiàn)各幾點status的狀態(tài)都是NotReady,需要部署網(wǎng)絡組件,這里使用flannel,也可以使用其他的網(wǎng)絡組件如canal、calico等
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get nodes 再次查看集群各節(jié)點的狀態(tài)以為ready,接下來可以部署應用測試。

9.kubernetes的web展示頁面dashboard部署,下載資源配置清單, 修改dashboard service的類型修改為NodePort以便在集群之外訪問。
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc7/aio/deploy/recommended.yaml

kubectl apply -f recommended.yaml 在集群上應用資源清單部署dashboard。
創(chuàng)建serviceaccount并綁定到clusterRole,使得dashboard可以訪問kubernetes集群的資源
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
-
kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard登錄dashboard并驗證,這里使用token方式登錄,到此基于yum的安裝方式的kubernetes集群單間完成
獲取serviceaccount的tokenkubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

