站长资讯网
最全最丰富的资讯网站

CentOS服务器分布式环境安装教程

2. 服务器规划

操作系统版本为:

CentOS Linux release 7

软件配置:

服务器

安装软件

Server1

Nginx+keepalived(主) 重定向服务(redirect)

Server2

Postgresql(主) redis(从) 重定向服务(redirect)

Server3

Postgresql(从) redis(主) Nginx+keepalived(备)

Server4

Postgresql(日志) redis(从) 重定向服务

3. 安装包目录

4. 安装前准备

将所需要的安装软件分别上传到对应的服务器上(如无特殊要求最好安装到/opt目录, 关闭防火墙, 上传yum安装包。

4.1Centos7 关闭防火墙(重要)

systemctl stop firewalld.service
systemctl disable firewalld.service

查看防火墙状态:

firewall-cmd –state

结果如上图所示,则代表已经关闭

4.2上传” rpm-packages”到/opt目录

4.3安装rpm packages

cd /opt/rpm-packages

rpm -Uvh –force–nodeps *.rpm

5. 安装JDK(建议所有服务器都安装)

5.1上传 ”软件-jdk”里面的文件到/opt

5.2查看现有jdk

rpm -qa|grep java

若结果如上则跳过5.3

5.3删除现有jdk

rpm -e –nodeps

5.4安装jdk1.7

rpm -ivh/opt/jdk-7u80-linux-x64.rpm

5.5验证JDK是否正确安装

java -version

如果结果如上,则代表安装正确

6. Nginx 安装(server1, server3)

6.1上传 ”软件-nginx” 里面的文件到/opt

6.2安装

cd /opt

tar -zxvfopenresty-1.11.2.5.tar.gz

cdopenresty-1.11.2.5

./configure

gmake

gmake install

6.3创建软链

ln -s/usr/local/openresty/nginx/sbin/nginx /usr/bin/nginx

6.4修改配置文件

vim /usr/local/openresty/nginx/conf/nginx.conf

增加upstream(如果有多个重定向服务器,需要配置多个server, 例子是2个, 位置与server平级,可以参考配置样例):

upstream myserver{

server 10.33.26.125:8080; # (重定向服务器IP+端口)

server 10.33.26.126:8080; # (重定向服务器IP+端口)

}

替换原来的location /

location / {

set $urlParams '';

rewrite_by_lua_filelua/get_args.lua;

proxy_passhttp://myserver$urlParams;

}

配置样例:

6.5上传get_args.lua

cd /usr/local/openresty/nginx

mkdir lua

cd lua

cp /opt/get_args.lua ./

6.6启动openResty

nginx -c /usr/local/openresty/nginx/conf/nginx.conf

6.7查看nginx是否安装成功

ps -ef|grep nginx

若结果如上图,则代表安装成功,如果失败,可以用以下脚本查看详情:

tail -f/usr/local/openresty/nginx/logs/error.log

7. Keepalived安装(server1,server3)

7.1解压安装

cd /opt

tar -zxvf keepalived-1.3.4.tar.gz

cd keepalived-1.3.4

./configure –prefix=/usr/local/keepalived–sysconf=/etc

make

make install

7.2建立软链

ln-s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

7.3修改配置文件()

注意:先清空原有文件内容,然后按主从添加对应的内容

Vim清空文件命令:dG

vim /etc/keepalived/keepalived.conf

=============主节点配置=================

global_defs {

router_id LVS_DEVEL_1

}

vrrp_scriptchk_nginx {

script"/etc/keepalived/nginx_check.sh"

interval 2

weight -20

}

vrrp_instance VI_1{

state MASTER

interface ens33 #网卡名称 可用ip addr查看

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_nginx

}

virtual_ipaddress {

10.33.26.120 #虚拟IP(VIP)

}

}

=============从节点配置=======================

global_defs {

router_id LVS_DEVEL_2

}

vrrp_scriptchk_nginx {

script"/etc/keepalived/nginx_check.sh"

interval 2

weight -20

}

