Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CentOS 6 配置:Keepalived + Nginx #19

Open
johnnian opened this issue Jul 13, 2017 · 0 comments
Open

CentOS 6 配置:Keepalived + Nginx #19

johnnian opened this issue Jul 13, 2017 · 0 comments

Comments

@johnnian
Copy link
Owner

johnnian commented Jul 13, 2017

操作系统:centos 6
服务器两台:A、B
目的:A、B 上都安装Nginx, 想要使用 keepalived 做HA

下面是具体的步骤:

步骤1: 安装Keepalived

[root@localhost ~]# yum groupinstall 'Development Tools' -y
[root@localhost ~]# yum install openssl-devel libnl-devel libnfnetlink-devel iptables-devel  -y
[root@localhost ~]# wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
[root@localhost ~]# tar zxf keepalived-1.2.23.tar.gz && cd keepalived-1.2.23
[root@localhost ~]# ./configure --prefix=/usr --sysconfdir=/etc
[root@localhost ~]# make && make install

步骤2: 配置Keepalived

1. 查看网络信息:本机IP、网卡名字

1

现在的网络情况:

A服务器:

IP: 10.177.101.114, 网卡: eth4

B服务器:

IP: 10.177.101.115, 网卡: eth6

虚拟IP: 只要配置好Keepalived,启动后,就会自动在当前网卡上创建一个虚拟IP

IP: 10.177.101.117

2. 配置Keepalived

原理是:

A、B 服务器在Keepalived上配置同一个虚拟IP:10.177.101.117, 分别启动两台服务器的 keepalived时,会自动在对应的网卡上创建同一个虚拟IP。

服务器 IP地址 虚拟IP 权重 级别
A服务器 10.177.101.114 10.177.101.117 101 MASTER
B服务器 10.177.101.115 10.177.101.117 100 BACKUP

Keepalived 会创建路由表,根据A、B服务器的级别、权重,进行请求转发;

按照下面配置两台机器:

A机器的配置:

[root@7fde0e436c09 ~]#  vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER #状态设置成MASTER
    interface eth4 #A服务器网卡
    virtual_router_id 51
    priority 101  #Master设置高一点的权重
    advert_int 1
    mcast_src_ip 10.177.101.114 #这个是服务器的内网地址
    virtual_ipaddress {
        10.177.101.117 #虚拟IP
    }
}

B机器的配置:

[root@7fde0e436c09 ~]#  vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP #状态设置成BACKUP
    interface eth6 #B服务器网卡
    virtual_router_id 51
    priority 100 #BACKUP设置低一点的权重
    advert_int 1
    mcast_src_ip 10.177.101.115 #这个是服务器的内网地址
    virtual_ipaddress {
        10.177.101.117 #虚拟IP
    }
}

配置完成后,两台都重启 keepalived:

[root@7fde0e436c09 ~]# /etc/init.d/keepalived restart

完成启动后,看下A、B服务器的网络信息如下:

2

3

步骤4: 配置防火墙

Keepalived 默认使用的端口是:112, 需要开启防火墙的端口

[root@c43bfab6f744 ~] vi /etc/sysconfig/iptables
#添加下面端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 112 -j ACCEPT
[root@c43bfab6f744 ~] service iptables restart

Keepalived 默认使用的端口是:112, 需要开启防火墙的端口

步骤5: 配置开机自启动

[root@localhost ~]# vi /etc/rc.d/rc.local
#在末尾添加
/etc/init.d/keepalived restart

链接

@johnnian johnnian changed the title Centos6 配置:Keepalived + Nginx CentOS 6 配置:Keepalived + Nginx Jul 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant