Linux installation and configuration under keepalived

@(Finally you are here)

Preparation Before Installation

Basic system: CentOS 7

yum -y install gcc gcc-c++ autoconf automake make 
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

1. Download keepalived

method one:

yum -y install keepalived

Method Two :
download the binary source package: http: //www.keepalived.org/download.html

#下载安装文件

cd /usr/local
wget http://www.keepalived.org/software/keepalived-2.0.18.tar.gz
#解压文件
tar -zxvf keepalived-2.0.18.tar.gz
#编译
cd keepalived-2.0.18/
#--prefix 指定安装地址
#/usr/local/keepalived/ 安装的目录,不要和自己安装文件一个目录,不然报错
./configure --prefix=/usr/local/keepalived/
#编译并安装
 make && make install

2. Configure

Copy the file:

[root@localhost ~]# cp /usr/local/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost ~]# mkdir /etc/keepalived
[root@localhost ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost ~]# cp /usr/local/keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

edit:

vi /etc/keepalived/keepalived.conf

/etc/keepalived/keepalived.conf follows

global_defs {
    notification_email {
        #[email protected]       # 指定keepalived在发生切换时需要发送email到的对象,一行一个
        #[email protected]
    }
    notification_email_from [email protected]   # 指定发件人
    smtp_server [email protected]              # smtp 服务器地址
    smtp_connect_timeout 30               # smtp 服务器连接超时时间
    router_id LVS_1 # 必填,标识本节点的字符串,通常为hostname,但不一定非得是hostname,故障发生时,邮件通知会用到
    }

vrrp_script chk_tomcat {                    #详细看下面
    script "/etc/keepalived/tomcat_check.sh"            #检测服务shell
    interval 2                                  #每个多长时间探测一次
    weight -20                                          #每个多长时间探测一次                                             
}

_instance VI_1 {  # 实例名称
    state MASTER      #  必填,可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER
    interface ens33    #  必填, 节点固有IP(非VIP)的网卡,用来发VRRP包做心跳检测
    mcast_src_ip 192.168.2.89 #本机的ip,需要修改
    virtual_router_id 101 #  必填,虚拟路由ID,取值在0-255之间,用来区分多个instance的VRRP组播,同一网段内ID不能重复;主备必须为一样;
    priority 100      #  必填,用来选举master的,要成为master那么这个选项的值最好高于其他机器50个点,该项取值范围是1-255(在此范围之外会被识别成默认值100)
    advert_int 1      #  必填,检查间隔默认为1秒,即1秒进行一次master选举(可以认为是健康查检时间间隔)
    authentication {  #  必填,认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)
        auth_type PASS  # 默认是PASS认证
        auth_pass 1111 # PASS认证密码
    }
    virtual_ipaddress {
        192.168.2.90    #  必填,虚拟VIP地址,允许多个
    }
    track_script {       # 检测shell                          
        chk_tomcat
    }
}

vrrp_script in the script returns a value of 0 that when the detection is successful , other values are as test failure ;

  1. When the weight is positive , when the detection is successful script this weight will be added to the priority , without failure is detected;
    A. Main failure: a main priority <priority + weight will be switched from.
    B. Main success: the main priority + weight> when priority + weight from the master is still the main
  2. When the weight is negative , the script detects weight does not affect the success of this priority , when detection fails priority - ABS (weight)
    A. Main failed: a main priority - abs (weight) <switches from priority from the master when
    the primary success: Main priority> from the main priority is still the main

Detection shell: /etc/keepalived/tomcat_check.sh can return tomcat service according to the detection result of different values, the successful implementation of exit 0 returns 0, failure to perform exit 1 return 1, vrrp_script return value can be combined to make processing of the current priority keepalived

3. Turn on Services

Set keepalived service boot

# chkconfig keepalived on  
# 启动keepalived 服务
# service keepalived start 

View keepalived process, the following three processes is the emergence of a successful start

[root@localhost ~]# ps -ef | grep keep
root       7925      1  0 11:27 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
root       7926      1  0 11:27 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
root       7927   7926  0 11:27 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
root       7956   7888  0 12:02 pts/1    00:00:00 grep --color=auto keep

4. Specify a log file output

Modify / etc / sysconfig

vim  /etc/sysconfig
#指定位置修改为如下内容
KEEPALIVED_OPTIONS="-D -d -S 0"

Modify /etc/rsyslog.conf

vim /etc/rsyslog.conf
#在最后一行添加如下内容
local0.* /var/log/keepalived.log

Restart service test

service rsyslog restart
service rsyslog restart

#如下命令有日志内容输出即为成功
tailf /var/log/keepalived.log

#centos 基本可以成功,但是 debain 可能失败,失败上面 2 个文件可以改为如下内容,再重启服务测试
KEEPALIVED_OPTIONS="-D -d -S 1"
local1.* /var/log/keepalived.log

Reference: https: //blog.csdn.net/weixin_33845881/article/details/86016142

What if the wrong places please identify, in advance thanks

Guess you like

Origin www.cnblogs.com/charmsongo/p/11373335.html