两种方式安装keepalived
yum安装keepalived
1. 准备一台纯新的 CentOS7.6 服务器,关闭 selinux,清空防火墙
[root@lvs1 ~]# hostname
lvs1
[root@lvs1 ~]# getenforce
Permissive
[root@lvs1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens32"
DEVICE="ens32"
ONBOOT="yes"
IPADDR="10.20.0.20"
NETMASK="255.0.0.0"
GATEWAY="10.0.0.2"
DNS1="223.5.5.5"
[root@lvs1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:49:94:f4 brd ff:ff:ff:ff:ff:ff
inet 10.20.0.20/8 brd 10.255.255.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe49:94f4/64 scope link
valid_lft forever preferred_lft forever
[root@lvs1 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@lvs1 ~]# yum list keepalived
[root@lvs1 ~]# yum install keepalived -y
[root@lvs1 ~]# vi /etc/keepalived/keepalived.conf
[root@lvs1 ~]# cat /etc/keepalived/keepalived.conf #自行修改
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.20.0.100/8 dev ens32 label ens32:0
}
}
[root@lvs1 ~]# systemctl stop keepalived
[root@lvs1 ~]# systemctl start keepalived
[root@lvs1 ~]# yum install psmisc -y #pstree等管理工具包
[root@lvs1 ~]# ps aux | grep keepalived
root 16121 0.0 0.0 122876 1420 ? Ss 15:27 0:00 /usr/sbin/keepalived -D
root 16122 0.0 0.1 133836 3352 ? S 15:27 0:00 /usr/sbin/keepalived -D
root 16123 0.0 0.1 137984 2944 ? S 15:27 0:00 /usr/sbin/keepalived -D
root 16149 0.0 0.0 112708 980 pts/1 S+ 15:28 0:00 grep --color=auto keepalived
[root@lvs1 ~]# pstree -p 16121
keepalived(16121)─┬─keepalived(16122)
└─keepalived(16123)
[root@lvs1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:49:94:f4 brd ff:ff:ff:ff:ff:ff
inet 10.20.0.20/8 brd 10.255.255.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet 10.20.0.100/8 scope global secondary ens32:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe49:94f4/64 scope link
valid_lft forever preferred_lft forever
上图中 keepalived 安装后我们修改了默认配置文件,仅保留了一个 vrrp_instance,指定了他的网卡设备,虚拟路由分组,密码验证,
虚拟地址,启动服务后发现 vip 添加成功。
_________________________________________________________________________________________________________________________________________________
源码编译安装 keepalived
1.我们在上一个实验的基础上,卸载 yum 安装的 keepalived
[root@lvs1 ~]# systemctl stop keepalived
[root@lvs1 ~]# rm -rf /etc/keepalived
[root@lvs1 ~]# yum remove keepalived
2. 下载最新版 keepalived 源码包
[root@lvs1 ~]# wget https://github.com/acassen/keepalived/archive/v2.0.18.tar.gz
3. 安装编译依赖的软件包
[root@lvs1 ~]# yum install curl gcc autoconf automake openssl-devel libnl3-devel iptables-devel ipset-devel net-snmp-devel libnfnetlink-devel file-devel -y
4. 进行编译安装
[root@lvs1 ~]# tar xf v2.0.18.tar.gz
[root@lvs1 ~]# cd keepalived-2.0.18/
[root@lvs1 keepalived-2.0.18]# ./build_setup
configure.ac:322: installing './ar-lib'
configure.ac:98: installing './install-sh'
configure.ac:98: installing './missing'
genhash/Makefile.am: installing './depcomp'
[root@lvs1 keepalived-2.0.18]# ./configure --prefix=/usr/local/keepalived --with-default-config-file=/usr/local/keepalived/etc/keepalived/keepalived.conf
[root@lvs1 keepalived-2.0.18]# make && make install
[root@lvs1 keepalived-2.0.18]# cd
[root@lvs1 ~]# yum install tree -y
[root@lvs1 ~]# tree /usr/local/keepalived
/usr/local/keepalived
├── bin
│ └── genhash
├── etc
│ ├── keepalived
│ │ ├── keepalived.conf
│ │ └── samples
│ │ ├── client.pem
│ │ ├── dh1024.pem
│ │ ├── keepalived.conf.conditional_conf
│ │ ├── keepalived.conf.fwmark
│ │ ├── keepalived.conf.HTTP_GET.port
│ │ ├── keepalived.conf.inhibit
│ │ ├── keepalived.conf.IPv6
│ │ ├── keepalived.conf.misc_check
│ │ ├── keepalived.conf.misc_check_arg
│ │ ├── keepalived.conf.quorum
│ │ ├── keepalived.conf.sample
│ │ ├── keepalived.conf.SMTP_CHECK
│ │ ├── keepalived.conf.SSL_GET
│ │ ├── keepalived.conf.status_code
│ │ ├── keepalived.conf.track_interface
│ │ ├── keepalived.conf.virtualhost
│ │ ├── keepalived.conf.virtual_server_group
│ │ ├── keepalived.conf.vrrp
│ │ ├── keepalived.conf.vrrp.localcheck
│ │ ├── keepalived.conf.vrrp.lvs_syncd
│ │ ├── keepalived.conf.vrrp.routes
│ │ ├── keepalived.conf.vrrp.rules
│ │ ├── keepalived.conf.vrrp.scripts
│ │ ├── keepalived.conf.vrrp.static_ipaddress
│ │ ├── keepalived.conf.vrrp.sync
│ │ ├── root.pem
│ │ ├── sample.misccheck.smbcheck.sh
│ │ └── sample_notify_fifo.sh
│ └── sysconfig
│ └── keepalived
├── sbin
│ └── keepalived
└── share
├── doc
│ └── keepalived
│ └── README
├── man
│ ├── man1
│ │ └── genhash.1
│ ├── man5
│ │ └── keepalived.conf.5
│ └── man8
│ └── keepalived.8
└── snmp
└── mibs
[root@lvs1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:49:94:f4 brd ff:ff:ff:ff:ff:ff
inet 10.20.0.20/8 brd 10.255.255.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe49:94f4/64 scope link
valid_lft forever preferred_lft forever
[root@lvs1 ~]# vi /usr/local/keepalived/etc/keepalived/keepalived.conf
[root@lvs1 ~]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
10.20.0.200/8 dev ens32 label ens32:0
}
}
[root@lvs1 ~]# systemctl start keepalived
[root@lvs1 ~]# ps aux | grep keepalived
root 49574 0.0 0.0 78092 868 ? Ss 16:36 0:00 /usr/local/keepalived/sbin/keepalived
root 49575 0.0 0.1 82368 2056 ? S 16:36 0:00 /usr/local/keepalived/sbin/keepalived
root 49601 0.0 0.0 112708 992 pts/1 S+ 16:39 0:00 grep --color=auto keepalived
[root@lvs1 ~]# pstree -p 49574
keepalived(49574)───keepalived(49575)
[root@lvs1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:49:94:f4 brd ff:ff:ff:ff:ff:ff
inet 10.20.0.20/8 brd 10.255.255.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet 10.20.0.200/8 scope global secondary ens32:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe49:94f4/64 scope link
valid_lft forever preferred_lft forever