Redis-cluster搭建详细攻略

2022-05-12 09:56:41 浏览数 (1)

redis-cluster实战

环境

代码语言:javascript复制
192.168.10.45
192.168.10.46
192.168.10.47

安装过程记录

install redis

代码语言:javascript复制
@all
cd /opt
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make

config and start node

代码语言:javascript复制
@192.168.10.45
mkdir /opt/redis-4.0.1/redis-cluster
cd /opt/redis-4.0.1/redis-cluster
mkdir 7000 7001 7002

cat > 7000/redis.conf << EOF
port 7000
bind 192.168.10.45
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7001/redis.conf << EOF
port 7001
bind 192.168.10.45
daemonize yes
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7002/redis.conf << EOF
port 7002
bind 192.168.10.45
daemonize yes
pidfile /var/run/redis_7002.pid
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes
EOF


@192.168.10.46
mkdir /opt/redis-4.0.1/redis-cluster
cd /opt/redis-4.0.1/redis-cluster
mkdir 7003 7004 7005

cat > 7003/redis.conf << EOF
port 7003
bind 192.168.10.46
daemonize yes
pidfile /var/run/redis_7003.pid
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7004/redis.conf << EOF
port 7004
bind 192.168.10.46
daemonize yes
pidfile /var/run/redis_7004.pid
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7005/redis.conf << EOF
port 7005
bind 192.168.10.46
daemonize yes
pidfile /var/run/redis_7005.pid
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes
EOF


@192.168.10.47
mkdir /opt/redis-4.0.1/redis-cluster
cd /opt/redis-4.0.1/redis-cluster
mkdir 7006 7007 7008

cat > 7006/redis.conf << EOF
port 7006
bind 192.168.10.47
daemonize yes
pidfile /var/run/redis_7006.pid
cluster-enabled yes
cluster-config-file nodes_7006.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7007/redis.conf << EOF
port 7007
bind 192.168.10.47
daemonize yes
pidfile /var/run/redis_7007.pid
cluster-enabled yes
cluster-config-file nodes_7007.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat > 7008/redis.conf << EOF
port 7008
bind 192.168.10.47
daemonize yes
pidfile /var/run/redis_7008.pid
cluster-enabled yes
cluster-config-file nodes_7008.conf
cluster-node-timeout 10100
appendonly yes
EOF


@192.168.10.45
for((i=0;i<=2;i  )); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done
@192.168.10.46
for((i=3;i<=5;i  )); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done
@192.168.10.47
for((i=6;i<=8;i  )); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

install cluster

代码语言:javascript复制
@all
yum -y install ruby ruby-devel rubygems rpm-build

# error
gem install redis

# update ruby
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB 

# retry
curl -sSL https://get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm install 2.4.1
rvm use 2.4.1
rvm use 2.4.1 --default
gem install redis

config and start cluster

代码语言:javascript复制
@192.168.10.45

/opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.10.45:7000 192.168.10.45:7001 192.168.10.45:7002 192.168.10.46:7003 192.168.10.46:7004 192.168.10.46:7005 192.168.10.47:7006 192.168.10.47:7007 192.168.10.47:7008

>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
192.168.10.45:7000
192.168.10.46:7003
192.168.10.47:7006
192.168.10.45:7001
Adding replica 192.168.10.46:7004 to 192.168.10.45:7000
Adding replica 192.168.10.47:7007 to 192.168.10.46:7003
Adding replica 192.168.10.45:7002 to 192.168.10.47:7006
Adding replica 192.168.10.46:7005 to 192.168.10.45:7001
Adding replica 192.168.10.47:7008 to 192.168.10.45:7000
M: 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1 192.168.10.45:7000
   slots:0-4095 (4096 slots) master
M: 534f8d52db1a84909fac8b087b824f4020a333cf 192.168.10.45:7001
   slots:12288-16383 (4096 slots) master
S: 72350104788ac47247035e718c02341342e43e0f 192.168.10.45:7002
   replicates f978a2c109a475474e6432d4c7399a771f265453
M: b977310fc96f8b9c6635cc655b14fd358593b06e 192.168.10.46:7003
   slots:4096-8191 (4096 slots) master
S: 782477dcc8ecbe06b07f229484e2e3b3a7bb6b8f 192.168.10.46:7004
   replicates 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1
S: e8177e568e25dc236f0a4fadd1aad3fb2d1c8405 192.168.10.46:7005
   replicates 534f8d52db1a84909fac8b087b824f4020a333cf
M: f978a2c109a475474e6432d4c7399a771f265453 192.168.10.47:7006
   slots:8192-12287 (4096 slots) master
S: 03e7300ba303014f20fecb28aa3a9618a9d9f805 192.168.10.47:7007
   replicates b977310fc96f8b9c6635cc655b14fd358593b06e