vrrp_instance VI_1{

state backup

interface ens33 #网卡名称 可用ip addr查看

virtual_router_id 51

priority 98

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_nginx

}

virtual_ipaddress {

10.33.26.120 #虚拟IP(VIP)

}

}

主配置截图如下:

从配置截图如下:

7.4上传nginx_check.sh

cd /etc/keepalived

cp /opt/nginx_check.sh ./

chmod +x nginx_check.sh

7.5启动keepalived

keepalived-f /etc/keepalived/keepalived.conf

7.6查看keepalived是否安装成功

ps-ef|grep keepalived

若结果如上图,则代表安装成功,如果失败,可以用以下脚本查看详情:

tail -f/var/log/messages

8. Postgresql安装(server2-主,server3-从)

8.1上传 “软件-postgresql”里面的文件到/opt

8.2解压安装

cd /opt

rpm -ivhpostgresql-9.6.6-1-x64-bigsql.rpm

8.3配置主从复制

8.3.1主机配置

8.3.1.1初始化postgres

/opt/postgresql/pgc start pg96

按要求输入密码(eg:postgres)

8.3.1.2初始化数据库(linux和windows二选一,建议用linux)

8.3.1.3Linux操作

上传 “数据库初始化脚本-linux” 到/opt

vim /opt/init_db.sh

修改/opt/init_db.sh中的PG_HOST为主机IP,PGPASSWORD为8.2输入的postgres帐户密码

chmod +x/opt/init_db.sh

sh/opt/init_db.sh

8.3.1.4Windows操作

确保Windows环境下有pg数据库的环境

修改setup.bat中的localhost为主机IP, PGPASSWORD为8.2输入的postgres帐户密码, 执行行以下脚本:

setup.bat

备注: 1_create_db.sql中的数据库名称:image_text,如果需要自定义修改,请修改成你们需要的名字,其他不要改动.

8.3.1.5修改pg_hba.conf

vim /opt/postgresql/data/pg96/pg_hba.conf

增加 replica 用户(10.33.26.125为从机IP),配置如下:

host replication replica<从机IP>/32md5

配置截图如下:

8.3.1.6重启

/opt/postgresql/pgcrestart pg96

8.3.2从机配置

8.3.2.1创建用户和组

groupadd postgres

useradd postgres-g postgres

8.3.2.2将/opt/postgresql目录授权给postgres

chown -R postgres:postgres/opt/postgresql

8.3.2.3切换到postgres用户

su postgres

8.3.2.4从主节点拷贝数据到从节点

/opt/postgresql/pg96/bin/pg_basebackup-h <主机IP> -U replica -D /opt/postgresql/data/pg96 -X stream

8.3.2.5配置recovery.conf

cp/opt/postgresql/pg96/share/postgresql/recovery.conf.sample /opt/postgresql/data/pg96/recovery.conf

vim/opt/postgresql/data/pg96/recovery.conf

修改如下配置:

standby_mode = on # 说明该节点是从服务器

primary_conninfo = 'host=<主机IP> port=5432 user=replica password=replica' # 主服务器的信息以及连接的用户

recovery_target_timeline ='latest'

配置截图如下:

8.3.2.6配置postgresql.conf

vim/opt/postgresql/data/pg96/postgresql.conf

修改配置如下:

hot_standby= on

8.3.2.7创建logs目录

mkdir/opt/postgresql/data/logs

8.3.2.8启动

/opt/postgresql/pg96/bin/pg_ctlstart -D /opt/postgresql/data/pg96/ &

8.3.3验证主从复制

登陆主机,查询:

select client_addr,sync_state frompg_stat_replication;

如果有记录,则代表配置成功

9. Redis安装

9.1说明

server2-从, server3-从, server4-主,建议3台或以上,高可用至少要保持2个sentinel节点存活

9.2上传“软件-redis- redis-4.0.6.tar.gz”到/opt

9.3解压安装

cd /opt

tar xzfredis-4.0.6.tar.gz

cd redis-4.0.6

make

make install

9.4修改配置文件

Redis.conf

