配置keepalived实现Nginx高可用(单主、双主模式)
一、前置条件:
(1).Http作为后端web服务器:192.168.88.130:80 test.field.com
[root@testconf.d]# cat /var/www/html/index.html
<h1>Welcometo Http on test.field.com!!</h1>
<p>If yousee this page,test ok. </p>
<p><em>Thankyou!</em></p>
(2).Http作为后端web服务器:192.168.88.129:80 web2.field.com
[root@web2 ~]#cat /var/www/html/index.html
<h1>Welcometo Http on web2.field.com!!</h1>
<p>If yousee this page,test ok. </p>
<p><em>Thankyou!</em></p>
(3).nginx作为负载均衡服务器:192.168.88.130:8080 test.field.com
Nginx配置:实现简单负载均衡
Fascgi配置部分可直接删除
[root@testnginx]# vi nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user[$time_local] "$request" $http_host '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# proxy_cache_path /cache/nginx/ levels=1:2keys_zone=mycache:32m;
fastcgi_cache_path /cache/fastcgi/levels=1:1 keys_zone=fcgicache:10m inactive=3m max_size=1g;
upstream upservers {
server 192.168.88.129 weight=1;
server 192.168.88.130 weight=1;
}
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@test conf.d]#vi default.conf
server {
listen 8080;
server_name localhost;
add_header X-Via $server_addr;
add_header X-Cache$upstream_cache_status;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://upservers/;
}
location /field/ {
# proxy_cache mycache;
# proxy_cache_valid 200 1d;
# proxy_cache_valid 301 302 10m;
# proxy_cache_valid any 1m;
# proxy_cache_use_stale error http_500http_502 http_503 http_504;
proxy_pass http://upservers/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
}
location ~* \.(jpg|png|gif)$ {
# proxy_cache mycache;
# proxy_cache_valid 200 1d;
# proxy_cache_valid 301 302 10m;
# proxy_cache_valid any 1m;
# proxy_cache_use_stale error http_500 http_502 http_503 http_504;
proxy_pass http://upservers;
proxy_set_header X-Real-IP $remote_addr;
}
#error_page 404 /404.html;
# redirect server error pages to the staticpage /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listeningon 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000
#
location ~ \.php$ {
# fastcgi_cache off;
#开关fastcgi_cache
fastcgi_cache fcgicache;
#fastcgi_cache:定义使用哪个缓存空间
fastcgi_cache_key $host$request_uri;
#fastcgi_cache_key:定义fastcgi_cache的key,该例中以请求的URI作为缓存的key,Nginx会取这个key的md5作为缓存文件,如果设置了缓存哈希目录,Nginx会从后往前取相应的位数做为目录
fastcgi_cache_min_uses 1;
#fastcgi_cache_min_uses:URL经过多少次请求将被缓存
fastcgi_cache_use_stale error timeout invalid_header http_500;
#fastcgi_cache_use_stale:定义哪些情况下可以使用过期缓存
fastcgi_cache_valid 200 10m;
#fastcgi_cache_valid:定义哪些http头要使用缓存
fastcgi_cache_valid 302 3m;
fastcgi_cache_valid any 1m;
root /usr/share/nginx/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, ifApache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
(4).nginx作为负载均衡服务器:192.168.88.131:8080 www.field.com
Nginx配置:实现简单负载均衡
[root@wwwnginx]# vi nginx.conf
upstream upservers {
server 192.168.88.129 weight=1;
server 192.168.88.130 weight=1;
}
[root@wwwnginx]#
[root@wwwconf.d]# vi default.conf
location / {
proxy_pass http://upservers/;
}
二、配置keepalived实现Nginx高可用
案例1、配置keepalived实现Nginx高可用
[root@testkeepalived]# service keepalived stop;ssh www 'service keepalived stop'
停止 keepalived:[确定]
停止 keepalived:[确定]
[root@testkeepalived]# service keepalived status;ssh www 'service keepalived status'
keepalived 已停
keepalived 已停
scp到129web服务器,实现免密ssh
[root@test ~]#scp -p .ssh/id_rsa .ssh/authorized_keys web2:/root/.ssh
root@web2'spassword:
id_rsa 100% 1675 1.6KB/s 00:00
authorized_keys 100% 401 0.4KB/s 00:00
[root@test ~]#ssh web2
Last login: SunApr 22 23:45:43 2018 from test.field.com
编辑简单通知脚本
[root@testkeepalived]# vi notify.sh
#!/bin/bash
# Author:MageEdu <linuxedu@foxmail.com>
# description:An example of notify script
#
vip=192.168.88.80
contact='root@localhost'
notify() {
mailsubject="`hostname` to be $1: $vipfloating"
mailbody="`date '+%F %H:%M:%S'`: vrrptransition, `hostname` changed to be $1"
echo $mailbody | mail -s"$mailsubject" $contact
}
case"$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo 'Usage: `basename $0`{master|backup|fault}'
exit 1
;;
esac
[root@testkeepalived]# ll
总用量 24
-rw-r--r-- 1root root 4564 4月 23 00:05 keepalived.conf
-rw-r--r-- 1root root 3562 4月 22 16:23 keepalived.conf.backup
-rw-r--r-- 1root root 4212 4月 22 23:59 keepalived.conf.backup01
-rw-r--r-- 1root root 619 4月 2300:08 notify.sh
注意修改脚本权限,否则无法下发邮件
[root@test keepalived]# chmod +xnotify.sh
[root@test keepalived]#vi keepalived.conf
! ConfigurationFile for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test.field.com
vrrp_mcast_group4 224.18.0.200
}
vrrp_scriptchk_maintanance {
script "[[ -f /etc/keepalived/down]] && exit 1 || exit 0"
interval 1
#interval多长时间检查一次
#如果失败返回1权重-2
weight -2
}
vrrp_script chk_nginx {
script "killall -0 nginx &>/dev/null"
interval 1
#interval多长时间检查一次
#如果失败返回1权重-2
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 4e78bb3a
}
virtual_ipaddress {
192.168.88.80/16 dev eth0 label eth0:0
}
track_script {
#track_script 调用脚本
chk_nginx
}
notify_master"/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.shbackup"
notify_fault"/etc/keepalived/notify.sh fault"
}
[root@test keepalived]# scpnotify.sh www:/etc/keepalived/
notify.sh 100% 619 0.6KB/s 00:00
[root@test keepalived]# scpkeepalived.conf www:/etc/keepalived/
keepalived.conf
修改备机