admin · 7月21日 · 2020年
#官网地址
#https://www.keepalived.org/download.html
#依赖
yum install openssl-devel gcc -y
wget -O /usr/local/keepalived.tar.gz https://www.keepalived.org/software/keepalived-2.1.5.tar.gz
mkdir -p /usr/local/keepalived
tar -xzvf /usr/local/keepalived.tar.gz -C /usr/local/keepalived --strip-components 1
cd /usr/local/keepalived/
./configure
make && make install
cp -a /usr/local/etc/keepalived /etc/
cp -a /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -a /usr/local/sbin/keepalived /usr/sbin/
#------------------------------------------------------------------------------------------
#配置keepalived.conf
#------------------------------------------------------------------------------------------
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id kp1
vvrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 100
priority 80
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 11111
}
virtual_ipaddress {
192.168.100.250
}
}
EOF
#------------------------------------------------------------------------------------------
#加入systemclt
#------------------------------------------------------------------------------------------
cat > /usr/lib/systemd/system/keepalived.service <<EOF
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
EOF
脚本
#官网地址:https://www.keepalived.org/download.html
#注:真的是因为懒才写的脚本
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
i=1
while [ $i == 1 ];do
{
cat <<EOF
请选择主机的角色:
1)MASTER
2)BACKUP
EOF
read -p "请输入此主机的类型:(1|2):" TYPE
case $TYPE in
1)
STATE="MASTER"
ROUTE_ID="1"
PRIORITH="100"
;;
2)
read -p "请输入此主机是集群中第几台BACKUP:" backup_number
STATE="BACKUP"
ROUTE_ID="$(( 1 + $backup_number ))"
PRIORITH="$(( 90 - $backup_number ))"
;;
*)
echo "请输入正确的选项或按Crtl+C终止安装"
continue
;;
esac
NET_DEV_ALL=`ifconfig | grep "mtu" | awk -F : '{print $1}' | xargs`
NET_DEV_IP=`ifconfig | grep "netmask" | awk '{print $2}' | xargs`
NET_DEV_ALL_NUM=`ifconfig | grep "mtu" | awk -F : '{print $1}' | wc -l`
echo "当前的网卡信息为:"
for ((j=1;j<=$NET_DEV_ALL_NUM;j++))
do
DEV[$j]=`echo "$NET_DEV_ALL" | awk -v t="${j}" '{print $t}'`
DEVIP[$j]=`echo "$NET_DEV_IP" | awk -v t="${j}" '{print $t}'`
echo -e "$j) ${DEV[$j]} \t ${DEVIP[$j]}"
done
read -p "请输入绑定的网卡全名:" NET_DEV
read -p "请输入虚拟IP地址:" NET_IP
echo "--------------------------------------"
cat <<EOF
主机的类型是:$STATE
绑定的网卡是:$NET_DEV
虚拟的IP地址:$NET_IP
EOF
echo "--------------------------------------"
read -p '请确认输入是否正确yes/no:' MAKE_SURE
if [ "$MAKE_SURE" == "yes" ];then
#------------------------------------------------------------------------------------------
yum install openssl-devel gcc -y >/dev/null
wget -q -O /usr/local/keepalived.tar.gz https://www.keepalived.org/software/keepalived-2.1.5.tar.gz
echo "安装中,请稍后..."
mkdir -p /usr/local/keepalived
tar -xzvf /usr/local/keepalived.tar.gz -C /usr/local/keepalived --strip-components 1 >/dev/null
cd /usr/local/keepalived/
./configure
make && make install
cp -a /usr/local/etc/keepalived /etc/
cp -a /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -a /usr/local/sbin/keepalived /usr/sbin/
#------------------------------------------------------------------------------------------
#配置keepalived.conf
#------------------------------------------------------------------------------------------
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id $ROUTE_ID
vvrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state $STATE
interface $NET_DEV
virtual_router_id 100
priority $PRIORITH
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 11111
}
virtual_ipaddress {
$NET_IP
}
}
EOF
#------------------------------------------------------------------------------------------
#加入systemclt
#------------------------------------------------------------------------------------------
cat > /usr/lib/systemd/system/keepalived.service <<EOF
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
EOF
#------------------------------------------------------------------------------------------
i=0
else
i=1
fi
}
cat <<EOF
*******************************************
* *
* 安装完成 *
* *
* by liu peng fei *
* *
*******************************************
EOF
rm -f keepalived_install.sh
done