您好,登錄后才能下訂單哦!
如何使用Kubeadm部署Kubernetes集群V1.15.3,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
我們使用最新的kubernetes v1.16.0搭建了集群,但是由于版本導(dǎo)致了遇到問題,首先是dashboard不兼容1.16.0的Api,其次是flannel-cni的問題,重新用穩(wěn)定版v1.15.3部署
一、基礎(chǔ)環(huán)境配置
1.準(zhǔn)備三臺(tái)虛擬機(jī)
docker 192.168.100.10
node01 192.168.100.11 kube-node01 master
node01 192.168.100.12 kube-node02 minion
node01 192.168.100.13 kube-node03 minion
2.配置hosts文件
cat >> /etc/hosts<<EOF
192.168.100.11 kube-node01
192.168.100.12 kube-node02
192.168.100.13 kube-node03
EOF
3.修改hostname文件
sudo hostnamectl set-hostname <newhostname>
4.關(guān)閉系統(tǒng)防火墻
systemctl stop firewalld && systemctl disable firewalld
5.禁用swap內(nèi)存交換
swapoff -a
echo "swapoff -a" >>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
注意:或開機(jī)禁用swap: 編輯/etc/fstab --> 注釋掉swap 分區(qū)
6.關(guān)閉selinux服務(wù)
臨時(shí)關(guān)閉:setenforce 0 永久關(guān)閉:vi /etc/selinux/config
將SELINUX=enforcing改為SELINUX=disabled 設(shè)置后需要重啟才能生效,命令如下:
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
7.配置iptable管理ipv4/6請(qǐng)求
sudo echo "1" > /proc/sys/net/ipv4/ip_forward
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
執(zhí)行 sysctl --system 使配置生效
8.校對(duì)系統(tǒng)時(shí)間
yum -y install ntp
systemctl start ntpd && systemctl enable ntpd
二、集群環(huán)境配置
1.安裝docker服務(wù)
配置源wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
安裝docker-ce容器服務(wù)
yum -y install docker-ce
查看docker版本號(hào)docker --version和詳細(xì)信息docker info
添加開機(jī)自啟動(dòng)和啟動(dòng)服務(wù)systemctl enable docker && systemctl start docker
修改docker啟動(dòng)參數(shù)
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["http://192.168.100.10"],
"insecure-registries":["192.168.100.10"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
修改docker的啟動(dòng)服務(wù)腳本docker.service:
在[Service]節(jié)點(diǎn)下增加
ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT
修改完成使用systemctl daemon-reload && systemctl restart docker重啟服務(wù)
2.安裝Kubernetes組件
配置源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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安裝組件yum install -y kubelet-1.15.3 kubeadm-1.15.3 kubectl-1.15.3
3.配置啟動(dòng)kubelet 組件
配置kubelet使用國(guó)內(nèi)pause鏡像和配置kubelet的cgroups:
cgroups要和docker的配置一樣,使用dokcer info可查看
vi /var/lib/kubelet/kubeadm-flags.env
vi /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
添加環(huán)境變量:
Environment="KUBELET_CGROUP_ARGS=--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice"
并將環(huán)境變量KUBELET_CGROUP_ARGS寫入啟動(dòng)參數(shù)
如果上面/var/lib/kubelet/kubeadm-flags.env沒有cgroup-driver和pod-infra-container-image的信息,則寫入/etc/sysconfig/kubelet的KUBELET_EXTRA_ARGS啟動(dòng)參數(shù)中加載
cat >/etc/sysconfig/kubelet<<EOF
KUBELET_EXTRA_ARGS=--cgroup-driver=systemd --pod-infra-container-image=192.168.100.10/kubernetes/pause:3.1
EOF
使配置生效:systemctl daemon-reload
添加自啟動(dòng):systemctl enable kubelet
4.配置Master節(jié)點(diǎn)
在master節(jié)點(diǎn)上創(chuàng)建初始化腳本:vi /etc/kubernetes/conf/kubeadm-init.sh
kubeadm init \
--kubernetes-version=v1.16.0 \
--pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address=192.168.100.11 \
--image-repository=192.168.100.10/kubernetes
修改腳本權(quán)限:chmod +x /etc/kubernetes/conf/kubeadm-init.sh
由于初始化時(shí),默認(rèn)會(huì)從k8s.gcr.io拉取鏡像,該鏡像被墻,這里從私有docker鏡像庫(kù)拉取,由--image-repository=192.168.100.10/kubernetes指定
私有docker鏡像庫(kù)搭建請(qǐng)參照我的另一篇:《Docker搭建私有鏡像倉(cāng)庫(kù)》
5.初始化Master節(jié)點(diǎn)
初始化時(shí)及搭建集群過程中所需要的所有鏡像我們都已托管到我們私有鏡像倉(cāng)庫(kù)中:
執(zhí)行/etc/kubernetes/conf/kubeadm-init.sh此時(shí)會(huì)初始化
注意:如果初始化過程出現(xiàn)問題,使用如下命令重置
kubeadm reset
rm -rf /var/lib/cni/ $HOME/.kube/config
初始化成功如下圖:
kubeadm join 192.168.100.11:6443
--token qjqxwd.2k79lzjvxc3t6hsj
--discovery-token-ca-cert-hash sha256:091e12d369cd83fda3187b5eddb1d07db074b220ff32cb0f21d8b82fc19d4ccb
上面這一句是給其他節(jié)點(diǎn)加入集群用的,要保存下來(lái),后面要用。
配置master上通過 kubectl 管理集群,執(zhí)行下面的命令:
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
使用kubectl get nodes -owide查看剛初始化的主節(jié)點(diǎn)信息:
我們看到master節(jié)點(diǎn)的狀態(tài)時(shí)未就緒狀態(tài),需要配置使用網(wǎng)絡(luò)flannel插件:
下載flannel配置文件:
wget -P /etc/kubernetes/conf
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
修改下載的flannel.yml文件,刪除多余部分,并指定網(wǎng)卡信息:
啟動(dòng)flannel組件:kubectl apply -f /etc/kubernetes/conf/kube-flannel.yml
6.加入各Node節(jié)
在每個(gè)節(jié)點(diǎn)的機(jī)器上執(zhí)行下面的加入集群的命令:
kubeadm join 192.168.100.11:6443
--token qjqxwd.2k79lzjvxc3t6hsj
--discovery-token-ca-cert-hash sha256:091e12d369cd83fda3187b5eddb1d07db074b220ff32cb0f21d8b82fc19d4ccb
7.部署Kubernetes Web
從kubernetes官方github下載配置文件:
wget /etc/kubernetes/conf https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
修改配置文件訪問類型為:NodePort
啟動(dòng)webui組件:kubectl apply -f /etc/kubernetes/conf/kubernetes-dashboard.yaml
查看dashboard pod的狀態(tài):kubectl get pods -n kube-system -owide
然后通過:https://192.168.100.11:31080/訪問
我們看到有兩種訪問方式,下面我們配置這兩種訪問方式:
我們創(chuàng)建dashboard用戶yaml文件:
vi /etc/kubernetes/conf/kubernetes-dashboard-admin.yaml
# Create Dashboard Service Account apiVersion: v1 kind: ServiceAccount metadata: name: dashboard-admin-user namespace: kube-system --- # Create ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: dashboard-admin-user roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: dashboard-admin-user namespace: kube-system
然后kubectl apply -f /etc/kubernetes/conf/kubernetes-dashboard-user.yaml
完成后執(zhí)行kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep dashboard-admin-user | awk '{print $1}') 查看token
獲取的token即可用來(lái)在頁(yè)面上輸入登錄:
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdXNlci10b2tlbi13NGg1biIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdXNlciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImNjNzE5NTMwLWY0NDAtNGNmYi1hNDkwLTZiZjkzNWNkMDYxOCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTpkYXNoYm9hcmQtYWRtaW4tdXNlciJ9.No8ic7iNedWgX9iX1cIPHrK81vDTApsayPHm2U5No65yndQX7l_GgE8Ze-oTqx1JQVN85EWdJGM4T_LdbxflkIiBElmKRST7m3VpAeLgNl1f56NyyUwf0GEVzsdZdhb0E-i1DJO0ofXkqQiHwtcpWMmq4wqlYymu_fDxblJl4LzTpPUV89emPSQhSA3ZopbNBKJr6u5kRYgsTiz8EGAhRRqWGzCyEkyp8Y_SJlYsnXXx-F8gmneBiyaqeCrNiO71eczz6sQDsEI37fhxs41xeLJNF7sA2LihAE9KD3YBBgRjxxMmfmsGb-IkTn68bhhsHiCN0AMW5xJFSXNiLyWGMQ
此時(shí)的dashboard沒有展示cpu、內(nèi)存等圖標(biāo)可視化信息,我們需要安裝heapster服務(wù)(因?yàn)閐ashboard要顯示圖表數(shù)據(jù)需要依賴heapster服務(wù))
下載 heapster 相關(guān) yaml 文件:
wget /etc/kubernetes/heapster https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/grafana.yaml
wget /etc/kubernetes/heapster https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/heapster.yaml
wget /etc/kubernetes/heapster https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
wget /etc/kubernetes/heapster https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
查看需要部署的鏡像:
cat grafana.yaml | grep image
cat heapster.yaml | grep image
cat influxdb.yaml | grep image
修改yaml文件:
因?yàn)閗8s高版本的api版本進(jìn)行了變化,將上面四個(gè)yaml文件中的apiVersion: extensions/v1beta1 改為apiVersion: apps/v1
因?yàn)閗ubelet 只在 10250 監(jiān)聽 https 請(qǐng)求,將heapster.yaml中的- --source=kubernetes:https://kubernetes.default 修改為:
- --source=kubernetes:https://kubernetes.default?kubeletHttps=true&kubeletPort=10250&insecure=true
修改上面四個(gè)yaml文件中的spec節(jié)點(diǎn),增加selector,如下圖:
然后在heapster配置文件的當(dāng)前目錄下執(zhí)行部署:kubectl apply -f .
注意:如果部署發(fā)生錯(cuò)誤,我們執(zhí)行kubectl delete -f . 進(jìn)行回退
Heapster各個(gè)組件部署成功如下:
然后我們生成使用config登錄的文件:
##將secret中的token使用base64方式進(jìn)行解碼,然后使用變量引用
DASH_TOCKEN=$(kubectl get secret -n kube-system dashboard-admin-token-q6pz8 -o jsonpath={.data.token}|base64 -d)
##創(chuàng)建一個(gè)集群
kubectl config set-cluster cluster-admin --server=192.168.100.11:6443 --kubeconfig=/etc/kubernetes/conf/dashbord-admin.conf
##創(chuàng)建一個(gè)集群用戶,并引用sa的token
kubectl config set-credentials dashboard-admin-user --token=$DASH_TOCKEN --kubeconfig=/etc/kubernetes/conf/dashbord-admin.conf
##創(chuàng)建一個(gè)上下文,指定集群名、集群用戶名
kubectl config set-context dashboard-admin-user@cluster-admin --cluster=cluster-admin --user=dashboard-admin-user
--kubeconfig=/etc/kubernetes/conf/dashbord-admin.conf
##設(shè)置集群中當(dāng)前使用的用戶
kubectl config use-context dashboard-admin-user@cluster-admin --kubeconfig=/etc/kubernetes/conf/dashbord-admin.conf
然后使用token或生成的文件登錄成功如下:
8.驗(yàn)證集群狀態(tài)
使用kubectl get nodes -n kube-system -owide 查看節(jié)點(diǎn)列表
使用kubectl get pods -n kube-system -owide查看pod列表
使用kubectl get svc -n kube-system -owide 查看服務(wù)列表
三、集群?jiǎn)栴}解決
1.初始化集群時(shí):/proc/sys/net/ipv4/ip_forward contents are not set to 1
問題描述:執(zhí)行kubeadm init時(shí)報(bào)出/proc/sys/net/ipv4/ip_forward contents are not set to 1的錯(cuò)誤
解決方案:sudo echo "1" > /proc/sys/net/ipv4/ip_forward
2.部署heapster提示版本問題
問題描述:部署heapster 組件提示no matches for king “Deployment” in version “extensions/v1beta1”
解決方案:是因?yàn)閗8s高版本的api版本進(jìn)行了變化,將對(duì)應(yīng)的yaml文件中的extensions/v1beta1 改為apiVersion: apps/v1
3.部署heapster提示selector錯(cuò)誤
問題描述:部署heapster 組件提示missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec的錯(cuò)誤
解決方案:修改heapster上面四個(gè)yaml文件中的spec節(jié)點(diǎn),增加selector,如下圖:
4.部署heapster后dashboard不顯示圖表信息
問題描述:heapster正常部署后,pods狀態(tài)都正常,但是dashboard不顯示圖表信息
查看pods的日志,發(fā)現(xiàn)一直提示“403 Forbidden”, response: "Forbidden (user=system:serviceaccount:kube-system:heapster, verb=create, resource=nodes, subresource=stats)"
解決方案:
查看ClusterRole: system:heapster的權(quán)限,發(fā)現(xiàn)的確沒有針對(duì)Resource: nodes/stats 的create權(quán)限
kubectl describe clusterrole system:heapster
添加Resource: nodes/stats的create權(quán)限,vi /etc/kubernetes/conf/heapster/heapster-role.yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: annotations: rbac.authorization.kubernetes.io/autoupdate: "true" labels: kubernetes.io/bootstrapping: rbac-defaults name: system:heapster rules: - apiGroups: - "" resources: - events - namespaces - nodes - pods - nodes/stats verbs: - create - get - list - watch - apiGroups: - extensions resources: - deployments verbs: - get - list - watch
cd /etc/kubernetes/conf/heapster
kubectl delete -f .
kubectl apply -f .
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。