S: 64dd5cdcdecf52b55a2cb586bbad683c9680fe2f 192.168.10.47:7008
   replicates 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1
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.45:7000)
M: 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1 192.168.10.45:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
S: 03e7300ba303014f20fecb28aa3a9618a9d9f805 192.168.10.47:7007
   slots: (0 slots) slave
   replicates b977310fc96f8b9c6635cc655b14fd358593b06e
S: 64dd5cdcdecf52b55a2cb586bbad683c9680fe2f 192.168.10.47:7008
   slots: (0 slots) slave
   replicates 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1
S: e8177e568e25dc236f0a4fadd1aad3fb2d1c8405 192.168.10.46:7005
   slots: (0 slots) slave
   replicates 534f8d52db1a84909fac8b087b824f4020a333cf
S: 782477dcc8ecbe06b07f229484e2e3b3a7bb6b8f 192.168.10.46:7004
   slots: (0 slots) slave
   replicates 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1
M: f978a2c109a475474e6432d4c7399a771f265453 192.168.10.47:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
M: b977310fc96f8b9c6635cc655b14fd358593b06e 192.168.10.46:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
M: 534f8d52db1a84909fac8b087b824f4020a333cf 192.168.10.45:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 72350104788ac47247035e718c02341342e43e0f 192.168.10.45:7002
   slots: (0 slots) slave
   replicates f978a2c109a475474e6432d4c7399a771f265453
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

stop cluster

代码语言:javascript复制
$ for((i=0;i<=2;i  )); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.10.45 -p 700$i shutdown; done

$ for((i=3;i<=5;i  )); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.10.46 -p 700$i shutdown; done

$ for((i=6;i<=8;i  )); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.10.47 -p 700$i shutdown; done

集群命令

测试1

代码语言:javascript复制
/opt/redis-4.0.1/src/redis-cli -h 192.168.10.45 -c -p 7000
192.168.10.45:7000>
192.168.10.45:7000> set name gaomingie
-> Redirected to slot [5798] located at 192.168.10.46:7003
OK
192.168.10.46:7003> get name
"gaomingie"
192.168.10.46:7003>

测试2

代码语言:javascript复制
# 配置之前
/opt/redis-4.0.1/src/redis-cli -h 192.168.10.46 -c -p 7003
192.168.10.46:7003> set a a
(error) CLUSTERDOWN Hash slot not served

# 配置之后
/opt/redis-4.0.1/src/redis-cli -h 192.168.10.45 -c -p 7000
cluster info

cluster nodes
03e7300ba303014f20fecb28aa3a9618a9d9f805 192.168.10.47:7007@17007 slave b977310fc96f8b9c6635cc655b14fd358593b06e 0 1577085092000 8 connected
64dd5cdcdecf52b55a2cb586bbad683c9680fe2f 192.168.10.47:7008@17008 slave 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1 0 1577085094057 9 connected
e8177e568e25dc236f0a4fadd1aad3fb2d1c8405 192.168.10.46:7005@17005 slave 534f8d52db1a84909fac8b087b824f4020a333cf 0 1577085094057 6 connected
782477dcc8ecbe06b07f229484e2e3b3a7bb6b8f 192.168.10.46:7004@17004 slave 84df927cc743c11fb9cb1f9caae90fe3ecbb69f1 0 1577085091000 5 connected
f978a2c109a475474e6432d4c7399a771f265453 192.168.10.47:7006@17006 master - 0 1577085091357 7 connected 8192-12287
84df927cc743c11fb9cb1f9caae90fe3ecbb69f1 192.168.10.45:7000@17000 myself,master - 0 1577085092000 1 connected 0-4095
b977310fc96f8b9c6635cc655b14fd358593b06e 192.168.10.46:7003@17003 master - 0 1577085094257 4 connected 4096-8191
534f8d52db1a84909fac8b087b824f4020a333cf 192.168.10.45:7001@17001 master - 0 1577085093257 2 connected 12288-16383
72350104788ac47247035e718c02341342e43e0f 192.168.10.45:7002@17002 slave f978a2c109a475474e6432d4c7399a771f265453 0 1577085094000 7 connected
集群
代码语言:javascript复制
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
节点
代码语言:javascript复制
cluster meet <ip> <port> :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :从集群中移除 node_id 指定的节点。
cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽(slot)
代码语言:javascript复制
cluster addslots <slot> [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots <slot> [slot ...] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot <slot> node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot <slot> migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot <slot> importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot <slot> stable :取消对槽 slot 的导入( import)或者迁移( migrate)。
代码语言:javascript复制
cluster keyslot <key> :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot <slot> :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot <slot> <count> :返回 count 个 slot 槽中的键 。

check cluster status

代码语言:javascript复制
/opt/redis-4.0.1/src/redis-trib.rb check 192.168.10.46:7000
代码语言:javascript复制
/opt/redis-4.0.1/src/redis-cli -h 192.168.10.46 -c -p 7000

0 人点赞