虚拟化容器,大数据,DBA,中间件,监控。

Redis cluster三主三从集群快速部署(十一)

11 11月
作者:admin|分类:DBA运维

部署三主三从redis cluster集群

架构图

请添加图片描述

环境准备

IP 主机名 端口号 节点
192.168.81.210 redis-1 6380 master
192.168.81.210 redis-1 6381 slave—>redis-2
192.168.81.220 redis-2 6380 master
192.168.81.220 redis-2 6381 slave—>redis-3
192.168.81.230 redis-3 6380 master
192.168.81.230 redis-3 6381 slave—>redis-1

1.redis运维脚本

使用这个脚本管理redis

#!/bin/bash
#redis控制脚本
redis_port=$2
redis_name="redis_${redis_port}"
redis_home=/data/redis_cluster/${redis_name}
redis_conf=${redis_home}/conf/${redis_name}.conf
redis_host=`ifconfig ens33 | awk 'NR==2{print $2}'`
redis_pass=$3
red="\e[031m"
green="\e[032m"
yellow="\e[033m"
black="\e[0m"

Usage(){
     
	echo "usage: sh $0 {start|stop|restart|login|ps|logs|-h} PORT"
}

Start(){
     
        redis_cz=`netstat -lnpt | grep redis | grep "${redis_port}" | wc -l`
        if [ $redis_cz -eq 0 ];then
                redis-server ${redis_conf}
                if [ -z $state ];then
                        echo -e "${green}redis ${redis_port}实例启动成功!${black}"
                else
                        echo -e "${green}redis ${redis_port}实例重启成功!${black}"
                fi
                netstat -lnpt | grep ${redis_port}
        else
                if [ -z $state ];then
                        echo -e "${yellow}redis "${redis_port}"实例已经是启动状态!${black}"
                        netstat -lnpt | grep ${redis_port}
                fi
        fi
}

Stop(){
     
        redis_cz=`netstat -lnpt | grep redis | grep "${redis_port}" | wc -l`
        if [ $redis_cz -gt 0 ];then
                redis-cli -h $redis_host -p $redis_port shutdown
                if [ -z $state ];then
                        echo -e "${green}redis ${redis_port}实例关闭成功!"
                fi
        else
                if [ -z $state ];then
                        echo -e "${red}redis "${redis_port}"实例没有启动!${black}"
                fi
        fi
}

Restart(){
     
	state=restart
	Stop
	Start
}

Login(){
     
        redis_cz=`netstat -lnpt | grep redis | grep "${redis_port}" | wc -l`
        if [ $redis_cz -gt 0 ];then
		redis-cli -h $redis_host -p $redis_port 
	else
		echo -e "${red}redis ${redis_port}实例没有启动!${black}"
		echo -en  "${yellow}是否要启动reis? [y/n]${black}"
		read action
		case $action in 
		y|Y)
			Start
			Login
			;;
		n|N)
			exit 1
			;;
		esac
	fi
}

Ps(){
     
	ps aux | grep redis
}

Logs(){
     
	tail -f ${redis_home}/logs/${redis_name}.log
}
	
Help(){
     
	Usage
	echo "+-------------------------------------------------------------------------------+"	
	echo "| start 启动redis |"
	echo "| stop 关闭redis |"
	echo "| restart 重启redis |"
	echo "| login 登陆redis |"
	echo "| ps 查看redis的进程信息,不需要加端口号 |"
	echo "| logs 查看redis日志持续输出 |"
	echo "| 除ps命令外,所有命令后面都需要加端口号 |"	
	echo "+-------------------------------------------------------------------------------+"	
}

if [ $# -ne 2 ];then
	if [ "$1" != "ps" ] &&  [ "$1" != "-h" ];then
		Usage
		exit 1
	fi
fi


case $1 in 
start)
	Start
	;;
stop)
	Stop
	;;
restart)
	Restart
	;;
login)
	Login
	;;
ps)
	Ps
	;;
logs)
	Logs
	;;
-h)
	Help
	;;
*)
	Help
	;;
esac

2.部署redis节点

三个主机都执行,共部署两个redis节点,一个6380一个6381端口

1.创建部署路径
mkdir -p /data/redis_cluster/redis_{6380,6381}/{conf,data,logs,pid}

2.准备配置文件
cat > /data/redis_cluster/redis_6380/conf/redis_6380.conf <<EOF
bind $(ifconfig | awk 'NR==2{print $2}')
port 6380  
daemonize yes
logfile /data/redis_cluster/redis_6380/logs/redis_6380.log
pidfile /data/redis_cluster/redis_6380/pid/redis_6380.log
dbfilename "redis_6380.rdb"
dir /data/redis_cluster/redis_6380/data
cluster-enabled yes
cluster-config-file node_6380.conf
cluster-node-timeout 15000
save 60 10000
save 300 10
save 900 1
EOF

cat > /data/redis_cluster/redis_6381/conf/redis_6381.conf <<EOF
bind $(ifconfig | awk 'NR==2{print $2}')
port 6381  
daemonize yes
logfile /data/redis_cluster/redis_6381/logs/redis_6381.log
pidfile /data/redis_cluster/redis_6381/pid/redis_6381.log
dbfilename "redis_6381.rdb"
dir /data/redis_cluster/redis_6381/data
cluster-enabled yes
cluster-config-file node_6381.conf
cluster-node-timeout 15000
save 60 10000
save 300 10
save 900 1
EOF

