在三台服務器,搭建redis三主三從集群


一、資源准備

1、准備三台服務器H1、H2、H3

172.26.237.83 H1
172.26.237.84 H2
172.26.237.85 H3

 

二、配置服務器

1、在H1服務器設置SSH免密登錄;在H1生成RSA公鑰和私鑰(在H1操作)

(1)執行ssh-keygen -t rsa命令
(2)進入生成密鑰所在的文件目錄cd /root/.ssh/
(3)將公鑰(名為id_rsa.pub文件)追加到認證文件(名為authorized_keys文件),先后執行以下指令:

ssh-copy-id 172.26.237.83
ssh-copy-id 172.26.237.84
ssh-copy-id 172.26.237.85

 

(注意:這里的IP是H1、H2、H3內網IP,執行后需要輸入yes和服務器密碼)

 

2、配置域名解析文件(負責將主機名稱映射到相應的IP地址

(1)在root目錄創建env目錄,並進入env目錄下執行以下命令

cat > /root/env/hosts.txt <<EOF
172.26.237.83 H1
172.26.237.84 H2
172.26.237.85 H3
EOF

cat /root/env/hosts.txt
cat /root/env/hosts.txt >> /etc/hosts

 (2)在每個節點進入env創建redis源配置文件,命名為redis-env.conf

echo -e "port 7001\ncluster-enabled yes\ndaemonize yes\ndir /root/soft/7001\ncluster-config-file nodes-7001.conf\npidfile "/root/soft/7001/redis-7001.pid"\nlogfile "/root/soft/7001/redis-7001.log"\nappendonly yes" > "redis-env.conf"

 配置文件說明:

port 7000
cluster-enabled yes
dir /root/soft/7000
cluster-config-file nodes-7000.conf
pidfile /root/soft/7000/redis-7000.pid
logfile /root/soft/7000/redis-7000.log
appendonly yes
daemonize yes
protected-mode no

 

3、在各個節點下載安裝redis安裝包

(1)創建/root/soft目錄

mkdir -p /root/soft

(2)下載安裝包、解壓、並進行編譯

cd /root/soft
# 下載
wget 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
 

4、指定配置文件

(1)創建/root/soft/7000目錄與/root/soft/7001目錄

cd /root/soft/
mkdir 7000
cd /root/soft/
mkdir 7001

(2)將之前創建的源配置文件拷貝到7000和7001目錄下

cd /root/env
cp -R redis-env.conf /root/soft/7000/redis.conf
cp -R redis-env.conf /root/soft/7001/redis.conf

(3)分別進入7000和7001目錄,修改redis.conf配置(修改成對應的端口),並檢查文件路徑是否正確

(4)啟動redis-server服務

cd redis-5.0.5
./src/redis-server ../7000/redis.conf
./src/redis-server ../70001/redis.conf

啟動redis-server需要注意,守護進程需要打開,不然啟動的時候是不成功的

(5)檢查是否啟動成功

ps -ef |grep redis

5、創建集群(在H1操作)

三主三從架構說明

三台服務器,啟動6個實例,形成三主三從,其中存儲相同數據的主從節點不能落在同一台機器上,目的是防止部署redis的虛擬機宕機從而造成主從節點全部失效。實驗機器的地址與端口如下:

172.26.237.83  7000 7001
172.26.237.84 7000 7001
172.26.237.85  7000 7001

 

為了使用主從節點不落在同一台機器上

使用如下命令:每台ip+port交叉(沒有找到命令指定主從節點的關系的方法).

在目錄下redis-5.0.5 執行

./src/redis-cli --cluster create --cluster-replicas 1 172.26.237.83:7000 172.26.237.84:7001 172.26.237.84:7000 172.26.237.85:7001 172.26.237.85:7000 172.26.237.83:7001

# 根據提示輸入yes

集群創建成功,如下圖:

 

6、測試

1)cd /root/soft/redis-5.0.5
(2)進入redis客戶端
  ./src/redis-cli -c -h H1 -p 7000
(3)輸入cluster info 查看集群健康狀態
(4)輸入cluster nodes 查看節點健康狀態

 

三、搭建過程遇過的異常

1、配置文件的保護模式沒有修改成no

(1)配置文件修改前

port 7000
cluster-enabled yes
dir /root/soft/7000
cluster-config-file nodes-7000.conf
pidfile /root/soft/7000/redis-7000.pid
logfile /root/soft/7000/redis-7000.log
appendonly yes
daemonize yes

 拋出異常如下-->

[ERR] Node 172.26.237.83:7000 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

 修改后配置文件,將保護模式修改成no;(protected-mode no)

port 7000
cluster-enabled yes dir /root/soft/7000 cluster-config-file nodes-7000.conf pidfile /root/soft/7000/redis-7000.pid logfile /root/soft/7000/redis-7000.log appendonly yes daemonize yes
protected-mode no

 

 2、redis配置了密碼,創建集群的時候沒有輸入密碼,也會報錯

(1)配置文件如下:

bind 172.26.237.83
port 7000
cluster-enabled yes
dir /root/soft/7000
cluster-config-file nodes-7000.conf
pidfile /root/soft/7000/redis-7000.pid
logfile /root/soft/7000/redis-7000.log
appendonly yes
daemonize yes
protected-mode yes
requirepass 0123456789

 注意:綁定地址、保護模式設置為yes,密碼最好一起設置!!

創建集群報錯前的命令:

./src/redis-cli --cluster create --cluster-replicas 1 172.26.237.83:7000 172.26.237.84:7001 172.26.237.84:7000 172.26.237.85:7001 172.26.237.85:7000 172.26.237.83:7001

報錯如下-->

[ERR] Node 172.26.237.83:7000 NOAUTH Authentication required.

 

修改后的命令:

./src/redis-cli --cluster create --cluster-replicas 1 172.26.237.83:7000 172.26.237.84:7001 172.26.237.84:7000 172.26.237.85:7001 172.26.237.85:7000 172.26.237.83:7001 -a 0123456789

 

3。如果服務器開啟了防火牆,需要開放對應的端口

在防火牆中對端口 7000 7001開放:

firewall-cmd --zone=public --add-port=7000/tcp --permanent

firewall-cmd --zone=public --add-port=7001/tcp --permanent

firewall-cmd --reload

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM