您的位置:首页 > 数据库 > Mongodb

Nginx+keepalived+MongoDB+nfs 实现分布式集群部署

2017-06-16 14:07 721 查看
一、准备工作

1、 准备四台服务器:

10.10.10.196(负载均衡器)(MongoDB Master 主)

10.10.10.197(负载均衡器热备)(MongoDB Arbiter 选举器)

10.10.10.198(Web服务器)(MongoDB Slave 从)

10.10.10.199(Web服务器)(MongoDB Slave 从)

有条件的话可以多开几个服务器,专门来跑MongoDB,实现web服务器分布式部署

2、 安装软件

      #分别在4台服务器上做如下操作

      #设置yum数据源

      yum -y install http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
     

      vi /etc/yum.repos.d/mongodb-org-3.4.repo

      #内容如下

[mongodb-org-3.4]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

 

      #安装Nginx、MongoDB3.4、keepalived

      #196、197服务器

      yum install -ynginx keepalived mongodb-org

      #198、199服务器(不需要keepalived)

      yum install -ynginx mongodb-org

 

二、负载均衡

1、 Nginx配置

#196、197设置

vi /etc/nginx/conf.d/default.conf

#内容如下

upstream fuzai.local{

    server10.10.10.198:8088;

    server10.10.10.199:8088;

}

server {

    listen       80;

   server_name  127.0.0.1;

    charset     utf-8;

 

    location / {

        roothtml;

        indexindex.html index.htm;

       proxy_pass     http:// fuzai.local;

       proxy_set_header  X-Real-IP  $remote_addr;

       client_max_body_size  100m;

    }

 

    location ~^/(WEB-INF)/ {

        denyall;

    }

 

    error_page404 /404.html;

        location= /40x.html {

    }

 

    error_page500 502 503 504 /50x.html;

        location= /50x.html {

    }

}

 

#198、199配置

#配置站点,监听8088端口,具体过程略

 

2、 Keepalived配置

#196服务器配置

vi /etc/keepalived/keepalived.conf

#配置如下

! Configuration File for keepalived

 

global_defs {

  notification_email {

    #acassen@firewall.loc

    #failover@firewall.loc

    #sysadmin@firewall.loc

   }

  #notification_email_from Alexandre.Cassen@firewall.loc

   #smtp_server192.168.200.1

  #smtp_connect_timeout 30

   router_idLVS_DEVEL

}

 

vrrp_script chk_http_port {

    script"</dev/tcp/127.0.0.1/80"

    interval 1

    weight -2

}

 

vrrp_instance VI_1 {

    state MASTER

    interfaceeth0

   virtual_router_id 51

    priority 100

    advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   virtual_ipaddress {

       10.10.10.10

    }

    track_script{

       chk_http_port

    }

}

 

#197服务器配置

vi /etc/keepalived/keepalived.conf

#配置如下

! Configuration File for keepalived

 

global_defs {

  notification_email {

    #acassen@firewall.loc

    #failover@firewall.loc

    #sysadmin@firewall.loc

   }

  #notification_email_from Alexandre.Cassen@firewall.loc

   #smtp_server192.168.200.1

  #smtp_connect_timeout 30

   router_idLVS_DEVEL

}

 

vrrp_script chk_http_port {

    script"</dev/tcp/127.0.0.1/80"

    interval 1

    weight -2

}

 

vrrp_instance VI_1 {

    state BACKUP

    interfaceeth0

   virtual_router_id 51

    priority 99

    advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   virtual_ipaddress {

       10.10.10.10

    }

    track_script{

       chk_http_port

    }

}

3、 添加开机启动

chkconfig keepalived on

chkconfig nginx on

4、 重启服务

/etc/init.d/nginxrestart

/etc/init.d/keepalivedrestart

5、 访问站点
http://10.10.10.10
 

三、MongoDB集群

1、196服务器

mkdir -p /mongodb/data/master

 

mkdir -p /mongodb/run

touch /mongodb/run/master.pid

 

mkdir -p /mongodb/log

touch /mongodb/log/master.log

 

mkdir -p /mongodb/conf

vi /mongodb/conf/master.conf

#内容如下

#master.conf

dbpath=/mongodb/data/master

logpath=/mongodb/log/master.log

pidfilepath=/mongodb/run/master.pid

directoryperdb=true

logappend=true

replSet=testrs

bind_ip=10.10.10.196

port=27017

oplogSize=10000

fork=true

noprealloc=true

 

#运行命令启动mongodb

#mongod -f/mongodb/conf/master.conf

 

2、197服务器

