一、单机安装
1.源码安装(方式一)
1.1 下载并上传tar包
将redis-6.2.6.tar.gz 上传到/home/data/install_pkg/redis下
1.2 解压缩
cd /home/data/install_pkg/redis //切到指定目录
tar -xvf redis-6.2.6.tar.gz //解压缩
1.3 编译安装
cd redis-6.2.6/
make && make PREFIX=/usr/local/redis6 install #安装目录
1.4 配置redis.conf
mkdir /usr/local/redis6/conf
cp redis.conf /usr/local/redis6/conf/
mkdir -p /home/data/redis6/data /home/data/redis6/logs #数据目录和日志目录
vi /usr/local/redis6/conf/redis.conf
#配置参数,其他默认
bind 0.0.0.0
protected-mode no
port 6379
daemonize yes
logfile "/home/data/redis6/logs/redis.log"
dir /home/data/redis6/data/
maxmemory 2048MB
io-threads 4
requirepass redis@4.105
1.5 配置systemd
vi /lib/systemd/system/redis6.service
[Unit]
Description=Redis
After=network.target[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis6/bin/redis-server /usr/local/redis6/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
1.6 服务启停及开机启动
systemctl daemon-reload && systemctl start redis6 && systemctl enable redis6
1.7 登录验证
ln -s /usr/local/redis6/bin/redis-cli /usr/bin/redis-cli //创建软链接
redis-cli -p 6379 -a redis@4.105
2.Docker离线安装(方式二)
ps:确保服务器上(/home/data/install_pkg/redis目录下)已上传如下内容:
2.1 redis.conf配置文件
port 6379
pidfile /home/data/redis/6379data/redis.pid
bind 0.0.0.0
requirepass redis@4.105
masterauth redis@4.105
cluster-enabled no
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
appendfsync everysec
daemonize no
protected-mode no
io-threads-do-reads yes
io-threads 1
cluster-require-full-coverage no
dir /data/data
save ""
stop-writes-on-bgsave-error yes
maxmemory 1000M
maxclients 10000
maxmemory-policy allkeys-lru
logfile /data/redis.log
cluster-node-timeout 15000
no-appendfsync-on-rewrite yes
tcp-backlog 65535
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
slave-lazy-flush yes
activedefrag yes
active-defrag-cycle-min 5
active-defrag-cycle-max 10
active-defrag-threshold-lower 50
active-defrag-threshold-upper 100
active-defrag-ignore-bytes 104857600
maxmemory-policy volatile-ttl
maxmemory 1000000000
2.2 安装脚本(install_redis.sh)
#!/bin/bash
##########################
# redis6-docker 安装脚本 #
##########################
WORK_PATH=/home/data/install_pkg
PKG_PATH=$WORK_PATH/redis#检查安装目录
if [ ! -d $PKG_PATH ];
then
echo -e "${PKG_PATH} 缺失,结束进程"
exit 1
fi#docker 导入tar包
docker load < $PKG_PATH/redis6.tar#新建redis目录 注册配置文件
mkdir -p /home/data/redis/6379/data
cd /home/data/redis/6379
cp -rf $PKG_PATH/redis.conf ./#docker启动redis
docker run -d -v /home/data/redis/6379:/data --privileged=true --restart=always --net host --name redis-6379 redis:6.2.1 redis-server /data/redis.confecho redis-server installed.
docker ps | grep redis
执行redis安装脚本:sh install_redis.sh
2.3 启动脚本(op_redis.sh)
#!/bin/sh
# ./op_redis.sh start 启动 stop 停止 restart 重启 status 状态 monitor 监控(未启动->自动重启)
# JVM参数
AppName=redis
opUser=root
RE_LOG_HOME=/home/data/monitorlog/crontab
RE_LOG_FILE=$RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
STATE_HOME=/home/data/monitorlog/component-status
STATE_FILE=$STATE_HOME/$AppNameif [ `whoami` != "$opUser" ];then
echo -e " only $opUser can run me"
exit 1
fiif [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status|monitor} \033[0m"
exit 1
fiif [ "$AppName" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fiif [ ! -d "$RE_LOG_HOME/$(date +%F)/" ];
then
mkdir $RE_LOG_HOME/$(date +%F)
fifunction start()
{
PID=`ps -ef|grep redis-server |grep -v 'grep' | awk '{print $2}'`if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
docker start redis-6379
echo "Start $AppName success..."
fi
}function stop()
{
echo "Stop $AppName"PID=""
query(){
PID=`ps -ef|grep redis-server |grep -v 'grep' | awk '{print $2}'`
}query
if [ x"$PID" != x"" ]; then
docker stop redis-6379
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]
do
sleep 1
query
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}function restart()
{
stop
sleep 2
start
}function status()
{
PID=`ps -ef|grep redis-server |grep -v 'grep' | awk '{print $2}' |wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
else
echo "$AppName is not running..."
fi
}function monitor()
{
PID=`ps -ef|grep redis-server |grep -v 'grep' | awk '{print $2}' |wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
echo $(date +%Y-%m-%d%n%H:%M:%S)_running > $STATE_FILE
else
start
echo "$AppName is not running,and restarting..."
# 重启保存到日志
echo $(date +%Y-%m-%d%n%H:%M:%S)_notrunning > $STATE_FILE
echo $(date +%Y-%m-%d%n%H:%M:%S) >> $RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
echo "$AppName is not running,and restarting..." >> $RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
fi
}case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
monitor)
monitor;;
*)
echo "操作名"$1"错误";;
esac
二、集群部署
1. 集群配置脚本
vi redis-config.sh
for port in $(seq 6379 6384); \
do \
rm -rf /home/data/redis/node-${port}mkdir /home/data/redis/node-${port}
touch /home/data/redis/node-${port}/redis.conf
cat << EOF > /home/data/redis/node-${port}/redis.confport ${port}
requirepass redis@198.62
masterauth redis@198.62appendonly yes
appendfsync everysec
daemonize no
protected-mode nocluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 宿主机ip
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
io-threads-do-reads yes
io-threads 1
cluster-require-full-coverage no
#dir /home/data/redis/
save ""
stop-writes-on-bgsave-error yes
maxmemory 1000M
maxclients 10000
maxmemory-policy allkeys-lru
#logfile /home/data/redis/redis.log
no-appendfsync-on-rewrite yes
tcp-backlog 65535
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
slave-lazy-flush yes
activedefrag yes
active-defrag-cycle-min 5
active-defrag-cycle-max 10
active-defrag-threshold-lower 50
active-defrag-threshold-upper 100
active-defrag-ignore-bytes 104857600
#maxmemory-policy volatile-ttl
#maxmemory 10000000000
#notify-keyspace-events Egx
EOF
done;
2. 集群启动脚本
vi redis-start.sh
docker stop redis-6379 redis-6380 redis-6381 redis-6382 redis-6383 redis-6384
docker rm redis-6379 redis-6380 redis-6381 redis-6382 redis-6383 redis-6384for port in $(seq 6379 6384);\
do \
docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /home/data/redis/node-${port}/redis.conf:/redis/data/redis.conf \
--privileged=true -v /home/data/redis/node-${port}/:/redis/data/ \
--restart always \
--name redis-${port} \
redis:6.2.1 redis-server /redis/data/redis.conf
done
3.创建redis集群
可随意选择一个节点进入,创建Redis集群。
3.1进入容器
docker exec -it redis-6379 /bin/bash
3.2 创建集群
hostip=配置文件中的IP地址
redis-cli -a redis密码 --cluster create $hostip:6379 $hostip:6380 $hostip:6381 $hostip:6382 $hostip:6383 $hostip:6384 --cluster-replicas 1
4.查看节点信息
进入容器后,通过redis-cli --raw -c -a redis密码
,进入redis
cluster info
cluster nodes
5.其他
排查redis连接数是否超限: netstat -an |grep 6379 |grep ESTABLISHED |wc -l
批量停止容器:
for port in $(seq 6379 6384);\
do \
docker stop redis-${port}
done
批量删除容器:
for port in $(seq 6379 6384);\
do \
docker rm redis-${port}
done