您好,登錄后才能下訂單哦!
接上一篇多節點部署(2)部署負載均衡
負載均衡
Nginx1:192.168.13.128/24
Nginx2:192.168.13.129/24
Master節點
master1:192.168.13.131/24 kube-apiserver kube-controller-manager kube-scheduler etcd
master2:192.168.13.130/24 kube-apiserver kube-controller-manager kube-scheduler etcd
Node節點
node1:192.168.13.132/24 kubelet kube-proxy docker flannel etcd
node2:192.168.13.133/24 kubelet kube-proxy docker flannel etcd
[root@nginx01 ~]# rz -E ##上傳nginx腳本和keepalive配置文件
[root@nginx01 ~]# ls
keepalived.conf nginx.sh
[root@nginx01 ~]# systemctl stop firewalld.service ##關閉防火墻
[root@nginx01 ~]# setenforce 0
vim nginx.sh ##nginx腳本
cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOF
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 10.0.0.3:6443;
server 10.0.0.8:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
[root@nginx01 ~]# vim /etc/yum.repos.d/nginx.repo ##配置nginx的yum源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
[root@nginx01 ~]# yum list ##更新yum
[root@nginx01 ~]# yum install -y nginx ##下載Nginx
[root@nginx01 ~]# 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.13.131:6443; ##master01地址
server 192.168.13.130:6443; ##master02地址
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
[root@nginx01 ~]# systemctl start nginx ##開啟nginx服務
##可以修改/usr/share/nginx/html/index.html主頁區分主master從backup
##瀏覽器查看兩個nginx網站
[root@nginx01 ~]# yum install -y keepalived ##安裝keepalived服務
[root@nginx01 ~]# cp keepalived.conf /etc/keepalived/keepalived.conf ##復制配置文件
[root@nginx01 ~]# vim /etc/keepalived/keepalived.conf ##主master的配置文件修改
! Configuration File for keepalived
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 NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh" ##nginx檢查腳本,需要自己去編輯的
}
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
}
virtual_ipaddress {
192.168.13.100/24 ##虛擬ip地址
}
track_script {
check_nginx
}
}
[root@nginx02 ~]# vim /etc/keepalived/keepalived.conf ##備backup的配置文件修改
! Configuration File for keepalived
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 NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh" ##nginx檢查腳本
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51 # VRRP 路由 ID實例,每個實例是唯一的
priority 90 # 優先級,備服務器設置 90
advert_int 1 # 指定VRRP 心跳包通告間隔時間,默認1秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.13.100/24
}
track_script {
check_nginx
}
}
[root@nginx01 ~]# vim /etc/nginx/check_nginx.sh ##編輯nginx檢查腳本
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi
[root@nginx01 ~]# chmod +x /etc/nginx/check_nginx.sh ##給執行權限
[root@nginx01 ~]# systemctl start keepalived.service ##開啟服務
[root@nginx01 ~]# ip a ##查看地址信息
inet 192.168.13.100/24 scope global secondary ens33 ##漂移地址在master上
##在nginx01中關閉nginx服務,此時keepalived服務也關閉了(check_nginx.sh)
[root@nginx01 ~]# pkill nginx
##在nginx02中查看漂移地址
[root@nginx02 ~]# ip a ##此時13.100在nginx02上
##在nginx01上恢復nginx和keepalived服務,查看漂移地址
[root@nginx01 ~]# systemctl start nginx
[root@nginx01 ~]# systemctl start keepalived.service
[root@nginx01 ~]# ip a ##此時漂移地址又到了nginx01上
##用瀏覽器訪問虛擬ip
[root@node01 ~]# vim /opt/kubernetes/cfg/bootstrap.kubeconfig
server: https://192.168.13.100:6443
[root@node01 ~]# vim /opt/kubernetes/cfg/kubelet.kubeconfig
server: https://192.168.13.100:6443
[root@node01 ~]# vim /opt/kubernetes/cfg/kube-proxy.kubeconfig
server: https://192.168.13.100:6443
[root@node01 ~]# cd /opt/kubernetes/cfg/ ##切換到配置文件目錄
[root@node01 cfg]# grep 100 * ##查看修改的情況
bootstrap.kubeconfig: server: https://192.168.13.100:6443
kubelet.kubeconfig: server: https://192.168.13.100:6443
kube-proxy.kubeconfig: server: https://192.168.13.100:6443
[root@node01 cfg]# systemctl restart kubelet.service ##重啟兩個服務
[root@node01 cfg]# systemctl restart kube-proxy.service
##在nginx01上查看訪問日志
[root@nginx01 ~]# tail /var/log/nginx/k8s-access.log
192.168.13.132 k8s-apiserver - [10/Feb/2020:13:17:11 +0800] 502 0
192.168.13.132 k8s-apiserver - [10/Feb/2020:13:17:11 +0800] 502 0
192.168.13.132 k8s-apiserver - [10/Feb/2020:13:17:11 +0800] 502 0
192.168.13.133 k8s-apiserver - [10/Feb/2020:13:17:11 +0800] 502 0
192.168.13.133 k8s-apiserver - [10/Feb/2020:13:17:11 +0800] 502 0
[root@master01 ~]# kubectl get pods ##查看pod
No resources found.
[root@master01 ~]# kubectl run nginx --image=nginx ##創建pod
[root@master01 ~]# kubectl get pods ##查看pod狀態是正在創建的狀態
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-brjlj 0/1 ContainerCreating 0 86s
[root@master01 ~]# kubectl get pods ##此時pod是已經運行的狀態
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-brjlj 1/1 Running 0 87s
[root@master01 ~]# kubectl logs nginx-dbddb74b8-brjlj ##此時日志文件不能查看
[root@master01 ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
##提權后日志文件就可以查看了
[root@master01 ~]# kubectl get pods -o wide ##查看pod網絡,此時pod容器分配到node01上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-dbddb74b8-brjlj 1/1 Running 0 5m18s 172.17.45.2 192.168.13.132 <none>
[root@node01 cfg]# curl 172.17.45.2 ##此時就可以訪問nginx信息
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
##用node01虛擬機的瀏覽器訪問
[root@master01 ~]# kubectl logs nginx-dbddb74b8-brjlj
172.17.45.1 - - [10/Feb/2020:05:29:23 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。