mkdir -p /mongodb/data/arbiter

 

mkdir -p /mongodb/run

touch /mongodb/run/arbiter.pid

 

mkdir -p /mongodb/log

touch /mongodb/log/arbiter.log

 

mkdir -p /mongodb/conf

vi /mongodb/conf/arbiter.conf

#内容如下

#arbiter.conf

dbpath=/mongodb/data/arbiter

logpath=/mongodb/log/arbiter.log

pidfilepath=/mongodb/run/arbiter.pid

directoryperdb=true

logappend=true

replSet=testrs

bind_ip=10.10.10.197

port=27017

oplogSize=10000

fork=true

noprealloc=true

 

#运行命令启动mongodb

#mongod -f /mongodb/conf/arbiter.conf

 

3、198、199服务器

mkdir -p /mongodb/data/slaver

 

mkdir -p /mongodb/run

touch /mongodb/run/slaver.pid

 

mkdir -p /mongodb/log

touch /mongodb/log/slaver.log

 

mkdir -p /mongodb/conf

vi /mongodb/conf/slaver.conf

#内容如下

#slaver.conf

dbpath=/mongodb/data/slaver

logpath=/mongodb/log/slaver.log

pidfilepath=/mongodb/run/slaver.pid

directoryperdb=true

logappend=true

replSet=testrs

bind_ip=10.10.10.198       #本机ip

port=27017

oplogSize=10000

fork=true

noprealloc=true

 

#运行命令启动mongodb

#mongod -f /mongodb/conf/slaver.conf

 

4、配置数据库

# mongo 10.10.10.196:27017

>use admin;

> rs.initiate({_id:"testrs",members:[{_id:0,host:'10.10.10.196:27017',priority:3},{_id:1,host:'10.10.10.198:27017',priority:2},{_id:2,host:'10.10.10.199:27017',priority:1},{_id:3,host:'10.10.10.197:27017',arbiterOnly:true}]});

最外层的_id表示replica set的名字,members里包含的是所有节点的地址以及优先级。优先级最高的即成为主节点,即这里的192.168.110.71:10111。特别注意的是,对于仲裁节点,需要有个特别的配置——arbiterOnly:true。这个千万不能少了,不然主备模式就不能生效。

 

#查看状态

> rs.status();

 

#从节点允许执行mongo语句

> rs.slaveOk();

 

#查看进程

netstat -nap | grepmongod

 

注:链接数据库:mongodb://10.10.10.196:27017,10.10.10.198:27017,10.10.10.199:27017/test?slaveOk=true


 

四、文件同步:(参考:http://blog.chinaunix.net/uid-23500957-id-4217525.html)

在需要文件俺同步的web服务器上安装

#yum -y install nfs-utils rpcbind

 

启动服务

#service rpcbindstart

#service nfs start

 

选择一台web服务器做为主服务器来共享目录(这里选10.10.10.198)

创建一个要共享的目录,这里选择web项目的主目录/usr/share/nginx/html/demo/

#vi /etc/exports

加入代码

/usr/share/nginx/html/demo/10.10.10.199(rw,no_root_squash,no_all_squash,sync)

如果要共享给多台服务器,就在追加一行上面代码,更换对应的ip即可

 

配置生效:

#exportfs -r

 

从服务器(10.10.10.199)挂载:

mount -t nfs 10.10.10.198:/usr/share/nginx/html/test//usr/share/nginx/html/test/

 

如果开启防火墙,需要设置例外

打开文件:vim /etc/sysconfig/iptables

加入红色部分的内容: 

-A INPUT -m state --stateNEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --stateNEW -m tcp -p tcp --dport 27017 -j ACCEPT

#rpc

-A INPUT -m state --state NEW -mtcp -p tcp --dport 121 -j ACCEPT

-A INPUT -m state --state NEW -mudp -p udp --dport 121 -j ACCEPT

#nfsd

-A INPUT -m state --state NEW -mtcp -p tcp --dport 2049 -j ACCEPT

-A INPUT -m state --state NEW -mudp -p udp --dport 2049 -j ACCEPT

#rquotad

-A INPUT -m state --state NEW -mtcp -p tcp --dport 875 -j ACCEPT

-A INPUT -m state --state NEW -mudp -p udp --dport 875 -j ACCEPT

#mountd

-A INPUT -m state --state NEW -mtcp -p tcp --dport 48620 -j ACCEPT

-A INPUT -m state --state NEW -mudp -p udp --dport 48620 -j ACCEPT

-A INPUT -j REJECT --reject-withicmp-host-prohibited

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: