基于keepalived实现两台realserver服务器中的nginx和php-fpm服务互为主从
思路:利用两个VIP,一个定位nginx,一个定位php-fpm
步骤:
1、准备两台基于LNMP架构的服务器(能够提供正常的web服务)
2、在nginx为主php-fpm为备的机器上操作:
①编辑keepalived的配置文件(文件内容的具体含义请参看博客:
http://13150617.blog.51cto.com/13140617/1979652)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 2
router_id LVS_DEVELhaha
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 11
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
virtual_ipaddress {
172.17.0.22
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 12
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
virtual_ipaddress {
192.168.199.33
}
}
②配置php-fpm,使其监听在9000端口并且允许所有主机访问
vim /etc/php-fpm.d/www.conf
listen = 9000
注释 listen.allowed_clients = any
③配置nginx,使其指向php所在的主机上
vim /etc/nginx/nginx.conf
fastcgi_pass 192.168.199.33:9000; 虚拟ip
④开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
⑤启动服务
systemctl start keepalive
3、php-fpm为主nginx为备的机器上操作:
①编辑keepalived的配置文件
vim /etc/keepalived/keepalived.conf
同上面的配置,只需要调换state状态和priority优先级即可
②配置php-fpm
vim /etc/php-fpm.d/www.conf
配置完全同上
③配置nginx
vim /etc/nginx/nginx.conf
配置完全同上
④开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
⑤启动服务:
systemctl start keepalive
4、测试:两台服务器都正常服务时,nginx为主的机器上和php-fpm为主的机器上都各有一个VIP,网站可正常访问
当有一台服务器出现故障时,那么该VIP就会漂移到正常的服务器上,而且网站依旧可以访问
感谢浏览,如有疑问,欢迎留言。