天天看點

Redis安裝部署(一主二從三哨兵)!/bin/shchkconfig:2345 80 90 <- 這裡需要添加這一行,否則chkconfig --add redis會報錯“service redis does not support chkconfig”。

Redis安裝部署(一主二從三哨兵)

需求:根據目前客戶的生産環境,模拟安裝部署Redis的測試環境,友善後續的功能測試。

1.準備工作

2.安裝編譯Redis

3.Redis運作環境配置

4.Redis啟動和關閉

Redis的版本和虛拟主機數量都按照客戶的生産環境來準備:Redis版本:3.2.10

準備3台虛拟機,具體環境資訊為:

系統版本 主機名 IP位址 主機記憶體 磁盤空間

RHEL6.8 test01 192.168.1.121 4G 20G

RHEL6.8 test02 192.168.1.122 4G 20G

RHEL6.8 test03 192.168.1.123 4G 20G

使用wget下載下傳redis-3.2.10.tar.gz

wget

http://download.redis.io/releases/redis-3.2.10.tar.gz

需要確定安裝有gcc @all nodes:

[root@test01 ~]# cluster_run_all_nodes "hostname; rpm -qa gcc"

test01

gcc-4.4.7-23.el6.x86_64

test02

test03

注:如果沒有gcc,可以使用yum安裝;

本文用到的cluster_run_all_nodes和cluster_copy_all_nodes兩個指令,功能分别是在叢集所有節點運作指令和同步檔案。

軟體安裝&編譯 @all nodes:

cd /u01/soft

tar -zxvf redis-3.2.10.tar.gz

mv redis-3.2.10 /usr/local/redis

cd /usr/local/redis

make PREFIX=/usr/local/redis install

實際我這裡軟體預設是在/root下,各節點同步軟體媒體、解壓、指定安裝路徑、編譯安裝:

[root@test01 ~]# cluster_copy_all_nodes /root/redis-3.2.10.tar.gz /root/

[root@test01 ~]# cluster_run_all_nodes "hostname;tar -zxvf redis-3.2.10.tar.gz"

[root@test01 ~]# cluster_run_all_nodes "hostname;mv redis-3.2.10 /usr/local/redis"

--編譯安裝這步建議手工@all nodes 執行,因為時間長,輸出資訊多:

cd /usr/local/redis && make PREFIX=/usr/local/redis install

3.1 相關目錄的建立:

--REDIS資料落地位置(資料目錄)

//資料目錄

mkdir -p /u01/redis/6379/data

//日志目錄

mkdir -p /u01/redis/6379/log

--SENTINEL日志存放位置

//哨兵sentinel

mkdir -p /u01/redis/6379/temp

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/data"

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/log"

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/temp"

3.2 redis參數配置(主從主要配置):

//生成redis參數檔案及存放位置

mkdir /etc/redis

//redis參數檔案命名可采用端口号方式,如下,端口為6379,命名為6379.conf

cp /usr/local/redis/redis.conf /etc/redis/6379.conf

cluster_run_all_nodes "hostname;mkdir /etc/redis"

cluster_run_all_nodes "hostname;cp /usr/local/redis/redis.conf /etc/redis/6379.conf"

具體參數設定及說明:

1)主節點:192.168.1.121

vi /etc/redis/6379.conf

bind 192.168.1.121

protected-mode no

port 6379

daemonize yes

pidfile "/var/run/redis_6379.pid"

dir "/u01/redis/6379/data/"

slave-priority 100

appendonly yes

appendfsync everysec

requirepass Redis123

masterauth Redis123

2)從節點1:192.168.1.122

bind 192.168.1.122

protected-mode no

pidfile "/var/run/redis_6379.pid"

dir "/u01/redis/6379/data/"

slaveof 192.168.1.121 6379

slave-read-only yes

slave-priority 80

appendonly yes

appendfsync everysec

requirepass Redis123

3)從節點2:192.168.1.123

bind 192.168.1.123

slave-priority 60

3.3 redis系統服務配置:

//拷貝redis服務啟動檔案到/etc/init.d

cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis

//redis啟動檔案修改分兩部分

vi /etc/init.d/redis

//第一部分

!/bin/sh

