使用nginx配置負載均衡

1.下載nginx

  • 準備環境

    yum install gcc-c++
    yum install -y pcre pcre-devel
    yum install -y openssl openssl-devel
    
  • 準備nginx:
    下載:# wget http://nginx.org/download/nginx-1.8.1.tar.gz
    解壓:# tar -zxvf nginx-1.8.1.tar.gz
    進入目錄:# cd nginx-1.8.1

  • 配置nginx:# ./configure --prefix=/usr/src/nginx --with-http_stub_status_module --with-http_ssl_module

  • 編譯安裝:# make && make install

  • 查看nginx版本:
    進入安裝目錄:# cd /usr/src/nginx/sbin
    查看版本:# ./nginx -v

  • 設置開機啓動
    編輯/etc/rc.local:# vim /etc/rc.local
    添加一行:/usr/src/nginx/sbin/nginx,之後,開機會自動啓動nginx

  • nginx 命令
    進入nginx目錄:# cd /usr/src/nginx/sbin
    啓動服務:# ./nginx
    停止服務:# ./nginx -s stop
    重啓服務:# ./nginx -s reopen
    重新載入配置文件:# ./nginx -s reload

2.配置負載均衡


#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 {
    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;

	upstream minio{
	        server 172.28.8.65:9000;
	        server 172.28.8.61:9000;
	        server 172.28.8.62:9000;
	        server 172.28.8.63:9000;
	}

    server {
        listen 9001;
        server_name 172.28.8.111;
        location / {
                proxy_pass http://minio;
                proxy_set_header Host $http_host;
                client_max_body_size 1000m;
        }
	}

}

3.配置雙主熱備

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYCAT_HA
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 52   #路由器標識,MASTER和BACKUP必須是一致的
    priority 101           #定義優先級,數字越大,優先級越高,在同一個vrrp_instance下,MASTER的優先級必須大於BACKUP的優先級。這樣MASTER故障恢復後,就可以將VIP資源再次搶回來 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.28.8.111/24
    }
}

virtual_server 172.28.8.111 80 {
    delay_loop 6
    protocol TCP

    real_server 172.28.8.65 80 {
        notify_down "kill -9 $(cat /var/run/keepalived.pid)" 
        TCP_CHECK { 
            connect_port 80  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }
    }
}

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