Kubernetes(K8S)容器集群管理環(huán)境完整部署詳細(xì)教程-上篇
Kubernetes(通常稱為"K8S")是Google開源的容器集群管理系統(tǒng)。其設(shè)計(jì)目標(biāo)是在主機(jī)集群之間提供一個能夠自動化部署、可拓展、應(yīng)用容器可運(yùn)營的平臺。Kubernetes通常結(jié)合docker容器工具工作,并且整合多個運(yùn)行著docker容器的主機(jī)集群,Kubernetes不僅僅支持Docker,還支持Rocket,這是另一種容器技術(shù)。Kubernetes是一個用于容器集群的自動化部署、擴(kuò)容以及運(yùn)維的開源平臺。
本文系列:
Kubernetes(K8S)容器集群管理環(huán)境完整部署詳細(xì)教程-上篇
通過Kubernetes, 可以快速有效地響應(yīng)用戶需求:
-> 快速而有預(yù)期地部署應(yīng)用;
-> 極速地?cái)U(kuò)展你的應(yīng)用;
-> 無縫對接新的應(yīng)用功能;
-> 節(jié)省資源,優(yōu)化硬件資源的使用;
Kubernetes功能特性:
-> 自動化容器部署與復(fù)制
-> 隨時擴(kuò)展或收縮容器規(guī)模
-> 組織容器成組,提供容器間的負(fù)載均衡
-> 快速更新及回滾容器版本
-> 提供彈性伸縮,如果某個容器失效就進(jìn)行替換
Kubernetes重要組件:
1)Master組件
Master節(jié)點(diǎn)上面主要由四個模塊組成:APIServer、scheduler、controller manager、etcd
-> APIServer: 負(fù)責(zé)對外提供RESTful的Kubernetes API服務(wù),它是系統(tǒng)管理指令的統(tǒng)一入口,任何對資源進(jìn)行增刪改查的操作都要交給APIServer處理后再提交給etcd。kubectl(k8s提供的客戶端工具,該工具內(nèi)部就是對Kubernetes API的調(diào)用)是直接和APIServer交互的。
-> schedule: 它的職責(zé)很明確,就是負(fù)責(zé)調(diào)度pod到合適的Node上。如果把scheduler看成一個黑匣子,那么它的輸入是pod和由多個Node組成的列表,輸出是Pod和一個Node的綁定,即將這個pod部署到這個Node上。Kubernetes目前提供了調(diào)度算法,但是同樣也保留了接口,用戶可以根據(jù)自己的需求定義自己的調(diào)度算法。
-> controller manager: 如果說APIServer做的是“前臺”的工作的話,那controller manager就是負(fù)責(zé)“后臺”的。每個資源一般都對應(yīng)有一個控制器,而controller manager就是負(fù)責(zé)管理這些控制器的。比如我們通過APIServer創(chuàng)建一個pod,當(dāng)這個pod創(chuàng)建成功后,APIServer的任務(wù)就算完成了。而后面保證Pod的狀態(tài)始終和我們預(yù)期的一樣的重任就由controller manager去保證了。
-> etcd: 它是一個高可用的鍵值存儲系統(tǒng),Kubernetes使用它來存儲各個資源的狀態(tài),從而實(shí)現(xiàn)了Restful的API。
2)Node組件
每個Node節(jié)點(diǎn)主要由三個模塊組成:kubelet、kube-proxy、runtime。
runtime。runtime指的是容器運(yùn)行環(huán)境,目前Kubernetes支持docker和rkt兩種容器。
-> kubelet:Kubelet是Master在每個Node節(jié)點(diǎn)上面的agent,是Node節(jié)點(diǎn)上面最重要的模塊,它負(fù)責(zé)維護(hù)和管理該Node上面的所有容器,但是如果容器不是通過Kubernetes創(chuàng)建的,它并不會管理。本質(zhì)上,它負(fù)責(zé)使Pod得運(yùn)行狀態(tài)與期望的狀態(tài)一致。
-> kube-proxy:該模塊實(shí)現(xiàn)了Kubernetes中的服務(wù)發(fā)現(xiàn)和反向代理功能。反向代理方面:kube-proxy支持TCP和UDP連接轉(zhuǎn)發(fā),默認(rèn)基于Round Robin算法將客戶端流量轉(zhuǎn)發(fā)到與service對應(yīng)的一組后端pod。服務(wù)發(fā)現(xiàn)方面,kube-proxy使用etcd的watch機(jī)制,監(jiān)控集群中service和endpoint對象數(shù)據(jù)的動態(tài)變化,并且維護(hù)一個service到endpoint的映射關(guān)系,從而保證了后端pod的IP變化不會對訪問者造成影響。另外kube-proxy還支持session affinity。
3)Pod
Pod是k8s進(jìn)行資源調(diào)度的最小單位,每個Pod中運(yùn)行著一個或多個密切相關(guān)的業(yè)務(wù)容器,這些業(yè)務(wù)容器共享這個Pause容器的IP和Volume,我們以這個不易死亡的Pause容器作為Pod的根容器,以它的狀態(tài)表示整個容器組的狀態(tài)。一個Pod一旦被創(chuàng)建就會放到Etcd中存儲,然后由Master調(diào)度到一個Node綁定,由這個Node上的Kubelet進(jìn)行實(shí)例化。每個Pod會被分配一個單獨(dú)的Pod IP,Pod IP + ContainerPort 組成了一個Endpoint。
4)Service
Service其功能使應(yīng)用暴露,Pods 是有生命周期的,也有獨(dú)立的 IP 地址,隨著 Pods 的創(chuàng)建與銷毀,一個必不可少的工作就是保證各個應(yīng)用能夠感知這種變化。這就要提到 Service 了,Service 是 YAML 或 JSON 定義的由 Pods 通過某種策略的邏輯組合。更重要的是,Pods 的獨(dú)立 IP 需要通過 Service 暴露到網(wǎng)絡(luò)中。
K8s集群可以幫助培育出一個組件及工具的生態(tài),幫助減輕在公有云及私有云上運(yùn)行應(yīng)用的負(fù)擔(dān)。
搭建Kubernetes集群環(huán)境有以下三種方式:
1. Minikube安裝方式
Minikube是一個工具,可以在本地快速運(yùn)行一個單點(diǎn)的Kubernetes,嘗試Kubernetes或日常開發(fā)的用戶使用。但是這種方式僅可用于學(xué)習(xí)和測試部署,不能用于生產(chǎn)環(huán)境。
2. Kubeadm安裝方式
kubeadm是一個kubernetes官方提供的快速安裝和初始化擁有最佳實(shí)踐(best practice)的kubernetes集群的工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。目前kubeadm還處于beta 和alpha狀態(tài),不推薦用在生產(chǎn)環(huán)境,但是可以通過學(xué)習(xí)這種部署方法來體會一些官方推薦的kubernetes最佳實(shí)踐的設(shè)計(jì)和思想。
kubeadm的目標(biāo)是提供一個最小可用的可以通過Kubernetes一致性測試的集群,所以并不會安裝任何除此之外的非必須的addon。kubeadm默認(rèn)情況下并不會安裝一個網(wǎng)絡(luò)解決方案,所以用kubeadm安裝完之后,需要自己來安裝一個網(wǎng)絡(luò)的插件。所以說,目前的kubeadm是不能用于生產(chǎn)環(huán)境的
3. 二進(jìn)制包安裝方式(生產(chǎn)部署的推薦方式)
從官方下載發(fā)行版的二進(jìn)制包,手動部署每個組件,組成Kubernetes集群,這種方式符合企業(yè)生產(chǎn)環(huán)境標(biāo)準(zhǔn)的Kubernetes集群環(huán)境的安裝,可用于生產(chǎn)方式部署。
一、基礎(chǔ)信息
使用Kubernetes1.14.2,所有節(jié)點(diǎn)機(jī)操作系統(tǒng)是Centos7.5。本文檔部署中所需kubernetes相關(guān)安裝包和鏡像可提前在FQ服務(wù)器上下載,然后同步到k8s部署機(jī)器上。具體信息如下:
ip地址 | 主機(jī)名 | 角色 |
172.16.60.241 | k8s-master01 | 主節(jié)點(diǎn)1、etc節(jié)點(diǎn)1 |
172.16.60.242 | k8s-master02 | 主節(jié)點(diǎn)2、etc節(jié)點(diǎn)2 |
172.16.60.243 | k8s-master03 | 主節(jié)點(diǎn)3、etc節(jié)點(diǎn)3 |
172.16.60.244 | k8s-node01 | 工作節(jié)點(diǎn)1 |
172.16.60.245 | k8s-node02 | 工作節(jié)點(diǎn)2 |
172.16.60.246 | k8s-node03 | 工作節(jié)點(diǎn)3 |
172.16.60.247 | k8s-ha01 | nginx節(jié)點(diǎn)1、harbor節(jié)點(diǎn)1 |
172.16.60.248 | k8s-ha02 | nginx節(jié)點(diǎn)2、harbor節(jié)點(diǎn)2 |
本套Kubernetes集群環(huán)境版本
- Kubernetes 1.14.2
- Docker 18.09.6-ce
- Etcd 3.3.13
- Flanneld 0.11.0
插件:
- Coredns
- Dashboard
- Metrics-server
鏡像倉庫:
- harbor(兩個倉庫相互同步,對外提供統(tǒng)一入口VIP地址)
主要配置策略
kube-apiserver高可用(Nginx負(fù)載層):
- 使用Nginx+Keepalived實(shí)現(xiàn)高可用, VIP1:172.16.60.250;
- 關(guān)閉非安全端口 8080 和匿名訪問;
- 在安全端口 6443 接收 https 請求;
- 嚴(yán)格的認(rèn)證和授權(quán)策略 (x509、token、RBAC);
- 開啟 bootstrap token 認(rèn)證,支持 kubelet TLS bootstrapping;
- 使用 https 訪問 kubelet、etcd,加密通信;
kube-controller-manager高可用:
- 3節(jié)點(diǎn)高可用;
- 關(guān)閉非安全端口,在安全端口 10252 接收 https 請求;
- 使用 kubeconfig 訪問 apiserver 的安全端口;
- 自動 approve kubelet 證書簽名請求 (CSR),證書過期后自動輪轉(zhuǎn);
- 各controller 使用自己的 ServiceAccount 訪問 apiserver;
kube-scheduler高可用:
- 3節(jié)點(diǎn)高可用;
- 使用 kubeconfig 訪問 apiserver 的安全端口;
kubelet:
- 使用 kubeadm 動態(tài)創(chuàng)建 bootstrap token,而不是在 apiserver 中靜態(tài)配置;
- 使用TLS bootstrap機(jī)制自動生成 client 和 server 證書,過期后自動輪轉(zhuǎn);
- 在 kubeletConfiguration 類型的 JSON 文件配置主要參數(shù);
- 關(guān)閉只讀端口,在安全端口 10250 接收 https 請求,對請求進(jìn)行認(rèn)證和授權(quán),拒絕匿名訪問和非授權(quán)訪問;
- 使用 kubeconfig 訪問 apiserver 的安全端口;
kube-proxy:
- 使用kubeconfig 訪問 apiserver 的安全端口;
- 在KubeProxyConfiguration 類型的 JSON 文件配置主要參數(shù);
- 使用ipvs代理模式;
集群插件:
- DNS:使用功能、性能更好的 coredns;
- Dashboard:支持登錄認(rèn)證;
- Metric:metrics-server,使用 https 訪問 kubelet 安全端口;
- Log:Elasticsearch、Fluend、Kibana;
- Registry 鏡像庫:Harbor私有倉庫,兩個節(jié)點(diǎn)相互同步;
kubernetes集群部署中生成的證書文件如下:
ca-key.pem 根私鑰(controller-manager配置的時候,跟上--service-account-private-key-file)
ca.pem 根證書(apiserver配置的時候,跟上--service-account-key-file)
kubernetes-key.pem 集群私鑰
kubernetes.pem 集群證書
kube-proxy.pem proxy證書-node節(jié)點(diǎn)進(jìn)行認(rèn)證
kube-proxy-key.pem proxy私鑰-node節(jié)點(diǎn)進(jìn)行認(rèn)證
admin.pem 管理員證書-主要用于kubectl認(rèn)證
admin-key.pem 管理員私鑰-主要用于kubectl認(rèn)證
TLS作用:
就是對通訊加密,防止中間人竊聽;同時如果證書不信任的話根本就無法與 apiserver 建立連接,更不用提有沒有權(quán)限向 apiserver 請求指定內(nèi)容。
RBAC作用:
RBAC 中規(guī)定了一個用戶或者用戶組(subject)具有請求哪些 api 的權(quán)限;在配合 TLS 加密的時候,實(shí)際上 apiserver 讀取客戶端證書的 CN 字段作為用戶名,讀取 O 字段作為用戶組。
總之想要與apiserver通訊就必須采用由apiserver CA簽發(fā)的證書,這樣才能形成信任關(guān)系,建立TLS連接;另外可通過證書的CN、O字段來提供RBAC所需用戶與用戶組。
kubernetes集群會默認(rèn)開啟RABC(角色訪問控制機(jī)制),這里提前了解幾個重要概念:
- DRBC
K8S 1.6引進(jìn),是讓用戶能夠訪問K8S API資源的授權(quán)方式(不授權(quán)就沒有資格訪問K8S的資源)
- 用戶
K8S有兩種用戶:User 和 Service Account。其中,User給用戶使用,Service Account給進(jìn)程使用,讓進(jìn)程有相關(guān)權(quán)限。如Dashboard就是一個進(jìn)程,可以創(chuàng)建一個Service Account給它使用。
- 角色
Role是一系列權(quán)限的集合,例如一個Role可包含讀取和列出Pod的權(quán)限(ClusterRole和Role類似,其權(quán)限范圍是整個集群)
- 角色綁定
RoleBinding把角色映射到用戶,從而讓這些用戶擁有該角色的權(quán)限(ClusterRoleBinding和RoleBinding類似,可讓用戶擁有ClusteRole的權(quán)限)
- Secret
Secret是一個包含少量敏感信息如密碼,令牌或密鑰的對象。把這些信息保存在Secret對象中,可以在這些信息被使用時加以控制,并可以減低信息泄露的風(fēng)險。
二、環(huán)境初始化準(zhǔn)備
Kubernetes集群部署過程均需要使用root賬號操作,下面初始化操作在k8s的master和node節(jié)點(diǎn)上操作。
這里先以k8s-master01節(jié)點(diǎn)為例,其他節(jié)點(diǎn)類似操作。 1)主機(jī)名修改 [root@k8s-master01 ~]# hostnamectl set-hostname k8s-master01 如果DNS不支持解析主機(jī)名稱,則需要修改/etc/hosts文件,添加主機(jī)名和IP的對應(yīng)關(guān)系: [root@k8s-master01 ~]# cat >> /etc/hosts <<EOF 172.16.60.241 k8s-master01 172.16.60.242 k8s-master02 172.16.60.243 k8s-master03 172.16.60.241 k8s-etcd01 172.16.60.242 k8s-etcd02 172.16.60.243 k8s-etcd03 172.16.60.244 k8s-node01 172.16.60.245 k8s-node02 172.16.60.246 k8s-node03 EOF 2) 添加docker賬戶 [root@k8s-master01 ~]# useradd -m docker 3) 無密碼ssh信任關(guān)系 本篇部署文檔有很有操作都是在k8s-master01節(jié)點(diǎn)上執(zhí)行,然后遠(yuǎn)程分發(fā)文件到其他節(jié)點(diǎn)機(jī)器上并遠(yuǎn)程執(zhí)行命令,所以需要添加該節(jié)點(diǎn)到其它節(jié)點(diǎn)的ssh信任關(guān)系。 [root@k8s-master01 ~]# ssh-keygen -t rsa [root@k8s-master01 ~]# cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-master01 [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-master02 [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-master03 [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-node01 [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-node02 [root@k8s-master01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@k8s-node03 以上信任關(guān)系設(shè)置后,最好手動驗(yàn)證下本節(jié)點(diǎn)登陸到其他節(jié)點(diǎn)的ssh無密碼信任關(guān)系 4) 更新PATH變量,將可執(zhí)行文件目錄添加到PATH環(huán)境變量中 將可執(zhí)行文件目錄添加到PATH環(huán)境變量中 [root@k8s-master01 ~]# echo 'PATH=/opt/k8s/bin:$PATH' >>/root/.bashrc [root@k8s-master01 ~]# source /root/.bashrc 5) 安裝依賴包 [root@k8s-master01 ~]# yum install -y epel-release [root@k8s-master01 ~]# yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget lsof telnet 關(guān)閉無關(guān)的服務(wù) [root@k8s-master01 ~]# systemctl stop postfix && systemctl disable postfix 6)關(guān)閉防火墻 在每臺機(jī)器上關(guān)閉防火墻,清理防火墻規(guī)則,設(shè)置默認(rèn)轉(zhuǎn)發(fā)策略: [root@k8s-master01 ~]# systemctl stop firewalld [root@k8s-master01 ~]# systemctl disable firewalld [root@k8s-master01 ~]# iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat [root@k8s-master01 ~]# iptables -P FORWARD ACCEPT [root@k8s-master01 ~]# firewall-cmd --state not running 7) 關(guān)閉SELinux 關(guān)閉SELinux,否則后續(xù)K8S掛載目錄時可能報(bào)錯 Permission denied: [root@k8s-master01 ~]# setenforce 0 [root@k8s-master01 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config 8) 關(guān)閉swap分區(qū) 如果開啟了swap分區(qū),kubelet會啟動失敗(可以通過將參數(shù) --fail-swap-on 設(shè)置為false來忽略swap on),故需要在每個node節(jié)點(diǎn)機(jī)器上關(guān)閉swap分區(qū)。 這里索性將所有節(jié)點(diǎn)的swap分區(qū)都關(guān)閉,同時注釋/etc/fstab中相應(yīng)的條目,防止開機(jī)自動掛載swap分區(qū): [root@k8s-master01 ~]# swapoff -a [root@k8s-master01 ~]# sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab 9) 關(guān)閉dnsmasq linux系統(tǒng)開啟了dnsmasq后(如 GUI 環(huán)境),將系統(tǒng)DNS Server設(shè)置為 127.0.0.1,這會導(dǎo)致docker容器無法解析域名,需要關(guān)閉它 (centos7系統(tǒng)可能默認(rèn)沒有安裝這個服務(wù)) [root@k8s-node01 ~]# systemctl stop dnsmasq [root@k8s-node01 ~]# systemctl disable dnsmasq 10)加載內(nèi)核模塊 [root@k8s-master01 ~]# modprobe ip_vs_rr [root@k8s-master01 ~]# modprobe br_netfilter 11)優(yōu)化內(nèi)核參數(shù) [root@k8s-master01 ~]# cat > kubernetes.conf <<EOF net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-ip6tables=1 net.ipv4.ip_forward=1 net.ipv4.tcp_tw_recycle=0 #由于tcp_tw_recycle與kubernetes的NAT沖突,必須關(guān)閉!否則會導(dǎo)致服務(wù)不通。 vm.swappiness=0 #禁止使用 swap 空間,只有當(dāng)系統(tǒng) OOM 時才允許使用它 vm.overcommit_memory=1 #不檢查物理內(nèi)存是否夠用 vm.panic_on_oom=0 #開啟 OOM fs.inotify.max_user_instances=8192 fs.inotify.max_user_watches=1048576 fs.file-max=52706963 fs.nr_open=52706963 net.ipv6.conf.all.disable_ipv6=1 #關(guān)閉不使用的ipv6協(xié)議棧,防止觸發(fā)docker BUG. net.netfilter.nf_conntrack_max=2310720 EOF [root@k8s-master01 ~]# cp kubernetes.conf /etc/sysctl.d/kubernetes.conf [root@k8s-master01 ~]# sysctl -p /etc/sysctl.d/kubernetes.conf 這里需要注意: 必須關(guān)閉 tcp_tw_recycle,否則和 NAT 沖突,會導(dǎo)致服務(wù)不通; 關(guān)閉 IPV6,防止觸發(fā) docker BUG; 12)設(shè)置系統(tǒng)時區(qū) # 調(diào)整系統(tǒng) TimeZone [root@k8s-master01 ~]# timedatectl set-timezone Asia/Shanghai # 將當(dāng)前的 UTC 時間寫入硬件時鐘 [root@k8s-master01 ~]# timedatectl set-local-rtc 0 # 重啟依賴于系統(tǒng)時間的服務(wù) [root@k8s-master01 ~]# systemctl restart rsyslog [root@k8s-master01 ~]# systemctl restart crond 13)設(shè)置rsyslogd 和systemd journald (每臺節(jié)點(diǎn)機(jī)都要操作) systemd 的 journald 是 Centos 7 缺省的日志記錄工具,它記錄了所有系統(tǒng)、內(nèi)核、Service Unit 的日志。相比 systemd,journald 記錄的日志有如下優(yōu)勢: -> 可以記錄到內(nèi)存或文件系統(tǒng);(默認(rèn)記錄到內(nèi)存,對應(yīng)的位置為 /run/log/jounal); -> 可以限制占用的磁盤空間、保證磁盤剩余空間; -> 可以限制日志文件大小、保存的時間; -> journald 默認(rèn)將日志轉(zhuǎn)發(fā)給 rsyslog,這會導(dǎo)致日志寫了多份,/var/log/messages 中包含了太多無關(guān)日志,不方便后續(xù)查看,同時也影響系統(tǒng)性能。 [root@k8s-master01 ~]# mkdir /var/log/journal #持久化保存日志的目錄 [root@k8s-master01 ~]# mkdir /etc/systemd/journald.conf.d [root@k8s-master01 ~]# cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF [Journal] # 持久化保存到磁盤 Storage=persistent # 壓縮歷史日志 Compress=yes SyncIntervalSec=5m RateLimitInterval=30s RateLimitBurst=1000 # 最大占用空間 10G SystemMaxUse=10G # 單日志文件最大 200M SystemMaxFileSize=200M # 日志保存時間 2 周 MaxRetentionSec=2week # 不將日志轉(zhuǎn)發(fā)到 syslog ForwardToSyslog=no EOF [root@k8s-master01 ~]# systemctl restart systemd-journald 14) 創(chuàng)建k8s相關(guān)目錄 (每臺節(jié)點(diǎn)機(jī)都要操作) [root@k8s-master01 ~]# mkdir -p /opt/k8s/{bin,work} /etc/{kubernetes,etcd}/cert 15) 升級內(nèi)核 (每臺節(jié)點(diǎn)機(jī)都要操作) CentOS 7.x系統(tǒng)自帶的3.10.x內(nèi)核存在一些Bugs,導(dǎo)致運(yùn)行的Docker、Kubernetes不穩(wěn)定,例如: -> 高版本的 docker(1.13 以后) 啟用了3.10 kernel實(shí)驗(yàn)支持的kernel memory account功能(無法關(guān)閉),當(dāng)節(jié)點(diǎn)壓力大如頻繁啟動和停止容器時會導(dǎo)致 cgroup memory leak; -> 網(wǎng)絡(luò)設(shè)備引用計(jì)數(shù)泄漏,會導(dǎo)致類似于報(bào)錯:"kernel:unregister_netdevice: waiting for eth0 to become free. Usage count = 1"; 解決方案如下: -> 升級內(nèi)核到 4.4.X 以上; -> 或者,手動編譯內(nèi)核,disable CONFIG_MEMCG_KMEM 特性; -> 或者安裝修復(fù)了該問題的 Docker 18.09.1 及以上的版本。但由于 kubelet 也會設(shè)置 kmem(它 vendor 了 runc),所以需要重新編譯 kubelet 并指定 GOFLAGS="-tags=nokmem"; 這里升級內(nèi)核方法: [root@k8s-master01 ~]# uname -r 3.10.0-862.el7.x86_64 [root@k8s-master01 ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 安裝完成后檢查 /boot/grub2/grub.cfg 中對應(yīng)內(nèi)核 menuentry 中是否包含 initrd16 配置,如果沒有,再安裝一次! [root@k8s-master01 ~]# yum --enablerepo=elrepo-kernel install -y kernel-lt 設(shè)置開機(jī)從新內(nèi)核啟動 [root@k8s-master01 ~]# grub2-set-default 0 重啟機(jī)器 [root@k8s-master01 ~]# init 6 安裝內(nèi)核源文件(在升級完內(nèi)核并重啟機(jī)器后執(zhí)行,也可以不用執(zhí)行這一步。可選): [root@k8s-master01 ~]# yum --enablerepo=elrepo-kernel install kernel-lt-devel-$(uname -r) kernel-lt-headers-$(uname -r) [root@k8s-master01 ~]# uname -r 4.4.180-2.el7.elrepo.x86_64 ==================================================================================================================================== 或者也可以采用下面升級內(nèi)核的方法: # git clone --branch v1.14.1 --single-branch --depth 1 https://github.com/kubernetes/kubernetes # cd kubernetes # KUBE_GIT_VERSION=v1.14.1 ./build/run.sh make kubelet GOFLAGS="-tags=nokmem" # init 6 ==================================================================================================================================== 16) 關(guān)閉NUMA [root@k8s-master01 ~]# cp /etc/default/grub{,.bak} [root@k8s-master01 ~]# vim /etc/default/grub ......... GRUB_CMDLINE_LINUX="...... numa=off" # 即添加"numa=0ff"內(nèi)容 重新生成 grub2 配置文件: # cp /boot/grub2/grub.cfg{,.bak} # grub2-mkconfig -o /boot/grub2/grub.cfg 17) 變量腳本文件 (這一步很關(guān)鍵) [root@k8s-master01 ~]# vim /opt/k8s/bin/environment.sh #!/usr/bin/bash # 生成 EncryptionConfig 所需的加密 key export ENCRYPTION_KEY=$(head -c 32 /dev/urandom | base64) # 集群中所有節(jié)點(diǎn)機(jī)器IP數(shù)組(master,node,etcd節(jié)點(diǎn)) export NODE_ALL_IPS=(172.16.60.241 172.16.60.242 172.16.60.243 172.16.60.244 172.16.60.245 172.16.60.246) # 集群中所有節(jié)點(diǎn)IP對應(yīng)的主機(jī)名數(shù)組 export NODE_ALL_NAMES=(k8s-master01 k8s-master02 k8s-master03 k8s-node01 k8s-node02 k8s-node03) # 集群中所有master節(jié)點(diǎn)集群IP數(shù)組 export NODE_MASTER_IPS=(172.16.60.241 172.16.60.242 172.16.60.243) # 集群中master節(jié)點(diǎn)IP對應(yīng)的主機(jī)名數(shù)組 export NODE_MASTER_NAMES=(k8s-master01 k8s-master02 k8s-master03) # 集群中所有node節(jié)點(diǎn)集群IP數(shù)組 export NODE_NODE_IPS=(172.16.60.244 172.16.60.245 172.16.60.246) # 集群中node節(jié)點(diǎn)IP對應(yīng)的主機(jī)名數(shù)組 export NODE_NODE_NAMES=(k8s-node01 k8s-node02 k8s-node03) # 集群中所有etcd節(jié)點(diǎn)集群IP數(shù)組 export NODE_ETCD_IPS=(172.16.60.241 172.16.60.242 172.16.60.243) # 集群中etcd節(jié)點(diǎn)IP對應(yīng)的主機(jī)名數(shù)組(這里是和master三節(jié)點(diǎn)機(jī)器共用) export NODE_ETCD_NAMES=(k8s-etcd01 k8s-etcd02 k8s-etcd03) # etcd 集群服務(wù)地址列表 export ETCD_ENDPOINTS="https://172.16.60.241:2379,https://172.16.60.242:2379,https://172.16.60.243:2379" # etcd 集群間通信的 IP 和端口 export ETCD_NODES="k8s-etcd01=https://172.16.60.241:2380,k8s-etcd02=https://172.16.60.242:2380,k8s-etcd03=https://172.16.60.243:2380" # kube-apiserver 的反向代理(地址端口.這里也就是nginx代理層的VIP地址 export KUBE_APISERVER="https://172.16.60.250:8443" # 節(jié)點(diǎn)間互聯(lián)網(wǎng)絡(luò)接口名稱. 這里我所有的centos7節(jié)點(diǎn)機(jī)的網(wǎng)卡設(shè)備是ens192,而不是eth0 export IFACE="ens192" # etcd 數(shù)據(jù)目錄 export ETCD_DATA_DIR="/data/k8s/etcd/data" # etcd WAL 目錄,建議是 SSD 磁盤分區(qū),或者和 ETCD_DATA_DIR 不同的磁盤分區(qū) export ETCD_WAL_DIR="/data/k8s/etcd/wal" # k8s 各組件數(shù)據(jù)目錄 export K8S_DIR="/data/k8s/k8s" # docker 數(shù)據(jù)目錄 export DOCKER_DIR="/data/k8s/docker" ## 以下參數(shù)一般不需要修改 # TLS Bootstrapping 使用的 Token,可以使用命令 head -c 16 /dev/urandom | od -An -t x | tr -d ' ' 生成 BOOTSTRAP_TOKEN="41f7e4ba8b7be874fcff18bf5cf41a7c" # 最好使用 當(dāng)前未用的網(wǎng)段 來定義服務(wù)網(wǎng)段和 Pod 網(wǎng)段 # 服務(wù)網(wǎng)段,部署前路由不可達(dá),部署后集群內(nèi)路由可達(dá)(kube-proxy 保證) SERVICE_CIDR="10.254.0.0/16" # Pod 網(wǎng)段,建議 /16 段地址,部署前路由不可達(dá),部署后集群內(nèi)路由可達(dá)(flanneld 保證) CLUSTER_CIDR="172.30.0.0/16" # 服務(wù)端口范圍 (NodePort Range) export NODE_PORT_RANGE="30000-32767" # flanneld 網(wǎng)絡(luò)配置前綴 export FLANNEL_ETCD_PREFIX="/kubernetes/network" # kubernetes 服務(wù) IP (一般是 SERVICE_CIDR 中第一個IP) export CLUSTER_KUBERNETES_SVC_IP="10.254.0.1" # 集群 DNS 服務(wù) IP (從 SERVICE_CIDR 中預(yù)分配) export CLUSTER_DNS_SVC_IP="10.254.0.2" # 集群 DNS 域名(末尾不帶點(diǎn)號) export CLUSTER_DNS_DOMAIN="cluster.local" # 將二進(jìn)制目錄 /opt/k8s/bin 加到 PATH 中 export PATH=/opt/k8s/bin:$PATH
三、創(chuàng)建集群中需要的CA證書和秘鑰
為確保安全,kubernetes 系統(tǒng)各組件需要使用 x509 證書對通信進(jìn)行加密和認(rèn)證。CA (Certificate Authority) 是自簽名的根證書,用來簽名后續(xù)創(chuàng)建的其它證書。這里使用 CloudFlare 的 PKI 工具集 cfssl 創(chuàng)建所有證書。下面部署命令均在k8s-master01節(jié)點(diǎn)上執(zhí)行,然后遠(yuǎn)程分發(fā)文件和執(zhí)行命令。
1)安裝cfssl工具集 [root@k8s-master01 ~]# mkdir -p /opt/k8s/work && cd /opt/k8s/work [root@k8s-master01 work]# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 [root@k8s-master01 work]# mv cfssl_linux-amd64 /opt/k8s/bin/cfssl [root@k8s-master01 work]# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 [root@k8s-master01 work]# mv cfssljson_linux-amd64 /opt/k8s/bin/cfssljson [root@k8s-master01 work]# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 [root@k8s-master01 work]# mv cfssl-certinfo_linux-amd64 /opt/k8s/bin/cfssl-certinfo [root@k8s-master01 work]# chmod +x /opt/k8s/bin/* [root@k8s-master01 work]# export PATH=/opt/k8s/bin:$PATH 2)創(chuàng)建根證書 (CA) CA 證書是集群所有節(jié)點(diǎn)共享的,只需要創(chuàng)建一個 CA 證書,后續(xù)創(chuàng)建的所有證書都由它簽名。 2.1)創(chuàng)建配置文件 CA 配置文件用于配置根證書的使用場景 (profile) 和具體參數(shù) (usage,過期時間、服務(wù)端認(rèn)證、客戶端認(rèn)證、加密等),后續(xù)在簽名其它證書時需要指定特定場景。 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cat > ca-config.json <<EOF { "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "usages": [ "signing", "key encipherment", "server auth", "client auth" ], "expiry": "87600h" } } } } EOF 配置說明: signing:表示該證書可用于簽名其它證書,生成的 ca.pem 證書中 CA=TRUE; server auth:表示 client 可以用該該證書對 server 提供的證書進(jìn)行驗(yàn)證; client auth:表示 server 可以用該該證書對 client 提供的證書進(jìn)行驗(yàn)證; 2.2)創(chuàng)建證書簽名請求文件 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cat > ca-csr.json <<EOF { "CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "4Paradigm" } ] } EOF 配置說明: CN:Common Name,kube-apiserver 從證書中提取該字段作為請求的用戶名 (User Name),瀏覽器使用該字段驗(yàn)證網(wǎng)站是否合法; O:Organization,kube-apiserver 從證書中提取該字段作為請求用戶所屬的組 (Group); kube-apiserver 將提取的 User、Group 作為 RBAC 授權(quán)的用戶標(biāo)識; 2.3)生成 CA 證書和私鑰 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca [root@k8s-master01 work]# ls ca* ca-config.json ca.csr ca-csr.json ca-key.pem ca.pem [root@k8s-master01 work]# 3)分發(fā)證書文件 將生成的 CA 證書、秘鑰文件、配置文件拷貝到所有節(jié)點(diǎn)的 /etc/kubernetes/cert 目錄下: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh root@${node_all_ip} "mkdir -p /etc/kubernetes/cert" scp ca*.pem ca-config.json root@${node_all_ip}:/etc/kubernetes/cert done
四、部署kubectl命令行工具
kubectl 是 kubernetes 集群的命令行管理工具. kubectl 默認(rèn)從 ~/.kube/config 文件讀取kube-apiserver地址和認(rèn)證信息,如果沒有配置,執(zhí)行kubectl命令時就會報(bào)錯!kubectl只需要部署一次,生成的kubeconfig文件是通用的,可以拷貝到需要執(zhí)行kubectl命令的節(jié)點(diǎn)機(jī)器,重命名為 ~/.kube/config;這里我將kubectl節(jié)點(diǎn)只部署到三個master節(jié)點(diǎn)機(jī)器上,其他節(jié)點(diǎn)不部署kubectl命令。也就是說后續(xù)進(jìn)行kubectl命令管理就只能在master節(jié)點(diǎn)上操作。下面部署命令均在k8s-master01節(jié)點(diǎn)上執(zhí)行,然后遠(yuǎn)程分發(fā)文件和執(zhí)行命令。
如果沒有部署kubectl工具,則執(zhí)行時會報(bào)錯說沒有該命令: [root@k8s-master01 ~]# kubectl get pods -bash: kubectl: command not found 1)下載和分發(fā)kubectl二進(jìn)制文件 二進(jìn)制包下載地址:https://pan.baidu.com/s/1HUWFqKVLyxIzoX2LDQSEBg 提取密碼:7kaf [root@k8s-master01 ~]# cd /opt/k8s/work [root@k8s-master01 work]# wget https://dl.k8s.io/v1.14.2/kubernetes-client-linux-amd64.tar.gz [root@k8s-master01 work]# tar -xzvf kubernetes-client-linux-amd64.tar.gz 分發(fā)到所有使用kubectl的節(jié)點(diǎn),這里只分發(fā)到三個master節(jié)點(diǎn) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_master_ip in ${NODE_MASTER_IPS[@]} do echo ">>> ${node_master_ip}" scp kubernetes/client/bin/kubectl root@${node_master_ip}:/opt/k8s/bin/ ssh root@${node_master_ip} "chmod +x /opt/k8s/bin/*" done 2) 創(chuàng)建admin證書和私鑰 kubectl與apiserver https安全端口通信,apiserver 對提供的證書進(jìn)行認(rèn)證和授權(quán)。 kubectl作為集群的管理工具,需要被授予最高權(quán)限,這里創(chuàng)建具有最高權(quán)限的 admin 證書。 創(chuàng)建證書簽名請求: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cat > admin-csr.json <<EOF { "CN": "admin", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "system:masters", "OU": "4Paradigm" } ] } EOF 配置說明: O為system:masters,kube-apiserver 收到該證書后將請求的 Group 設(shè)置為 system:masters; 預(yù)定義的 ClusterRoleBinding cluster-admin 將Group system:masters 與 Role cluster-admin 綁定,該 Role 授予所有 API的權(quán)限; 該證書只會被kubectl當(dāng)做client證書使用,所以hosts字段為空; 生成證書和私鑰: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem \ -ca-key=/opt/k8s/work/ca-key.pem \ -config=/opt/k8s/work/ca-config.json \ -profile=kubernetes admin-csr.json | cfssljson -bare admin [root@k8s-master01 work]# ls admin* admin.csr admin-csr.json admin-key.pem admin.pem 3)創(chuàng)建 kubeconfig 文件 kubeconfig 為 kubectl 的配置文件,包含訪問 apiserver 的所有信息,如 apiserver 地址、CA 證書和自身使用的證書; [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh 設(shè)置集群參數(shù) [root@k8s-master01 work]# kubectl config set-cluster kubernetes \ --certificate-authority=/opt/k8s/work/ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=kubectl.kubeconfig 設(shè)置客戶端認(rèn)證參數(shù) [root@k8s-master01 work]# kubectl config set-credentials admin \ --client-certificate=/opt/k8s/work/admin.pem \ --client-key=/opt/k8s/work/admin-key.pem \ --embed-certs=true \ --kubeconfig=kubectl.kubeconfig 設(shè)置上下文參數(shù) [root@k8s-master01 work]# kubectl config set-context kubernetes \ --cluster=kubernetes \ --user=admin \ --kubeconfig=kubectl.kubeconfig 設(shè)置默認(rèn)上下文 [root@k8s-master01 work]# kubectl config use-context kubernetes --kubeconfig=kubectl.kubeconfig 配置說明: --certificate-authority:驗(yàn)證 kube-apiserver 證書的根證書; --client-certificate、--client-key:剛生成的 admin 證書和私鑰,連接 kube-apiserver 時使用; --embed-certs=true:將 ca.pem 和 admin.pem 證書內(nèi)容嵌入到生成的 kubectl.kubeconfig 文件中(不加時,寫入的是證書文件路徑, 后續(xù)拷貝 kubeconfig 到其它機(jī)器時,還需要單獨(dú)拷貝證書文件,這就很不方便了) 4)分發(fā) kubeconfig 文件, 保存的文件名為 ~/.kube/config; 分發(fā)到所有使用 kubectl 命令的節(jié)點(diǎn),即分發(fā)到三個master節(jié)點(diǎn)上 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_master_ip in ${NODE_MASTER_IPS[@]} do echo ">>> ${node_master_ip}" ssh root@${node_master_ip} "mkdir -p ~/.kube" scp kubectl.kubeconfig root@${node_master_ip}:~/.kube/config done
五、部署etcd集群
etcd是基于Raft的分布式key-value存儲系統(tǒng),由CoreOS開發(fā),常用于服務(wù)發(fā)現(xiàn)、共享配置以及并發(fā)控制(如leader選舉、分布式鎖等)。kubernetes使用etcd存儲所有運(yùn)行數(shù)據(jù)。需要注意的是:由于etcd是負(fù)責(zé)存儲,所以不建議搭建單點(diǎn)集群,如zookeeper一樣,由于存在選舉策略,所以一般推薦奇數(shù)個集群,如3,5,7。只要集群半數(shù)以上的結(jié)點(diǎn)存活,那么集群就可以正常運(yùn)行,否則集群可能無法正常使用。下面部署命令均在k8s-master01節(jié)點(diǎn)上執(zhí)行,然后遠(yuǎn)程分發(fā)文件和執(zhí)行命令。
1)下載和分發(fā)etcd二進(jìn)制文件 [root@k8s-master01 ~]# cd /opt/k8s/work [root@k8s-master01 work]# wget https://github.com/coreos/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz [root@k8s-master01 work]# tar -xvf etcd-v3.3.13-linux-amd64.tar.gz 分發(fā)二進(jìn)制文件到etcd集群所有節(jié)點(diǎn): [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" scp etcd-v3.3.13-linux-amd64/etcd* root@${node_etcd_ip}:/opt/k8s/bin ssh root@${node_etcd_ip} "chmod +x /opt/k8s/bin/*" done 2) 創(chuàng)建etcd證書和私鑰 創(chuàng)建證書簽名請求: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cat > etcd-csr.json <<EOF { "CN": "etcd", "hosts": [ "127.0.0.1", "172.16.60.241", "172.16.60.242", "172.16.60.243" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "4Paradigm" } ] } EOF 配置說明: hosts 字段指定授權(quán)使用該證書的 etcd 節(jié)點(diǎn) IP 或域名列表,需要將 etcd 集群的三個節(jié)點(diǎn) IP 都列在其中; 生成證書和私鑰 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem \ -ca-key=/opt/k8s/work/ca-key.pem \ -config=/opt/k8s/work/ca-config.json \ -profile=kubernetes etcd-csr.json | cfssljson -bare etcd [root@k8s-master01 work]# ls etcd*pem etcd-key.pem etcd.pem 分發(fā)生成的證書和私鑰到各etcd節(jié)點(diǎn) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" ssh root@${node_etcd_ip} "mkdir -p /etc/etcd/cert" scp etcd*.pem root@${node_etcd_ip}:/etc/etcd/cert/ done 3) 創(chuàng)建etcd的systemd unit模板文件 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# cat > etcd.service.template <<EOF [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target Documentation=https://github.com/coreos [Service] Type=notify WorkingDirectory=${ETCD_DATA_DIR} ExecStart=/opt/k8s/bin/etcd \\ --data-dir=${ETCD_DATA_DIR} \\ --wal-dir=${ETCD_WAL_DIR} \\ --name=##NODE_ETCD_NAME## \\ --cert-file=/etc/etcd/cert/etcd.pem \\ --key-file=/etc/etcd/cert/etcd-key.pem \\ --trusted-ca-file=/etc/kubernetes/cert/ca.pem \\ --peer-cert-file=/etc/etcd/cert/etcd.pem \\ --peer-key-file=/etc/etcd/cert/etcd-key.pem \\ --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem \\ --peer-client-cert-auth \\ --client-cert-auth \\ --listen-peer-urls=https://##NODE_ETCD_IP##:2380 \\ --initial-advertise-peer-urls=https://##NODE_ETCD_IP##:2380 \\ --listen-client-urls=https://##NODE_ETCD_IP##:2379,http://127.0.0.1:2379 \\ --advertise-client-urls=https://##NODE_ETCD_IP##:2379 \\ --initial-cluster-token=etcd-cluster-0 \\ --initial-cluster=${ETCD_NODES} \\ --initial-cluster-state=new \\ --auto-compaction-mode=periodic \\ --auto-compaction-retention=1 \\ --max-request-bytes=33554432 \\ --quota-backend-bytes=6442450944 \\ --heartbeat-interval=250 \\ --election-timeout=2000 Restart=on-failure RestartSec=5 LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF 配置說明: WorkingDirectory、--data-dir:指定工作目錄和數(shù)據(jù)目錄為 ${ETCD_DATA_DIR},需在啟動服務(wù)前創(chuàng)建這個目錄; --wal-dir:指定 wal 目錄,為了提高性能,一般使用 SSD 或者和 --data-dir 不同的磁盤; --name:指定節(jié)點(diǎn)名稱,當(dāng) --initial-cluster-state 值為 new 時,--name 的參數(shù)值必須位于 --initial-cluster 列表中; --cert-file、--key-file:etcd server 與 client 通信時使用的證書和私鑰; --trusted-ca-file:簽名 client 證書的 CA 證書,用于驗(yàn)證 client 證書; --peer-cert-file、--peer-key-file:etcd 與 peer 通信使用的證書和私鑰; --peer-trusted-ca-file:簽名 peer 證書的 CA 證書,用于驗(yàn)證 peer 證書; 4)為各etcd節(jié)點(diǎn)創(chuàng)建和分發(fā) etcd systemd unit 文件 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for (( i=0; i < 3; i++ )) do sed -e "s/##NODE_ETCD_NAME##/${NODE_ETCD_NAMES[i]}/" -e "s/##NODE_ETCD_IP##/${NODE_ETCD_IPS[i]}/" etcd.service.template > etcd-${NODE_ETCD_IPS[i]}.service done [root@k8s-master01 work]# ls *.service etcd-172.16.60.241.service etcd-172.16.60.242.service etcd-172.16.60.243.service 最好手動查看其中一個etcd節(jié)點(diǎn)的啟動文件里的--name名稱和ip是否都已修改過來了 [root@k8s-master01 work]# cat etcd-172.16.60.241.service ....... --name=k8s-etcd01 \ ....... --listen-peer-urls=https://172.16.60.241:2380 \ --initial-advertise-peer-urls=https://172.16.60.241:2380 \ --listen-client-urls=https://172.16.60.241:2379,http://127.0.0.1:2379 \ --advertise-client-urls=https://172.16.60.241:2379 \ --initial-cluster-token=etcd-cluster-0 \ --initial-cluster=k8s-etcd01=https://172.16.60.241:2380,k8s-etcd02=https://172.16.60.242:2380,k8s-etcd03=https://172.16.60.243:2380 \ ....... 配置說明: NODE_ETCD_NAMES 和 NODE_ETCD_IPS 為相同長度的bash數(shù)組,分別為etcd集群節(jié)點(diǎn)名稱和對應(yīng)的IP; 分發(fā)生成的 systemd unit 文件: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" scp etcd-${node_etcd_ip}.service root@${node_etcd_ip}:/etc/systemd/system/etcd.service done 配置說明: 文件重命名為 etcd.service; 5)啟動 etcd 服務(wù) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" ssh root@${node_etcd_ip} "mkdir -p ${ETCD_DATA_DIR} ${ETCD_WAL_DIR}" ssh root@${node_etcd_ip} "systemctl daemon-reload && systemctl enable etcd && systemctl restart etcd " & done 配置說明: 必須先創(chuàng)建 etcd 數(shù)據(jù)目錄和工作目錄; etcd 進(jìn)程首次啟動時會等待其它節(jié)點(diǎn)的 etcd 加入集群,命令 systemctl start etcd 會卡住一段時間,為正?,F(xiàn)象; 6)檢查etcd服務(wù)啟動結(jié)果 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" ssh root@${node_etcd_ip} "systemctl status etcd|grep Active" done 預(yù)期輸出結(jié)果為: >>> 172.16.60.241 Active: active (running) since Tue 2019-06-04 19:55:32 CST; 7min ago >>> 172.16.60.242 Active: active (running) since Tue 2019-06-04 19:55:32 CST; 7min ago >>> 172.16.60.243 Active: active (running) since Tue 2019-06-04 19:55:32 CST; 7min ago 確保狀態(tài)均為為active (running),否則查看日志,確認(rèn)原因 (可以執(zhí)行"journalctl -u etcd"命令查看啟動失敗原因) 6)驗(yàn)證服務(wù)狀態(tài) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_etcd_ip in ${NODE_ETCD_IPS[@]} do echo ">>> ${node_etcd_ip}" ssh root@${node_etcd_ip} " ETCDCTL_API=3 /opt/k8s/bin/etcdctl \ --endpoints=https://${node_etcd_ip}:2379 \ --cacert=/etc/kubernetes/cert/ca.pem \ --cert=/etc/etcd/cert/etcd.pem \ --key=/etc/etcd/cert/etcd-key.pem endpoint health " done 預(yù)期輸出結(jié)果為: https://172.16.60.241:2379 is healthy: successfully committed proposal: took = 2.44394ms >>> 172.16.60.242 https://172.16.60.242:2379 is healthy: successfully committed proposal: took = 7.044349ms >>> 172.16.60.243 https://172.16.60.243:2379 is healthy: successfully committed proposal: took = 1.865713ms 輸出均為 healthy 時表示集群服務(wù)正常。 7)查看當(dāng)前etcd集群中的leader 在三臺etcd節(jié)點(diǎn)中的任意一個節(jié)點(diǎn)機(jī)器上執(zhí)行下面命令: [root@k8s-etcd03 ~]# source /opt/k8s/bin/environment.sh [root@k8s-etcd03 ~]# ETCDCTL_API=3 /opt/k8s/bin/etcdctl \ -w table --cacert=/etc/kubernetes/cert/ca.pem \ --cert=/etc/etcd/cert/etcd.pem \ --key=/etc/etcd/cert/etcd-key.pem \ --endpoints=${ETCD_ENDPOINTS} endpoint status 預(yù)期輸出結(jié)果為: +----------------------------+------------------+---------+---------+-----------+-----------+------------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX | +----------------------------+------------------+---------+---------+-----------+-----------+------------+ | https://172.16.60.241:2379 | 577381f5de0f4495 | 3.3.13 | 16 kB | false | 2 | 8 | | https://172.16.60.242:2379 | bf4ce221cdf39fb0 | 3.3.13 | 16 kB | false | 2 | 8 | | https://172.16.60.243:2379 | 3bc2e49bc639590 | 3.3.13 | 16 kB | true | 2 | 8 | +----------------------------+------------------+---------+---------+-----------+-----------+------------+ 由上面結(jié)果可見,當(dāng)前的leader節(jié)點(diǎn)為172.16.60.243
六、Flannel容器網(wǎng)絡(luò)方案部署
kubernetes要求集群內(nèi)各節(jié)點(diǎn)(這里指master和node節(jié)點(diǎn))能通過Pod網(wǎng)段互聯(lián)互通。flannel使用vxlan技術(shù)為各節(jié)點(diǎn)創(chuàng)建一個可以互通的Pod網(wǎng)絡(luò),使用的端口為UDP 8472(需要開放該端口,如公有云AWS等)。flanneld第一次啟動時,從etcd獲取配置的Pod網(wǎng)段信息,為本節(jié)點(diǎn)分配一個未使用的地址段,然后創(chuàng)建flannedl.1網(wǎng)絡(luò)接口(也可能是其它名稱,如flannel1等)。flannel將分配給自己的Pod網(wǎng)段信息寫入/run/flannel/docker文件,docker后續(xù)使用這個文件中的環(huán)境變量設(shè)置docker0網(wǎng)橋,從而從這個地址段為本節(jié)點(diǎn)的所有Pod容器分配IP。下面部署命令均在k8s-master01節(jié)點(diǎn)上執(zhí)行,然后遠(yuǎn)程分發(fā)文件和執(zhí)行命令。
1) 下載和分發(fā) flanneld 二進(jìn)制文件 從flannel的release頁面(https://github.com/coreos/flannel/releases)下載最新版本的安裝包: [root@k8s-master01 ~]# cd /opt/k8s/work [root@k8s-master01 work]# mkdir flannel [root@k8s-master01 work]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz [root@k8s-master01 work]# tar -zvxf flannel-v0.11.0-linux-amd64.tar.gz -C flannel 分發(fā)二進(jìn)制文件到集群所有節(jié)點(diǎn): [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" scp flannel/{flanneld,mk-docker-opts.sh} root@${node_all_ip}:/opt/k8s/bin/ ssh root@${node_all_ip} "chmod +x /opt/k8s/bin/*" done 2) 創(chuàng)建 flannel 證書和私鑰 flanneld 從 etcd 集群存取網(wǎng)段分配信息,而 etcd 集群啟用了雙向 x509 證書認(rèn)證,所以需要為 flanneld 生成證書和私鑰。 創(chuàng)建證書簽名請求: [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# cat > flanneld-csr.json <<EOF { "CN": "flanneld", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "4Paradigm" } ] } EOF 該證書只會被 kubectl 當(dāng)做 client 證書使用,所以 hosts 字段為空; 生成證書和私鑰: [root@k8s-master01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem \ -ca-key=/opt/k8s/work/ca-key.pem \ -config=/opt/k8s/work/ca-config.json \ -profile=kubernetes flanneld-csr.json | cfssljson -bare flanneld 將生成的證書和私鑰分發(fā)到所有節(jié)點(diǎn)(master 和 node): [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh root@${node_all_ip} "mkdir -p /etc/flanneld/cert" scp flanneld*.pem root@${node_all_ip}:/etc/flanneld/cert done 3)向 etcd 寫入集群 Pod 網(wǎng)段信息 (注意:本步驟只需執(zhí)行一次) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# etcdctl \ --endpoints=${ETCD_ENDPOINTS} \ --ca-file=/opt/k8s/work/ca.pem \ --cert-file=/opt/k8s/work/flanneld.pem \ --key-file=/opt/k8s/work/flanneld-key.pem \ mk ${FLANNEL_ETCD_PREFIX}/config '{"Network":"'${CLUSTER_CIDR}'", "SubnetLen": 21, "Backend": {"Type": "vxlan"}}' 解決說明: flanneld 當(dāng)前版本 (v0.11.0) 不支持 etcd v3,故使用 etcd v2 API 寫入配置 key 和網(wǎng)段數(shù)據(jù); 寫入的 Pod 網(wǎng)段 ${CLUSTER_CIDR} 地址段(如 /16)必須小于 SubnetLen,必須與 kube-controller-manager 的 --cluster-cidr 參數(shù)值一致; 4)創(chuàng)建 flanneld 的 systemd unit 文件 [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# cat > flanneld.service << EOF [Unit] Description=Flanneld overlay address etcd agent After=network.target After=network-online.target Wants=network-online.target After=etcd.service Before=docker.service [Service] Type=notify ExecStart=/opt/k8s/bin/flanneld \\ -etcd-cafile=/etc/kubernetes/cert/ca.pem \\ -etcd-certfile=/etc/flanneld/cert/flanneld.pem \\ -etcd-keyfile=/etc/flanneld/cert/flanneld-key.pem \\ -etcd-endpoints=${ETCD_ENDPOINTS} \\ -etcd-prefix=${FLANNEL_ETCD_PREFIX} \\ -iface=${IFACE} \\ -ip-masq ExecStartPost=/opt/k8s/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker Restart=always RestartSec=5 StartLimitInterval=0 [Install] WantedBy=multi-user.target RequiredBy=docker.service EOF 解決說明: mk-docker-opts.sh 腳本將分配給 flanneld 的 Pod 子網(wǎng)段信息寫入 /run/flannel/docker 文件,后續(xù) docker 啟動時使用這個文件中的環(huán)境變量配置 docker0 網(wǎng)橋; flanneld 使用系統(tǒng)缺省路由所在的接口與其它節(jié)點(diǎn)通信,對于有多個網(wǎng)絡(luò)接口(如內(nèi)網(wǎng)和公網(wǎng))的節(jié)點(diǎn),可以用 -iface 參數(shù)指定通信接口; flanneld 運(yùn)行時需要 root 權(quán)限; -ip-masq: flanneld 為訪問 Pod 網(wǎng)絡(luò)外的流量設(shè)置 SNAT 規(guī)則,同時將傳遞給 Docker 的變量 --ip-masq(/run/flannel/docker 文件中)設(shè)置為 false,這樣 Docker 將不再創(chuàng)建 SNAT 規(guī)則; Docker 的 --ip-masq 為 true 時,創(chuàng)建的 SNAT 規(guī)則比較“暴力”:將所有本節(jié)點(diǎn) Pod 發(fā)起的、訪問非 docker0 接口的請求做 SNAT,這樣訪問其他節(jié)點(diǎn) Pod 的請求來源 IP 會被設(shè)置為 flannel.1 接口的 IP,導(dǎo)致目的 Pod 看不到真實(shí)的來源 Pod IP。 flanneld 創(chuàng)建的 SNAT 規(guī)則比較溫和,只對訪問非 Pod 網(wǎng)段的請求做 SNAT。 5)分發(fā) flanneld systemd unit 文件到所有節(jié)點(diǎn) [root@k8s-master01 work]# cd /opt/k8s/work [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" scp flanneld.service root@${node_all_ip}:/etc/systemd/system/ done 6)啟動 flanneld 服務(wù) [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh root@${node_all_ip} "systemctl daemon-reload && systemctl enable flanneld && systemctl restart flanneld" done 6)檢查啟動結(jié)果 [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh root@${node_all_ip} "systemctl status flanneld|grep Active" done 確保狀態(tài)為 active (running),否則查看日志,確認(rèn)原因"journalctl -u flanneld" 7) 檢查分配給各 flanneld 的 Pod 網(wǎng)段信息 查看集群 Pod 網(wǎng)段(/16): [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# etcdctl \ --endpoints=${ETCD_ENDPOINTS} \ --ca-file=/etc/kubernetes/cert/ca.pem \ --cert-file=/etc/flanneld/cert/flanneld.pem \ --key-file=/etc/flanneld/cert/flanneld-key.pem \ get ${FLANNEL_ETCD_PREFIX}/config 預(yù)期輸出: {"Network":"172.30.0.0/16", "SubnetLen": 21, "Backend": {"Type": "vxlan"}} 查看已分配的 Pod 子網(wǎng)段列表(/24): [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# etcdctl \ --endpoints=${ETCD_ENDPOINTS} \ --ca-file=/etc/kubernetes/cert/ca.pem \ --cert-file=/etc/flanneld/cert/flanneld.pem \ --key-file=/etc/flanneld/cert/flanneld-key.pem \ ls ${FLANNEL_ETCD_PREFIX}/subnets 預(yù)期輸出: /kubernetes/network/subnets/172.30.40.0-21 /kubernetes/network/subnets/172.30.88.0-21 /kubernetes/network/subnets/172.30.56.0-21 /kubernetes/network/subnets/172.30.72.0-21 /kubernetes/network/subnets/172.30.232.0-21 /kubernetes/network/subnets/172.30.152.0-21 查看某一 Pod 網(wǎng)段對應(yīng)的節(jié)點(diǎn) IP 和 flannel 接口地址: [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# etcdctl \ --endpoints=${ETCD_ENDPOINTS} \ --ca-file=/etc/kubernetes/cert/ca.pem \ --cert-file=/etc/flanneld/cert/flanneld.pem \ --key-file=/etc/flanneld/cert/flanneld-key.pem \ get ${FLANNEL_ETCD_PREFIX}/subnets/172.30.40.0-21 預(yù)期輸出:{"PublicIP":"172.16.60.243","BackendType":"vxlan","BackendData":{"VtepMAC":"f2:de:47:06:4b:d3"}} 解決說明: 172.30.40.0/21 被分配給節(jié)點(diǎn)k8s-master03(172.16.60.243); VtepMAC 為k8s-master03節(jié)點(diǎn)的 flannel.1 網(wǎng)卡 MAC 地址; 8)檢查節(jié)點(diǎn) flannel 網(wǎng)絡(luò)信息 (比如k8s-master01節(jié)點(diǎn)) [root@k8s-master01 work]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:ac:7c:81 brd ff:ff:ff:ff:ff:ff inet 172.16.60.241/24 brd 172.16.60.255 scope global ens192 valid_lft forever preferred_lft forever 3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default link/ether 7a:2a:36:99:75:5f brd ff:ff:ff:ff:ff:ff inet 172.30.232.0/32 scope global flannel.1 valid_lft forever preferred_lft forever 注意: flannel.1 網(wǎng)卡的地址為分配的 Pod 子網(wǎng)段的第一個 IP(.0),且是 /32 的地址; [root@k8s-master01 work]# ip route show |grep flannel.1 172.30.40.0/21 via 172.30.40.0 dev flannel.1 onlink 172.30.56.0/21 via 172.30.56.0 dev flannel.1 onlink 172.30.72.0/21 via 172.30.72.0 dev flannel.1 onlink 172.30.88.0/21 via 172.30.88.0 dev flannel.1 onlink 172.30.152.0/21 via 172.30.152.0 dev flannel.1 onlink 到其它節(jié)點(diǎn) Pod 網(wǎng)段請求都被轉(zhuǎn)發(fā)到 flannel.1 網(wǎng)卡; flanneld 根據(jù) etcd 中子網(wǎng)段的信息,如 ${FLANNEL_ETCD_PREFIX}/subnets/172.30.232.0-21 ,來決定進(jìn)請求發(fā)送給哪個節(jié)點(diǎn)的互聯(lián) IP; 9)驗(yàn)證各節(jié)點(diǎn)能通過 Pod 網(wǎng)段互通 在各節(jié)點(diǎn)上部署 flannel 后,檢查是否創(chuàng)建了 flannel 接口(名稱可能為 flannel0、flannel.0、flannel.1 等): [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh ${node_all_ip} "/usr/sbin/ip addr show flannel.1|grep -w inet" done 預(yù)期輸出: >>> 172.16.60.241 inet 172.30.232.0/32 scope global flannel.1 >>> 172.16.60.242 inet 172.30.152.0/32 scope global flannel.1 >>> 172.16.60.243 inet 172.30.40.0/32 scope global flannel.1 >>> 172.16.60.244 inet 172.30.88.0/32 scope global flannel.1 >>> 172.16.60.245 inet 172.30.56.0/32 scope global flannel.1 >>> 172.16.60.246 inet 172.30.72.0/32 scope global flannel.1 在各節(jié)點(diǎn)上 ping 所有 flannel 接口 IP,確保能通: [root@k8s-master01 work]# source /opt/k8s/bin/environment.sh [root@k8s-master01 work]# for node_all_ip in ${NODE_ALL_IPS[@]} do echo ">>> ${node_all_ip}" ssh ${node_all_ip} "ping -c 1 172.30.232.0" ssh ${node_all_ip} "ping -c 1 172.30.152.0" ssh ${node_all_ip} "ping -c 1 172.30.40.0" ssh ${node_all_ip} "ping -c 1 172.30.88.0" ssh ${node_all_ip} "ping -c 1 172.30.56.0" ssh ${node_all_ip} "ping -c 1 172.30.72.0" done
七、基于nginx 四層代理環(huán)境
這里采用nginx 4 層透明代理功能實(shí)現(xiàn) K8S 節(jié)點(diǎn)( master 節(jié)點(diǎn)和 worker 節(jié)點(diǎn))高可用訪問 kube-apiserver??刂乒?jié)點(diǎn)的 kube-controller-manager、kube-scheduler 是多實(shí)例(3個)部署,所以只要有一個實(shí)例正常,就可以保證高可用;搭建nginx+keepalived環(huán)境,對外提供一個統(tǒng)一的vip地址,后端對接多個 apiserver 實(shí)例,nginx 對它們做健康檢查和負(fù)載均衡;kubelet、kube-proxy、controller-manager、scheduler 通過vip地址訪問 kube-apiserver,從而實(shí)現(xiàn) kube-apiserver 的高可用;
一、安裝和配置nginx,下面操作在172.16.60.247、172.16.60.247兩個節(jié)點(diǎn)機(jī)器上操作 1)下載和編譯 nginx [root@k8s-ha01 ~]# yum -y install gcc pcre-devel zlib-devel openssl-devel wget lsof [root@k8s-ha01 ~]# cd /opt/k8s/work [root@k8s-ha01 work]# wget http://nginx.org/download/nginx-1.15.3.tar.gz [root@k8s-ha01 work]# tar -xzvf nginx-1.15.3.tar.gz [root@k8s-ha01 work]# cd nginx-1.15.3 [root@k8s-ha01 nginx-1.15.3]# mkdir nginx-prefix [root@k8s-ha01 nginx-1.15.3]# ./configure --with-stream --without-http --prefix=$(pwd)/nginx-prefix --without-http_uwsgi_module --without-http_scgi_module --without-http_fastcgi_module 解決說明: --with-stream:開啟 4 層透明轉(zhuǎn)發(fā)(TCP Proxy)功能; --without-xxx:關(guān)閉所有其他功能,這樣生成的動態(tài)鏈接二進(jìn)制程序依賴最?。? 預(yù)期輸出: Configuration summary + PCRE library is not used + OpenSSL library is not used + zlib library is not used nginx path prefix: "/root/tmp/nginx-1.15.3/nginx-prefix" nginx binary file: "/root/tmp/nginx-1.15.3/nginx-prefix/sbin/nginx" nginx modules path: "/root/tmp/nginx-1.15.3/nginx-prefix/modules" nginx configuration prefix: "/root/tmp/nginx-1.15.3/nginx-prefix/conf" nginx configuration file: "/root/tmp/nginx-1.15.3/nginx-prefix/conf/nginx.conf" nginx pid file: "/root/tmp/nginx-1.15.3/nginx-prefix/logs/nginx.pid" nginx error log file: "/root/tmp/nginx-1.15.3/nginx-prefix/logs/error.log" nginx http access log file: "/root/tmp/nginx-1.15.3/nginx-prefix/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" 繼續(xù)編譯和安裝: [root@k8s-ha01 nginx-1.15.3]# make && make install 2)驗(yàn)證編譯的 nginx [root@k8s-ha01 nginx-1.15.3]# ./nginx-prefix/sbin/nginx -v nginx version: nginx/1.15.3 查看 nginx 動態(tài)鏈接的庫: [root@k8s-ha01 nginx-1.15.3]# ldd ./nginx-prefix/sbin/nginx linux-vdso.so.1 => (0x00007ffc7e0ef000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f00b5c2d000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f00b5a11000) libc.so.6 => /lib64/libc.so.6 (0x00007f00b5644000) /lib64/ld-linux-x86-64.so.2 (0x00007f00b5e31000) 由于只開啟了 4 層透明轉(zhuǎn)發(fā)功能,所以除了依賴 libc 等操作系統(tǒng)核心 lib 庫外,沒有對其它 lib 的依賴(如 libz、libssl 等),這樣可以方便部署到各版本操作系統(tǒng)中; 3)安裝和部署 nginx [root@k8s-ha01 ~]# cp /opt/k8s/work/nginx-1.15.3/nginx-prefix/sbin/nginx /opt/k8s/kube-nginx/sbin/kube-nginx [root@k8s-ha01 ~]# chmod a+x /opt/k8s/kube-nginx/sbin/* [root@k8s-ha01 ~]# mkdir -p /opt/k8s/kube-nginx/{conf,logs,sbin} 配置 nginx,開啟 4 層透明轉(zhuǎn)發(fā)功能: [root@k8s-ha01 ~]# vim /opt/k8s/kube-nginx/conf/kube-nginx.conf worker_processes 2; events { worker_connections 65525; } stream { upstream backend { hash $remote_addr consistent; server 172.16.60.241:6443 max_fails=3 fail_timeout=30s; server 172.16.60.242:6443 max_fails=3 fail_timeout=30s; server 172.16.60.243:6443 max_fails=3 fail_timeout=30s; } server { listen 8443; proxy_connect_timeout 1s; proxy_pass backend; } } [root@k8s-ha01 ~]# ulimit -n 65525 [root@k8s-ha01 ~]# vim /etc/security/limits.conf # 文件底部添加下面四行內(nèi)容 * soft nofile 65525 * hard nofile 65525 * soft nproc 65525 * hard nproc 65525 4) 配置 systemd unit 文件,啟動服務(wù) [root@k8s-ha01 ~]# vim /etc/systemd/system/kube-nginx.service [Unit] Description=kube-apiserver nginx proxy After=network.target After=network-online.target Wants=network-online.target [Service] Type=forking ExecStartPre=/opt/k8s/kube-nginx/sbin/kube-nginx -c /opt/k8s/kube-nginx/conf/kube-nginx.conf -p /opt/k8s/kube-nginx -t ExecStart=/opt/k8s/kube-nginx/sbin/kube-nginx -c /opt/k8s/kube-nginx/conf/kube-nginx.conf -p /opt/k8s/kube-nginx ExecReload=/opt/k8s/kube-nginx/sbin/kube-nginx -c /opt/k8s/kube-nginx/conf/kube-nginx.conf -p /opt/k8s/kube-nginx -s reload PrivateTmp=true Restart=always RestartSec=5 StartLimitInterval=0 LimitNOFILE=65536 [Install] WantedBy=multi-user.target [root@k8s-ha01 ~]# systemctl daemon-reload && systemctl enable kube-nginx && systemctl restart kube-nginx [root@k8s-ha01 ~]# lsof -i:8443 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME kube-ngin 31980 root 5u IPv4 145789 0t0 TCP localhost:pcsync-https (LISTEN) kube-ngin 31981 nobody 5u IPv4 145789 0t0 TCP localhost:pcsync-https (LISTEN) kube-ngin 31982 nobody 5u IPv4 145789 0t0 TCP localhost:pcsync-https (LISTEN) 測試下8443代理端口連通性 [root@k8s-ha01 ~]# telnet 172.16.60.250 8443 Trying 172.16.60.250... Connected to 172.16.60.250. Escape character is '^]'. Connection closed by foreign host. 這是因?yàn)槿齻€kube-apiserver服務(wù)還沒有部署,即后端三個apiserver實(shí)例的6443端口還沒有起來。 二、安裝和配置keepalived 1)編譯安裝keepalived (兩個節(jié)點(diǎn)上同樣操作) [root@k8s-ha01 ~]# cd /opt/k8s/work/ [root@k8s-ha01 work]# wget https://www.keepalived.org/software/keepalived-2.0.16.tar.gz [root@k8s-ha01 work]# tar -zvxf keepalived-2.0.16.tar.gz [root@k8s-ha01 work]# cd keepalived-2.0.16 [root@k8s-ha01 keepalived-2.0.16]# ./configure [root@k8s-ha01 keepalived-2.0.16]# make && make install [root@k8s-ha01 keepalived-2.0.16]# cp keepalived/etc/init.d/keepalived /etc/rc.d/init.d/ [root@k8s-ha01 keepalived-2.0.16]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@k8s-ha01 keepalived-2.0.16]# mkdir /etc/keepalived [root@k8s-ha01 keepalived-2.0.16]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@k8s-ha01 keepalived-2.0.16]# cp /usr/local/sbin/keepalived /usr/sbin/ [root@k8s-ha01 keepalived-2.0.16]# echo "/etc/init.d/keepalived start" >> /etc/rc.local 2) 配置keepalived 172.16.60.207節(jié)點(diǎn)上的keepalived配置內(nèi)容 [root@k8s-ha01 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@k8s-ha01 ~]# >/etc/keepalived/keepalived.conf [root@k8s-ha01 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { ops@wangshibo.cn tech@wangshibo.cn } notification_email_from ops@wangshibo.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id master-node } vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface ens192 mcast_src_ip 172.16.60.247 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.60.250 } track_script { chk_http_port } } 另一個節(jié)點(diǎn)172.16.60.248上的keepalived配置內(nèi)容為: [root@k8s-ha02 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@k8s-ha02 ~]# >/etc/keepalived/keepalived.conf [root@k8s-ha02 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { ops@wangshibo.cn tech@wangshibo.cn } notification_email_from ops@wangshibo.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id slave-node } vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface ens192 mcast_src_ip 172.16.60.248 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.60.250 } track_script { chk_http_port } } 2) 配置兩個節(jié)點(diǎn)的nginx監(jiān)控腳本(該腳本會在keepalived.conf配置中被引用) [root@k8s-ha01 ~]# vim /opt/chk_nginx.sh #!/bin/bash counter=$(ps -ef|grep -w kube-nginx|grep -v grep|wc -l) if [ "${counter}" = "0" ]; then systemctl start kube-nginx sleep 2 counter=$(ps -ef|grep kube-nginx|grep -v grep|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fi fi [root@k8s-ha01 ~]# chmod 755 /opt/chk_nginx.sh 3) 啟動兩個節(jié)點(diǎn)的keepalived服務(wù) [root@k8s-ha01 ~]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ OK ] [root@k8s-ha01 ~]# ps -ef|grep keepalived root 5358 1 0 00:32 ? 00:00:00 /usr/local/sbin/keepalived -D root 5359 5358 0 00:32 ? 00:00:00 /usr/local/sbin/keepalived -D root 5391 29606 0 00:32 pts/0 00:00:00 grep --color=auto keepalived 查看vip情況. 發(fā)現(xiàn)vip默認(rèn)起初會在master節(jié)點(diǎn)上 [root@k8s-ha01 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:ac:3a:a6 brd ff:ff:ff:ff:ff:ff inet 172.16.60.247/24 brd 172.16.60.255 scope global ens192 valid_lft forever preferred_lft forever inet 172.16.60.250/32 scope global ens192 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:feac:3aa6/64 scope link valid_lft forever preferred_lft forever 4) 測試vip故障轉(zhuǎn)移 參考:https://www.cnblogs.com/kevingrace/p/6138185.html 當(dāng)master節(jié)點(diǎn)的keepalived服務(wù)掛掉,vip會自動漂移到slave節(jié)點(diǎn)上 當(dāng)master節(jié)點(diǎn)的keepliaved服務(wù)恢復(fù)后,從將vip資源從slave節(jié)點(diǎn)重新?lián)屨蓟貋恚╧eepalived配置文件中的priority優(yōu)先級決定的) 當(dāng)兩個節(jié)點(diǎn)的nginx掛掉后,keepaived會引用nginx監(jiān)控腳本自啟動nginx服務(wù),如啟動失敗,則強(qiáng)殺keepalived服務(wù),從而實(shí)現(xiàn)vip轉(zhuǎn)移。
點(diǎn)擊查看 Kubernetes(K8S)容器集群管理環(huán)境完整部署詳細(xì)教程-中篇
相關(guān)文章
CentOS 出現(xiàn)no space left on device錯誤解決辦法
這篇文章主要介紹了CentOS 出現(xiàn)no space left on device錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Kubernetes kubectl中Pod創(chuàng)建流程源碼解析
這篇文章主要為大家介紹了Kubernetes kubectl中Pod創(chuàng)建流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11KubeSphere接入外部Elasticsearch實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了KubeSphere接入外部Elasticsearch實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Dashboard管理Kubernetes集群與API訪問配置
這篇文章介紹了Dashboard管理Kubernetes集群與API訪問配置的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Docker與K8s關(guān)系介紹不會Docker也可以使用K8s
想學(xué)K8s,必須得先學(xué)會Docker嗎?這是很多網(wǎng)友在開始有想法想要學(xué)?K8s的時候都會冒出來的想法,要回答這個問題,我們需要先搞清楚?Docker?和?K8s?他們的角色是什么,相互之間是什么關(guān)系2022-06-06