keepalived+nginx搭建主从负载服务器
组网图:
用户通过10.141.179.93访问
主nginx负载均衡器:10.141.177.205
辅nginx负载均衡器:10.141.177.206
虚拟IP:10.141.179.93
掩码都是255.255.248.0
keepalived用于监控主机是否宕机,当205宕机,服务会迁到206上,启用206的nginx
1 相关软件下载地址
keepalive:http://www.keepalived.org/download.html
nginx:http://nginx.org/download/
pcre:http://www.pcre.org/
openssh:http://www.openssl.org/source
2 软件安装
安装nginx之前需要先安装pcre和openssl,安装kkepalived依赖于zlib和openssl-devel
2.1 pcre安装
将pcre-8.35.tar.gz上传到/opt目录
cd /opt
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
mkdir /usr/local/pcre-8.35
./configure --prefix=/usr/local/pcre-8.35 --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
make
make install
在/etc/ld.so.conf中加入: /usr/local/lib/pcre,然后运行ldconfig
2.2 openssh安装
将openssl-1.0.0h.tar.gz上传到/opt目录
tar zxvf openssl-1.0.0h.tar.gz
cd /opt/openssl-1.0.0h
./Configure --prefix=/usr/local/openssh 这步如果失败,貌似不执行也行
make
make install
2.3 安装nginx
压缩包nginx-1.7.2.tar.gz放在/opt目录下
cd /opt
tar zxvf nginx-1.7.2.tar.gz
cd nginx-1.7.2/
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-mail --with-openssl=/opt/openssl-1.0.0h --with-mail_ssl_module --with-debug --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre=/opt/pcre-8.35/
make
make install
2.4 安装zlib
rpm -ivh zlib-devel-1.2.3-106.34.x86_64.rpm
2.5 安装openssl-devel
rpm -ivh libopenssl-devel-0.9.8h-30.11.x86_64.rpm
如果在安装openssl-devel时报错:
添加如下参数:
rpm -ivh libopenssl-devel-0.9.8h-30.11.x86_64.rpm --force --nodeps
2.6 安装keepalived
keepalived-1.2.13.tar.gz压缩包上传到/opt目录下
cd /opt
tar zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13/
./configure -prefix=/usr/local/keepalive
make
make install
3 修改配置文件
3.1 Nginx配置
vi /usr/local/nginx-1.7.2/conf/nginx.conf
在#gzip on;之后添加
server 10.141.178.141:80 weight=3;
}
server {
location / {
proxy_pass http://mts;
}
}
其中upstream后面的名字任意取,和location中的网址对应,upstream下的server可以有多个,做负载均衡,例子中只有一个web服务器
3.2 Keepalived配置
vi /etc/keepalived/keepalived.conf
主:
global_defs {
notification_email { #发邮件用的,没试过能不能给notes发,估计也可以
admin@163.com
}
notification_email_from admin@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主,从用BACKUP标识
interface eth0
virtual_router_id 51 #这个id所有主从要一样
mcast_src_ip 10.141.177.205 #本机ip
priority 100 #主的priority值必须最大
advert_int 1
authentication { #权限和密码所有主从都要一样
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.141.179.93/21 #虚拟ip
}
}
从:
global_defs {
notification_email {
admin@163.com
}
notification_email_from admin@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 10.141.177.206
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.141.179.93/21
}
}
4 启动nginx和keepalived
4.1 Nginx
4.1.1 判断Nginx配置是否正确
cd /usr/local/nginx-1.7.2/sbin/
./nginx -t -c /usr/local/nginx-1.7.2/conf/nginx.conf
4.1.2 加载配置文件
/usr/local/nginx-1.7.2/sbin/nginx -t
4.1.3 启动
/usr/local/nginx-1.7.2/sbin/nginx
4.1.4 重新加载
/usr/local/nginx-1.7.2/sbin/nginx -s reload
4.1.5 停止服务
步骤1:查询nginx主进程号
ps -ef | grep nginx
在进程列表里面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
4.2 keepalived
4.2.1 启动
/usr/local/keepalive/sbin/keepalived -f /etc/keepalived/keepalived.conf
- 点赞
- 收藏
- 关注作者
评论(0)