64位HA部署

  • 来源:
  • 更新日期:2018-05-03

摘要:一提到HA大家都知道heartbeat,heartbeat是个非常不错的HA开源软件,今天我们来介绍一下利用keepalived来做HA,keepalived的最大特点就是切换比较快,废话少说 我们来操作吧。

一提到HA大家都知道heartbeat,heartbeat是个非常不错的HA开源软件,今天我们来介绍一下利用keepalived来做HA,keepalived的最大特点就是切换比较快,废话少说
我们来操作吧。

002UASMrzy7605pjKJv15&690.jpg
一、部署环境
两台服务器IP分别为:
10.8.1.108 master
10.8.1.109 slaved
10.8.1.110 virtual_ip


二、将压缩文件复制至Linux的opt目录下:
keepalived-1.2.1.tar.gz
nginx64-1.2.4.tar.gz
将“解决opensel和PCRE问题方法”目录内文件复制至opt目录下


三、先执行以下更新包以解决安装nginx和keepalived过程中遇到问题:
解决安装nginx出现问题“./configure: error: the HTTP rewrite module requires the PCRE library.”
rpm -ivh --nodeps pcre-devel-6.6-6.el5_6.1.x86_64.rpm
rpm -ivh --nodeps pcre-6.6-6.el5_6.1.x86_64.rpm

解决安装keepalived出现问题“configure: error: !!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!! ”
rpm -ivh --nodeps e2fsprogs-devel-1.39-33.el5.x86_64.rpm
rpm -ivh --nodeps keyutils-libs-devel-1.2-1.el5.x86_64.rpm
rpm -ivh --nodeps libsepol-devel-1.15.2-3.el5.x86_64.rpm
rpm -ivh --nodeps libselinux-devel-1.33.4-5.7.el5.x86_64.rpm
rpm -ivh --nodeps krb5-devel-1.6.1-70.el5.x86_64.rpm
rpm -ivh --nodeps zlib-devel-1.2.3-4.el5.x86_64.rpm
rpm -ivh --nodeps openssl-devel-0.9.8e-22.el5.x86_64.rpm


四.安装nginx
nginx的安装,其他的web软件都可以例如apache,lighttpd等等
tar -zxvf nginx64-1.2.4.tar.gz
cd nginx64-1.2.4
执行以下命令:
./configure --with-http_stub_status_module
make && make install


五、keepalived的安装
tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
分别执行以下命令:
./configure
make && make install

查看进程应该是三个:
ps -ef|grep keepalived

两台服务器的安装步骤完全一样


六.配置文件的修改
1.nginx配置文件的修改,由于我们只是简单的测试所以只要显示的页面能识别是哪台服务器的即可
master服务器:
cd /usr/local/nginx/html
echo "nginx master" > index.html
slaved服务器:
cd /usr/local/nginx/html
echo "nginx slaved" > index.html
2.keepalived配置文件的修改:
master服务器;
cd /usr/local/etc/keepalived
vi keepalived.conf
内容如下:
! Configuration File for keepalived

global_defs {
notification_email {
jlsfwq@163.com
}
notification_email_from keepalived@staff.sina.com.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BAUCK
interface eth0
virtual_router_id 51
mcast_src_ip 10.8.1.108
priority 102 ########### 权值要比 bauck 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.1.110 ############ 虚IP地址
}
}

slaved服务器:
cd /usr/local/etc/keepalived
vi keepalived.conf
内容如下:
! Configuration File for keepalived

global_defs {
notification_email {
jlsfwq@163.com
}
notification_email_from keepalived@staff.sina.com.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 10.8.1.109
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.1.110
}
}



3.nginx和keepalived的启动(启动顺序先master后slaved)
nginx的启动:
/usr/local/nginx/sbin/nginx -t 测试配置文件是否正确
/usr/local/nginx/sbin/nginx 启动nginx

修改nginx.conf 中监听端口80->8088:
Conf代码
listen 8088;
server_name localhost;

查看nginx是否启动命令:
lsof -i:8088

强制停止Nginx:
pkill -9 nginx

查询nginx主进程号
ps -ef | grep nginx

keepalived的启动:
108 /usr/local/sbin/keepalived -D -f /usr/local/etc/keepalived/keepalived.conf
109 cd /usr/local/keepalive/sbin/
./keepalived -D -f /usr/local/keepalive/etc/keepalived/keepalived.conf

四. 测试
1.先测试一下master和slaved的nginx的服务是否正常
会显示:nginx master
会显示:nginx slaved
证明两台服务器的nginx服务都正常
2.测试虚拟IP的连通性和查看虚拟ip在master还是在slaved上
测试虚拟IP的连通性:
在本地ping 10.8.1.109能ping通证明虚拟IP已经启动
查看虚拟ip在的主机:
分别在master和slave上执行ip a命令,例如:
[root@glusterfs_client keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a9:71:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.4.188/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.189/32 scope global eth0
inet6 fe80::20c:29ff:fea9:715e/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
哪台主机能看到这行"inet 192.168.4.189/32 scope global eth0" 证明虚拟主机就在哪台机器上
3. 测试虚拟IP的应用:
能正常显示:nginx master
4.测试keepalived的切换功能
在本地一直ping 192.168.4.189,到192.168.4.188-master上切断网络,例如:service network stop
本地ping 192.168.4.189什么时候通证明切换成功,不通的话证明切换失败
验证切换到slaved上:
可以到192.168.4.190-slave上执行:ip a 产看虚拟IP:
好了到此我们已经成功的实现了keepalived的HA功能,将192.168.4.188-master的网络恢复虚拟IP又自动切换到master的上来