非root用户下 redis sentinel 安装

本次部署采用redis sentinel哨兵集群模式在三台服务器上进行部署,部署策略都采用一主一哨兵的方式。所以步骤只以一个服务器的部署为例。切换到非root用户下(此处的用户为test,该用户需具备sudo可执行权限用于redis的安装)做相应的安装配置。,此处略过centos虚拟机安装以及test用户创建过程;

1.1. 在test用户目录下,创建redis目录及相关的文件

#mkdir /home/test/redis
#mkdir /home/test/run
#touch /home/test/run/redis.log
#touch /home/test/run/sentinel.log
#touch /home/test/run/redis_6379.pid

1.2. 开放test用户的读写权限

#su - root
#chmod u+w /etc/sudoers
#vi /etc/sudoers

找到root ALL=(ALL) ALL,在他下面添加xxx ALL=(ALL) ALL (这里的xxx是你的用户名)  

test  ALL=(ALL) ALL

保存退出;

撤销sudoers的写权限
#chmod u-w /etc/sudoers

1.3. 安装离线包(请自行下载redis在centos下的rpm包https://pkgs.org/download/redis):

下载redis的rpm包,并上传到linux服务器指定路径下,此处演示使用的路径为 /home/offline-rpm/redis/,线下安装请随意

redis 创建多账户 redis多用户权限_配置文件

通过yum 命令安装redis

#sudo yum install -y --nogpgcheck /home/offline-rpm/redis/redis40u-4.0.14-1.ius.centos7.x86_64.rpm

1.4.拷贝相关配置和脚本

#cp /usr/bin/redis-server /home/test/redis/
#cp /usr/bin/redis-cli /home/test/redis/
#cp /etc/redis.conf /home/test/redis/
#cp /etc/redis-sentinel.conf /home/test/redis/
#chmod 775 /home/test/redis/*
#chmod 775 /home/test/redis/run/*

1.5. 修改redis.conf和redis-sentinel.conf配置文件,修改主服务和哨兵配置文件,因为哨兵监测都是主服务器的主服务,所以哨兵的配置文件三个服务器(三台服务器ip分别为192.168.1.100、192.168.1.101、192.168.1.102)一样。每个服务器的主服务配置文件不一样。主服务配置文件修改主要内容如下:vi /etc/redis.conf

protected-mode no
port 6379
daemonize yes
bind 127.0.0.1 192.168.1.100(这个IP是指服务器本身的IP,所以每个服务器的redis.conf文件的这个地方不一样,或者可以将该bind属性注释小掉)
logfile "/home/test/redis/run/redis.log"
pidfile "/home/test/redis/run/redis_6379.pid"
dir "/home/test/redis"
masterauth “test”
slave-read-only no
requirepass “test”
appendonly no
slave-priority 100  (三台机器上的slave的优先级权重,可分别设置为100、90、80)

哨兵的配置文件如下,vi /etc/redis-sentinel.conf

protected-mode no
daemonize yes
port 26379
sentinel monitor mymaster 192.168.1.100 6379 2
sentinel down-after-milliseconds mymaster 5000
entinel failover-timeout mymaster 60000
sentinel auth-pass mymaster test
logfile "/home/test/redis/run/sentinel.log"

1.6. 开通防火墙,分别在三台机器上开通防火墙端口

#firewall-cmd --zone=public --add-port=6379/tcp --permanent
#firewall-cmd --zone=public --add-port=26379/tcp --permanent
#firewall-cmd --reload
#firewall-cmd --zone=public --query-port=6379/tcp
#firewall-cmd --zone=public --query-port=26379/tcp

1.7. 启动redis,分别在三台机器上的test用户下启动reidis

#cd /home/test/redis/ 
#redis-server redis.conf
#redis-server redis-sentinel.conf --sentinel

1.8. 分别在自己设想的两个备用服务器上,/home/test/redis/redis-cli -h 192.168.1.101 -p 6379 -a test登录本机redis服务。然后slaveof 192.168.1.100 6379,返回ok(此作用是把次机器设置为备用redis服务),输入info replication,可以看到自己的role是slave,并且可以看到主服务器的IP和信息

# /home/test/redis/redis-cli -h 192.168.1.101 -p 6379 -a test
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.1.101:6379>slaveof 192.168.1.100 6379
ok
192.168.1.101:6379> info replication
# Replication
role:slave
master_host:192.168.1.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:39885374
slave_priority:100
slave_read_only:0
connected_slaves:0
master_replid:c5c6f1fe5f8b11c3cf43b620077bb3aa00ee8109
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:39885374
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:38836799
repl_backlog_histlen:1048576
192.168.1.101:6379> get m
"m"
192.168.1.101:6379>

b、在redis主服务器上,/home/test/redis/redis-cli -h 192.168.1.100 -p 6379 -a test登录本机redis服务,输入info replication,可以看到自己的role是master,并且可以看到connected_slaves:2

# /usr/bin/redis-cli -h 192.168.1.100 -p 6379 -a test
Warning: Using a password with '-a' option on the command line interface may not be safe.
192.168.1.100:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.101,port=6379,state=online,offset=39920415,lag=1
slave1:ip=192.168.1.102,port=6379,state=online,offset=39919263,lag=1
master_replid:c5c6f1fe5f8b11c3cf43b620077bb3aa00ee8109
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:39920415
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:38871840
repl_backlog_histlen:1048576
192.168.1.100:6379> set m m
OK
192.168.1.100:6379> get m
"m"
192.168.1.100:6379>

继续输入set m m,是指在主服务设置一个键值。设置完成在备用redis服务输入get m返回获取变量m的值是m,说明redis集群部署完成.