Keepalived+nfs+shell Script Implementation Nfs-ha high availability

Source: Internet
Author: User
Tags rsync

Originally wanted to do drbd+heartbeat, but the leader said to add hard disk waste of resources, not necessary, and in the existing hard disk to do a large risk, so it can only use rsync to achieve data synchronization, the experiment found a lot of pits, are filled with scripts and planning tasks, and intends to use this set directly in the production environment , then if you still encounter any problems, and then make corrections and completion, the following is the project details:

Host Configuration:

web:192.168.6.10 Centos 6.4

nfs1:192.168.6.1 Centos 6.4

nfs2:192.168.6.2 Centos 6.4

Keepalived 1.2.13 vip:192.168.6.105

1. Installing NFS and Keepalived

This part is relatively simple, directly with Yum-y install NFS keepalived

2. Configuring NFS (two same configurations)

Mkdir/home/shares

Vim/etc/exports

/home/shares 192.168.6.0/24 (Rw,sync,no_root_squash)

Cd/home/shares

echo AAA >/home/shares/a.txt #用作后面脚本判断

Touch files{1..10}

Service NFS Start

3. Configure keepalived

Vim/etc/keepalived/keepalived.conf

! Configuration File for Keepalived

Global_defs {
router_id Lvs_devel
}

Vrrp_instance Vi_1 {
State MASTER #辅改成BACKUP
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority #辅改成50
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.6.105
}
}

Service keepalived Start

Web:mount-t NFS 192.168.6.1:/home/shares/shares

4.ssh Password-Free login

Ssh-keygen

Ssh-copy-id

5.shell Script

Cd/etc/keepalived

Vim notify_master.sh #判断NFS端口是否存在, stop keepalived service if not present

#!/bin/bash

port= ' Netstat-anp|grep 2049 '
If ["$port" = = ""];then
/sbin/service keepalived Stop
Fi

Vim change.sh (required for two NFS units)

#!/bin/bash
ip= '/sbin/ip a|grep "eth0" |grep "105"
web= "192.168.6.10"
If ["$ip"! = ""];then #判断VIP是否存在, if it exists, it is now master.
/usr/bin/ssh $web "Cat/shares/a.txt" #在web上运行命令 to see if/shares directory is available, perform cat a.txt consume less memory CPU
If ["$?"! = 0];then #如果不可用 $? The return value is not 0, then uninstall the original/shares directory, and then mount the directory again, the reason is that the VIP switch must be re-mounted/shares, otherwise you cannot use this directory
/usr/bin/ssh $web "umount/shares&&mount-t NFS 192.168.6.105:/home/shares/shares"
Fi
Fi

chmod +x change.sh

chmod +x notify_master.sh

6. Scheduled Tasks crontab

Crontab-e

* * * * * */etc/keepalived/notify_master.sh #每分钟检查一次NFS服务是否正常
* * * * * */etc/keepalived/change.sh #每分钟检查一次keepalived状态

0 XX * * * rsync/home/shares 192.168.6.2:/home/shares #每天夜间同步数据

Keepalived+nfs+shell Script Implementation nfs-ha high availability

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.