Redis集羣安裝
1集羣機器分佈
192.168.1.133 redis1
192.168.1.134 redis2
192.168.1.135 redis3
2 免密登錄
ssh-keygen
ssh-copy-id 192.168.1.133
ssh-copy-id 192.168.1.134
ssh-copy-id 192.168.1.135
3 關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
4設置yum源
這裏添加阿里源
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/repo/Centos-7.repo
wget https://mirrors.aliyun.com/repo/epel-7.repo
5修改系統參數
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 655350
* soft nproc 65535
* hard nproc 655350
安裝依賴
yum install gcc make tcl –y
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel make bzip2 autoconf automake libtool bison sqlite-devel iconv-devel
6 安裝redis
mkdir /opt/haizhi/redis
mkdir -p /opt/haizhi/redis
cd /opt/haizhi/redis/
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar zxvf redis-4.0.6.tar.gz
cd redis-4.0.6/ #編譯安裝完成後會生成bin目錄
make & make PREFIX=/opt/haizhi/redis install
cp -a /opt/haizhi/redis/redis-4.0.6/src/redis-trib.rb /opt/haizhi/redis/bin/
#上面是複製集羣命令 到bin目錄
注意:如果make出錯 使用make MALLOC=libc
7 創建集羣需要的目錄和配置
創建3個實例目錄
cd /opt/haizhi/redis/
mkdir -p cluster-6379/{conf,data,log,pid}
mkdir -p cluster-6380/{conf,data,log,pid}
mkdir -p cluster-6381/{conf,data,log,pid}
爲每個實例創建配置文件(以6379爲例)
[root@localhost redis]# cat cluster-6379/conf/redis_6379.conf
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /opt/haizhi/redis/cluster-6379/conf/redis_6379.pid
loglevel notice
logfile "/opt/haizhi/redis/cluster-6379/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.rdb
dir /opt/haizhi/redis/cluster-6379/data
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
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 /opt/haizhi/redis/cluster-6379/conf/nodes-6379.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
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
其他實例只需修改6379爲6380 6381 即可使用
8 啓動各實例
./redis-server /opt/haizhi/redis/cluster-6379/conf/redis_6379.conf
./redis-server /opt/haizhi/redis/cluster-6380/conf/redis_6380.conf
./redis-server /opt/haizhi/redis/cluster-6381/conf/redis_6381.conf
[root@localhost redis]# netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 17736/./redis-serve
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 17741/./redis-serve
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 17746/./redis-serve
以上步驟 三個節點都要執行安裝操作
安裝啓動後再選一臺機器設置集羣
9 設置集羣(選一臺機器安裝)
redis官方提供了redis-trib.rb工具,但是在使用之前需要安裝ruby,以及redis和ruby連接
參考https://blog.csdn.net/woxingwosu0100/article/details/77840528
9.1安裝工具依賴環境(前面安裝過)
yum install -y gcc-c++ gcc* patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison sqlite-devel iconv-devel
9.2 安裝rvm(ruby管理工具)
執行下面語句
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
或者
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
然後
執行 curl -L get.rvm.io | bash -s stable
#如果上面的連接失敗,可以嘗試:
curl -Lhttps://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer| bash -s stable
執行 source /usr/local/rvm/scripts/rvm
rvm install 2.3.3
[root@server bin]# ruby --version
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
最後執行 gem install redis 安裝
10 創建集羣
cd /opt/haizhi/redis/bin
./redis-trib.rb create --replicas 2 192.168.1.133:6379 192.168.1.133:6380 192.168.1.133:6381 192.168.1.134:6379 192.168.1.134:6380 192.168.1.134:6381 192.168.1.135:6379 192.168.1.135:6380 192.168.1.135:6381
[root@server bin]# ./redis-trib.rb create --replicas 2 192.168.1.133:6379 192.168.1.133:6380 192.168.1.133:6381 192.168.1.134:6379 192.168.1.134:6380 192.168.1.134:6381 192.168.1.135:6379 192.168.1.135:6380 192.168.1.135:6381
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 3 masters:
192.168.1.133:6379
192.168.1.134:6379
192.168.1.135:6379
Adding replica 192.168.1.134:6380 to 192.168.1.133:6379
Adding replica 192.168.1.135:6380 to 192.168.1.133:6379
Adding replica 192.168.1.133:6380 to 192.168.1.134:6379
Adding replica 192.168.1.133:6381 to 192.168.1.134:6379
Adding replica 192.168.1.134:6381 to 192.168.1.135:6379
Adding replica 192.168.1.135:6381 to 192.168.1.135:6379
M: 19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379
slots:0-5460 (5461 slots) master
S: b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380
replicates 4f0f85289ad961682e995b3f9e547b16e3251268
S: 8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381
replicates 4f0f85289ad961682e995b3f9e547b16e3251268
M: 4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379
slots:5461-10922 (5462 slots) master
S: efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380
replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e
S: 6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381
replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933
M: 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379
slots:10923-16383 (5461 slots) master
S: 06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380
replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e
S: 7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381
replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933
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.1.133:6379)
M: 19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379
slots:0-5460 (5461 slots) master
2 additional replica(s)
S: 6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381
slots: (0 slots) slave
replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933
M: 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379
slots:10923-16383 (5461 slots) master
2 additional replica(s)
S: efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380
slots: (0 slots) slave
replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e
S: b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380
slots: (0 slots) slave
replicates 4f0f85289ad961682e995b3f9e547b16e3251268
S: 7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381
slots: (0 slots) slave
replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933
M: 4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379
slots:5461-10922 (5462 slots) master
2 additional replica(s)
S: 06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380
slots: (0 slots) slave
replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e
S: 8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381
slots: (0 slots) slave
replicates 4f0f85289ad961682e995b3f9e547b16e3251268
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看集羣
[root@localhost bin]# ./redis-cli -c -p 6379
127.0.0.1:6379> CLUSTER nodes
4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379@16379 myself,master - 0 1534227574000 4 connected 5461-10922
7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381@16381 slave 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 0 1534227576097 9 connected
efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380@16380 slave 19fd62cb41e794f6478eff3d209d7c9a8587f45e 0 1534227575595 5 connected
6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381@16381 slave 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 0 1534227575094 7 connected
06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380@16380 slave 19fd62cb41e794f6478eff3d209d7c9a8587f45e 0 1534227575595 8 connected
8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379@16379 master - 0 1534227576000 7 connected 10923-16383
8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381@16381 slave 4f0f85289ad961682e995b3f9e547b16e3251268 0 1534227575000 4 connected
19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379@16379 master - 0 1534227575000 1 connected 0-5460
b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380@16380 slave 4f0f85289ad961682e995b3f9e547b16e3251268 0 1534227575595 4 connected
127.0.0.1:6379>