数据库
首页 > 数据库> > 1redis集群搭建

1redis集群搭建

作者:互联网

一准备:

在db01中操作:

[root@db01 ~]# mkdir -p /opt/redis_cluster/redis_{6380,6381}/{conf,logs,pid}
[root@db01 ~]# mkdir –p /data/redis_cluster/redis_{6380,6381}
[root@db01 ~]# cat >/opt/redis_cluster/redis_6380/conf/redis_6380.conf<<EOF
> bind 10.0.0.201
> port 6380
> daemonize yes
> pidfile "/opt/redis_cluster/redis_6380/pid/redis_6380.pid"
> logfile "/opt/redis_cluster/redis_6380/logs/redis_6380.log"
> dbfilename "redis_6380.rdb"
> dir "/data/redis_cluster/redis_6380/"
> cluster-enabled yes
> cluster-config-file nodes_6380.conf
> cluster-node-timeout 15000
> EOF

[root@db01 ~]# cd /opt/redis_cluster/
[root@db01 redis_cluster]# cp redis_6380/conf/redis_6380.conf redis_6381/conf/redis_6381.conf
[root@db01 redis_cluster]# sed -i 's#6380#6381#g' redis_6381/conf/redis_6381.conf

[root@db01 redis_cluster]# rsync -avz /opt/redis_cluster/redis_638* db02:/opt/redis_cluster/
[root@db01 redis_cluster]# rsync -avz /opt/redis_cluster/redis_638* db03:/opt/redis_cluster/

[root@db01 redis_cluster]# redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
[root@db01 redis_cluster]# redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
[root@db01 redis_cluster]# ps -ef|grep redis
root       1529      1  0 03:47 ?        00:00:07 redis-server 10.0.0.201:6379
root       1572      1  0 04:13 ?        00:00:00 redis-server 10.0.0.201:6380 [cluster]
root       1576      1  1 04:14 ?        00:00:00 redis-server 10.0.0.201:6381 [cluster]
root       1580   1298  0 04:14 pts/0    00:00:00 grep --color=auto redis
[root@db01 redis_cluster]# 

 在db02中操作

[root@db02 ~]# find /opt/redis_cluster/redis_638* -type f -name "*.conf"|xargs sed -i "/bind/s#201#202#g"
[root@db02 ~]# mkdir –p /data/redis_cluster/redis_{6380,6381}
[root@db02 ~]# redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
[root@db02 ~]# redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
[root@db02 ~]# ps -ef|grep redis
root       1037      1  0 4月10 ?       00:04:56 redis-server 10.0.0.202:6379
root       1046      1  0 4月10 ?       00:06:13 redis-sentinel 10.0.0.202:26379 [sentinel]
root       2291      1  0 04:40 ?        00:00:00 redis-server 10.0.0.202:6380 [cluster]
root       2295      1  0 04:40 ?        00:00:00 redis-server 10.0.0.202:6381 [cluster]
root       2299   2131 19 04:40 pts/0    00:00:00 grep --color=auto redis
[root@db02 ~]# 

 在db03中操作:

[root@db03 redis_6381]# find /opt/redis_cluster/redis_638* -type f -name "*.conf"|xargs sed -i "/bind/s#201#203#g"
[root@db03 redis_6381]# redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
[root@db03 redis_6381]# redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
[root@db03 redis_6381]# ps -ef|grep redis
root       1530      1  0 4月10 ?       00:05:03 redis-server 10.0.0.203:6379
root       1543      1  0 4月10 ?       00:06:41 redis-sentinel 10.0.0.203:26379 [sentinel]
root       3234      1  1 04:52 ?        00:00:00 redis-server 10.0.0.203:6380 [cluster]
root       3238      1  0 04:52 ?        00:00:00 redis-server 10.0.0.203:6381 [cluster]
root       3242   3012  0 04:52 pts/0    00:00:00 grep --color=auto redis
[root@db03 redis_6381]# 

 检查(查看一下是不是有四个端口)

redis集群开通redis客户端连接的端口6380和6381后,系统会自动开通集群总线的通讯端口,总线通讯端口为redis客户端连接端口+10000

[root@db01 ~]# netstat -lntup|grep redis
 
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1529/redis-server 1  
tcp        0      0 10.0.0.201:6380         0.0.0.0:*               LISTEN      1572/redis-server 1 
tcp        0      0 10.0.0.201:6381         0.0.0.0:*               LISTEN      1576/redis-server 1 
tcp        0      0 10.0.0.201:16380        0.0.0.0:*               LISTEN      1572/redis-server 1 
tcp 0 0 10.0.0.201:16381 0.0.0.0:* LISTEN 1576/redis-server 1
[root@db01 ~]# 

 二编写工具脚本并将脚本分别拷贝到db01,db02,db03中

[root@db01 ~]# vim redis_shell.sh

#!/bin/bash
USAG(){
    echo "sh $0 {start|stop|restart|login|ps|tail} PORT"
}

if [ "$#" = 1 ]
then
    REDIS_PORT='6379'
elif 
    [ "$#" = 2 -a -z "$(echo "$2"|sed 's#[0-9]##g')" ]
then
    REDIS_PORT="$2"
else
    USAG
    exit 0
fi

REDIS_IP=$(hostname -I|awk '{print $1}')
PATH_DIR=/opt/redis_cluster/redis_${REDIS_PORT}/
PATH_CONF=/opt/redis_cluster/redis_${REDIS_PORT}/conf/redis_${REDIS_PORT}.conf
PATH_LOG=/opt/redis_cluster/redis_${REDIS_PORT}/logs/redis_${REDIS_PORT}.log

