负载均衡:keepalived+nginx+nginx
准备4台虚拟机:centos7
192.168.4.5 lbserver1
192.168.4.6 lbserver2
192.168.4.7 web01
192.168.4.8 web02
准备以上4台虚拟机!
192.169.4.3 #vip
1,在4台服务器中分别安装nginx服务
yum -y install pcre pcre-devel openssl openssl-devel
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
useradd -s /sbin/nologin -M www
./configure --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.6.3/
make && make install
ln -s /application/nginx-1.6.3/ /application/nginx
启动nginx服务: /application/nginx/sbin/nginx
分别在web01和web02中写入测试页面:
echo “web01” > /application/nginx/html/index.html
echo “web01” > /application/nginx/html/index.html
2,配置lbserver1和lbserver2
grep -Ev ‘^$|#’ /application/nginx/conf/nginx.conf.default > /application/nginx/conf/nginx.conf
添加一下文件内容:
upstream www_nginx_pools {
server 192.168.4.7 weight=1;
server 192.168.4.8 weight=1;
}
server模块引用:
proxy_pass http://www_nginx_pools;
重启nginx服务: /application/nginx/sbin/nginx -s reload
3,安装keepalived,配置lbserver1和lbserver2
yum -y install keepalived
#主lbserver1服务器
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.3
}
}
#副lbserver2服务器
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.3
}
}
重启keepalived 服务。