CentOS7安装配置redis5集群

一.服务器准备

本文准备了3台服务器 , 分别是

172.18.0.231

172.18.0.232

172.18.0.233

每台运行2个redis实例, 端口分别为7000 7001 ,即总共6个redis节点

 

二.安装必要包并配置环境

yum install gcc

 

调整服务器参数 , 以得到最大性能

1.调整服务器最大文件打开数

打开文件 /etc/security/limits.conf 在最后加入以下文件 , 并保存 

* 代表所有方式

- 代表 both hard and soft

* - nofile 65535
* - nproc 65535

打开文件 /etc/pam.d/login 在文件最后加上 , 先检查系统,确保该文件存在

session required /lib64/security/pam_limits.so

2.关闭THP

在root下执行 

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

 把这段命令加入到 /etc/rc.local中 , 确保每次启动后都执行该命令

注意 : 如果redis处于启动状态 , 必须重启redis才能使该配置生效..所以最好在装redis之前就关闭

3.调整系统tcp参数

打开文件 /etc/sysctl.conf

#设置最大连接数
net.core.somaxconn = 32768
#内存分配策略 1表示内核允许分配所有的物理内存,而不管当前的内存状态如何
vm.overcommit_memory = 1

执行以下命令使配置生效

sysctl –p

 

三.防火墙配置(踩坑)

redis中,除了自身需要的端口之外(本文是7000和7001) , 如果需要建立集群 , 则还需要开放总线端口 , 总线端口是数据端口+10000

也就是 每台机器需要开放4个端口 , 分别为 7000 17000 7001 17001 

附上一份iptable的配置脚本:

#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 7000 -j ACCEPT
iptables -A INPUT -p tcp --dport 7001 -j ACCEPT
iptables -A INPUT -p tcp --dport 17000 -j ACCEPT
iptables -A INPUT -p tcp --dport 17001 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

 

四.下载并安装redis 5

#下载
curl -O http://download.redis.io/releases/redis-5.0.5.tar.gz
tar zxvf redis-5.0.5.tar.gz
cd redis-5.0.5
make

 

建立redis文件夹(文件夹随意 , 只是存放配置文件使用) 

mkdir -p /home/redishome/redis_7000
mkdir -p /home/redishome/redis_7001

 

写redis配置文件 , 例子是7000端口的配置 , 可复制一份,更改端口变成7001的配置

[root@localhost redis_7000]# more redis.conf 
#载入默认配置
include /home/redishome/redis/redis-5.0.5/redis.conf
#端口
port 7000
#启用集群模式
cluster-enabled yes
cluster-config-file nodes_7000.conf
#超时时间
cluster-node-timeout 5000 
appendonly yes
#后台运行
daemonize yes
#非保护模式
protected-mode no
#文件存放路径
dir /home/redishome/redis_7000
#运行pid存放位置
pidfile  /var/run/redis_7000.pid
#集群密码
masterauth FEEA50BC03C64BBFB6E22CAC521985E0
#访问密码
requirepass FEEA50BC03C64BBFB6E22CAC521985E0
#取消本机限制
bind 0.0.0.0
#禁用危险命令
rename-command FLUSHALL "ACA1B8FF9A8B2483C919C7943E74D674B"
rename-command FLUSHDB "A7B682D816AF048AF840E1E48F6B0484B"
rename-command KEYS "AEEB6C33C536646F2B8D9096BA58BD290"
rename-command CONFIG "A7A59439DB1A54C16AC8C143AD289CD2B"

 

三台服务器全部使用一样的配置(注意 , 密码也需要所有集群都一样)

 

安装好之后,每台服务器编制启动文件,redis-start.sh

#!/bin/sh
/home/redishome/redis/redis-5.0.5/src/redis-server /home/redishome/redis_7000/redis.conf &
/home/redishome/redis/redis-5.0.5/src/redis-server /home/redishome/redis_7001/redis.conf &

直接启动即可.

 

五 . 建立集群

redis-cli -a FEEA50BC03C64BBFB6E22CAC521985E0 --cluster create \
172.18.0.231:7000 172.18.0.231:7001 \
172.18.0.232:7000 172.18.0.232:7001 \
172.18.0.233:7000 172.18.0.233:7001 \
--cluster-replicas 1

其中 --cluster-replicas 1 代表建立的是1主1从的方式 (按照顺序配置) , 2代表1主2从

 

执行结果 :  (注意 , 中间提示确认时, 一定要打入yes全部,而不是y)

[root@localhost ~]# redis-cli -a FEEA50BC03C64BBFB6E22CAC521985E0 --cluster create 172.18.0.231:7000 172.18.0.231:7001 172.18.0.232:7000 172.18.0.232:7001 172.18.0.233:7000 172.18.0.233:7001 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.18.0.232:7001 to 172.18.0.231:7000
Adding replica 172.18.0.233:7001 to 172.18.0.232:7000
Adding replica 172.18.0.231:7001 to 172.18.0.233:7000
M: f40abe2e5c79d06fbe803baa01b7f0501a8bcf3e 172.18.0.231:7000
   slots:[0-5460] (5461 slots) master
S: 502b9e3a8f1ed01653d0f3f93946f9c3cea5b0cc 172.18.0.231:7001
   replicates 9fa843537e5cdd34bb37f8117c51f86aa311b918
M: b73514294398fc8f45d2cb163499e2ef046cc6bb 172.18.0.232:7000
   slots:[5461-10922] (5462 slots) master
S: 9542274db9dc6ee9f8b7ef91f9c3eb22351cbf88 172.18.0.232:7001
   replicates f40abe2e5c79d06fbe803baa01b7f0501a8bcf3e
M: 9fa843537e5cdd34bb37f8117c51f86aa311b918 172.18.0.233:7000
   slots:[10923-16383] (5461 slots) master
S: cc2d72c68ca90732232ecef03d66e65de25e62d0 172.18.0.233:7001
   replicates b73514294398fc8f45d2cb163499e2ef046cc6bb
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 172.18.0.231:7000)
M: f40abe2e5c79d06fbe803baa01b7f0501a8bcf3e 172.18.0.231:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 9fa843537e5cdd34bb37f8117c51f86aa311b918 172.18.0.233:7000
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 502b9e3a8f1ed01653d0f3f93946f9c3cea5b0cc 172.18.0.231:7001
   slots: (0 slots) slave
   replicates 9fa843537e5cdd34bb37f8117c51f86aa311b918
S: cc2d72c68ca90732232ecef03d66e65de25e62d0 172.18.0.233:7001
   slots: (0 slots) slave
   replicates b73514294398fc8f45d2cb163499e2ef046cc6bb
M: b73514294398fc8f45d2cb163499e2ef046cc6bb 172.18.0.232:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9542274db9dc6ee9f8b7ef91f9c3eb22351cbf88 172.18.0.232:7001
   slots: (0 slots) slave
   replicates f40abe2e5c79d06fbe803baa01b7f0501a8bcf3e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

集群建立就成功了.

 

六 . 其他命令

#批量kill掉所有redis进程
ps -ef|grep redis|grep -v grep|awk  '{print "kill -9 " $2}' | sh

 

#查看集群状态
redis-cli -c -p 7000 -a FEEA50BC03C64BBFB6E22CAC521985E0
#查看节点明细情况
> cluster nodes
#查看集群情况
> cluster info

 

posted on 2015-04-15 12:11  太清  阅读(38097)  评论(5编辑  收藏  举报