1. 程式人生 > >ubuntu16.04安裝redis主從叢集

ubuntu16.04安裝redis主從叢集

安裝單機版

前置環境

apt-get update
apt install make
apt install gcc gcc+ build-essential tcl

下載安裝

cd /app
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 MALLOC=libc
make test
cd src && make install PREFIX=/usr/local/redis


# 安裝成功後
root@redis-node1:/app/redis-5.0.5/src# make install  PREFIX=/usr/local/redis

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

make test出錯

!!! WARNING The following tests failed:

*** [err]: PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires in tests/unit/expire.tcl
Expected 'somevalue {}' to equal or match '{} {}'
Cleanup: may take some time... OK
Makefile:262: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/app/redis-5.0.5/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2

某些操作性需要更多的時間,修改tests/unit/expire.tcl
tags {"slow"} {
        test {EXPIRE - After 2.1 seconds the key should no longer be here} {
            after 21000  # 這裡加個0
            list [r get x] [r exists x]
        } {{} 0}
    }

    test {EXPIRE - write on expire should work} {
        r del x
        r lpush x foo
        r expire x 10000  # 這裡加個0
        r lpush x bar
        r lrange x 0 -1
    } {bar foo}


# 如下輸出安裝成功
\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory '/app/redis-5.0.5/src'

配置

mkdir -p /data/redis/log
mkdir -p /data/redis/dir
mkdir -p /data/redis/run

cd /usr/local/redis
mkdir conf
vim conf/6379

配置檔案如下

bind 10.13.6.21
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/run/redis_6379.pid
loglevel notice
logfile "/data/redis/log/6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /data/redis/dir
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
slave-priority 100

啟動

/usr/local/bin/redis-server /usr/local/redis/conf/6379 --daemonize no

# 啟動後會報如下警告
3195:M 15 Sep 2019 22:17:50.852 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3195:M 15 Sep 2019 22:17:50.852 # Server initialized
3195:M 15 Sep 2019 22:17:50.852 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
3195:M 15 Sep 2019 22:17:50.852 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
3195:M 15 Sep 2019 22:17:50.852 * Ready to accept connections


#安裝提示解決即可

# 解決第一個,第二個
vim /etc/sysctl.conf
新增
net.core.somaxconn = 1024
vm.overcommit_memory = 1

# 生效
sysctl -p

# 解決第三個
# shell執行,重啟會失效
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 永久生效
vim /etc/rc.local
新增
echo never > /sys/kernel/mm/transparent_hugepage/enabled

加入systemctl

vim /etc/systemd/system/redis.service
systemctl daemon-reload
systemctl enable redis.service
systemctl start redis

# 內容如下
root@redis-node1:/data/redis# cat /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/6379 --daemonize no
ExecStop=/usr/local/redis/bin/redis-cli -h 10.13.6.21 -p 6379 shutdown
Restart=always

[Install]
WantedBy=multi-user.target

客戶端測試

root@redis-node1:/data/redis# redis-cli -h 10.13.6.21 -p 6379
10.13.6.21:6379> keys *
(empty list or set)
10.13.6.21:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:98832eb375d0399255e1094bf546c88efb8ccd97
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

安裝主從模式

主從模式 - slave安裝方式同上,配置檔案略有不同

slaveof 10.13.6.21 6379
slave-priority 90

完整配置

bind 10.13.6.22
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/run/redis_6379.pid
loglevel notice
logfile "/data/redis/log/6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /data/redis/dir
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
slaveof 10.13.6.21 6379
slave-priority 90

主從測試

10.13.6.21:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.13.6.22,port=7000,state=online,offset=168,lag=0
slave1:ip=10.13.6.22,port=7001,state=online,offset=168,lag=1
master_replid:ee4636f02a76df701b61507656c833de3cde259e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168
10.13.6.21:6379> exit

可以看