Kubeadm方式部署企業級K8s叢集
一、前置知識點
1.1 生產環境可部署Kubernetes叢集的兩種方式
目前生產部署Kubernetes叢集主要有兩種方式:
- kubeadm
Kubeadm是一個K8s部署工具,提供kubeadm init和kubeadm join,用於快速部署Kubernetes叢集。
- 二進位制包
從github下載發行版的二進位制包,手動部署每個元件,組成Kubernetes叢集。
這裡採用kubeadm搭建叢集。
kubeadm工具功能:
- kubeadm init:初始化一個Master節點
- kubeadm join:將工作節點加入叢集
- kubeadm upgrade:升級K8s版本
- kubeadm token:
- kubeadm reset:清空 kubeadm init 或者 kubeadm join 對主機所做的任何更改
- kubeadm version:列印 kubeadm 版本
- kubeadm alpha:預覽可用的新功能
1.2 準備環境
伺服器要求:
- 建議最小硬體配置:2核CPU、2G記憶體、30G硬碟
- 伺服器最好可以訪問外網,會有從網上拉取映象需求,如果伺服器不能上網,需要提前下載對應映象並匯入節點
軟體環境:
軟體 |
版本 |
作業系統 |
CentOS7.8_x64 (最小化安裝) |
Docker |
19-ce |
Kubernetes |
1.20 |
伺服器整體規劃:
角色 |
IP |
其他單裝元件 |
k8s-master1 |
192.168.31.61 |
docker,etcd,nginx,keepalived |
k8s-master2 |
192.168.31.62 |
docker,etcd,nginx,keepalived |
k8s-node1 |
192.168.31.63 |
docker,etcd |
負載均衡器對外IP |
192.168.31.88 (VIP) |
架構圖
1.3 作業系統初始化配置
# 關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
# 關閉selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 臨時
# 關閉swap
swapoff -a # 臨時
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 根據規劃設定主機名
hostnamectl set-hostname <hostname>
# 在master新增hosts
cat >> /etc/hosts << EOF
192.168.31.61 k8s-master1
192.168.31.62 k8s-master2
192.168.31.63 k8s-node1
EOF
# 將橋接的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 # 生效
# 時間同步
yum install ntpdate -y
ntpdate time.windows.com
二、部署Nginx+Keepalived高可用負載均衡器
Kubernetes作為容器集群系統,通過健康檢查+重啟策略實現了Pod故障自我修復能力,通過排程演算法實現將Pod分散式部署,並保持預期副本數,根據Node失效狀態自動在其他Node拉起Pod,實現了應用層的高可用性。
針對Kubernetes叢集,高可用性還應包含以下兩個層面的考慮:Etcd資料庫的高可用性和Kubernetes Master元件的高可用性。 而kubeadm搭建的K8s叢集,Etcd只起了一個,存在單點,所以我們這裡會獨立搭建一個Etcd叢集。
Master節點扮演著總控中心的角色,通過不斷與工作節點上的Kubelet和kube-proxy進行通訊來維護整個叢集的健康工作狀態。如果Master節點故障,將無法使用kubectl工具或者API做任何叢集管理。
Master節點主要有三個服務kube-apiserver、kube-controller-manager和kube-scheduler,其中kube-controller-manager和kube-scheduler元件自身通過選擇機制已經實現了高可用,所以Master高可用主要針對kube-apiserver元件,而該元件是以HTTP API提供服務,因此對他高可用與Web伺服器類似,增加負載均衡器對其負載均衡即可,並且可水平擴容。
kube-apiserver高可用架構圖:
- Nginx是一個主流Web服務和反向代理伺服器,這裡用四層實現對apiserver實現負載均衡。
- Keepalived是一個主流高可用軟體,基於VIP繫結實現伺服器雙機熱備,在上述拓撲中,Keepalived主要根據Nginx執行狀態判斷是否需要故障轉移(偏移VIP),例如當Nginx主節點掛掉,VIP會自動繫結在Nginx備節點,從而保證VIP一直可用,實現Nginx高可用。
注:為了節省機器,這裡與K8s master節點機器複用。也可以獨立於k8s叢集之外部署,只要nginx與apiserver能通訊就行。
2.1 安裝軟體包(主/備)
yum install epel-release -y
yum install nginx keepalived -y
2.2 Nginx配置檔案(主/備一樣)
cat > /etc/nginx/nginx.conf << "EOF"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
# 四層負載均衡,為兩臺Master apiserver元件提供負載均衡
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /var/log/nginx/k8s-access.log main;
upstream k8s-apiserver {
server 192.168.31.61:6443; # Master1 APISERVER IP:PORT
server 192.168.31.62:6443; # Master2 APISERVER IP:PORT
}
server {
listen 16443; # 由於nginx與master節點複用,這個監聽埠不能是6443,否則會衝突
proxy_pass k8s-apiserver;
}
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80 default_server;
server_name _;
location / {
}
}
}
EOF
2.3 keepalived配置檔案(Nginx Master)
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # 修改為實際網絡卡名
virtual_router_id 51 # VRRP 路由 ID例項,每個例項是唯一的
priority 100 # 優先順序,備伺服器設定 90
advert_int 1 # 指定VRRP 心跳包通告間隔時間,預設1秒
authentication {
auth_type PASS
auth_pass 1111
}
# 虛擬IP
virtual_ipaddress {
192.168.31.88/24
}
track_script {
check_nginx
}
}
EOF
- vrrp_script:指定檢查nginx工作狀態指令碼(根據nginx狀態判斷是否故障轉移)
- virtual_ipaddress:虛擬IP(VIP)
準備上述配置檔案中檢查nginx執行狀態的指令碼:
cat > /etc/keepalived/check_nginx.sh << "EOF"
#!/bin/bash
count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
exit 1
else
exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh
2.4 keepalived配置檔案(Nginx Backup)
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_BACKUP
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51 # VRRP 路由 ID例項,每個例項是唯一的
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.88/24
}
track_script {
check_nginx
}
}
EOF
準備上述配置檔案中檢查nginx執行狀態的指令碼:
cat > /etc/keepalived/check_nginx.sh << "EOF"
#!/bin/bash
count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
exit 1
else
exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh
注:keepalived根據指令碼返回狀態碼(0為工作正常,非0不正常)判斷是否故障轉移。
2.5 啟動並設定開機啟動
systemctl daemon-reload
systemctl start nginx
systemctl start keepalived
systemctl enable nginx
systemctl enable keepalived
2.6 檢視keepalived工作狀態
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:04:f7:2c brd ff:ff:ff:ff:ff:ff
inet 192.168.31.80/24 brd 192.168.31.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.31.88/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe04:f72c/64 scope link
valid_lft forever preferred_lft forever
可以看到,在ens33網絡卡綁定了192.168.31.88 虛擬IP,說明工作正常。
2.7 Nginx+Keepalived高可用測試
關閉主節點Nginx,測試VIP是否漂移到備節點伺服器。
在Nginx Master執行 pkill nginx
在Nginx Backup,ip addr命令檢視已成功繫結VIP。
三、部署Etcd叢集
如果你在學習中遇到問題或者文件有誤可聯絡阿良~ 微信: k8init
Etcd 是一個分散式鍵值儲存系統,Kubernetes使用Etcd進行資料儲存,kubeadm搭建預設情況下只啟動一個Etcd Pod,存在單點故障,生產環境強烈不建議,所以我們這裡使用3臺伺服器組建叢集,可容忍1臺機器故障,當然,你也可以使用5臺組建叢集,可容忍2臺機器故障。
節點名稱 |
IP |
etcd-1 |
192.168.31.61 |
etcd-2 |
192.168.31.62 |
etcd-3 |
192.168.31.63 |
注:為了節省機器,這裡與K8s節點機器複用。也可以獨立於k8s叢集之外部署,只要apiserver能連線到就行。
3.1 準備cfssl證書生成工具
cfssl是一個開源的證書管理工具,使用json檔案生成證書,相比openssl更方便使用。
找任意一臺伺服器操作,這裡用Master節點。
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
3.2 生成Etcd證書
1. 自簽證書頒發機構(CA)
建立工作目錄:
mkdir -p ~/etcd_tls
cd ~/etcd_tls
自籤CA:
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
生成證書:
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
會生成ca.pem和ca-key.pem檔案。
2. 使用自籤CA簽發Etcd HTTPS證書
建立證書申請檔案:
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"192.168.31.61",
"192.168.31.62",
"192.168.31.63"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注:上述檔案hosts欄位中IP為所有etcd節點的叢集內部通訊IP,一個都不能少!為了方便後期擴容可以多寫幾個預留的IP。
生成證書:
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
會生成server.pem和server-key.pem檔案。
3.3 從Github下載二進位制檔案
下載地址:https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
3.4 部署Etcd叢集
以下在節點1上操作,為簡化操作,待會將節點1生成的所有檔案拷貝到節點2和節點3。
1. 建立工作目錄並解壓二進位制包
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
2. 建立etcd配置檔案
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.31.61:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.31.61:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.31.61:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.31.61:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.31.61:2380,etcd-2=https://192.168.31.62:2380,etcd-3=https://192.168.31.63:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
- ETCD_NAME:節點名稱,叢集中唯一
- ETCDDATADIR:資料目錄
- ETCDLISTENPEER_URLS:叢集通訊監聽地址
- ETCDLISTENCLIENT_URLS:客戶端訪問監聽地址
- ETCDINITIALADVERTISEPEERURLS:叢集通告地址
- ETCDADVERTISECLIENT_URLS:客戶端通告地址
- ETCDINITIALCLUSTER:叢集節點地址
- ETCDINITIALCLUSTER_TOKEN:叢集Token
- ETCDINITIALCLUSTER_STATE:加入叢集的當前狀態,new是新叢集,existing表示加入已有叢集
3. systemd管理etcd
cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
4. 拷貝剛才生成的證書
把剛才生成的證書拷貝到配置檔案中的路徑:
cp ~/etcd_tls/ca*pem ~/etcd_tls/server*pem /opt/etcd/ssl/
5. 啟動並設定開機啟動
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
6. 將上面節點1所有生成的檔案拷貝到節點2和節點3
scp -r /opt/etcd/ [email protected]:/opt/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
scp -r /opt/etcd/ [email protected]:/opt/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
然後在節點2和節點3分別修改etcd.conf配置檔案中的節點名稱和當前伺服器IP:
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" # 修改此處,節點2改為etcd-2,節點3改為etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.31.71:2380" # 修改此處為當前伺服器IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.31.71:2379" # 修改此處為當前伺服器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.31.71:2380" # 修改此處為當前伺服器IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.31.71:2379" # 修改此處為當前伺服器IP
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.31.71:2380,etcd-2=https://192.168.31.72:2380,etcd-3=https://192.168.31.73:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
最後啟動etcd並設定開機啟動,同上。
7. 檢視叢集狀態
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.31.61:2379,https://192.168.31.62:2379,https://192.168.31.63:2379" endpoint health --write-out=table
+----------------------------+--------+-------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+----------------------------+--------+-------------+-------+
| https://192.168.31.61:2379 | true | 10.301506ms | |
| https://192.168.31.63:2379 | true | 12.87467ms | |
| https://192.168.31.62:2379 | true | 13.225954ms | |
+----------------------------+--------+-------------+-------+
如果輸出上面資訊,就說明叢集部署成功。
如果有問題第一步先看日誌:/var/log/message 或 journalctl -u etcd
四、安裝Docker/kubeadm/kubelet【所有節點】
這裡使用Docker作為容器引擎,也可以換成別的,例如containerd
4.1 安裝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
systemctl enable docker && systemctl start docker
配置映象下載加速器:
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
docker info
4.2 新增阿里雲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
4.3 安裝kubeadm,kubelet和kubectl
由於版本更新頻繁,這裡指定版本號部署:
yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
systemctl enable kubelet
五、部署Kubernetes Master
如果你在學習中遇到問題或者文件有誤可聯絡阿良~ 微信: k8init
5.1 初始化Master1
生成初始化配置檔案:
cat > kubeadm-config.yaml << EOF
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: 9037x2.tcaqnpaqkra9vsbw
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.31.61
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8s-master1
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
certSANs: # 包含所有Master/LB/VIP IP,一個都不能少!為了方便後期擴容可以多寫幾個預留的IP。
- k8s-master1
- k8s-master2
- 192.168.31.61
- 192.168.31.62
- 192.168.31.63
- 127.0.0.1
extraArgs:
authorization-mode: Node,RBAC
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: 192.168.31.88:16443 # 負載均衡虛擬IP(VIP)和埠
controllerManager: {}
dns:
type: CoreDNS
etcd:
external: # 使用外部etcd
endpoints:
- https://192.168.31.61:2379 # etcd叢集3個節點
- https://192.168.31.62:2379
- https://192.168.31.63:2379
caFile: /opt/etcd/ssl/ca.pem # 連線etcd所需證書
certFile: /opt/etcd/ssl/server.pem
keyFile: /opt/etcd/ssl/server-key.pem
imageRepository: registry.aliyuncs.com/google_containers # 由於預設拉取映象地址k8s.gcr.io國內無法訪問,這裡指定阿里雲映象倉庫地址
kind: ClusterConfiguration
kubernetesVersion: v1.20.0 # K8s版本,與上面安裝的一致
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 # Pod網路,與下面部署的CNI網路元件yaml中保持一致
serviceSubnet: 10.96.0.0/12 # 叢集內部虛擬網路,Pod統一訪問入口
scheduler: {}
EOF
或者使用配置檔案引導:
kubeadm init --config kubeadm-config.yaml
...
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join 192.168.31.88:16443 --token 9037x2.tcaqnpaqkra9vsbw \
--discovery-token-ca-cert-hash sha256:b1e726042cdd5df3ce62e60a2f86168cd2e64bff856e061e465df10cd36295b8 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.31.88:16443 --token 9037x2.tcaqnpaqkra9vsbw \
--discovery-token-ca-cert-hash sha256:b1e726042cdd5df3ce62e60a2f86168cd2e64bff856e061e465df10cd36295b8
初始化完成後,會有兩個join的命令,帶有 --control-plane 是用於加入組建多master叢集的,不帶的是加入節點的。
拷貝kubectl使用的連線k8s認證檔案到預設路徑:
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 node
NAME STATUS ROLES AGE VERSION
k8s-master1 NotReady control-plane,master 6m42s v1.20.0
5.2 初始化Master2
將Master1節點生成的證書拷貝到Master2:
scp -r /etc/kubernetes/pki/ 192.168.31.62:/etc/kubernetes/
複製加入master join命令在master2執行:
kubeadm join 192.168.31.88:16443 --token 9037x2.tcaqnpaqkra9vsbw \
--discovery-token-ca-cert-hash sha256:b1e726042cdd5df3ce62e60a2f86168cd2e64bff856e061e465df10cd36295b8 \
--control-plane
拷貝kubectl使用的連線k8s認證檔案到預設路徑:
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 node
NAME STATUS ROLES AGE VERSION
k8s-master1 NotReady control-plane,master 28m v1.20.0
k8s-master2 NotReady control-plane,master 2m12s v1.20.0
注:由於網路外掛還沒有部署,還沒有準備就緒 NotReady
5.3 訪問負載均衡器測試
找K8s叢集中任意一個節點,使用curl檢視K8s版本測試,使用VIP訪問:
curl -k https://192.168.31.88:16443/version
{
"major": "1",
"minor": "20",
"gitVersion": "v1.20.0",
"gitCommit": "e87da0bd6e03ec3fea7933c4b5263d151aafd07c",
"gitTreeState": "clean",
"buildDate": "2021-02-18T16:03:00Z",
"goVersion": "go1.15.8",
"compiler": "gc",
"platform": "linux/amd64"
}
可以正確獲取到K8s版本資訊,說明負載均衡器搭建正常。該請求資料流程:curl -> vip(nginx) -> apiserver
通過檢視Nginx日誌也可以看到轉發apiserver IP:
tail /var/log/nginx/k8s-access.log -f
192.168.31.71 192.168.31.71:6443 - [02/Apr/2021:19:17:57 +0800] 200 423
192.168.31.71 192.168.31.72:6443 - [02/Apr/2021:19:18:50 +0800] 200 423
六、加入Kubernetes Node
在192.168.31.63(Node)執行。
向叢集新增新節點,執行在kubeadm init輸出的kubeadm join命令:
kubeadm join 192.168.31.88:16443 --token 9037x2.tcaqnpaqkra9vsbw \
--discovery-token-ca-cert-hash sha256:e6a724bb7ef8bb363762fbaa088f6eb5975e0c654db038560199a7063735a697
後續其他節點也是這樣加入。
注:預設token有效期為24小時,當過期之後,該token就不可用了。這時就需要重新建立token,可以直接使用命令快捷生成:kubeadm token create --print-join-command
七、部署網路元件
Calico是一個純三層的資料中心網路方案,是目前Kubernetes主流的網路方案。
部署Calico:
kubectl apply -f calico.yaml
kubectl get pods -n kube-system
等Calico Pod都Running,節點也會準備就緒:
kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master1 Ready control-plane,master 50m v1.20.0
k8s-master2 Ready control-plane,master 24m v1.20.0
k8s-node1 Ready <none> 20m v1.20.0
八、部署 Dashboard
Dashboard是官方提供的一個UI,可用於基本管理K8s資源。
kubectl apply -f kubernetes-dashboard.yaml
# 檢視部署
kubectl get pods -n kubernetes-dashboard
訪問地址:https://NodeIP:30001
建立service account並繫結預設cluster-admin管理員叢集角色:
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
使用輸出的token登入Dashboard。