Redis单机安装和集群部署
一、Redis单机安装
注:本文安装Redis版本为redis-5.0.7
yum install -y gcc
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
tar xzf redis-5.0.7.tar.gz
cd redis-5.0.7
make
mkdir -p /usr/local/redis/bin
cp src/redis-server /usr/local/redis/bin
cp src/redis-cli /usr/local/redis/bin
cp src/redis-sentinel /usr/local/redis/bin
cp src/redis-trib.rb /usr/local/redis/bin
cp src/redis-check-aof /usr/local/redis/bin
cp src/redis-check-rdb /usr/local/redis/bin
cp src/redis-benchmark /usr/local/redis/bin
echo 'export PATH=$PATH:/usr/local/redis/bin' >> .bash_profile
. .bash_profile
cp ./redis.conf /etc
二、修改Redis配置:
$ vim /etc/redis.conf
修改redis.conf中的以下内容:
bind 0.0.0.0 # 把 127.0.0.1 改为 0.0.0.0,监听所有 IPV4 地址,可以根据需求设置
daemonize yes # 把 no 修改为 yes,后台运行
三、启动Redis
启动redis服务:
$ redis-server /etc/redis.conf
验证redis是否启动成功:
$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
四、设置开机启动
vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis Server
After=network.target
[Service]
Type=simple
# User=redis
# Group=redis
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/redis/bin/redis-server /etc/redis.conf --daemonize no
# ExecStop=/usr/local/redis/bin/redis-cli shutdown
ExecStop=/usr/local/redis/bin/redis-cli -p 6379 shutdown
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start redis
systemctl enable redis
五、Redis集群部署
创建6个配置文件:
mkdir /etc/redis-cluster
cp redis.conf /etc/redis-cluster/redis-7001.conf
cp redis.conf /etc/redis-cluster/redis-7002.conf
cp redis.conf /etc/redis-cluster/redis-7003.conf
cp redis.conf /etc/redis-cluster/redis-7004.conf
cp redis.conf /etc/redis-cluster/redis-7005.conf
cp redis.conf /etc/redis-cluster/redis-7006.conf
mkdir /opt/redis-7001
mkdir /opt/redis-7002
mkdir /opt/redis-7003
mkdir /opt/redis-7004
mkdir /opt/redis-7005
mkdir /opt/redis-7006
修改所有的配置文件 (端口以及目录依次类推)
$ vim /etc/redis-cluster/redis-7001.conf
bind 0.0.0.0
protected-mode no
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
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.rdb
dir /opt/redis-7001
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.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
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 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
启动节点
redis-server /etc/redis-cluster/redis-7001.conf
redis-server /etc/redis-cluster/redis-7002.conf
redis-server /etc/redis-cluster/redis-7003.conf
redis-server /etc/redis-cluster/redis-7004.conf
redis-server /etc/redis-cluster/redis-7005.conf
redis-server /etc/redis-cluster/redis-7006.conf
创建集群
redis-cli --cluster create 192.168.223.111:7001 192.168.223.111:7002 192.168.223.111:7003 192.168.223.111:7004 192.168.223.111:7005 192.168.223.111:7006 --cluster-replicas 1
查询集群信息
[root@xxx redis-5.0.7]# redis-cli -c -h 10.241.100.100 -p 7001
10.241.100.100:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2152545
cluster_stats_messages_pong_sent:2162731
cluster_stats_messages_publish_sent:790700
cluster_stats_messages_sent:5105976
cluster_stats_messages_ping_received:2162726
cluster_stats_messages_pong_received:2152543
cluster_stats_messages_meet_received:5
cluster_stats_messages_publish_received:771607
cluster_stats_messages_received:5086881
10.241.150.160:7001> exit
[root@xxx redis-5.0.7]#