k8s多節點master部署(負載均衡+keepalived)
阿新 • • 發佈:2021-10-29
目錄
接上篇部落格:單節點master部署
一、多節點master2節點部署
1.1 從master01節點上拷貝證書檔案、配置檔案和服務管理檔案到master02
scp -r /opt/etcd/ [email protected]:/opt/ scp -r /opt/kubernetes/ [email protected]:/opt scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service [email protected]:/usr/lib/systemd/system/
1.2 修改配置檔案kube-apiserver中的IP
vim /opt/kubernetes/cfg/kube-apiserver KUBE_APISERVER_OPTS="--logtostderr=true \ --v=4 \ --etcd-servers=https://192.168.80.11:2379,https://192.168.80.12:2379,https://192.168.80.13:2379 \ --bind-address=192.168.80.16 \ #修改master02的ip --secure-port=6443 \ --advertise-address=192.168.80.16 \ #修改master02的ip
1.3 在master02節點上啟動各服務並設定開機自啟
systemctl enable --now kube-apiserver.service
systemctl enable --now kube-controller-manager.service
systemctl enable --now kube-scheduler.service
1.4 檢視node節點狀態
ln -s /opt/kubernetes/bin/* /usr/local/bin/
kubectl get nodes
kubectl get nodes -o wide #-o=wide:輸出額外資訊;對於Pod,將輸出Pod所在的Node名
//此時在master02節點查到的node節點狀態僅是從etcd查詢到的資訊,而此時node節點實際上並未與master02節點建立通訊連線,因此需要使用一個VIP把node節點與master節點都關聯起來
二、負載均衡部署+keepalived高可用(192.168.80.14/15)
2.1 配置nginx的官方線上yum源,配置本地nginx的yum源
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOF
yum install nginx -y
2.2 修改nginx配置檔案,配置四層反向代理負載均衡
##指定k8s群集2臺master的節點ip和6443埠
vim /etc/nginx/nginx.conf
events {
worker_connections 1024;
}
#新增
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.80.11:6443;
server 192.168.80.16:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
http {
......
2.3 檢查配置檔案語法並啟動nginx服務
1.檢查配置檔案語法
nginx -t
2.啟動nginx服務,檢視已監聽6443埠
systemctl enable --now nginx
netstat -natp | grep nginx
2.4 部署keepalived服務
yum install keepalived -y
2.5 修改keepalived配置檔案(額外編寫健康檢查指令碼)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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 #lb01節點的為NGINX_MASTER,lb02節點的為 NGINX_BACKUP
}
#新增一個週期性執行的指令碼
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh" #指定檢查nginx存活的指令碼路徑
}
vrrp_instance VI_1 {
state MASTER #lb01節點的為 MASTER,lb02節點的為 BACKUP
interface ens33 #指定網絡卡名稱 ens33
virtual_router_id 51 #指定vrid,兩個節點要一致
priority 100 #lb01節點的為 100,lb02節點的為 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.100 #指定 VIP
}
track_script {
check_nginx #指定vrrp_script配置的指令碼
}
}
==========================================================
##建立nginx狀態檢查指令碼
vim /etc/nginx/check_nginx.sh
#!/bin/bash
#egrep -cv "grep|$$" 用於過濾掉包含grep 或者 $$ 表示的當前Shell程序ID
count=$(ps -ef | grep nginx | egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi
chmod +x /etc/nginx/check_nginx.sh
2.6 啟動keepalived服務(一定要先啟動了nginx服務,再啟動keepalived服務)
systemctl enable --now nginx
systemctl enable --now keepalived
ip a #檢視VIP是否生成
三、修改node節點上的kubeconfig配置檔案
//修改bootstrap.kubeconfig,kubelet.kubeconfig配置檔案為VIP
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig
server: https://192.168.80.100:6443
vim kubelet.kubeconfig
server: https://192.168.80.100:6443
vim kube-proxy.kubeconfig
server: https://192.168.80.100:6443
//重啟kubelet和kube-proxy服務
systemctl restart kubelet.service
systemctl restart kube-proxy.service
四、在master01節點上操作
1.測試建立pod
kubectl create deployment nginx-test --image=nginx
2.檢視Pod的狀態資訊
kubectl get pod
kubectl get pods -o wide
3.在對應網段的node節點上操作,可以直接使用瀏覽器或者curl命令訪問
curl 172.17.47.2
4.這時在master01節點上檢視nginx日誌,發現沒有許可權檢視
kubectl logs nginx-test-7d965f56df-q8qlp
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) (nginx-test-7d965f56df-q8qlp)
5.在master01節點上,將cluster-admin角色授予使用者system:anonymous
命令:kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
clusterrolebinding.rbac.authorization.k8s.io/cluster-system-anonymous created
6.再次檢視nginx日誌
kubectl logs nginx-test-7d965f56df-q8qlp