redis多机集群及开机自启动

安装redis

yum install gcc gcc-c++ automake autoconf jemalloc jemalloc-devel hiredis-devel
cd /opt
wget http://download.redis.io/releases/redis-4.0.7.tar.gz
tar xf redis-4.0.7.tar.gz
cd redis-4.0.7
make install
cp src/redis-trib.rb /usr/bin/

配置文件

单点配置

protected-mode no
port 6379
tcp-backlog 5120
timeout 0
tcp-keepalive 120
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis_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 "/opt/redis-4.0.7"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 4gb
maxmemory-policy noeviction
maxmemory-samples 5
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.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 no
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass "redistest"
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
appendfilename "appendonly.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
jemalloc-bg-thread yes

集群配置

protected-mode no
port 6379
tcp-backlog 5120
timeout 0
tcp-keepalive 120
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis_6379.log"
databases 1
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 "/opt/redis-4.0.7"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 4gb
maxmemory-policy noeviction
maxmemory-samples 5
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.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 no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file "nodes-6379.conf"
cluster-node-timeout 10000
cluster-require-full-coverage yes
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
masterauth "redistest"
requirepass "redistest"

拷贝配置文件到其它机器

scp /etc/redis.conf root@192.168.66.227:/etc/redis.conf
scp /etc/redis.conf root@192.168.66.82:/etc/redis.conf
scp /etc/redis.conf root@192.168.66.28:/etc/redis.conf
scp /etc/redis.conf root@192.168.66.81:/etc/redis.conf
scp /etc/redis.conf root@192.168.66.178:/etc/redis.conf

每台机器启动redis实例

redis-server /etc/redis.conf 

登录

redis-cli -h 192.168.66.147 -a redistest

Redis集群配置

yum -y groupinstall "Development Tools"
yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel
cd /usr/local/src
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
tar xf ruby-2.4.2.tar.gz
cd ruby-2.4.2
./configure
make
make install
gem install redis

gem离线安装redis

wget https://rubygems.org/downloads/redis-4.7.1.gem
gem install redis-4.7.1.gem

如果redis设定了密码则修改脚本redis-trib.rb

@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60)
改为
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60, :password => "你的密码")
scp /usr/bin/redis-trib.rb root@192.168.66.227:/usr/bin/
scp /usr/bin/redis-trib.rb root@192.168.66.82:/usr/bin/
scp /usr/bin/redis-trib.rb root@192.168.66.28:/usr/bin/
scp /usr/bin/redis-trib.rb root@192.168.66.81:/usr/bin/
scp /usr/bin/redis-trib.rb root@192.168.66.178:/usr/bin/

创建集群

redis-trib.rb create --replicas 1 192.168.66.147:6379 192.168.66.227:6379 192.168.66.82:6379 192.168.66.28:6379 192.168.66.81:6379 192.168.66.178:6379

...
M: 9f12aa5b643029ed76a0d4ff28dc16dcdad9885d 192.168.66.147:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 98db66b12e69648b42c9f927bf4e29e0cebd8c8b 192.168.66.178:6379
   slots: (0 slots) slave
   replicates 7d919387d197884edfd45e77bf92e2fba6b3cf61
S: a26f5a618639788bdcfa2f299fac0261d496e797 192.168.66.28:6379
   slots: (0 slots) slave
   replicates 4337ddf3801b8e39f9ccbe2540064ee02c4b5a7c
S: 4d77d46e56912b71e88fb7267bfdbf3379024061 192.168.66.81:6379
   slots: (0 slots) slave
   replicates 9f12aa5b643029ed76a0d4ff28dc16dcdad9885d
M: 7d919387d197884edfd45e77bf92e2fba6b3cf61 192.168.66.227:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 4337ddf3801b8e39f9ccbe2540064ee02c4b5a7c 192.168.66.82:6379
   slots:10923-16383 (5461 slots) master


集群主节点状态

redis-cli -a redistest cluster nodes | grep master

集群从节点状态

redis-cli -a redistest cluster nodes | grep slave

开机自启动

vim /etc/systemd/system/redis.service

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable redis.service
scp /etc/systemd/system/redis.service root@192.168.66.227:/etc/systemd/system/redis.service
scp /etc/systemd/system/redis.service root@192.168.66.82:/etc/systemd/system/redis.service
scp /etc/systemd/system/redis.service root@192.168.66.28:/etc/systemd/system/redis.service
scp /etc/systemd/system/redis.service root@192.168.66.81:/etc/systemd/system/redis.service
scp /etc/systemd/system/redis.service root@192.168.66.178:/etc/systemd/system/redis.service

高可用配置VIP

所有主机配置VIP 192.168.41.35

yum install -y keepalived

keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id rediscluster1
}

vrrp_script chk_rediscluster {
    script "/etc/keepalived/check_all.sh"
    interval 5
}

vrrp_instance rediscluster {
    state BACKUP
    interface eth0
    virtual_router_id 110
    priority 100
    nopreempt
    advert_int 2
    unicast_src_ip 192.168.41.36
    unicast_peer {
        192.168.41.37
        192.168.41.38
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.41.35/24 dev eth0
    }
    track_script {
        chk_rediscluster
    }

}
! Configuration File for keepalived

global_defs {
   router_id rediscluster2
}

vrrp_script chk_rediscluster {
    script "/etc/keepalived/check_all.sh"
    interval 5
}

vrrp_instance rediscluster {
    state BACKUP
    interface eth0
    virtual_router_id 110
    priority 90
    nopreempt
    advert_int 2
    unicast_src_ip 192.168.41.37
    unicast_peer {
        192.168.41.36
        192.168.41.38
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.41.35/24 dev eth0
    }
    track_script {
        chk_rediscluster
    }

}
! Configuration File for keepalived

global_defs {
   router_id rediscluster3
}

vrrp_script chk_rediscluster {
    script "/etc/keepalived/check_all.sh"
    interval 5
}

vrrp_instance rediscluster {
    state BACKUP
    interface eth0
    virtual_router_id 110
    priority 80
    nopreempt
    advert_int 2
    unicast_src_ip 192.168.41.38
    unicast_peer {
        192.168.41.36
        192.168.41.37
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.41.35/24 dev eth0
    }
    track_script {
        chk_rediscluster
    }

}

/etc/keepalived/check_all.sh

#!/bin/bash
redis-cli -c -p 6379 -h 127.0.0.1 -a redistest cluster nodes |grep master &> /dev/null
if [ $? != 0 ]
then
  killall  keepalived
fi

负载均衡

stream {
    upstream redis {
        server 192.168.51.144:6379     max_fails=3 fail_timeout=30s;
        server 192.168.51.145:6379     max_fails=3 fail_timeout=30s;
        server 192.168.51.146:6379     max_fails=3 fail_timeout=30s;
        server 192.168.51.147:6379     max_fails=3 fail_timeout=30s;
        server 192.168.51.148:6379     max_fails=3 fail_timeout=30s;
        server 192.168.51.149:6379     max_fails=3 fail_timeout=30s;
    }
    server {
        listen 6379;
        proxy_connect_timeout 2s;
        proxy_timeout 900s;
        proxy_pass redis;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值