从机将标红的IP地址 改成 主机IP地址后上传至/opt/redis-4.0.6

主机直接注掉281行上传至上传至/opt/redis-4.0.6

Sentinel.conf

将标红的IP地址 改成 主机IP地址后上传至/opt/redis-4.0.6

notify_mymaster.sh

修改后上传至/opt/redis-4.0.6

9.5启动redis

/opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/redis.conf

9.6验证主从配置

/opt/redis-4.0.6/src/redis-cli -h <主机IP> -p 6379 info replication

如果出现以上类似截图, 则代表配置成功

9.7高可用(HA )

9.7.1准备

创建sentinel目录:

mkdir/opt/redis-4.0.6/sentinel-dir

授执行权限

chmod +x /opt/redis-4.0.6/notify_mymaster.sh

9.7.2启动sentinel, 启用自动切换

/opt/redis-4.0.6/src/redis-sentinel/opt/redis-4.0.6/sentinel.conf

9.7.3验证sentinel是否搭建成功

/opt/redis-4.0.6/src/redis-cli-h <主机IP> -p 26379 info sentinel

若结果如截图所示, 则代表成功。

9.7.4在主机上增加虚拟IP

/sbin/ip addr add <虚拟IP>/24 dev <网卡名称>

虚拟IP和网卡名称需要和9.4中配置的notify_mymaster中的配置保持一致

10.Zookeeper安装

10.1上传“软件-zookeeper”里面的文件到/opt

10.2解压

cd /opt

tar -zxvf zookeeper-3.4.6.tar.gz

cd zookeeper-3.4.6/conf

cp zoo_sample.cfg zoo.cfg

sh/opt/zookeeper-3.4.6/bin/zkServer.sh start

10.3验证zookeeper是否安装成功

ps -ef|grepQuorumPeerMain

若上现上述截图, 则代表配置成功

tomcat移除项目名

cd /opt/tomcat-transform/conf/Catalina/localhost

vim ROOT.xml

增加如下配置

13. 启动脚本汇总

13.1 Nginx

杀死:nginx -s stop

启动:nginx

13.2 Keepalived

杀死: pkill keepalived

启动: keepalived -f /etc/keepalived/keepalived.conf

13.3 Postgresql

重启:/opt/postgresql/pgc restart pg96

13.4 Redis

杀死: pkill redis

启动:

/opt/redis-4.0.6/src/redis-server/opt/redis-4.0.6/redis.conf

/opt/redis-4.0.6/src/redis-sentinel/opt/redis-4.0.6/sentinel.conf

get_args.lua:

local cjson = require "cjson"
local request_method = ngx.var.request_method
local args = ""
local request_uri = ngx.var.request_uri
if "POST" == request_method then
ngx.req.read_body()
local args = ngx.req.get_post_args()
local param = ""
for key, val in pairs(args) do
param = key
end
param = cjson.decode(param)
ngx.var.urlParams = request_uri.."username="..tostring(param.username).."&sign="..tostring(param.sign).."×tamp="..tostring(param.timestamp)
else
ngx.var.urlParams = request_uri

end

nginx_check.sh

#!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
nginx || killall keepalived
else
echo "nginx is running"

fi

notify_master.sh

#!/bin/bash
MASTER_IP=$6
LOCAL_IP='10.33.26.125' # 本机IP
VIP='10.33.26.127' #虚拟IP, 不能与keepalived中的虚拟IP一致
NETMASK='24'
INTERFACE='eno16777736' #网卡名称
if [ ${MASTER_IP} = ${LOCAL_IP} ]; then
/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
exit 0
else
/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
exit 0
fi

exit 1

sentinel.conf

daemonize yes
port 26379
protected-mode no
dir "/opt/redis-4.0.6/sentinel-dir"
logfile "/opt/redis-4.0.6/sentinel-dir/sentinel26379.log"
sentinel monitor mymaster 10.33.26.125 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

sentinel client-reconfig-script mymaster /opt/redis-4.0.6/notify_mymaster.sh

赞(0)
分享到: 更多 (0)
网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号