k8s-多master-nginx-keepalived配置


結構圖(提交規划好虛擬vip地址)

 1、Nginx主備配置(4層復制)在http外面加一個stream

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.56.11:6443;
        server 192.168.56.14:6443;
    }
    server {
                listen 6443;
                proxy_pass k8s-apiserver;
    }
    } 
 1 user  nginx;
 2 worker_processes  1;
 3 
 4 error_log  /var/log/nginx/error.log warn;
 5 pid        /var/run/nginx.pid;
 6 
 7 
 8 events {
 9     worker_connections  1024;
10 }
11 
12 stream {
13 
14    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
15     access_log  /var/log/nginx/k8s-access.log  main;
16 
17     upstream k8s-apiserver {
18         server 192.168.56.11:6443;
19         server 192.168.56.14:6443;
20     }
21     server {
22                 listen 6443;
23                 proxy_pass k8s-apiserver;
24     }
25     }
26 
27 http {
28     include       /etc/nginx/mime.types;
29     default_type  application/octet-stream;
30 
31     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
32                       '$status $body_bytes_sent "$http_referer" '
33                       '"$http_user_agent" "$http_x_forwarded_for"';
34 
35     access_log  /var/log/nginx/access.log  main;
36 
37     sendfile        on;
38     #tcp_nopush     on;
39 
40     keepalive_timeout  65;
41 
42     #gzip  on;
43 
44     include /etc/nginx/conf.d/*.conf;
45 }
完整配置

2、主keepalived配置

! 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 "/usr/local/nginx/sbin/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state MASTER 
    interface eth0
    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.56.17/24 
    } 
    track_script {
        check_nginx
    } 
}

3、備keepalived配置

! 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 "/usr/local/nginx/sbin/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state BACKUP
    interface eth0
    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.56.17/24 
    } 
    track_script {
        check_nginx
    } 
}

4、主備檢查腳本

check_nginx.sh

count=$(ps -ef|grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    systemctl stop keepalived
fi


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM