nginx + keepalived 是怎么样实现高可用负载均衡的
下文给大家带来nginx + keepalived 是怎么样实现高可用负载均衡的,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。
虚拟IP: 192.168.2.52
nginx-1: 192.168.2.50
nginx-2: 192.168.2.51
关闭/etc/sysconfig/selinux
SELINUX=disabled
nginx-1
yum install keepalived
vim /etc/keepalived/keepalived.conf
---------------------------------------------------
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER //定义为MASTER主机
interface eth0
virtual_router_id 51 //虚拟id
priority 100 //优先级
advert_int 1 //检测间隔
authentication {
auth_type PASS //认证方式
auth_pass abcd //认证密码
}
virtual_ipaddress {
192.168.2.52 //虚拟ip,即VIP
}
-------------------------------------------------
chkconfig keepalived on
service keepalived start
nginx-2
yum install keepalived
vim /etc/keepalived/keepalived.conf
--------------------------------------------------只改动如下
state BACKUP //定义为BACKUP
priority 99 //优先级
virtual_ipaddress {
192.168.2.52
}
-------------------------------------------------
chkconfig keepalived on
service keepalived start
ip a
已经绑定成功
检测keepalived自动切换
客户机:
ping 192.168.2.52 -t
nginx-1: 关闭keepalived服务
service keepalived stop
客户机是否正常ping
安装nginx
因为centos 没有nginx,先安装第三方epel-release源
yum install -y epel-release
yum install -y nginx
负载均衡
vi /etc/nginx/nginx.conf
添加以下内容
------------------------
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream zhizhimao.com {
server 192.168.2.101:80; #web云服务器
server 192.168.2.100:80; #web云服务器
}
}
------------------------
反向代理
vi /etc/nginx/conf.d/default.conf
--------------------------------------------
server {
listen 80;
listen [::]:80default_server;
server_name zhizhimao.com; #如只做代理填写IP
root /usr/share/nginx/html;
#Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://zhizhimao.com; #如只做代理填写IP
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
-----------------------------------------------------
chkconfig nginx on
service nginx start
防火墙打开80端口
看了以上关于nginx + keepalived 是怎么样实现高可用负载均衡的,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。