keepalived+nginx
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived+nginx相关的知识,希望对你有一定的参考价值。
高集成:keepalived
负载均衡:nginx
1、服务器IP
Client: 172.25.254.25
Keepalived+Nginx1: 172.25.254.115 Vip: 172.25.254.100
Keepalived+Nginx2: 172.25.254.215 Vip: 172.25.254.100
二、安装
1、安装keepalived
yum install keepalived
2、编译安装Nginx
[[email protected] ~]
#useradd -r nginx
[[email protected] ~]
#yum -y groupinstall "Development tools" "Server Platform Development"
[[email protected] ~]
#yum -y install pcre-devel
[[email protected] ~]
#tar xf nginx-1.4.2.tar.gz
[[email protected] ~]
#cd nginx-1.4.2
[[email protected] nginx-1.4.2]
# ./configure \
--prefix=
/usr
\
--sbin-path=
/usr/sbin/nginx
\
--conf-path=
/etc/nginx/nginx
.conf \
--error-log-path=
/var/log/nginx/error
.log \
--http-log-path=
/var/log/nginx/access
.log \
--pid-path=
/var/run/nginx/nginx
.pid \
--lock-path=
/var/lock/nginx
.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=
/var/tmp/nginx/client/
\
--http-proxy-temp-path=
/var/tmp/nginx/proxy/
\
--http-fastcgi-temp-path=
/var/tmp/nginx/fcgi/
\
--http-uwsgi-temp-path=
/var/tmp/nginx/uwsgi
\
--http-scgi-temp-path=
/var/tmp/nginx/scgi
\
--with-pcre
提示:在两台服务器上都要安装Nginx
[[email protected] nginx-1.4.2]
# service nginx start
Starting nginx: [ OK ]
三、实现高可用
1、修改配置keepalived
[[email protected] ~] # cd /etc/keepalived [[email protected] keepalived] # mv keepalived.conf keepalived.conf.bak [[email protected] keepalived] #vim keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL } vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight 2 } vrrp_instance VI_10 {
state MASTER
interface eth0
virtual_router_id 131
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.16.3.100
}
track_script {
chk_nginx
} |
3、验证 |
[[email protected] ~] # ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link /ether 00:0c:29:65:a0:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.3.3 /16 brd 172.16.255.255 scope global eth0
inet 172.16.3.100 /32 scope global eth0 # Vip此时在Keepalived+Nginx1上
inet6 fe80::20c:29ff:fe65:a02f /64 scope link
valid_lft forever preferred_lft forever |
然后关闭Keepalived+Nginx1上的keepalived服务Vip会跳转到Keepalived上
四、实现负载均衡
1、修改Keepalived配置文件
Keepalived+Nginx1:
[[email protected] ~] # cd /etc/keepalived [[email protected] keepalived] # mv keepalived.conf keepalived.conf.bak # 备份keepalived配置文件 [[email protected] keepalived] #vim keepalived.conf # 创建配置文件,如下 ! Configuration File for keepalived global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL } vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight 2 } vrrp_instance VI_10 {
state MASTER
interface eth0
virtual_router_id 131
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.16.3.100
}
track_script {
chk_nginx
} vrrp_instance VI_20 {
state BACKUP
interface eth0
virtual_router_id 132
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.16.3.200
}
track_script {
chk_nginx }
} |
Keepalived+Nginx2 同理
[[email protected] ~] # cd /etc/keeplived [[email protected] keepalived] # mv keepalived.conf keepalived.conf.bak [[email protected] keepalived] # vim keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL } vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight 2 } vrrp_instance VI_10 {
state BACKUP
interface eth0
virtual_router_id 131
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.16.3.100
}
track_script {
chk_nginx
} } vrrp_instance VI_20 {
state MASTER
interface eth0
virtual_router_id 132
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.16.3.200
}
track_script {
chk_nginx
}
2、重启Keepalived服务 |
[[email protected] ~] # service keepalived restart [[email protected] ~] # service keepalived restart |
3、验证
[[email protected] ~] # ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link /ether 00:0c:29:52:ee:44 brd ff:ff:ff:ff:ff:ff
inet 172.16.3.4 /16 brd 172.16.255.255 scope global eth0
inet 172.16.3.200 /32 scope global eth0 #此时vip2在Keepalived+Nginx2上
inet6 fe80::20c:29ff:fe52:ee44 /64 scope link
valid_lft forever preferred_lft forever |
4、关闭Keepalived+Nginx2上的Keepalived服务模拟故障,vip2会转移到Keepalived+Nginx1上,反之依然
[[email protected] ~] # ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link /ether 00:0c:29:65:a0:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.3.3 /16 brd 172.16.255.255 scope global eth0
inet 172.16.3.100 /32 scope global eth0
inet 172.16.3.200 /32 scope global eth0
inet6 fe80::20c:29ff:fe65:a02f /64 scope link
valid_lft forever preferred_lft forever |
以上是关于keepalived+nginx的主要内容,如果未能解决你的问题,请参考以下文章