1. 程式人生 > 其它 >Redis部署文件

Redis部署文件

Redis部署文件

Redis部署文件ChangeLog一、安裝與部署1. 下載路徑2. 解壓編譯3. 啟動redis服務4. 客戶端連線5. 停止redis服務6. 開機啟動7. 提供的工具8. 配置說明二、叢集搭建1. 概述2. 叢集架構3. 叢集搭建步驟3.1 選擇伺服器3.2 建立目錄3.3 拷貝配置檔案3.4 修改配置3.5 建立節點3.6 建立叢集3.6.1 安裝ruby3.6.2 安裝gem3.6.3 叢集搭建3.7 建立密碼3.7.1 設定密碼3.7.2 訪問叢集4. 測試4.1 建立資料4.2 檢視節點資訊4.3 檢視叢集資訊

ChangeLog

  • 2019-06-28:by wenjiaqi

    • 預設開啟密碼

    • 關閉持久化配置

  • 2019-06-10:by wenjiaqi

    • 增加建立密碼步驟

一、安裝與部署

1. 下載路徑

https://redis.io/download

2. 解壓編譯

//解壓
root@ubuntu:/# cd /usr/local
root@ubuntu:/usr/local# tar -xvf redis-4.0.14.tar.gz

//編譯
root@ubuntu:/usr/local# cd redis-4.0.14/
root@ubuntu:/usr/local/redis-4.0.14# make

//進入src目錄執行安裝
root@ubuntu:/usr/local/redis-4.0.14# cd src/
root@ubuntu:/usr/local/redis-4.0.14/src# make install PREFIX=/usr/local/redis

//建立/usr/local/redis/etc目錄
root@ubuntu:/usr/local/redis-4.0.14/src# cd /usr/local/redis
root@ubuntu:/usr/local/redis# mkdir etc

//建立日誌目錄
root@ubuntu:/usr/local/redis# mkdir logs

//拷貝配置檔案
root@ubuntu:/usr/local/redis# cp /usr/local/redis-4.0.14/redis.conf ./etc/

3. 啟動redis服務

root@ubuntu:/usr/local/redis# ./bin/redis-server ./etc/redis.conf 
2136:C 29 May 23:07:04.636 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2136:C 29 May 23:07:04.637 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=2136, just started
2136:C 29 May 23:07:04.637 # Configuration loaded
2136:M 29 May 23:07:04.638 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.14 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 2136
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

預設情況下,redis不是後臺執行的,我們需要把redis放在後臺執行,修改配置如下:

//將daemonize改為yes
daemonize yes

//更改日誌檔案路徑
logfile "/usr/local/redis/logs/redis.log"

4. 客戶端連線

root@ubuntu:/usr/local/redis/bin# 
root@ubuntu:/usr/local/redis/bin#
root@ubuntu:/usr/local/redis/bin# ./redis-cli
127.0.0.1:6379>

5. 停止redis服務

root@ubuntu:/usr/local/redis/bin# ./redis-cli shutdown
或者
root@ubuntu:/usr/local/redis/bin# pkill redis-server

6. 開機啟動

暫未成功

7. 提供的工具

//redis/bin目錄下的幾個檔案
root@ubuntu:/usr/local/redis/bin# ls
dump.rdb redis-check-aof redis-cli redis-server
redis-benchmark redis-check-rdb redis-sentinel

//各檔案作用
redis-benchmark:redis效能測試工具
redis-check-aof:檢查aof日誌的工具
redis-check-rdb:檢查rdb日誌的工具
redis-cli:連線用的客戶端
redis-server:redis服務程序

8. 配置說明

參見redis基礎教程:https://www.runoob.com/redis/redis-conf.html

二、叢集搭建

1. 概述

redis2.6~3.0之間,redis使用哨兵機制進行伺服器監控,哨兵的含義就是監控redis系統的執行狀態。

redis3.0之後,redis之後支援cluster

2. 叢集架構

架構細節:

(1)所有的redis節點彼此互聯(PING-PONG機制),內部使用二進位制協議優化傳輸速度和頻寬.

