1. 引言
1.1. 目的
为更好的指导后续其他项目部署及项目运维,编写该文档。
1.2. 目标范围
1.3. 读者对象
1) 部署工程师
2) 运维工程师
1.4. 参考文献
http://www.cnblogs.com/kgdxpr/p/3272861.html
http://www.cnblogs.com/kgdxpr/p/3325788.html
2. 安装依赖包
2.1. 挂载iso镜像文件
1) 上传文件CentOS-6.4-x86_64-bin-DVD1.iso到目录:/usr/local/
2) 执行命令:
mkdir /media/CentOS mount -o loop /usr/local/CentOS-6.4-x86_64-bin-DVD1.iso /media/CentOS |
2.2. 修改yum源
cd /etc/yum.repos.d vi CentOS-Media.repo |
CentOS-Media.repo文件修改,红色加粗为修改内容
[c6-media] name=CentOS-$releasever - Media baseurl=file:///media/CentOS/ gpgcheck=0 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 |
vi CentOS-Base.repo |
CentOS-Media.repo文件修改,红色加粗为修改内容
[base] name=CentOS-$releasever - Base mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
#released updates [updates] name=CentOS-$releasever - Updates mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 |
2.3. 安装依赖包
yum install gcc gcc-c++ openssl-devel popt-devel |
3. 安装haproxy
3.1. 在HA1服务器上安装
上传文件haproxy-1.4.24.tar.gz到/usr/local
cd /usr/local tar xf haproxy-1.4.24.tar.gz cd haproxy-1.4.24 uname -r make TARGET=linux26 ARCH=x86_64 #####注释:TARGET指定内核版本,而uname -r是查看内核版本;ARCH指定CPU架构 make install mkdir /etc/haproxy mkdir /usr/share/haproxy cp examples/haproxy.cfg /etc/haproxy cp examples/haproxy.init /etc/init.d/haproxy ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy chmod +x /etc/init.d/haproxy |
上传文件haproxy.cfg到/etc/haproxy目录,对应修改haproxy.cfg配置文件红色部分
global log 127.0.0.1 local0 #日志配置,所有日志都记录在本地,通过local0输出 #log 127.0.0.1 local1 notice maxconn 4096 #最大连接数 chroot /usr/share/haproxy #改变Haproxy的工作目录 uid 99 #用户的UID gid 99 #用户的GID daemon #以后台守护进程方式运行Haproxy nbproc 1 #进程数据(可以设置多个) pidfile /usr/local/haproxy/run/haproxy.pid #debug #是否开启调试 #quiet defaults log global log 127.0.0.1 local3 #日志文件的输出定向 mode http #默认使用协议,可以为{http|tcp|health} http:是七层协议 tcp:是四层 health:只返回OK option httplog #详细记录http日志 option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 option dontlognull #不记录健康检查的日志信息 option forwardfor #如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IP option redispatch #ServerID对应的服务器宕机后,强制定向到其他运行正常的服务器 retries 3 #3次连接失败则认为服务不可用 maxconn 30000 #默认的最大连接数 balance roundrobin #负载均衡算法 stats uri /haproxy-stats #haproxy 监控页面的访问地址 stats refresh 30s #统计页面自动刷新时间 stats realm page-title\ Haproxy #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 stats hide-version #隐藏统计页面上HAProxy的版本信息 contimeout 5000 #连接超时 clitimeout 50000 #客户端超时 srvtimeout 50000 #服务器超时 timeout check 2000 #=心跳检测超时 listen localhost 0.0.0.0:80 mode http option httpchk GET /portal/index.htm #健康检测 server web1 172.16.7.9:7009 check rise 2 fall 1 weight 2 server web2 172.16.7.11:7011 check rise 2 fall 1 weight 2 server web3 172.16.7.13:7013 check rise 2 fall 1 weight 2 |
3.2. 开机自启动
chkconfig --add haproxy chkconfig --level 2345 haproxy on |
3.3. 启动Haproxy服务
service haproxy start |
查看haproxy是否启动
ps -ef|grep haproxy |
停止Haproxy
|
输入http://{ip}/haproxy-stats查看状态 用户名:admin 密码:admin
3.4. 在HA2服务器上安装
在HA2服务器上安装Haproxy;这里就不在介绍了,安装与配置方法与在HA1服务器上安装相同。
4. 安装keepalived
4.1. 在主服务器上安装
上传keepalived-1.2.8.tar.gz到/usr/local目录下
cd /usr/local tar xf /usr/local/keepalived-1.2.8.tar.gz cd /usr/local/keepalived-1.2.8 ./configure --prefix=/usr/local/keepalived make make install |
4.2. 做成服务模式
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ |
4.3. 开机自启动
chkconfig --add keepalived chkconfig --level 2345 keepalived on |
4.4. 配置文件
mkdir -p /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf chmod +x /etc/init.d/keepalived |
將本地目录“主服务配置”下的文件keepalived.conf和chk_haproxy.sh上传到/etc/keepalived目录
vi /etc/keepalived/keepalived.conf |
Keepalived.conf文件,修改红色加粗部分
global_defs { router_id LVS_DEVEL } #监测haproxy进程状态,每2秒执行一次 vrrp_script chk_haproxy { script "/etc/keepalived/chk_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #从服务器为BACKUP interface eth0 #eth0根据实际情况更改 virtual_router_id 51 priority 100 #从服务器要低于100 advert_int 1 mcast_src_ip 172.16.7.11 #主服务器IP,从服务器写从服务器的IP authentication { auth_type PASS auth_pass 111111 } track_script { chk_haproxy #监测haproxy进程状态 } virtual_ipaddress { 172.16.7.8/24 dev eth0 #虚拟IP及eth0,根据实际需求修改 } } |
chk_haproxy.sh文件内容:
#!/bin/bash status=`echo $(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)` echo "Start Status:" $status if [ $status = "0" ] ; then echo "haproxy stoped.starting haproxy ..."; /etc/init.d/haproxy start status=`echo $(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)` #status="1" if [ $status = "0" ] ; then /etc/init.d/keepalived stop echo "haproxy start failed. keepalived stoped."; else echo "haproxy started."; fi fi echo "End Status:" $status |
4.5. 启动服务
service keepalived start |
查看查看主服务器的IP地址
ip addr show eth0 |
4.6. 在从服务器上安装
安装与主服务器安装相同,不同之处在于將/etc/keepalived目录下的keepalived.conf文件修改红色加粗部分。
4.7. 高可用测试
1) 将主服务器上的keepalived停止
service keepalived stop |
2) 查看Master上的日志
tail -f /var/log/messages |
Sep 23 01:24:34 181 Keepalived[3125]: Stopping Keepalived v1.2.8 (09/23,2013) Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) sending 0 priority Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) removing protocol VIPs. |
发现Master释放了VIP
再查看Backup上的日志
Sep 23 01:20:26 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Transition to MASTER STATE Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering MASTER STATE Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) setting protocol VIPs. Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180 Sep 23 01:20:27 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 added |
发现Backup绑定了VIP
3) 将主服务器上的keepalived启动
查看Master上的日志
tail -f /var/log/messages |
Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Entering MASTER STATE Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) setting protocol VIPs. Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180 Sep 23 01:27:56 181 Keepalived_healthcheckers[3449]: Netlink reflector reports IP 10.10.3.180 added |
发现Master绑定了VIP
再查看Backup上的日志
Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Received higher prio advert Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering BACKUP STATE Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) removing protocol VIPs. Sep 23 01:21:15 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 removed |
发现Backup释放了VIP
5. 注意事项
5.1. 关闭防火墙
RedHat:
/etc/init.d/iptables stop 关闭防火墙。
chkconfig iptables off 关闭开机启动。
Ubuntu:
ufw disable (重启生效)
5.2. 双网卡造成IP冲突
经测试,使用keepalived做虚拟IP时,服务器同时启用多个网卡时,会造成虚拟IP冲突。