redis多实例搭建
一、安装redis单节点
官网下载二进制源码包安装,包里面为预编译过的,解压后可以直接二进制安装:
[root@localhost ~]# tar -zxf redis
[root@localhost ~]# cd redis
[root@localhost ~]# make && make install
接着进入redis目录下的src目录,src目录下这些文件作用如下
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具.你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能.
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况(部分版本没有此文件)
修改配置文件,一般需要把daemonize no 改为 daemonize yes,bind后的IP改为0.0.0.0,其他的看需要修改:
启动redis:
redis-server redis.conf
二、配置redis单机多实例
配置redis集群。
redis安装完成后,创建redis_cluster目录,并分别创建6379/7000/7001/7002/7003节点目录:
[root@localhost ~]#cd /usr/local/
[root@localhost ~]#mkdir -p redis-cluster/{6379,7000,7001,7002,7003,7004}
在每个节点目录里创建redis.conf配置文件,以6379节点为例:
port 6379
bind 0.0.0.0
protected-mode no
daemonize yes
pidfile /usr/local/redis-cluster/6379/redis_6379.pid
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
appendonly yes
其余各节点只需修改6379为各自端口号即可。
启动redis主节点和其余节点:
[root@localhost redis-cluster]# cd 6379
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7000/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7001/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7002/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7003/
[root@localhost redis-cluster]# redis-server redis.conf
组件集群
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
[testapp@k8s-node1 ~/redis/redis_cluster]$ cat flushCluster.sh
##判断参数
if [ $# -eq 0 ]
then echo -e "usage: sh $0 ip \nexit!!" exit
fi
##删除文件
rm /home/ap/testapp/redis/redis_cluster/appendonly.aof /home/ap/testapp/redis/redis_cluster/dump.rdb /home/ap/testapp/redis/redis_cluster/nodes-*.conf
echo "remove appendonly.aof dump.rdb nodes-*.conf,but will be re-created once restart done"
##清除redsi库
for port in 6380 6381 6382
do
##清除redsi库
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -h $1 -p ${port} <2>/dev/null
flushdb
cluster reset
exit
/
EOF
echo "reflushdb redis $1:$port finished"
##关闭服务
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -p ${port} shutdown
echo "shut down redis service on $1:$port finished"
##启动服务
/home/ap/testapp/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis${port}.conf
echo "start redis service on $1:$port finished"
done
echo "all port restart finished"
9.为集群中的每一个节点设置密码
编写批量启动脚本
[testapp@k8s-node1 ~/redis/redis_cluster]$ pwd
/home/ap/testapp/redis/redis_cluster
[testapp@k8s-node1 ~/redis/redis_cluster]$ ll
总用量 236
-rw-rw-r-- 1 testapp testapp 92 8月 14 18:56 appendonly.aof
-rw-r--r-- 1 testapp testapp 175 8月 14 18:56 dump.rdb
-rw-rw-r-- 1 testapp testapp 957 8月 14 18:55 flushCluster.sh
-rw-r--r-- 1 testapp testapp 817 8月 14 18:56 nodes-6380.conf
-rw-r--r-- 1 testapp testapp 817 8月 14 18:56 nodes-6381.conf
-rw-r--r-- 1 testapp testapp 817 8月 14 18:56 nodes-6382.conf
-rw-rw-r-- 1 testapp testapp 62340 8月 14 18:30 redis6380.conf
-rw-rw-r-- 1 testapp testapp 6 8月 14 18:55 redis_6380.pid
-rw-rw-r-- 1 testapp testapp 62344 8月 14 16:57 redis6381.conf
-rw-rw-r-- 1 testapp testapp 6 8月 14 18:55 redis_6381.pid
-rw-rw-r-- 1 testapp testapp 62344 8月 14 16:59 redis6382.conf
-rw-rw-r-- 1 testapp testapp 6 8月 14 18:55 redis_6382.pid
-rw-rw-r-- 1 testapp testapp 337 8月 14 19:30 startAll.sh
-rw-rw-r-- 1 testapp testapp 220 8月 14 19:31 stopAll.sh
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ cat startAll.sh
/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis6380.conf
/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis6381.conf
/home/ap/testapp/iot/redis/redis-5.0