转[ https://www.yangzb.com/?p=91 ]
部署环境说明
3数据分片节点,每1主节点2从节点,生产使用中尽量保证所有主、从节点分布在不同服务器上,用redis-trib.rb create创建集群后可以再次调整。
当主节点挂掉后从节点将升为主节点继续提供服务。
redis安装
1.准备 三主机同步操作
分别在三台主机上创建redis用户、创建需要的目录、下载应用
#创建用户,创建目录并赋权
[root@db1 ~]# useradd redis
[root@db1 ~]# mkdir -p /data/redis/{conf,log,db,pid}
[root@db1 ~]# mkdir -p /data/redis/db/{6379,6380,6381}
[root@db1 ~]# mkdir /usr/local/redis
[root@db1 ~]# chown redis:redis /usr/local/redis
[root@db1 ~]# chown -R redis:redis /data/redis/
#下载解压
[root@bd52 local]# su - redis
[redis@db1 ~]$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[redis@db1 ~]$ tar -zxvf redis-3.0.7.tar.gz
2.编译安装 三主机同步操作
[redis@db1 ~]$ cd redis-3.0.7
[redis@db1 redis-3.0.7]$ make
[redis@db1 redis-3.0.7]$ cd src/
[redis@db1 src]$ make install PREFIX=/usr/local/redis
[redis@db1 src]$ cp /home/redis/redis-3.0.7/redis.conf /data/redis/conf/6379.conf
[redis@db1 src]$ cp /home/redis/redis-3.0.7/redis.conf /data/redis/conf/6380.conf
[redis@db1 src]$ cp /home/redis/redis-3.0.7/redis.conf /data/redis/conf/6381.conf
建立软连接方便直接使用命令,需要root执行
[root@db1 redis]# ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
[root@db1 redis]# ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
3.编译安装 三主机同步操作
分别修改/data/redis/conf/下配置文件
logfile /data/redis/log/端口.log
pidfile /data/redis/pid/端口.pid
dir /data/redis/db/端口/
port 端口
如:
logfile "/data/redis/log/6379.log"
pidfile "/data/redis/pid/6379.pid"
dir "/data/redis/db/6379"
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file /data/redis/conf/6379_nodes.conf
masterauth 密码
requirepass 密码
daemonize yes
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
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
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
lua-time-limit 5000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
list-max-ziplist-entries 512
list-max-ziplist-value 64
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
4.设置开机启动 三主机同步操作
需要root执行
[root@db1 utils]# cp /home/redis/redis-3.0.7/utils/redis_init_script /etc/init.d/redis6379
[root@bd52 ~]# cp /home/redis/redis-3.0.7/utils/redis_init_script /etc/init.d/redis6380
[root@bd52 ~]# cp /home/redis/redis-3.0.7/utils/redis_init_script /etc/init.d/redis6381
[root@bd52 ~]# chmod 755 /etc/init.d/redis*
#redis6380 redis6381 redis6379均需要修改
[root@db1 utils]# vi /etc/init.d/redis6379
在行首加
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
修改下面配置
REDISPORT=6379
PIDFILE=/data/redis/pid/${REDISPORT}.pid
CONF="/data/redis/conf/${REDISPORT}.conf"
$CLIEXEC -p $REDISPORT -a 密码 shutdown
#启动
[redis@bd52 src]$ service redis6379 start
Starting Redis server...
[redis@bd52 src]$ service redis6380 start
Starting Redis server...
[redis@bd52 src]$ service redis6381 start
Starting Redis server...
#查看进程
[redis@bd52 src]$ ps -ef|grep redis
redis 26794 1 0 15:26 ? 00:00:00 /usr/local/bin/redis-server *:6379
redis 26821 1 0 15:26 ? 00:00:00 /usr/local/bin/redis-server *:6380
redis 26822 1 0 15:26 ? 00:00:00 /usr/local/bin/redis-server *:6381
创建集群
[redis@db1 redis]$ wget https://rubygems.org/downloads/redis-3.0.7.gem
[redis@db1 redis]$ gem install redis-3.0.7.gem
[redis@db1 redis]$ /home/redis/redis-3.0.7/src/redis-trib.rb create --replicas 2 192.168.10.16:6379 192.168.10.15:6379 192.168.10.52:6379 192.168.10.15:6380 192.168.10.16:6380 192.168.10.52:6380 192.168.10.16:6381 192.168.10.15:6381 192.168.10.52:6381
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 3 masters:
192.168.10.16:6379
192.168.10.15:6379
192.168.10.52:6379
Adding replica 192.168.10.15:6380 to 192.168.10.16:6379
Adding replica 192.168.10.52:6380 to 192.168.10.16:6379
Adding replica 192.168.10.16:6380 to 192.168.10.15:6379
Adding replica 192.168.10.16:6381 to 192.168.10.15:6379
Adding replica 192.168.10.15:6381 to 192.168.10.52:6379
Adding replica 192.168.10.52:6381 to 192.168.10.52:6379
M: 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d 192.168.10.16:6379
slots:0-5460 (5461 slots) master
M: 2cba61ff5607ea795f95665c5a1db1f68eb47824 192.168.10.15:6379
slots:5461-10922 (5462 slots) master
M: 363e8743a3b8d37f42cf2dc342f1b95ca0270582 192.168.10.52:6379
slots:10923-16383 (5461 slots) master
S: e78648e33fb63bf8b68883d3bad1bd8f421627af 192.168.10.15:6380
replicates 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d
S: 041862edaad64b47d1dadf27aa533355c483e1d6 192.168.10.16:6380
replicates 2cba61ff5607ea795f95665c5a1db1f68eb47824
S: 032c9b2308c6efbdc62aac09a79da009b9f59a45 192.168.10.52:6380
replicates 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d
S: 15397466421a55238cc0a3fc9534a6e5971d9c4c 192.168.10.16:6381
replicates 2cba61ff5607ea795f95665c5a1db1f68eb47824
S: ca454cba67ee3c2d98a429fc77b261061fec0e77 192.168.10.15:6381
replicates 363e8743a3b8d37f42cf2dc342f1b95ca0270582
S: 4c1a8c1411e477ad4f234d365e7728dbe436d3d0 192.168.10.52:6381
replicates 363e8743a3b8d37f42cf2dc342f1b95ca0270582
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join........
>>> Performing Cluster Check (using node 192.168.10.16:6379)
M: 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d 192.168.10.16:6379
slots:0-5460 (5461 slots) master
M: 2cba61ff5607ea795f95665c5a1db1f68eb47824 192.168.10.15:6379
slots:5461-10922 (5462 slots) master
M: 363e8743a3b8d37f42cf2dc342f1b95ca0270582 192.168.10.52:6379
slots:10923-16383 (5461 slots) master
M: e78648e33fb63bf8b68883d3bad1bd8f421627af 192.168.10.15:6380
slots: (0 slots) master
replicates 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d
M: 041862edaad64b47d1dadf27aa533355c483e1d6 192.168.10.16:6380
slots: (0 slots) master
replicates 2cba61ff5607ea795f95665c5a1db1f68eb47824
M: 032c9b2308c6efbdc62aac09a79da009b9f59a45 192.168.10.52:6380
slots: (0 slots) master
replicates 2bcec58b763f3d10e48dae4cbbcd5d8de76cc98d
M: 15397466421a55238cc0a3fc9534a6e5971d9c4c 192.168.10.16:6381
slots: (0 slots) master
replicates 2cba61ff5607ea795f95665c5a1db1f68eb47824
M: ca454cba67ee3c2d98a429fc77b261061fec0e77 192.168.10.15:6381
slots: (0 slots) master
replicates 363e8743a3b8d37f42cf2dc342f1b95ca0270582
M: 4c1a8c1411e477ad4f234d365e7728dbe436d3d0 192.168.10.52:6381
slots: (0 slots) master
replicates 363e8743a3b8d37f42cf2dc342f1b95ca0270582
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
设置密码,在所有节点(包含从节点)
config set masterauth 密码
config set requirepass 密码
config rewrite