首页 > 其他 > 详细

keepalived+nginx

时间:2017-07-30 23:16:40      阅读:803      评论:0      收藏:0      [点我收藏+]

高集成: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

[root@node1 ~]#useradd -r nginx
[root@node1 ~]#yum -y groupinstall "Development tools" "Server  Platform Development"
[root@node1 ~]#yum -y install pcre-devel
[root@node1 ~]#tar xf nginx-1.4.2.tar.gz
[root@node1 ~]#cd nginx-1.4.2
[root@node1 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

   [root@node1 nginx-1.4.2]# service nginx start

Starting nginx:                                            [  OK  ]

三、实现高可用

1、修改配置keepalived


[root@node1 ~]# cd /etc/keepalived
[root@node1 keepalived]# mv keepalived.conf keepalived.conf.bak    
[root@node1 keepalived]#vim keepalived.conf                    
! Configuration File forkeepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
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、验证

[root@node1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128scope host
valid_lft forever preferred_lft forever
2:  eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether00:0c:29:65:a0:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.3.3/16brd 172.16.255.255 scope global eth0
inet 172.16.3.100/32scope global eth0          # Vip此时在Keepalived+Nginx1上
inet6 fe80::20c:29ff:fe65:a02f/64scope link
valid_lft forever preferred_lft forever

然后关闭Keepalived+Nginx1上的keepalived服务Vip会跳转到Keepalived上

四、实现负载均衡

1、修改Keepalived配置文件

Keepalived+Nginx1:


[root@node1 ~]# cd /etc/keepalived
[root@node1 keepalived]# mv keepalived.conf keepalived.conf.bak     # 备份keepalived配置文件
[root@node1 keepalived]#vim keepalived.conf                         # 创建配置文件,如下
! Configuration File forkeepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
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  同理


[root@node2 ~]# cd /etc/keeplived
[root@node2 keepalived]# mv keepalived.conf keepalived.conf.bak
[root@node2 keepalived]# vim keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
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服务


[root@node1 ~]# service keepalived restart
[root@node2 ~]# service keepalived restart

3、验证


[root@node2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether00:0c:29:52:ee:44 brd ff:ff:ff:ff:ff:ff
inet 172.16.3.4/16brd 172.16.255.255 scope global eth0
inet 172.16.3.200/32scope global eth0    #此时vip2在Keepalived+Nginx2上
inet6 fe80::20c:29ff:fe52:ee44/64scope link
valid_lft forever preferred_lft forever

4、关闭Keepalived+Nginx2上的Keepalived服务模拟故障,vip2会转移到Keepalived+Nginx1上,反之依然


[root@node1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether00:0c:29:65:a0:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.3.3/16brd 172.16.255.255 scope global eth0
inet 172.16.3.100/32scope global eth0
inet 172.16.3.200/32scope global eth0
inet6 fe80::20c:29ff:fe65:a02f/64scope link
valid_lft forever preferred_lft forever

keepalived+nginx

原文:http://ybzbfs.blog.51cto.com/12765816/1952129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!