(2)節點的fail是通過叢集中超過半數的節點檢測失效時才生效.

(3)客戶端與redis節點直連,不需要中間proxy層.客戶端不需要連線叢集所有節點,連線叢集中任何一個可用節點即可

(4)redis-cluster把所有的物理節點對映到[0-16383]slot上,cluster 負責維護node<->slot<->value

3. 叢集搭建步驟

3.1 選擇伺服器

Redis叢集一般至少需要6個伺服器,3個主節點,3個從節點,每個伺服器啟動一個redis服務,多個redis服務組成Redis叢集

本文件測試使用了單臺虛擬機器啟動6個服務的方式,搭建偽叢集進行測試(效能測試需要6個伺服器)

3.2 建立目錄

//建立叢集目錄
root@ubuntu:/usr/local# mkdir redis_cluster

//建立節點目錄
root@ubuntu:/usr/local# cd redis_cluster/
root@ubuntu:/usr/local/redis_cluster# mkdir 7000 7001 7002 7003 7004 7005

//建立日誌目錄
root@ubuntu:/usr/local/redis_cluster# mkdir logs

3.3 拷貝配置檔案

//拷貝配置到7000、7001、7002、7003、7004、7005目錄
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7000/
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7001/
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7002/
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7003/
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7004/
root@ubuntu:/usr/local/redis_cluster# cp ../redis/etc/redis.conf ./7005/

3.4 修改配置

bind  192.168.15.128          //指定IP,預設是127.0.0.1
port 7000 //埠7000,7001,7002...
daemonize yes //redis後臺執行
pidfile /var/run/redis_7000.pid //pidfile檔案對應7000,7001,7002...
logfile "/usr/local/redis_cluster/logs/redis_7000.log" //日誌儲存路徑7000,7001,7002...
dir /usr/local/redis_cluster/7000/ //工作目錄7000,7001,7002...
cluster-enabled yes //開啟叢集 把註釋#去掉
cluster-config-file nodes_7000.conf //叢集的配置 配置檔案首次啟動自動生成
cluster-node-timeout 5000 //請求超時 設定5秒夠了
protected-mode yes //保護模式如果配置yes,需要配置節點密碼,線上需要開啟
masterauth ppp //配置連線主節點所需的密碼
requirepass ppp //配置本節點的密碼,叢集內各節點密碼需要相同

//修改rdb持久化配置,遮蔽此三行,增加save "",關閉rdb持久化
#save 900 1
#save 300 10
#save 60 10000
save ""

//修改aof持久化配置,關閉aof持久化
appendonly no

3.5 建立節點

root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7000/redis.conf
root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7001/redis.conf
root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7002/redis.conf
root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7003/redis.conf
root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7004/redis.conf
root@ubuntu:/usr/local/redis/bin# ./redis-server ../../redis_cluster/7005/redis.conf

檢視redis是否啟動成功:

root@ubuntu:~# ps -ef|grep redis
root 2840 1 0 02:24 ? 00:00:17 ./redis-server 127.0.0.1:7000 [cluster]
root 2847 1 0 02:24 ? 00:00:08 ./redis-server 127.0.0.1:7001 [cluster]
root 2857 1 0 02:25 ? 00:00:08 ./redis-server 127.0.0.1:7002 [cluster]
root 2862 1 0 02:25 ? 00:00:09 ./redis-server 127.0.0.1:7003 [cluster]
root 2867 1 0 02:25 ? 00:00:07 ./redis-server 127.0.0.1:7004 [cluster]
root 2872 1 0 02:25 ? 00:00:08 ./redis-server 127.0.0.1:7005 [cluster]

3.6 建立叢集

3.6.1 安裝ruby

下載地址:http://www.ruby-lang.org/en/downloads/

//先安裝這三個庫,否則下面會報錯
root@ubuntu:/usr/local# sudo apt-get install libssl-dev libreadline-dev libgdbm-dev

