springboot 負載均衡 nignx+高可用 keepalived

 

1>搭建redis 主從+哨兵

2>搭建nginx 主從+keepalived 高可用

3>源碼:https://github.com/hd19940104/java

4>各個配置

一:redis:先配置主從:之後在每一個節點配置哨兵:

# 這個是Redis6379配置內容,其他文件同理新增然後改一下端口即可,26380,和 26381。

#當前Sentinel服務運行的端口
port 26379
# 哨兵監聽的主服務器
#sentinel myid 551269539259ffd5a9fa767401b7c6a37c9b5ccd
# 3s內mymaster無響應,則認爲mymaster宕機了
sentinel myid b8ec6b8a30e649678fd7346ed2e85108adbd5232
#如果10秒後,mysater仍沒啓動過來,則啓動failover
sentinel monitor mymaster 192.168.1.114 6379 1
# 執行故障轉移時, 最多有1個從服務器同時對新的主服務器進行同步
sentinel down-after-milliseconds mymaster 3000
# Generated by CONFIG REWRITE
dir "/myredis"
#sentinel config-epoch mymaster 0
#sentinel leader-epoch mymaster 0
#sentinel current-epoch 0
protected-mode no
sentinel auth-pass mymaster admin
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 1
sentinel known-slave mymaster 192.168.1.103 6379
sentinel known-slave mymaster 192.168.1.113 6379
sentinel current-epoch 1

二:

安裝nginx:解壓

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m;
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    

    # another virtual host using mix of IP-, name-, and port-based configuration
     upstream backserver {
     server 192.168.1.114:8080 weight=1;
     server 192.168.1.114:8081 weight=1;
     server 192.168.1.114:8082 weight=1;
    }

    server {
    listen    80;
    server_name www.test.com;
    location / {
        proxy_pass http://backserver;
        index    login.html;
        }
    }        

    
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

三:安裝keepalived(每個nginx服務器安裝)

tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/

yum install -y openssl openssl-devel(需要安裝一個軟件包)

cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

make && make install

將keepalived安裝成Linux系統服務,因爲沒有使用keepalived的默認安裝路徑(默認路徑:/usr/local),安裝完成之後,需要做一些修改工作:

首先創建文件夾,將keepalived配置文件進行復制:

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

然後複製keepalived腳本文件:

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

ln -s /usr/local/sbin/keepalived /usr/sbin/

ln -s /usr/local/keepalived/sbin/keepalived /sbin/

可以設置開機啓動:chkconfig keepalived on

 

service keepalived start

service keepalived stop

 

 

#!/bin/bash
A=`ps -C nginx ¨Cno-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

 

 

---------------

! Configuration File for keepalived
global_defs {
   router_id hadoop106 ##標識節點的字符串,通常爲hostname
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" #運行腳本,腳本內容下面有,就是起到一個nginx宕機以後,自動開啓服務
    interval 2 #檢測時間間隔
    weight -20 #如果條件成立的話,則權重 -20
}
# 定義虛擬路由,VI_1 爲虛擬路由的標示符,自己定義名稱
vrrp_instance VI_1 {
    state MASTER #來決定主從
    interface eth0 # 綁定虛擬 IP 的網絡接口,根據自己的機器填寫
    virtual_router_id 119 # 虛擬路由的 ID 號, 兩個節點設置必須一樣
    mcast_src_ip 192.168.1.106 #填寫本機ip
    priority 100 # 節點優先級,主要比從節點優先級高
    nopreempt # 優先級高的設置 nopreempt 解決異常恢復後再次搶佔的問題
    advert_int 1 # 組播信息發送間隔,兩個節點設置必須一樣,默認 1s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 將 track_script 塊加入 instance 配置塊
    track_script {
        chk_nginx #執行 Nginx 監控的服務
    }

    virtual_ipaddress {
        192.168.1.200 # 虛擬ip,也就是解決寫死程序的ip怎麼能切換的ip,也可擴展,用途廣泛。可配置多個。
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章