目录
1. 环境
IP | 系统版本 | 部署服务 | 端口 |
---|---|---|---|
192.168.0.40 | CentOS6.5_64 | Nginx1.12.0 | 18089 |
192.168.0.41 | CentOS6.5_64 | Nginx1.12.0 | 18089 |
192.168.0.44 | VIP | Keepalived | 18089 |
准备安装包:
1 2 |
keepalived-1.2.24.tar.gz |
2. 安装依赖
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel
如果安装 libnfnetlink-devel 有以下错误,则说明 yum 找不到源。可以去 http://mirrors.163.com/
找到对应系统的源去替换,并做里面提示的操作 yum clean all
和 yum makecache
1 2 3 4 5 6 |
Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Setting up Install Process No package libnfnetlink-devel available. Error: Nothing to do |
3. 安装keepalived
1 2 3 4 5 6 |
tar -zxvf keepalived-1.2.24.tar.gz cd keepalived-1.2.24 ./configure --prefix=/home/zhaomin/soft/keepalived-1.2.24 make make install |
make 可能出现以下错误,可能是 glib 包不是最新的,更新一下即可 yum -y update glib*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
make[2]: Leaving directory `/home/zhaomin/soft/keepalived-1.2.24/lib' make[1]: Leaving directory `/home/zhaomin/soft/keepalived-1.2.24/lib' Making all in keepalived make[1]: Entering directory `/home/zhaomin/soft/keepalived-1.2.24/keepalived' Making all in core make[2]: Entering directory `/home/zhaomin/soft/keepalived-1.2.24/keepalived/core' CC main.o CC daemon.o CC pidfile.o CC layer4.o CC smtp.o CC global_data.o CC global_parser.o CC process.o CC namespaces.o namespaces.c: In function ‘setns’: namespaces.c:184: error: ‘SYS_setns’ undeclared (first use in this function) namespaces.c:184: error: (Each undeclared identifier is reported only once namespaces.c:184: error: for each function it appears in.) make[2]: *** [namespaces.o] Error 1 make[2]: Leaving directory `/home/zhaomin/soft/keepalived-1.2.24/keepalived/core' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/home/zhaomin/soft/keepalived-1.2.24/keepalived' make: *** [all-recursive] Error 1 |
3. 配置
配置文件 ./etc/keepalived/keepalived.conf
3.1. Nginx HA 相关配置
分别修改[41-42] 上的 ./etc/keepalived/keepalived.conf
内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.0.44 } } |
上面配置中可以看到有一个脚本文件:/etc/keepalived/nginx_check.sh
查看nginx是否启动,如果没启动则启动,如果启动不起来,停掉keepalived服务,此时心跳断掉,服务转向另一个nginx。
vi /etc/keepalived/nginx_check.sh
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /home/zhaomin/soft/nginx-1.12.0/objs/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then service keepalived stop fi fi |
chmod 755 /etc/keepalived/nginx_check.sh
Tips:
1、state 参数值:主的是MASTER、备用的是BACKUP
2、priority 参数值: MASTER > BACKUP
3、virtual_router_id: 参数值要一样
3.2. 映射 /etc/keepalived/keepalived.conf
1 2 3 |
mkdir /etc/keepalived/ ln -s /home/zhaomin/soft/keepalived-1.2.24/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf |
3.3. 设置开机启动并启动
1 2 3 4 5 6 7 8 9 10 11 |
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 ln -s /home/zhaomin/soft/keepalived-1.2.24/etc/rc.d/init.d/keepalived /etc/init.d/keepalived ln -s /home/zhaomin/soft/keepalived-1.2.24/etc/sysconfig/keepalived /etc/sysconfig/keepalived ln -s /home/zhaomin/soft/keepalived-1.2.24/sbin/keepalived /usr/sbin/keepalived chmod 755 /etc/init.d/keepalived source /etc/sysconfig/keepalived chkconfig --add keepalived chkconfig keepalived on ./sbin/keepalived -f ./etc/keepalived/keepalived.conf |
3.4. 校验是否启动成功
ip add show eth0
查看 ip 中是否有 vip
1 2 3 4 5 6 7 8 9 |
[root@hadoop34 keepalived-1.2.24]# ip add show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:0c:29:16:cb:1d brd ff:ff:ff:ff:ff:ff inet 192.168.0.34/24 brd 192.168.0.255 scope global eth0 inet 192.168.0.133/32 scope global eth0 inet6 fe80::20c:29ff:fe16:cb1d/64 scope link valid_lft forever preferred_lft forever You have mail in /var/spool/mail/root |
4. 测试 TomcatHA效果
1、分别在 192.168.0.[40-41] 部署Nginx应用。
2、修改nginx下http/index.html,增加特定标识以便测试;
3、修改tomcat下conf/nginx.conf,修改http/server/listen端口为18089,为防止端口冲突;
4、浏览器打开: http://192.168.0.41:18089 刷新页面,正常显示如下信息;
5、浏览器打开: http://192.168.0.40:18089 刷新页面,正常显示如下信息;
6、keepalived访问看其效果 192.168.0.44:18089 由于我设置的 40 为Master 所以这里显示的是 40 的Nginx
5. 命令
5.1. 帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@namenode40 keepalived-1.2.24]# ./sbin/keepalived -h Usage: ./sbin/keepalived [OPTION...] -f, --use-file=FILE Use the specified configuration file -P, --vrrp Only run with VRRP subsystem -C, --check Only run with Health-checker subsystem -l, --log-console Log messages to local console -D, --log-detail Detailed log messages -S, --log-facility=[0-7] Set syslog facility to LOG_LOCAL[0-7] -X, --release-vips Drop VIP on transition from signal. -V, --dont-release-vrrp Don't remove VRRP VIPs and VROUTEs on daemon stop -I, --dont-release-ipvs Don't remove IPVS topology on daemon stop -R, --dont-respawn Don't respawn child processes -n, --dont-fork Don't fork the daemon process -d, --dump-conf Dump the configuration data -p, --pid=FILE Use specified pidfile for parent process -r, --vrrp_pid=FILE Use specified pidfile for VRRP child process -c, --checkers_pid=FILE Use specified pidfile for checkers child process -s, --namespace=NAME Run in network namespace NAME (overrides config) -m, --core-dump Produce core dump if terminate abnormally -M, --core-dump-pattern=PATN Also set /proc/sys/kernel/core_pattern to PATN (default 'core') -v, --version Display the version number -h, --help Display this help message |
5.2. 启动
./sbin/keepalived -f ./etc/keepalived/keepalived.conf
Tips:
启动时可能报错:
1 2 3 |
[root@hadoop34 keepalived-1.2.24]# ./sbin/keepalived -f etc/keepalived/keepalived.conf ./sbin/keepalived: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory |
默认libssl.so.1.1(openssl组件)会安装在/usr/local/lib64下面;需要通过软连接放置到/usr/lib64下面:
1 2 3 |
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 |
5.3. 日志目录
/var/log/messages
原创文章,转载请注明: 转载自LoserZhao – 诗和远方[ http://www.loserzhao.com/ ]
本文链接地址: http://www.loserzhao.com/bigdata-maintenance/keepalived-install-and-configuration-nginx-ha.html
文章的脚注信息由WordPress的wp-posturl插件自动生成
0 条评论。