使用兩臺虛擬機簡單搭建一個 keepalived 的單實例環境linux
環境介紹:主機名 IP地址vim
keepalived-01 192.168.240.137bash
keepalived-02 192.168.240.139服務器
內核版本(兩臺主機系統相同)ide
[root@keepalived-01 ~]# uname -r測試
2.6.32-573.el6.x86_64spa
[root@keepalived-01 ~]# cat /etc/redhat-releasedebug
CentOS release 6.7 (Final)3d
########################keepalived-01上的操做###########################rest
建立軟鏈接
[root@keepalived-01 ~]# ln -s /usr/src/kernels/2.6.32-573.el6.x86_64/ /usr/src/linux
[root@keepalived-01 ~]# ll /usr/src/
total 8
drwxr-xr-x. 2 root root 4096 Sep 23 2011 debug
drwxr-xr-x. 3 root root 4096 May 23 17:59 kernels
lrwxrwxrwx 1 root root 39 Jun 22 20:40 linux -> /usr/src/kernels/2.6.32-573.el6.x86_64/
安裝依賴軟件
[root@keepalived-01 ~]# yum -y install openssl* libnl* popt*
安裝 ip_vs 軟件
[root@keepalived-01 ~]# pwd
/root
[root@keepalived-01 ~]# ls
anaconda-ks.cfg install.log.syslog keepalived-1.1.19.tar.gz install.log ipvsadm-1.26.tar.gz
[root@keepalived-01 ~]# tar zxf ipvsadm-1.26.tar.gz
[root@keepalived-01 ~]# cd ipvsadm-1.26
[root@keepalived-01 ipvsadm-1.26]# make && make install
安裝完成後須要在執行一遍命令才能生效
[root@keepalived-01 ipvsadm-1.26]# /sbin/ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
安裝完成後要使用 lsmod 命令查看 ip_vs 是否存在
[root@keepalived-01 ~]# lsmod | grep ip_vs
ip_vs 126534 0
libcrc32c 1246 1 ip_vs
ipv6 335589 270 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
編譯安裝 keepalived 軟件
[root@keepalived-01 ~]# tar zxf keepalived-1.1.19.tar.gz
[root@keepalived-01 ~]# cd keepalived-1.1.19
[root@keepalived-01 keepalived-1.1.19]# ./configure
[root@keepalived-01 keepalived-1.1.19]# make && make install
拷貝文件,用於 /etc/init.d/ 啓動
/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
/bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived -p
/bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
/bin/cp /usr/local/sbin/keepalived /usr/sbin/
啓動 keepalived 服務
/etc/init.d/keepalived start
檢查 keepalived 服務
ps -ef | grep keep
root 34234 1 0 06:49 ? 00:00:00 keepalived -D
root 34236 34234 0 06:49 ? 00:00:00 keepalived -D
root 34237 34234 0 06:49 ? 00:00:00 keepalived -D
root 34243 28089 0 06:50 pts/1 00:00:00 grep keep
服務啓動成功後,須要中止服務
/etc/init.d/keepalived stop
keepalived 的配置文件是在 /etc/keepalived/ 目錄下的 keepalived.conf 文件
cd /etc/keepalived/
先將源文件備份
mv keepalived.conf keepalived.conf.ori
配置文件
vim keepalived.conf
#!/bin/bash
! Configure File for keepadlived #註釋
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER #狀態
interface eth0 #端口
virtual_router_id 55 #虛擬路由id,在兩個keeplived之間必須同樣
priority 150 #優先級
advert_int 1 #接管的間隔,超過1秒接管
authentication { #受權
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.10/24 #vip地址
}
}
########################keepalived-01操做結束###########################
########################keepalived-02上的操做###########################
在 keepalived-02 上的操做和 01相同,可是要注意配置文件 keepalived.conf 文件
實驗中的 keepalived-02 的配置文件以下
#!/bin/bash
! Configure File for keepadlived
global_defs {
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.10/24
}
}
########################keepalived-02操做結束###########################
以上標黃的都是兩個配置文件絕對不能同樣的地方
兩臺服務器的優先級有 priority 爲主,此次實驗的兩臺機器 keepalived-01 priority 值爲 150,keepalived-02 priority 值爲 100
(官方文檔介紹最好兩臺相差 50)
因此主爲 keepalived-01
兩臺機器分別重啓 keepalived 服務
[root@keepalived-01 ~]# /etc/init.d/keepalived restart
[root@keepalived-02 ~]# /etc/init.d/keepalived restart
到此爲止一個簡單的 keepalived 單實例環境搭建完成
檢查環境搭建是否成功
要查看 vip 地址須要使用命令 ip add 查看
在 keepalived-01 上查看 vip 地址
在 keepalived-02 上查看 vip 地址
並無發現 vip 地址
正常狀況下
keepalived-01 有 vip 地址
keepalived-02 沒有 vip 地址
若是兩臺服務器都有 vip 地址,說明 keepalived 發生腦裂現象
進行高可用測試,中止 keepalived-01 主機上的 keepalived 服務,應該自動切換到 keepalived-02 上
[root@keepalived-01 ~]# /etc/init.d/keepalived stop
[root@keepalived-02 ~]# ip add
在 keepalived-02 上成功查看到 vip 地址
一次簡單的 keepalived 單實例高可用環境搭建完成