#MASTER config: 172.16.20.116
[root@localhost conf]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
songshuyou@opzoon.com
}
notification_email_from songshuyou@opzoon.com
smtp_server mail.opzoon.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
notify_master "/etc/keepalived/master.sh"
notify_backup "/etc/keepalived/backup.sh"
notify_fault "/etc/keepalived/fault.sh"
track_script {
check_nginx
}
virtual_ipaddress {
172.16.20.118/22 dev ens192
}
}
#BACKUP CONFIG: 172.16.20.117
! Configuration File for keepalived
global_defs {
notification_email {
songshuyou@opzoon.com
}
notification_email_from face-test@opzoon.com
smtp_server mail.opzoon.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
notify_master "/etc/keepalived/master.sh"
notify_backup "/etc/keepalived/backup.sh"
notify_fault "/etc/keepalived/fault.sh"
track_script {
check_nginx
}
virtual_ipaddress {
172.16.20.118/22 dev ens192
}
}
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
[root@localhost keepalived]# more backup.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
[root@localhost keepalived]# more fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Fault]" >> $LOGFILE
date >> $LOGFILE
[root@localhost keepalived]# more nginx_pid.sh
#!/bin/bash
#while :
#do
nginxpid=`lsof -i:80 |wc -l`
if [ $nginxpid -eq 0 ];then
/usr/bin/systemctl restart nginx.service
# sleep 5
nginxpid=`lsof -i:80 |wc -l`
echo $nginxpid
if [ $nginxpid -eq 0 ];then
/usr/bin/systemctl stop keepalived.service
fi
fi
#sleep 5
#done
##############################nginx config############################
[root@localhost opt]# vim /opt/opzoonface/openresty/nginx/conf/nginx.conf
#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 {
use epoll;
worker_connections 65536;
}
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;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /home/data/tmp;
proxy_cache_path /home/data/cache levels=1:2 keys_zone=cache_one:200m inactive=2d max_size=30g;
worker_connections 65536;
}
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;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /home/data/tmp;
proxy_cache_path /home/data/cache levels=1:2 keys_zone=cache_one:200m inactive=2d max_size=30g;
upstream tomcatsite {
ip_hash;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /home/data/tmp;
proxy_cache_path /home/data/cache levels=1:2 keys_zone=cache_one:200m inactive=2d max_size=30g;
upstream tomcatsite {
ip_hash;
server 172.16.20.116:8080;
server 172.16.20.117:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcatsite;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server{
listen 9090;
server_name localhost;
client_max_body_size 1024M;
keepalive_timeout 1800s;
location / {
root /home/data/pic;
client_body_temp_path /home/data/client_temp;
dav_methods PUT DELETE;
create_full_put_path on;
dav_access group:rw all:r;
limit_except GET {
allow all;
#deny all;
}
}
access_log off;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#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;
# }
#}
}