3.启动
[root@redis-1 ~]# ./redis_shell.sh start 6380
redis 6380实例启动成功!
tcp        0      0 192.168.81.210:6380     0.0.0.0:*               LISTEN      127999/redis-server 
tcp        0      0 192.168.81.210:16380    0.0.0.0:*               LISTEN      127999/redis-server 
[root@redis-1 ~]# ./redis_shell.sh start 6381
redis 6381实例启动成功!
tcp        0      0 192.168.81.210:6381     0.0.0.0:*               LISTEN      128014/redis-server 
tcp        0      0 192.168.81.210:16381    0.0.0.0:*               LISTEN      128014/redis-server 

在这里插入图片描述

3.配置集群节点之间相互发现

互相发现只需要在一个节点上操作即可

[root@redis-2 ~]# redis-cli -h 192.168.81.210 -p 6380
192.168.81.210:6380> CLUSTER MEET 192.168.81.210 6381
OK
192.168.81.210:6380> CLUSTER MEET 192.168.81.220 6380
OK
192.168.81.210:6380> CLUSTER MEET 192.168.81.220 6381
OK
192.168.81.210:6380> CLUSTER MEET 192.168.81.230 6380
OK
192.168.81.210:6380> CLUSTER MEET 192.168.81.230 6381
OK

192.168.81.210:6380> CLUSTER NODES
34e698b929948c3c61c4ba40c129161b736ee5cc 192.168.81.230:6381 master - 0 1612334628247 5 connected
4827ff5cbc7122a4459b1d91bd15a89e0661a91b 192.168.81.220:6381 master - 0 1612334624212 2 connected
8d918c708db4a9fbe6b08d9f707d501eb328885e 192.168.81.230:6380 master - 0 1612334625222 0 connected
4e99bc582fede8e359fb25d163e7267f616409ff 192.168.81.210:6381 master - 0 1612334626229 1 connected
5f19db5c04ccaea18270819f5af37cacbf41a800 192.168.81.210:6380 myself,master - 0 0 3 connected
454fe9862588f97f969c181b3f8c5e24f6135265 192.168.81.220:6380 master - 0 1612334627241 4 connected

6个redis节点已经全部发现

在这里插入图片描述

4.配置cluster分配槽位

在redis-1上操作即可

1.分配槽位
[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6380 cluster addslots {0..5461}
OK
[root@redis-1 ~]# redis-cli -h 192.168.81.220 -p 6380 cluster addslots {5462..10922}
OK
[root@redis-1 ~]# redis-cli -h 192.168.81.230 -p 6380 cluster addslots {10923..16383}
OK

2.查看集群状态
[root@redis-1 ~]# redis-cli -h 192.168.81.210 -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:1
cluster_current_epoch:5
cluster_my_epoch:3
cluster_stats_messages_sent:488
cluster_stats_messages_received:488

5.配置三主三从集群

1.获取集群信息
[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6380 cluster nodes | grep 6380 | awk '{print $1,$2}'
454fe9862588f97f969c181b3f8c5e24f6135265 192.168.81.220:6380
5f19db5c04ccaea18270819f5af37cacbf41a800 192.168.81.210:6380
8d918c708db4a9fbe6b08d9f707d501eb328885e 192.168.81.230:6380

2.配置交叉复制
[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6381
192.168.81.210:6381> CLUSTER REPLICATE 454fe9862588f97f969c181b3f8c5e24f6135265
OK

[root@redis-1 ~]# redis-cli -h 192.168.81.220 -p 6381
192.168.81.220:6381> CLUSTER REPLICATE 8d918c708db4a9fbe6b08d9f707d501eb328885e
OK

[root@redis-1 ~]# redis-cli -h 192.168.81.230 -p 6381
192.168.81.230:6381> CLUSTER REPLICATE 5f19db5c04ccaea18270819f5af37cacbf41a800
OK

[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6380 cluster nodes
454fe9862588f97f969c181b3f8c5e24f6135265 192.168.81.220:6380 master - 0 1612336046339 4 connected 5462-10922
4827ff5cbc7122a4459b1d91bd15a89e0661a91b 192.168.81.220:6381 slave 8d918c708db4a9fbe6b08d9f707d501eb328885e 0 1612336049371 2 connected
5f19db5c04ccaea18270819f5af37cacbf41a800 192.168.81.210:6380 myself,master - 0 0 3 connected 0-5461
8d918c708db4a9fbe6b08d9f707d501eb328885e 192.168.81.230:6380 master - 0 1612336047350 0 connected 10923-16383
34e698b929948c3c61c4ba40c129161b736ee5cc 192.168.81.230:6381 slave 5f19db5c04ccaea18270819f5af37cacbf41a800 0 1612336045330 5 connected
4e99bc582fede8e359fb25d163e7267f616409ff 192.168.81.210:6381 slave 454fe9862588f97f969c181b3f8c5e24f6135265 0 1612336048360 4 connected

已经交叉复制,到此三主三从redis cluster集群部署完成

在这里插入图片描述

6.快速删除一个redis cluster集群方式

两种方法:

  • 将所有redis节点的数据文件全部删除包括rdb、node_6380.conf
  • 登陆每一个节点,执行cluster reset
浏览483 评论0
返回
目录
返回
首页
Redis运维脚本(十二) Redis哨兵集群主库故障数据恢复(九)