CMD_START(){
    redis-server ${PATH_CONF}
}

CMD_SHUTDOWN(){
    redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} shutdown
}

CMD_LOGIN(){
    redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT}
}

CMD_PS(){
    ps -ef|grep redis
}

CMD_TAIL(){
    tail -f ${PATH_LOG}
}

case $1 in
    start)
        CMD_START
        CMD_PS
        ;;
    stop)
        CMD_SHUTDOWN
        CMD_PS
        ;;
    restart)
CMD_SHUTDOWN CMD_START CMD_PS ;; login) CMD_LOGIN ;; ps) CMD_PS ;; tail) CMD_TAIL ;; *) USAG esac
[root@db01 ~]# scp redis_shell.sh db02:/root/
redis_shell.sh                                                                                              100% 1085    55.8KB/s   00:00    
[root@db01 ~]# scp redis_shell.sh db03:/root/
redis_shell.sh                                                                                              100% 1085   182.6KB/s   00:00    
[root@db01 ~]# 

 redis脚本用法解释:

[root@db01 ~]# sh redis_shell.sh stop 6380 #停止6380

[root@db01 ~]# sh redis_shell.sh ps #查看redis进程

[root@db01 ~]# sh redis_shell.sh start 6380 #运行6380

[root@db01 ~]# sh redis_shell.sh tail 6380 #查看端口日志

[root@db01 ~]# sh redis_shell.sh restart 6380 #重启6380端口

 三配置redis集群互相发现:

[root@db01 ~]# sh redis_shell.sh login 6380
10.0.0.201:6380> cluster meet 10.0.0.201 6381
OK
10.0.0.201:6380> cluster nodes
4c007a5e2d894aa4c6ce950467221f01fd50f645 10.0.0.201:6380 myself,master - 0 0 1 connected
6139c63daaac449a5173a41ae3be3f9d86ed8fa8 10.0.0.201:6381 master - 0 1618103793050 0 connected
10.0.0.201:6380> 
10.0.0.201:6380> 
[root@db01 ~]# redis-cli -h db01 -p 6380 cluster meet 10.0.0.202 6380
OK
[root@db01 ~]# redis-cli -h db01 -p 6380 cluster meet 10.0.0.202 6381
OK
[root@db01 ~]# redis-cli -h db01 -p 6380 cluster meet 10.0.0.203 6380
OK
[root@db01 ~]# redis-cli -h db01 -p 6380 cluster meet 10.0.0.203 6381
OK
[root@db01 ~]# redis-cli -h db01 -p 6380 cluster nodes
070b91942844a34c1871642eef2d24bd3e816403 10.0.0.203:6380 master - 0 1618104586306 4 connected
74bdf79d95556ffd38c47b01d0c76f393706bbda 10.0.0.202:6380 master - 0 1618104590839 5 connected
4c007a5e2d894aa4c6ce950467221f01fd50f645 10.0.0.201:6380 myself,master - 0 0 1 connected
6139c63daaac449a5173a41ae3be3f9d86ed8fa8 10.0.0.201:6381 master - 0 1618104589328 2 connected
2e616daa28ca6518b908148227edf562280a2d61 10.0.0.202:6381 master - 0 1618104590336 3 connected
ddee11b9c4c6622bbf28497c53aa1eb84846084f 10.0.0.203:6381 master - 0 1618104588321 0 connected
[root@db01 ~]# 

 四给每个主节点分配槽位:

默认槽位总数是16384,而本例中有3个主节点,那么16384/3=5461.33

因此我们可以分配5461,5461,5462

[root@db01 ~]# redis-cli -h db01 -p 6380 cluster addslots {0..5461}
OK
[root@db01 ~]# redis-cli -h db02 -p 6380 cluster addslots {5462..10922}
OK
[root@db01 ~]# redis-cli -h db03 -p 6380 cluster addslots {10923..16383}
OK
[root@db01 ~]# 

[root@db01 ~]# redis-cli -h db01 -p 6380 cluster nodes
070b91942844a34c1871642eef2d24bd3e816403 10.0.0.203:6380 master - 0 1618107857045 4 connected 10923-16383
74bdf79d95556ffd38c47b01d0c76f393706bbda 10.0.0.202:6380 master - 0 1618107860069 5 connected 5462-10922
4c007a5e2d894aa4c6ce950467221f01fd50f645 10.0.0.201:6380 myself,master - 0 0 1 connected 0-5461
6139c63daaac449a5173a41ae3be3f9d86ed8fa8 10.0.0.201:6381 master - 0 1618107863089 2 connected
2e616daa28ca6518b908148227edf562280a2d61 10.0.0.202:6381 master - 0 1618107861075 3 connected
ddee11b9c4c6622bbf28497c53aa1eb84846084f 10.0.0.203:6381 master - 0 1618107862086 0 connected
[root@db01 ~]# redis-cli -h db01 -p 6380 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:5
cluster_my_epoch:1
cluster_stats_messages_sent:8611
cluster_stats_messages_received:8611
[root@db01 ~]#

 

 五集群添加数据的方法:

[root@db01 ~]# redis-cli -h db01 -p 6380 set k1 v1
(error) MOVED 12706 10.0.0.203:6380
[root@db01 ~]# redis-cli -c -h db01 -p 6380 set k1 v1
OK
[root@db01 ~]# 

 

 

1

1

 

1

1

 

标签:10.0,redis,cluster,集群,6380,1redis,db01,root,搭建
来源: https://www.cnblogs.com/tyjs09/p/14647202.html