chkconfig:2345 80 90 <- 這裡需要添加這一行,否則chkconfig --add redis會報錯“service redis does not support chkconfig”。

//第二部分

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server <- 這裡修改路徑為/usr/local/redis

CLIEXEC=/usr/local/redis/bin/redis-cli <- 這裡修改路徑為/usr/local/redis

//添加redis服務

chkconfig --add redis

實際我這裡操作為:

--這裡按照上面引用文檔的提示修改對應兩部分内容,然後再分發複制到各節點、添加redis服務開機啟動:

[root@test01 local]# cluster_copy_all_nodes /etc/init.d/redis /etc/init.d/redis

cluster_run_all_nodes "hostname;chkconfig --add redis"

cluster_run_all_nodes "hostname;chkconfig --list redis"

配置環境變量,确認redis相關指令可用:

//linux環境變量,針對所有使用者

vim /etc/profile

export PATH="$PATH:/usr/local/redis/bin"

//立即生效

source /etc/profile

cluster_run_all_nodes "hostname;echo 'export PATH="$PATH:/usr/local/redis/bin"' >> /etc/profile'"

重新登陸@all nodes,驗證環境變量生效。

3.4 哨兵sentinel配置

每台主機可以配置一個或者多個哨兵,取決與每個伺服器上跑多少個redis。

系統參數配置:

vi /etc/sysctl.conf

//定義了系統中每一個端口最大的監聽隊列的長度,這是個全局的參數,預設值為128

net.core.somaxconn= 1024

//sysctl.conf 生效

sysctl -p

//若臨時生效,可使用如下指令:

echo 1024 >/proc/sys/net/core/somaxconn

[root@test01 6379]# cluster_run_all_nodes "hostname; echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf"

[root@test01 6379]# cluster_run_all_nodes "hostname; sysctl -p"

[root@test01 6379]# cluster_run_all_nodes "hostname; cat /proc/sys/net/core/somaxconn"

哨兵配置檔案:

//哨兵配置檔案位置

cp /usr/local/redis/sentinel.conf /etc/redis

//建立哨兵日志存放位置,最好是與redis的資料檔案存放在一起

mkdir -p /u01/redis/6379/temp/

具體配置(每個節點都一樣)

vi /etc/redis/sentinel.conf

port 26379

dir "/u01/redis/6379/temp/"

logfile "/u01/redis/6379/temp/sentinel.log"

sentinel monitor redis1 192.168.1.121 6379 2

sentinel down-after-milliseconds redis1 10000

sentinel parallel-syncs redis1 2

sentinel failover-timeout redis1 60000

sentinel auth-pass redis1 hundsun@bbep

[root@test01 local]# cluster_run_all_nodes "hostname; mkdir -p /u01/redis/6379/temp"

--按上面文檔配置sentinel.conf 然後分發複制到@all nodes:

[root@test01 local]# cluster_copy_all_nodes /etc/redis/sentinel.conf /etc/redis/sentinel.conf

4.1 啟動&關閉REDIS

啟動Redis:

service redis start

//檢視redis程序

[root@test01 ~]# ps -ef|grep redis

root 29097 1 0 01:14 ? 00:01:07 /usr/local/redis/bin/redis-server 192.168.1.121:6379

root 32072 31964 0 14:18 pts/0 00:00:00 grep redis

關閉Redis:

redis-cli -h

auth

shutdown

實際操作如下:

[root@test01 ~]# redis-cli -h 192.168.1.121

192.168.1.121:6379> auth Redis123

OK

192.168.1.121:6379> shutdown

not connected> exit

4.2 啟動&關閉哨兵

啟動哨兵:

redis-sentinel /etc/redis/sentinel.conf

[root@test01 ~]# redis-sentinel /etc/redis/sentinel.conf

[root@test01 ~]# ps -ef|grep sentinel

root 32112 1 0 14:21 ? 00:00:00 redis-sentinel *:26379 [sentinel]

root 32117 31964 0 14:21 pts/0 00:00:00 grep sentinel

關閉哨兵:

//殺程序法

ps -ef|grep sentinel

kill

kill -9 32112

AlfredZhao©版權所有「從Oracle起航,領略精彩的IT技術。」

原文位址

https://www.cnblogs.com/jyzhao/p/12760557.html