CentOS7安裝配置redis5集羣

一.服務器準備node

本文準備了3臺服務器 , 分別是redis

172.18.0.231服務器

172.18.0.232session

172.18.0.233app

每臺運行2個redis實例, 端口分別爲7000 7001 ,即總共6個redis節點curl

 

二.安裝必要包並配置環境tcp

yum install gcc

 

調整服務器參數 , 以獲得最大性能性能

1.調整服務器最大文件打開數ui

打開文件 /etc/security/limits.conf 在最後加入如下文件 , 並保存 url

* 表明全部方式

- 表明 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
相關文章
相關標籤/搜索