使用kubeadm快速部署一套K8S叢集以及眾多神坑記錄
一、Kubernetes概述
1.1 Kubernetes是什麼
-
Kubernetes是Google在2014年開源的一個容器叢集管理系統,Kubernetes簡稱K8S。
-
K8S用於容器化應用程式的部署,擴充套件和管理。
-
K8S提供了容器編排,資源排程,彈性伸縮,部署管理,服務發現等一系列功能。
-
Kubernetes目標是讓部署容器化應用簡單高效。
1.2 Kubernetes特性
-
自我修復
-
在節點故障時重新啟動失敗的容器,替換和重新部署,保證預期的副本數量;殺死健康檢查失敗的容器,並且在未準備好之前不會處理客戶端請求,確保線上服務不中斷。
-
彈性伸縮
-
使用命令、UI或者基於CPU使用情況自動快速擴容和縮容應用程式例項,保證應用業務高峰併發時的高可用性;業務低峰時回收資源,以最小成本執行服務。
-
自動部署和回滾
-
K8S採用滾動更新策略更新應用,一次更新一個Pod,而不是同時刪除所有Pod,如果更新過程中出現問題,將回滾更改,確保升級不受影響業務。
-
服務發現和負載均衡
-
K8S為多個容器提供一個統一訪問入口(內部IP地址和一個DNS名稱),並且負載均衡關聯的所有容器,使得使用者無需考慮容器IP問題。
-
機密和配置管理
-
管理機密資料和應用程式配置,而不需要把敏感資料暴露在映象裡,提高敏感資料安全性。並可以將一些常用的配置儲存在K8S中,方便應用程式使用。
-
儲存編排
-
掛載外部儲存系統,無論是來自本地儲存,公有云(如AWS),還是網路儲存(如NFS、GlusterFS、Ceph)都作為叢集資源的一部分使用,極大提高儲存使用靈活性。
-
批處理
-
提供一次性任務,定時任務;滿足批量資料處理和分析的場景。
1.3 Kubernetes叢集架構與元件
1.4 Kubernetes叢集元件介紹
1.4.1 Master元件
-
kube-apiserver
-
Kubernetes API,
叢集的統一入口,各元件協調者,以RESTful API提供介面服務,所有物件資源的增刪改查和監聽操作都交給APIServer處理後再提交給Etcd儲存。
-
kube-controller-manager
-
處理叢集中常規後臺任務,一個資源對應一個控制器,而ControllerManager就是負責管理這些控制器的。
-
kube-scheduler
-
根據排程演算法為新建立的Pod選擇一個Node節點,可以任意部署,可以部署在同一個節點上,也可以部署在不同的節點上。
-
etcd
-
分散式鍵值儲存系統。用於儲存叢集狀態資料,比如Pod、Service等物件資訊。
1.4.2 Node元件
-
kubelet
-
kubelet是Master在Node節點上的Agent,管理本機執行容器的生命週期,比如建立容器、Pod掛載資料卷、下載secret、獲取容器和節點狀態等工作。kubelet將每個Pod轉換成一組容器。
-
kube-proxy
-
在Node節點上實現Pod網路代理,維護網路規則和四層負載均衡工作。
-
docker或rocket
-
容器引擎,執行容器。
1.5 Kubernetes 核心概念
-
Pod
-
最小部署單元
-
一組容器的集合
-
一個Pod中的容器共享網路名稱空間
-
Pod是短暫的
-
Controllers
-
ReplicaSet :確保預期的Pod副本數量
-
Deployment :無狀態應用部署
-
StatefulSet :有狀態應用部署
-
DaemonSet :確保所有Node運行同一個Pod
-
Job :一次性任務
-
Cronjob :定時任務
更高階層次物件,部署和管理Pod
-
Service
- 防止Pod失聯
- 定義一組Pod的訪問策略
-
Label :標籤,附加到某個資源上,用於關聯物件、查詢和篩選
-
Namespaces:名稱空間,將物件邏輯上隔離
-
Annotations :註釋
二、kubeadm 快速部署K8S叢集
2.1 kubernetes 官方提供的三種部署方式
-
minikube
Minikube是一個工具,可以在本地快速執行一個單點的Kubernetes,僅用於嘗試Kubernetes或日常開發的使用者使用。部署地址:https://kubernetes.io/docs/setup/minikube/
-
kubeadm
Kubeadm也是一個工具,提供kubeadm init和kubeadm join,用於快速部署Kubernetes叢集。部署地址:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
-
二進位制包
推薦,從官方下載發行版的二進位制包,手動部署每個元件,組成Kubernetes叢集。下載地址:https://github.com/kubernetes/kubernetes/releases
2.2 安裝kubeadm環境準備
以下操作,在三臺節點都執行
2.2.1 環境需求
環境:centos 7.4+(版本儘量在7.3-7.5之間,儘量不要用8或者以上版本)
硬體需求:CPU>=2c ,記憶體>=2G
2.2.2 環境角色
IP | 角色 | 安裝軟體 |
---|---|---|
192.168.73.138 | k8s-Master | kube-apiserver kube-schduler kube-controller-manager docker flannel kubelet |
192.168.73.139 | k8s-node01 | kubelet kube-proxy docker flannel |
192.168.73.140 | k8s-node01 | kubelet kube-proxy docker flannel |
2.2.3 環境初始化
PS : 以下所有操作,在三臺節點全部執行
1、關閉防火牆及selinux
$ systemctl stop firewalld && systemctl disable firewalld $ sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && setenforce 0
2、關閉 swap 分割槽
$ swapoff -a # 臨時 $ sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab #永久
3、分別在192.168.73.138、192.168.73.139、192.168.73.140上設定主機名及配置hosts
$ hostnamectl set-hostname k8s-master(192.168.73.138主機打命令) $ hostnamectl set-hostname k8s-node01(192.168.73.139主機打命令) $ hostnamectl set-hostname k8s-node02 (192.168.73.140主機打命令)
4、在所有主機上上新增如下命令
$ cat >> /etc/hosts << EOF192.168.4.34 k8s-master192.168.4.35 k8s-node01192.168.4.36 k8s-node02EOF
5、核心調整,將橋接的IPv4流量傳遞到iptables的鏈
cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # 生效
6、設定系統時區並同步時間伺服器
yum install ntpdate -y
ntpdate time.windows.com
噩夢的開始,第一個坑:如果centos版本是8的話,它是不支援安裝ntpdate的,所以就要用另一個方法:
#安裝軟體 chrony
[root@kiccleaf home]# yum install -y chrony
#安裝好工具,先啟動
[root@kiccleaf home]# systemctl start chronyd
#設為系統自動啟動
[root@kiccleaf home]# systemctl enable chronyd
#編輯一下配置檔案
[root@kiccleaf home]# vim /etc/chrony.conf
/etc/chrony.conf配置檔案內容:
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.centos.pool.ntp.org iburst (這一行註釋掉,增加以下兩行)
server ntp.aliyun.com iburst
server cn.ntp.org.cn iburst
重新載入配置:
[root@kiccleaf home]# systemctl restart chronyd.service
[root@kiccleaf home]# chronyc sources -v
210 Number of sources = 2
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 7 2 -188us[+6871us] +/- 24ms
^- 61.177.189.190 3 6 17 19 +663us[ +663us] +/- 97ms
#再次檢視時間
[root@kiccleaf home]# date
Sat Aug 29 16:26:08 CST 2020
2.2.4 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
# docker生效並啟動 $ systemctl enable docker && systemctl start docker
# 檢視docker版本 $ docker --version Docker version 18.06.1-ce, build e68fc7a
2.2.5 新增kubernetes YUM軟體源
$ 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
2.2.6 安裝kubeadm,kubelet和kubectl
2.2.6上所有主機都需要操作,由於版本更新頻繁,這裡指定版本號部署
$ yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0 $ systemctl enable kubelet
2.3 部署Kubernetes Master
只需要在Master 節點執行,這裡的apiserve需要修改成自己的master地址
[root@k8s-master ~]# kubeadm init \ --apiserver-advertise-address=192.168.73.138 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.15.0 \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16
由於預設拉取映象地址k8s.gcr.io國內無法訪問,這裡指定阿里雲映象倉庫地址。
輸出結果:
[preflight] Pulling images required for setting up a Kubernetes cluster [preflight] This might take a minute or two, depending on the speed of your internet connection [preflight] You can also perform this action in beforehand using 'kubeadm config images pull' [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Activating the kubelet service [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "ca" certificate and key [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.1.0.1 192.168.4.34] [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.4.34 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.4.34 127.0.0.1 ::1] [certs] Generating "sa" key and public key [kubeconfig] Using kubeconfig folder "/etc/kubernetes" ......(省略) [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy 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 192.168.73.138:6443 --token 2nm5l9.jtp4zwnvce4yt4oj \ --discovery-token-ca-cert-hash sha256:12f628a21e8d4a7262f57d4f21bc85f8802bb717d
根據輸出提示操作:
[root@k8s-master ~]# mkdir -p $HOME/.kube [root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
預設token的有效期為24小時,當過期之後,該token就不可用了,
如果後續有nodes節點加入,解決方法如下:
重新生成新的token
kubeadm token create [root@k8s-master ~]# kubeadm token create 0w3a92.ijgba9ia0e3scicg [root@k8s-master ~]# kubeadm token list TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS 0w3a92.ijgba9ia0e3scicg 23h 2019-09-08T22:02:40+08:00 authentication,signing <none> system:bootstrappers:kubeadm:default-node-token t0ehj8.k4ef3gq0icr3etl0 22h 2019-09-08T20:58:34+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token [root@k8s-master ~]#
獲取ca證書sha256編碼hash值
[root@k8s-master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' ce07a7f5b259961884c55e3ff8784b1eda6f8b5931e6fa2ab0b30b6a4234c09a
節點加入叢集
[root@k8s-node01 ~]# kubeadm join --token aa78f6.8b4cafc8ed26c34f --discovery-token-ca-cert-hash sha256:0fd95a9bc67a7bf0ef42da968a0d55d92e52898ec37c971bd77ee501d845b538 192.168.73.138:6443 --skip-preflight-chec
2.4加入Kubernetes Node
在兩個 Node 節點執行
使用kubeadm join 註冊Node節點到Matser
kubeadm join 的內容,在上面kubeadm init 已經生成好了
[root@k8s-node01 ~]# kubeadm join 192.168.4.34:6443 --token 2nm5l9.jtp4zwnvce4yt4oj \ --discovery-token-ca-cert-hash sha256:12f628a21e8d4a7262f57d4f21bc85f8802bb717dd6f513bf9d33f254fea3e89
輸出內容:
[preflight] Running pre-flight checks [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/ [preflight] Reading configuration from the cluster... [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Activating the kubelet service [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.