1、集群相关
2、keepalived相关
3、用keepalived配置高可用集群
安装:yum install keepalived -y
高可用,主要是针对于服务器硬件或服务器上的应用服务而言的,在软件方面就得根据自己的需要而定,这里就针对于nginx服务进行设置
两台电脑都安装上nginx服务修改keepalived 的配制文件:
vim /etc/keepalived/keepalived.conf加入:global_defs {
notification_email { aming@aminglinux.com } notification_email_from root@aminglinux.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh" interval 3}vrrp_instance VI_1 {
state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { 192.168.188.100 }track_script {
chk_nginx }}
编辑执行脚本文件:
vim /usr/local/sbin/check_ng.sh加入:#!/bin/bash
#时间变量,用于记录日志d=`date --date today +%Y%m%d_%H:%M:%S`#计算nginx进程数量n=`ps -C nginx --no-heading|wc -l`#如果进程为0,则启动nginx,并且再次检测nginx进程数量,#如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fifi改变脚本权限:chmod 755 /usr/local/sbin/check_ng.sh (增加可执行权限)然后启动keepalived服务:systemctl start keepalived 或 service keepalived start进行查看:ps aux |greep keepalive查看VIP:进行测试:这样子,主的keepalived就设置好了。
配制重服务器keepalived
安装:yum install keepalived -y
修改配制文件:vim /etc/keepalived/keepalived.conf加入:global_defs {
notification_email { aming@aminglinux.com } notification_email_from root@aminglinux.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh" interval 3}vrrp_instance VI_1 {
state BACKUP interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { 192.168.188.100 }track_script {
chk_nginx }}
配制检测nginx的脚本:vim /usr/local/sbin/check_ng.sh
加入:#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`#计算nginx进程数量n=`ps -C nginx --no-heading|wc -l`#如果进程为0,则启动nginx,并且再次检测nginx进程数量,#如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $n -eq "0" ]; then systemctl start nginx (根据自己的具体情况进行配制相关的服务) n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fifi
增加执行权限:chmod 755 /usr/local/sbin/check_ng.sh
启动keepalived: systemctl start keepalived查看:ps aux |grep keepalived由于都是外网机器,所以这里暂时无法测试。