redis服务部署:单点+集群(5.0以上版本)
前言:
此次安装以 redis-5.0.9 版本来讲解单点和集群的部署,因为 redis 所有版本单点部署的方式都一样,但是如果需要安装 redis 集群的话,redis-5.0 以上(包含)的版本中,可以直接使用 "redis-cli --cluster create" 指令构建 redis 集群。redis-5.0 以下的版本,则需要安装 ruby、rubygems 环境,通过使用 "redis-trib.rb create" 指令构建 redis 集群。
官方下载网址:https://download.redis.io/releases/
1、部署 redis 单点服务:
在官网下载 redis-5.0.9.tar.gz 安装包并上传到服务器 /usr/local/src/ 目录下:
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# ll total 1944 -rw-r--r--. 1 root root 1986574 Dec 22 22:26 redis-5.0.9.tar.gz [root@localhost src]#
解压安装包:
[root@localhost src]# tar -zxf redis-5.0.9.tar.gz
进入安装包目录,并执行安装命令:
[root@localhost src]# cd redis-5.0.9/ [root@localhost redis-5.0.9]# make PREFIX=/usr/local/redis install
复制 redis.conf 到安装目录下,并修改相关配置:
[root@localhost redis-5.0.9]# cp redis.conf /usr/local/redis/ [root@localhost redis-5.0.9]# vim /usr/local/redis/redis.conf 修改下面参数: bind 0.0.0.0 daemonize yes # 后台启动 masterauth text-2020@135 # 启动 redis 密码验证,主要是针对 master 对应的 slave 节点设置的,在 slave 节点数据同步的时候用到 requirepass text-2020@135 # 启动 redis 密码验证,一定要 requirepass 和 masterauth 同时设置。对登录权限做限制,redis每个节点的requirepass可以是独立、不同的,但建议和 masterauth 设置成一样
设置环境变量:
[root@localhost redis-5.0.9]# vim /etc/profile 在最底下添加一行: export PATH=$PATH:/usr/local/redis/bin [root@localhost redis-5.0.9]# source /etc/profile # 使配置立即生效
启动 redis 单点服务:
[root@localhost redis-5.0.9]# redis-server /usr/local/redis/redis.conf
查看 redis 启动进程和监听端口:
[root@localhost redis-5.0.9]# ps aux | grep redis # 查看 redis 启动进程 [root@localhost redis-5.0.9]# netstat -anltp | grep 6379 # 查看 redis 监听端口
2、部署 redis 集群服务:
搭建一个最简单的 redis 集群,至少需要6个节点:3个 master 和3个 slave ,因为资源有限,所以此次演示部署的 redis 集群都部署在同一台 Linux 虚拟机上。redis 安装步骤这里不再进行讲解,请参考上面的 "部署 redis 单点服务",下面说一下 redis 集群的配置:
[root@localhost redis-5.0.9]# cd /usr/local/redis/ [root@localhost redis]# mkdir redis-cluster && cd redis-cluster # 创建集群配置主目录 [root@localhost redis-cluster]# mkdir 638{1,2,3,4,5,6} # 创建分别存放6个节点各自的配置文件目录
创建 redis 集群配置文件,注意删掉注释:
[root@localhost redis-cluster]# cat << EOF > 6381/redis.conf protected-mode no bind 0.0.0.0 port 6381 # 服务监听端口 daemonize yes # 后台启动 dir /usr/local/redis/redis-cluster/6381/ pidfile /usr/local/redis/redis-cluster/6381/redis.pid masterauth test-cluster # 启动 redis 密码验证,主要是针对 master 对应的 slave 节点设置的,在 slave 节点数据同步的时候用到 requirepass test-cluster # 启动 redis 密码验证,一定要 requirepass 和 masterauth 同时设置。对登录权限做限制,redis每个节点的requirepass可以是独立、不同的,但建议和 masterauth 设置成一样 appendonly yes # 启用守护进程 cluster-enabled yes # 启用集群 cluster-config-file "nodes.conf" # 关联集群的配置文件 cluster-node-timeout 5000 # 集群超时时间 cluster-require-full-coverage no # 只要有结点宕机导致 16384 个槽没全被覆盖,整个集群就全部停止服务,所以一定要改为 no EOF [root@localhost redis-cluster]# cat << EOF > 6382/redis.conf protected-mode no bind 0.0.0.0 port 6382 daemonize yes dir /usr/local/redis/redis-cluster/6382/ pidfile /usr/local/redis/redis-cluster/6382/redis.pid masterauth test-cluster requirepass test-cluster appendonly yes cluster-enabled yes cluster-config-file "nodes.conf" cluster-node-timeout 5000 cluster-require-full-coverage no EOF [root@localhost redis-cluster]# cat << EOF > 6383/redis.conf protected-mode no bind 0.0.0.0 port 6383 daemonize yes dir /usr/local/redis/redis-cluster/6383/ pidfile /usr/local/redis/redis-cluster/6383/redis.pid masterauth test-cluster requirepass test-cluster appendonly yes cluster-enabled yes cluster-config-file "nodes.conf" cluster-node-timeout 5000 cluster-require-full-coverage no EOF [root@localhost redis-cluster]# cat << EOF > 6384/redis.conf protected-mode no bind 0.0.0.0 port 6384 daemonize yes dir /usr/local/redis/redis-cluster/6384/ pidfile /usr/local/redis/redis-cluster/6384/redis.pid masterauth test-cluster requirepass test-cluster appendonly yes cluster-enabled yes cluster-config-file "nodes.conf" cluster-node-timeout 5000 cluster-require-full-coverage no EOF [root@localhost redis-cluster]# cat << EOF > 6385/redis.conf protected-mode no bind 0.0.0.0 port 6385 daemonize yes dir /usr/local/redis/redis-cluster/6385/ pidfile /usr/local/redis/redis-cluster/6385/redis.pid masterauth test-cluster requirepass test-cluster appendonly yes cluster-enabled yes cluster-config-file "nodes.conf" cluster-node-timeout 5000 cluster-require-full-coverage no EOF [root@localhost redis-cluster]# cat << EOF > 6386/redis.conf protected-mode no bind 0.0.0.0 port 6386 daemonize yes dir /usr/local/redis/redis-cluster/6386/ pidfile /usr/local/redis/redis-cluster/6386/redis.pid masterauth test-cluster requirepass test-cluster appendonly yes cluster-enabled yes cluster-config-file "nodes.conf" cluster-node-timeout 5000 cluster-require-full-coverage no EOF
启动 redis 集群服务:
[root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6381/redis.conf [root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6382/redis.conf [root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6383/redis.conf [root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6384/redis.conf [root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6385/redis.conf [root@localhost ~]# redis-server /usr/local/redis/redis-cluster/6386/redis.conf
查看 redis 集群启动进程:
[root@localhost ~]# ps aux | grep redis [root@localhost ~]# netstat -anltp | grep redis
创建 redis 集群:
[root@localhost ~]# redis-cli -a "test-cluster" --cluster create 192.168.109.135:6381 192.168.109.135:6382 192.168.109.135:6383 192.168.109.135:6384 192.168.109.135:6385 192.168.109.135:6386 --cluster-replicas 1 参数说明: --cluster create # 创建集群 --cluster-replicas 1 # 表示每个主节点对应多少个从节点
集群验证:
[root@localhost ~]# redis-cli -h 192.168.109.135 -c -p 6381 -a "test-cluster" 192.168.109.135:6381> set hello world # 创建一个 key OK 192.168.109.135:6381> exit # 退出当前 redis 数据库 [root@localhost ~]# redis-cli -h 192.168.109.135 -c -p 6385 -a "test-cluster" 192.168.109.135:6385> get hello # 查看刚才创建的 key -> Redirected to slot [866] located at 192.168.109.135:6381 "world"
简单说一下原理:
redis cluster在设计的时候,就考虑到了去中心化,去中间件,也就是说,集群中的每个节点都是平等的关系,都是对等的,每个节点都保存各自的数据和整个集群的状态。每个节点都和其他所有节点连接,而且这些连接保持活跃,这样就保证了我们只需要连接集群中的任意一个节点,就可以获取到其他节点的数据。
redis 集群并没有使用传统的一致性哈希来分配数据,而是采用另外一种叫做哈希槽 (hash slot)的方式来分配的。redis cluster 默认分配了 16384 个slot,当我们set一个key 时,会用CRC16算法来取模得到所属的slot,然后将这个key 分到哈希槽区间的节点上,具体算法就是:CRC16(key) % 16384。所以我们在测试的时候看到set 和 get 的时候,直接跳转到了6379端口的节点。
redis 集群会把数据存在一个 master 节点,然后在这个 master 和其对应的salve 之间进行数据同步。当读取数据时,也根据一致性哈希算法到对应的 master 节点获取数据。只有当一个master 挂掉之后,才会启动一个对应的 salve 节点,充当 master 。
需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败,并且当存活的主节点数小于总节点数的一半时,整个集群就无法提供服务了。