昨天在公司内部分享了高可用负载均衡,现将环境搭建过程记录一下。
本文主要讲keepalived高可用,与keepalived自检的脚本。
环境:
web01: 10.8.8.51 centos7.3 #web服务器01
web02: 10.8.8.52 centos7.3 #web服务器02
proxy01:10.8.8.90 centos7.3 vip:10.8.8.55 #nginx负载均衡主服务器,keepalived主服务器
proxy02:10.8.8.91 centos7.3 vip:10.8.8.55 #nginx负载均衡备服务器,keepalived备服务器
一、web01、web02环境搭建
由于本文主讲keepalived高可用,环境搭建过程忽略。
web01、web02各自搭建一个简单的web,域名均为:www.test.com
分别测试单机访问是否成功。
二、配置nginx负载均衡
在10.8.8.90和10.8.8.91主机上,分别配置nginx负载均衡。简单配置如下。分别测试负载均衡是否成功。
upstream backend {
server 10.8.8.51:80 max_fails=3 fail_timeout=30s;
server 10.8.8.52:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
三、安装配置keepalived
keepalived官网:www.keepalived.org
因为是测试用,所以用最新版本:
- Keepalived for Linux - Version 2.0.6 - July 23, 2018
下载地址:http://www.keepalived.org/software/keepalived-2.0.6.tar.gz
#iptables防火墙互相允许对方ip通过
#在proxy02机子上: -I INPUT -s 10.8.8.90 -j ACCEPT #在proxy01机子上: -I INPUT -s 10.8.8.91 -j ACCEPT
1、安装keepalived
[root@proxy01 ~]# mkdir /home/tools [root@proxy01 ~]# cd /home/tools/ [root@proxy01 tools]# wget http://www.keepalived.org/software/keepalived-2.0.6.tar.gz [root@proxy01 tools]# tar xf keepalived-2.0.6.tar.gz [root@proxy01 tools]# cd keepalived-2.0.6 [root@proxy01 keepalived-2.0.6]# ./configure --prefix=/usr/local/keepalived [root@proxy01 keepalived-2.0.6]# make [root@proxy01 keepalived-2.0.6]# make install
#我的安装过lnmp环境,没有报错,如果编辑或安装时报错,先安装依赖。
yum install openssl-devel psmisc libnl* libnfnetlink-devel -y #如果报错,就安装依赖。
2、配置keepalived为系统服务
#配置后即可使用/etc/init.d/keepalived启停keepalived服务,必须配置,在脚本中需要用到,如果使用service命令启停服务,crontab定时任务不会执行。
[root@proxy01 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@proxy01 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@proxy01 ~]# touch /etc/init.d/keepalived [root@proxy01 ~]# chmod +x /etc/init.d/keepalived [root@proxy01 ~]# vi /etc/init.d/keepalived
#/etc/init.d/keepalived内容如下:
#!/bin/sh # # Startup script for the Keepalived daemon # # processname: keepalived # pidfile: /var/run/keepalived.pid # config: /etc/keepalived/keepalived.conf # chkconfig: - 21 79 # description: Start and stop Keepalived # Source function library . /etc/rc.d/init.d/functions # Source configuration file (we set KEEPALIVED_OPTIONS there) . /etc/sysconfig/keepalived RETVAL=0 prog="keepalived" start() { echo -n $"Starting $prog: " daemon keepalived ${KEEPALIVED_OPTIONS} RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog } stop() { echo -n $"Stopping $prog: " killproc keepalived RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog } reload() { echo -n $"Reloading $prog: " killproc keepalived -1 RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; condrestart) if [ -f /var/lock/subsys/$prog ]; then stop start fi ;; status) status keepalived RETVAL=$? ;; *) echo "Usage: $0 {start|stop|reload|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL
/etc/init.d/keepalived文件内容
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:centos7搭建keepalived高可用nginx负载均衡集群 - Python技术站