文章目錄
- 實驗環境
- 實驗程序
- 部署master02節點
- 部署nginx負載均衡集群
- 實驗故障
實驗環境

角色分配:
主機名 IP地址 安裝軟體包
Master01:14.0.0.50 kube-apiserver kube-controller-manager kube-scheduler etcd
Master02:14.0.0.80 kube-apiserver kube-controller-manager kube-scheduler
Node01: 14.0.0.60 kubelet kube-proxy docker flannel etcd
Node02: 14.0.0.70 kubelet kube-proxy docker flannel etcd
Nginx01+keepalived:14.0.0.90 nginx、keepalived
Nginx02+keepalived:14.0.0.100 nginx、keepalived
實驗程序
在部署完單節點集群后,繼續部署多節點,前面的操作可以參考上一篇博客:
https://blog.csdn.net/chengu04/article/details/108899870
部署master02節點
1.關閉防火墻,關閉核心防護,關閉網路管理功能(生成環境中一定要關閉它)
[root@localhost ~]# hostnamectl set-hostname master02 #修改主機名
[root@localhost ~]# su
[root@master02 ~]# systemctl stop firewalld #關閉防火墻
[root@master02 ~]# setenforce 0 && sed -i "s/SELINUX=enforcing/SELNIUX=disabled/g" /etc/selinux/config #關閉核心防護
[root@master02 ~]# systemctl stop NetworkManager && systemctl disable NetworkManager #關閉網路管理功能
2.將master01節點的kubernetes組態檔和啟動腳本復制到master02節點
[root@master ~]# scp -r /opt/kubernetes/ root@14.0.0.80:/opt/
[root@master ~]# scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@14.0.0.80:/usr/lib/systemd/system/
3.將master01節點的etcd證書復制到master02節點(master02上一定要有etcd證書,用來與etcd通信)
[root@master ~]# scp -r /opt/etcd/ root@14.0.0.80:/opt
4.master02上修改apiserver組態檔中的IP地址
[root@master02 ~]# cd /opt/kubernetes/cfg/
[root@master02 cfg]# ls
kube-apiserver kube-controller-manager kube-scheduler token.csv
[root@master02 cfg]# vim kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://14.0.0.50:2379,https://14.0.0.60:2379,https://14.0.0.70:2379 \
--bind-address=14.0.0.80 \ #修改此處的系結IP地址
--secure-port=6443 \
--advertise-address=14.0.0.80 \ #修改此處的IP地址
...省略
5.啟動master02中的三個組件服務
[root@localhost cfg]# systemctl start kube-apiserver.service
[root@localhost cfg]# systemctl start kube-controller-manager.service
[root@localhost cfg]# systemctl start kube-scheduler.service
6.添加環境變數并查看狀態
[root@master02 ~]# echo export PATH=$PATH:/opt/kubernetes/bin >> /etc/profile
[root@master02 ~]# source /etc/profile
[root@master02 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
14.0.0.60 Ready <none> 23h v1.12.3
14.0.0.70 Ready <none> 23h v1.12.3
#看到兩個node節點Ready說明master02部署成功
部署nginx負載均衡集群
1.兩個nginx主機關閉防火墻和核心防護,編輯nginx的yum源
[root@localhost ~]# hostnamectl set-hostname nginx01 #修改主機名
[root@localhost ~]# su
[root@nginx01 ~]# systemctl stop firewalld && systemctl disable firewalld #關閉防火墻與核心防護
[root@nginx01 ~]# setenforce 0 && sed -i "s/SELINUX=enforcing/SELNIUX=disabled/g" /etc/selinux/config
[root@nginx01 ~]# vi /etc/yum.repos.d/nginx.repo #編輯nginx的yum源
[nginx]
name=nginx.repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
enabled=1
gpgcheck=0
[root@nginx01 ~]# yum list
2.兩臺nginx主機安裝nginx并開啟四層轉發(僅展示nginx01的操作)
[root@nginx01 ~]# yum -y install nginx '//安裝nginx'
[root@nginx01 ~]# vi /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/access.log main;
upstream k8s-apiserver { #定義代理的IP地址及埠
server 14.0.0.50:6443;
server 14.0.0.80:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
...省略內容
3.啟動nginx服務
[root@nginx01 ~]# nginx -t #檢查nginx語法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx01 ~]# systemctl start nginx #開啟服務
[root@nginx01 ~]# systemctl status nginx
[root@nginx01 ~]# netstat -ntap |grep nginx #會檢測出來6443埠
tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 1849/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1849/nginx: master
4.兩臺nginx主機部署keepalived服務(僅展示節點nginx01的配置)
[root@nginx01 ~]# yum -y install keepalived
[root@nginx01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx { #定義一個函式check_nginx
script "/usr/local/nginx/sbin/check_nginx.sh" #函式內容為一個檢測nginx服務是否存活的腳本
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL01 #定義該主機在群集中的id,nginx02需要命名為不一樣的
}
vrrp_instance VI_1 {
state MASTER #nginx02節點命名為BACKUP
interface ens33 #修改網卡名,centos7開始為ens33,centos6為eth0
virtual_router_id 51
priority 100 #nginx02節點優先級設為90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #設定VIP
14.0.0.88
}
track_script { #該vrrp實體VI_1呼叫上面定義的函式check_nginx
check_nginx
}
}
5.創建監控nginx行程的腳本,啟動keepalived服務,查看VIP地址
[root@nginx01 ~]# mkdir -p /usr/local/nginx/sbin/ #創建監控腳本目錄
[root@nginx01 ~]# vim /usr/local/nginx/sbin/check_nginx.sh #撰寫監控腳本組態檔
count=$(ps -ef |grep nginx |egrep -cv "grep|$$") #查看nginx行程
if [ "$count" -eq 0 ];then #如果nginx行程關閉了,則關閉keepalived服務
systemctl stop keepalived
fi
[root@nginx01 ~]# chmod +x /usr/local/nginx/sbin/check_nginx.sh
[root@nginx01 ~]# systemctl start keepalived #開啟服務
[root@nginx01 ~]# systemctl status keepalived
[root@nginx01 ~]# ip a #兩個nginx服務器查看IP地址
可以發現VIP在節點nginx01上
[root@nginx02 ~]# ip a
6.驗證漂移地址
[root@nginx01 ~]# pkill nginx #關閉nginx01節點的nginx服務
[root@nginx01 ~]# systemctl status keepalived #發現keepalived服務關閉了
[root@nginx02 ~]# ip a #現在發現VIP地址漂移到nginx02上了
7.恢復漂移地址的操作
[root@nginx01 ~]# systemctl start nginx
[root@nginx01 ~]# systemctl start keepalived #先開啟nginx,在啟動keepalived服務
[root@nginx01 ~]# ip a #再次查看,發現VIP又回到了nginx01節點上
8.修改兩個node節點組態檔(bootstrap.kubeconfig ),使用VIP地址,僅展示node01節點的操作
[root@node01 ~]# vi /opt/kubernetes/cfg/bootstrap.kubeconfig
server: https://14.0.0.88:6443 #此地址修改為VIP地址
[root@node01 ~]# vi /opt/kubernetes/cfg/kubelet.kubeconfig
server: https://14.0.0.88:6443 #此地址修改為VIP地址
[root@node01 ~]# vi /opt/kubernetes/cfg/kube-proxy.kubeconfig
server: https://14.0.0.88:6443 #此地址修改為VIP地址
9.重啟兩個node節點的服務
[root@node01 ~]# systemctl restart kubelet
[root@node01 ~]# systemctl restart kube-proxy
[root@node01 ~]# cd /opt/kubernetes/cfg/
[root@node01 cfg]# grep 88 * #過濾當前目錄下所有內容中是否包含88,如下代表VIP修改成功
bootstrap.kubeconfig: server: https://14.0.0.88:6443
kubelet.kubeconfig: server: https://14.0.0.88:6443
kube-proxy.kubeconfig: server: https://14.0.0.88:6443
10.在節點nginx01上查看nginx的日志,查看負載均衡是否生效
[root@nginx01 ~]# vim /var/log/nginx/access.log #下面的日志是重啟服務后產生的
14.0.0.60 14.0.0.50:6443 - [30/Sep/2020:11:01:22 +0800] 200 15319
14.0.0.60 14.0.0.50:6443 - [30/Sep/2020:11:01:23 +0800] 200 1115
14.0.0.60 14.0.0.80:6443 - [30/Sep/2020:11:01:23 +0800] 200 1115
14.0.0.60 14.0.0.80:6443 - [30/Sep/2020:11:01:31 +0800] 200 3010
14.0.0.70 14.0.0.50:6443 - [30/Sep/2020:11:01:38 +0800] 200 1115
14.0.0.70 14.0.0.50:6443 - [30/Sep/2020:11:01:38 +0800] 200 1114
#nginx負載均衡生效后,會進行狀態檢查
11.master節點測驗創建pod
[root@master01 ~]# kubectl run nginx --image=nginx #創建一個運行nginx服務的pod
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created
[root@master01 ~]# kubectl get pods #查看狀態,是正在創建
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-5s6h7 0/1 ContainerCreating 0 13s
[root@master01 ~]# kubectl get pods #過會兒再次查看,發現pod已經創建完成,在master02節點也可以查看到
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-5s6h7 1/1 Running 0 23s
12.查看剛才創建的運行nginx服務的pod的日志
[root@master01 ~]# kubectl logs nginx-dbddb74b8-5s6h #查看pod日志
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( pods/log nginx-dbddb74b8-5s6h7)
#發現是因為使用了system:anonymous(匿名)用戶進行操作,沒有權限
[root@master01 ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
#將集群中的匿名用戶系結到管理員用戶,使其擁有權限
[root@master ~]# kubectl logs nginx-dbddb74b8-5s6h #此時可以查看,這時沒有日志產生
13.訪問node節點的pod中的web業務,從而產生日志,并在兩個master節點查看
[root@master ~]# kubectl get pods -o wide #查看pod的完整資訊(IP資訊)
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-dbddb74b8-5s6h7 1/1 Running 0 6m29s 172.17.26.2 14.0.0.60 <none>
[root@node01 ~]# curl 172.17.26.2 #在對應的node節點訪問pod
[root@master ~]# kubectl logs nginx-dbddb74b8-5s6h7 #再次在master節點查看日志情況,master02節點同樣可以查看到
172.17.26.1 - - [30/Apr/2020:17:38:48 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
實驗故障
搭建完k8s集群后,在master01上創建了一個運行nginx服務的pod,查看其日志時出現如下報錯:
[root@master01 ~]# kubectl logs nginx-dbddb74b8-5s6h #查看pod日志命令
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( pods/log nginx-dbddb74b8-5s6h7)
故障原因:
默認會使用system:anonymous(匿名)用戶進行操作,而該用戶沒有權限
解決方法:
[root@master01 ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
#將集群中的匿名用戶系結到管理員用戶,使其擁有權限
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/157353.html
標籤:其他
下一篇:windows域的創建
