Redis主從模式部署
主從模式是Redis三種叢集模式中最簡單的,主資料庫(master)和從資料庫(slave)。其中,主從複製有如下特點:
- 主資料庫可以進行讀寫操作,當讀寫操作導致資料變化時會自動將資料同步給從資料庫;
- 從資料庫一般是隻讀的,並且接收主資料庫同步過來的資料;
- 一個master可以擁有多個slave,但是一個slave只能對應一個master;
- slave掛了不影響其他slave的讀和master的讀和寫,重新啟動後會將資料從master同步過來;
- master掛了以後,不影響slave的讀,但redis不再提供寫服務,master重啟後redis將重新對外提供寫服務;
- master掛了以後,不會在slave節點中重新選一個master;
工作機制:
- 當slave啟動後,主動向master傳送SYNC命令。master接收到SYNC命令後在後臺儲存快照(RDB持久化)和快取儲存快照這段時間的命令,然後將儲存的快照檔案和快取的命令傳送給slave。slave接收到快照檔案和命令後載入快照檔案和快取的執行命令。
- 複製初始化後,master每次接收到的寫命令都會同步傳送給slave,保證主從資料一致性。
環境
IP | 角色 |
---|---|
192.168.1.21 | master |
192.168.1.22 | slave1 |
192.168.1.23 | slave2 |
安裝編譯環境
# ubuntu
apt install make gcc
# centos
yum install make gcc
安裝 Redis
# 檢視 Redis 版本
http://download.redis.io/releases/
# 下載 Redis
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解壓
tar xvf redis-7.2.5.tar.gz
cd redis-7.2.5/
# 進行編譯
make && make install
配置服務
cat << EOF > /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
配置停止指令碼
mkdir /usr/local/redis
vim /usr/local/redis/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
授權啟動服務
chmod +x /usr/local/redis/redis-shutdown
useradd -s /sbin/nologin redis
cp /root/redis-7.2.5/redis.conf /usr/local/redis/ && chown -R redis:redis /usr/local/redis
mkdir -p /usr/local/redis/data && chown -R redis:redis /usr/local/redis/data
修改配置
vim /usr/local/redis/redis.conf
# master節點配置
bind 0.0.0.0 -::1 # 監聽ip,多個ip用空格分隔
daemonize yes # 允許後臺啟動
logfile "/usr/local/redis/redis.log" # 日誌路徑
dir /usr/local/redis/data # 資料庫備份檔案存放目錄
masterauth 123123 # slave連線master密碼,master可省略
requirepass 123123 # 設定master連線密碼,slave可省略
appendonly yes # 在/usr/local/redis/data目錄生成appendonly.aof檔案,將每一次寫操作請求都追加到appendonly.aof 檔案中
vim /usr/local/redis/redis.conf
#slave1節點配置
bind 0.0.0.0 -::1 # 監聽ip,多個ip用空格分隔
daemonize yes # 允許後臺啟動
logfile "/usr/local/redis/redis.log" # 日誌路徑
dir /usr/local/redis/data # 資料庫備份檔案存放目錄
replicaof 192.168.1.21 6379 # replicaof用於追隨某個節點的redis,被追隨的節點為主節點,追隨的為從節點。就是設定master節點
masterauth 123123 # slave連線master密碼,master可省略
requirepass 123123 # 設定master連線密碼,slave可省略
appendonly yes # 在/usr/local/redis/data目錄生成appendonly.aof檔案,將每一次寫操作請求都追加到appendonly.aof 檔案中
vim /usr/local/redis/redis.conf
#slave2節點配置
bind 0.0.0.0 -::1 # 監聽ip,多個ip用空格分隔
daemonize yes # 允許後臺啟動
logfile "/usr/local/redis/redis.log" # 日誌路徑
dir /usr/local/redis/data # 資料庫備份檔案存放目錄
replicaof 192.168.1.21 6379 # replicaof用於追隨某個節點的redis,被追隨的節點為主節點,追隨的為從節點。就是設定master節點
masterauth 123123 # slave連線master密碼,master可省略
requirepass 123123 # 設定master連線密碼,slave可省略
appendonly yes # 在/usr/local/redis/data目錄生成appendonly.aof檔案,將每
修改linux核心引數
# 臨時生效
sysctl -w vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
### 可選值:0,1,2。
# 0,:表示核心將檢查是否有足夠的可用記憶體供應用程序使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,並把錯誤返回給應用程序。
# 1:表示核心允許分配所有的實體記憶體,而不管當前的記憶體狀態如何。
# 2: 表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體。
啟動 Redis
systemctl daemon-reload
systemctl enable redis
systemctl stop redis
systemctl start redis
systemctl status redis
檢視叢集
# 互動式
redis-cli -h 192.168.1.21 -a 123123
1192.168.1.21:6379> info replication
role:master
connected_slaves:2
slave0:ip=192.168.1.22,port=6379,state=online,offset=14,lag=0
slave1:ip=192.168.1.23,port=6379,state=online,offset=14,lag=0
master_failover_state:no-failover
master_replid:449440daec10a3eb742b13e690de4adb26b20a07
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
192.168.1.21:6379>
# 互動式
redis-cli -h 192.168.1.21
192.168.1.21:6379>
192.168.1.21:6379> info replication
NOAUTH Authentication required.
192.168.1.21:6379>
192.168.1.21:6379> auth 123123
OK
192.168.1.21:6379>
192.168.1.21:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.22,port=6379,state=online,offset=56,lag=0
slave1:ip=192.168.1.23,port=6379,state=online,offset=56,lag=0
master_failover_state:no-failover
master_replid:449440daec10a3eb742b13e690de4adb26b20a07
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
192.168.1.21:6379>
# 非互動式
redis-cli -h 192.168.1.21 -a 123123 info replication
壓測
root@cby:~# redis-benchmark -t set,get -n 100000 -a 123123 -h 192.168.1.21
====== SET ======
100000 requests completed in 0.98 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.103 milliseconds (cumulative count 8)
50.000% <= 0.343 milliseconds (cumulative count 50319)
75.000% <= 0.399 milliseconds (cumulative count 75102)
87.500% <= 0.431 milliseconds (cumulative count 88783)
93.750% <= 0.447 milliseconds (cumulative count 93936)
96.875% <= 0.463 milliseconds (cumulative count 96878)
98.438% <= 0.487 milliseconds (cumulative count 98770)
99.219% <= 0.503 milliseconds (cumulative count 99227)
99.609% <= 0.615 milliseconds (cumulative count 99619)
99.805% <= 0.815 milliseconds (cumulative count 99807)
99.902% <= 1.071 milliseconds (cumulative count 99906)
99.951% <= 1.175 milliseconds (cumulative count 99954)
99.976% <= 1.247 milliseconds (cumulative count 99976)
99.988% <= 1.295 milliseconds (cumulative count 99989)
99.994% <= 1.319 milliseconds (cumulative count 99995)
99.997% <= 1.327 milliseconds (cumulative count 99997)
99.998% <= 1.335 milliseconds (cumulative count 99999)
99.999% <= 1.343 milliseconds (cumulative count 100000)
100.000% <= 1.343 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.008% <= 0.103 milliseconds (cumulative count 8)
1.338% <= 0.207 milliseconds (cumulative count 1338)
35.037% <= 0.303 milliseconds (cumulative count 35037)
78.556% <= 0.407 milliseconds (cumulative count 78556)
99.227% <= 0.503 milliseconds (cumulative count 99227)
99.604% <= 0.607 milliseconds (cumulative count 99604)
99.736% <= 0.703 milliseconds (cumulative count 99736)
99.804% <= 0.807 milliseconds (cumulative count 99804)
99.842% <= 0.903 milliseconds (cumulative count 99842)
99.884% <= 1.007 milliseconds (cumulative count 99884)
99.922% <= 1.103 milliseconds (cumulative count 99922)
99.966% <= 1.207 milliseconds (cumulative count 99966)
99.991% <= 1.303 milliseconds (cumulative count 99991)
100.000% <= 1.407 milliseconds (cumulative count 100000)
Summary:
throughput summary: 102249.49 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.343 0.096 0.343 0.455 0.495 1.343
====== GET ======
100000 requests completed in 0.81 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.063 milliseconds (cumulative count 9)
50.000% <= 0.263 milliseconds (cumulative count 52284)
75.000% <= 0.319 milliseconds (cumulative count 77215)
87.500% <= 0.351 milliseconds (cumulative count 90174)
93.750% <= 0.367 milliseconds (cumulative count 95109)
96.875% <= 0.383 milliseconds (cumulative count 97068)
98.438% <= 0.407 milliseconds (cumulative count 98532)
99.219% <= 0.487 milliseconds (cumulative count 99222)
99.609% <= 0.711 milliseconds (cumulative count 99619)
99.805% <= 0.919 milliseconds (cumulative count 99806)
99.902% <= 1.127 milliseconds (cumulative count 99908)
99.951% <= 1.231 milliseconds (cumulative count 99953)
99.976% <= 1.343 milliseconds (cumulative count 99976)
99.988% <= 1.391 milliseconds (cumulative count 99989)
99.994% <= 1.415 milliseconds (cumulative count 99995)
99.997% <= 1.423 milliseconds (cumulative count 99997)
99.998% <= 1.431 milliseconds (cumulative count 99999)
99.999% <= 1.439 milliseconds (cumulative count 100000)
100.000% <= 1.439 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.034% <= 0.103 milliseconds (cumulative count 34)
24.823% <= 0.207 milliseconds (cumulative count 24823)
70.395% <= 0.303 milliseconds (cumulative count 70395)
98.532% <= 0.407 milliseconds (cumulative count 98532)
99.251% <= 0.503 milliseconds (cumulative count 99251)
99.458% <= 0.607 milliseconds (cumulative count 99458)
99.608% <= 0.703 milliseconds (cumulative count 99608)
99.707% <= 0.807 milliseconds (cumulative count 99707)
99.795% <= 0.903 milliseconds (cumulative count 99795)
99.855% <= 1.007 milliseconds (cumulative count 99855)
99.895% <= 1.103 milliseconds (cumulative count 99895)
99.945% <= 1.207 milliseconds (cumulative count 99945)
99.966% <= 1.303 milliseconds (cumulative count 99966)
99.993% <= 1.407 milliseconds (cumulative count 99993)
100.000% <= 1.503 milliseconds (cumulative count 100000)
Summary:
throughput summary: 122850.12 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.265 0.056 0.263 0.367 0.431 1.439
root@cby:~#
關於
https://www.oiox.cn/
https://www.oiox.cn/index.php/start-page.html
CSDN、GitHub、51CTO、知乎、開源中國、思否、部落格園、掘金、簡書、華為雲、阿里雲、騰訊雲、嗶哩嗶哩、今日頭條、新浪微博、個人部落格
全網可搜《小陳運維》
文章主要釋出於微信公眾號