nginx load balancing high availability configuration
Server A: 172.16.100.2
Server B: 172.16.100.3
First, after the first and keepalived nginx are installed on two servers, and then performs the following operations
Configuration keepalived availability (preemption)
server A: 172.16.100.2
Server B: 172.16.100.3
the VIP: 172.16.100.14
First, the server A and server B are each installed keepalived, installed after the following operations continue.
Executed on the server A
1. scripting nginx status monitoring
echo '#!/bin/bash
count = `ps aux | grep -v grep | grep nginx | wc -l`
if [ $count > 0 ];then
exit 0
else
exit 1
fi' > /etc/keepalived/checknginx.sh
Configuring globally defined block
global_defs {
router_id nginxserver01
}
3.keepalived can do monitoring of network failures and keepalived itself, the configuration script mysql service status monitoring
Effect on weight priority details, refer https://www.cnblogs.com/arjenlee/p/9258188.html
vrrp_script checkmysql
{
Script "/etc/keepalived/checknginx.sh"
interval The. 3
weight -20
}
4. Configuration example of defining block VRRP
vrrp_instance instance1 {
state MASTER
virtual_router_id 1
interface eth0
mcast_src_ip 172.16.100.2
priority 100
advert_int 3
nopreempt
authentication {
auth_type PASS
auth_pass kee1234
}
virtual_ipaddress {
172.16.100.14
}
track_script {
checknginx
}
}
Executed on the server B
1. scripting nginx status monitoring
echo '#!/bin/bash
count = `ps aux | grep -v grep | grep nginx | wc -l`
if [ $count > 0 ];then
exit 0
else
exit 1
fi' > /etc/keepalived/checknginx.sh
Configuring globally defined block
global_defs {
router_id nginxserver02
}
3.keepalived can do monitoring of network failures and keepalived itself, the configuration script mysql service status monitoring
Effect on weight priority details, refer https://www.cnblogs.com/arjenlee/p/9258188.html
vrrp_script checknginx
{
Script "/etc/keepalived/checknginx.sh"
interval The. 3
weight -20
}
4. Configuration example of defining block VRRP
vrrp_instance instance1 {
state BACKUP
virtual_router_id 1
interface eth0
mcast_src_ip 172.16.100.3
priority 90
advert_int 3
nopreempt
authentication {
auth_type PASS
auth_pass kee1234
}
virtual_ipaddress {
172.16.100.14
}
track_script {
checknginx
}
}
nginx http proxy configuration (session remains open, secure configuration)
cat << EOF > /usr/local/myapps/nginx/http8888.conf
upstream http8888{
sticky;
server 172.16.100.2:8080;
}
server{
listen 8888;
charset utf-8;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffering off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://http8888;
}
access_log logs/http8888.access.log access;
}
The session remains
1. Source address hashing algorithm
Instruction: ip hash;
inadequate:
when the back-end server is down, session will be lost;
the client from the same LAN will be forwarded to the same back-end server, can lead to load imbalances;
does not apply to CDN network, does not apply to the preceding there are situations agent.
2.cookie session remains
Advantages:
can achieve the same LAN client load balancing
directive: sticky;
some parameters of sticky, such as sticky buffer duration, acting like. Here detail can check the readme sticky unzip the bag
to enable the session cookie to maintain sticky modules need to be added.
(To add a new module: stop nginx, nginx backup, before compiling belt parameter and a new module parameters recompiling the compiled binary file containing the new module nginx cover sbin directory nginx, nginx can restart)
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
the tar-zxf master.tar.gz
Music Videos-Goodies-Nginx Nginx-Sticky-Module1-ng-08a395c66e42 Sticky-Module1-Nginx
PS -ef | grep Nginx | grep -v grep | awk 'Print $ {2}' | xargs the kill -9
CP -Ar / usr / local / MyApps / Nginx / usr / local / MyApps / nginx_bak`date "the Y +% m% D%" `
CD /root/nginx-1.15.9
Nginx -V
./configure --prefix=/usr/local/myapps/nginx --sbin-path=/usr/sbin/nginx --pid-path=/usr/local/myapps/nginx/logs/nginx.pid --error-log-path=/usr/local/myapps/nginx/logs/error.log --http-log-path=/usr/local/myapps/nginx/logs/access.log --with-pcre=/usr/local/myapps/pcre-8.43 --with-zlib=/usr/local/myapps/zlib-1.2.11 --with-http_stub_status_module --with-stream --add-module=/root/nginx-sticky-module
/usr/bin/cp /root/nginx-1.15.9/objs/nginx /usr/sbin/
nginx -c /usr/local/myapps/nginx/conf/nginx.conf