结构如图

用keepalived实现Samba高可用_高可用

keepalived 虚拟IP: 172.31.193.1

SMB1: 172.31.206.55 (MASTER)

SMB2: 172.31.207.120 (BACKUP)

安装

sudo apt-get install keepalived

sudo vim /etc/keepalived/keepalived.conf

在机器SMB1上

vrrp_instance smb1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        172.31.193.1
    }
}

virtual_server 172.31.193.1 445 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 172.31.206.55 445 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 445
        }
    }

    real_server 172.31.207.120 445 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 445
        }
    }
}

virtual_server 172.31.193.1 139 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 172.31.206.55 139 {        
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 139
        }
    }

    real_server 172.31.207.120 139 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 139
        }
    }
}

在SMB2上,主要区别是state 分MASTER和BACKUP,然后priority master 为100,backup为90.

vrrp_instance smb2 {
	state BACKUP
	interface eth0
	virtual_router_id 51
	priority 90
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 1234
	}
	virtual_ipaddress {
		172.31.193.1
	}
}

virtual_server 172.31.193.1 445 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 172.31.206.55 445 {
        weight 1
        TCP_CHECK {
			connect_timeout 10
			nb_get_retry 3
			delay_before_retry 3
			connect_port 445
        }
    }

    real_server 172.31.207.120 445 {
        weight 1
        TCP_CHECK {
			connect_timeout 10
			nb_get_retry 3
			delay_before_retry 3
			connect_port 445
        }
    }
}

virtual_server 172.31.193.1 139 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 172.31.206.55 139 {		
		weight 1
		TCP_CHECK {
			connect_timeout 10
			nb_get_retry 3
			delay_before_retry 3
			connect_port 139
        }
    }

    real_server 172.31.207.120 139 {
        weight 1
        TCP_CHECK {
			connect_timeout 10
			nb_get_retry 3
			delay_before_retry 3
			connect_port 139
        }
    }
}

安装配置Samba服务

sudo apt-get install samba

sudo vim /etc/samba/smb.conf

添加共享文件夹

[shareDoc]
comment = Folder for File Sharing
path = /shareDoc
public = no
writable = yes
valid users = user1,user2

添加smb用户

sudo smbpasswd -a user1

sudo service smbd restart

sudo service keepalived restart

注意服务重启后即可ping同这个虚拟IP: 172.31.193.1, 但是通过\\IP 共享的方式访问还需要一些时间让网络通知完成。

此前在AWS上通过EC2做这个实验,发现无论如何都无法ping通这个虚拟IP,查询资料后是AWS的VPC网络阻挡了这个通讯,即使我在security group里将子网间的所有All TCP/UDP全部打开也没用,后来使用本机自带的Hyper-V搭建虚拟机,顺利实现。