//安裝
root@ubuntu:/usr/local# tar -xvf ruby-2.6.3.tar.gz
root@ubuntu:/usr/local# cd ruby-2.6.3/
root@ubuntu:/usr/local/ruby-2.6.3# ./configure
root@ubuntu:/usr/local/ruby-2.6.3# make
root@ubuntu:/usr/local/ruby-2.6.3# sudo make install

//檢視版本
root@ubuntu:/usr/local/ruby-2.6.3# ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
3.6.2 安裝gem

再用 gem 這個命令來安裝 redis介面 gem是ruby的一個工具包

gem install redis
3.6.3 叢集搭建

接下來執行一下redis-trib.rb

//拷貝redis-trib.rb指令碼
root@ubuntu:~# cd /usr/local/redis_cluster/
root@ubuntu:/usr/local/redis_cluster# cp /usr/local/redis-4.0.14/src/redis-trib.rb ./

//版本4.0啟動叢集方式
root@ubuntu:/usr/local/redis_cluster# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

//版本5.0不支援上面的啟動方式了,改成以下方式
root@ubuntu:/usr/local/redis/bin# ./redis-cli -a pass --cluster create 127.0.0.1:7000 127.0.0.1:7000 127.0.0.1:7000 127.0.0.1:7000 127.0.0.1:7000 127.0.0.1:7000 --cluster-replicas 1

解釋下,--replicas 1 表示自動為每個master節點分配一個slave節點,上面是6個節點,程式會按照一定規則生產3個master(主)、3個slave(從)

到這裡,redis叢集已經初步搭建完成了!

3.7 建立密碼

3.7.1 設定密碼

方式一:

//修改所有Redis節點的配置檔案
masterauth ppp
requirepass ppp

注:這種方式需要重啟各節點

方式二:

//進入各例項進行設定
root@ubuntu:/usr/local/redis/bin# ./redis-cli -c -p 7000
127.0.0.1:7000> config set masterauth ppp
OK
127.0.0.1:7000> config set requirepass ppp
OK
127.0.0.1:7000> auth ppp
OK
127.0.0.1:7000> config rewrite

//然後分別使用./redis-cli -c -p 7001,./redis-cli -c -p 7002等設定各個節點密碼

注:各個節點密碼設定必須要一致,否則Redirected會失敗,推薦方式二

3.7.2 訪問叢集

a) 帶密碼訪問叢集方式:

root@ubuntu:/usr/local/redis/bin# ./redis-cli -c -p 7000 -a ppp

b) 建立密碼後使用redis-trib.rb指令碼會報錯,解決方法:find / -name client.rb,找到對應版本的此檔案,舉例:

/usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb,然後修改password

class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "ppp",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:reconnect_delay => 0,
:reconnect_delay_max => 0.5,
:inherit_socket => false
}

4. 測試

4.1 建立資料

//set和get資料
//注意,這裡要連線master節點,因為slave節點不可讀也不可寫
root@ubuntu:/usr/local/redis/bin# ./redis-cli -c -p 7000
127.0.0.1:7000>
127.0.0.1:7000> set b 1
OK
127.0.0.1:7000> get b
"1"
127.0.0.1:7000>

4.2 檢視節點資訊

//檢視節點info
root@ubuntu:/usr/local/redis/bin# ./redis-cli -c -p 7000
127.0.0.1:7000>
127.0.0.1:7000> info
# Server
redis_version:4.0.14
redis_git_sha1:00000000
...

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:2641592
used_memory_human:2.52M
...

# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7005,state=online,offset=988,lag=0
...

4.3 檢視叢集資訊

//版本4.0檢查單個節點
root@ubuntu:/usr/local/redis_cluster# ./redis-trib.rb check 127.0.0.1:7000

//版本5.0不支援上面的檢查方式了,改為以下方式
root@ubuntu:/usr/local/redis/bin# ./redis-cli --cluster check 192.168.101.19:7000

//檢視叢集節點
root@ubuntu:/usr/local/redis/bin# ./redis-cli -p 7000 cluster nodes