Redis单点部署及exporter的安装

Redis单点一键部署脚本

maxmemory需要按需修改

shell 复制代码
#!/bin/bash

# 安装依赖
apt update
apt install -y gcc make pkg-config tcl

# 创建安装目录
mkdir -p /data/redis
cd /data/redis

# 修改内核参数允许redis进行内存申请
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p

# 下载并编译Redis
wget https://download.redis.io/releases/redis-7.2.4.tar.gz
tar xzf redis-7.2.4.tar.gz
cd redis-7.2.4
make

# 安装Redis
make install

# 创建必要的目录
mkdir -p /data/redis/conf
mkdir -p /data/redis/log
mkdir -p /data/redis/data

# 创建redis配置文件
cat > /data/redis/conf/redis.conf << 'EOF'
# 基本配置
bind 0.0.0.0
port 6379
daemonize yes
pidfile /data/redis/redis.pid
dir /data/redis/data
logfile /data/redis/log/redis.log
# 安全配置
protected-mode yes
maxmemory-policy volatile-lru
maxmemory 5120M
requirepass xxxxxxx
# RDB配置
save 60 1
save 300 10
save 60 10000
dbfilename dump.rdb
rdbcompression yes
rdb-save-incremental-fsync yes
# 禁用AOF
appendonly no
EOF

# 创建systemd服务文件
cat > /etc/systemd/system/redis.service << 'EOF'
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
User=root
PIDFile=/data/redis/redis.pid
ExecStart=/usr/local/bin/redis-server /data/redis/conf/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 重新加载systemd并启动Redis
systemctl daemon-reload
systemctl enable redis
systemctl start redis

# 检查Redis状态
systemctl status redis

redis exporter一键部署脚本

shell 复制代码
#!/bin/bash

# 变量配置
REDIS_CONF="/data/redis/conf/redis.conf"  # Redis配置文件路径
EXPORTER_VERSION="v1.34.0"               # Redis Exporter版本
EXPORTER_DIR="/data/redis/redis_exporter" # Redis Exporter安装路径
REDIS_EXPORTER_CMD="/usr/local/bin/redis_exporter"  # Redis Exporter命令路径
REDIS_EXPORTER_SERVICE="/etc/systemd/system/redis_exporter.service"  # systemd服务文件路径

# 下载并安装Redis Exporter
echo "下载 Redis Exporter ${EXPORTER_VERSION}..."
cd /tmp
wget https://github.com/oliver006/redis_exporter/releases/download/${EXPORTER_VERSION}/redis_exporter-${EXPORTER_VERSION}.linux-amd64.tar.gz
tar xvf redis_exporter-${EXPORTER_VERSION}.linux-amd64.tar.gz
mv redis_exporter-${EXPORTER_VERSION}.linux-amd64/redis_exporter $REDIS_EXPORTER_CMD
chmod +x $REDIS_EXPORTER_CMD

# 创建服务启动脚本
echo "创建 Redis Exporter 启动脚本..."
cat << 'EOF' > /data/redis/redis_exporter.sh
#!/bin/bash

# 设置Redis配置文件路径
REDIS_CONF="/data/redis/conf/redis.conf"

# 从redis.conf中提取requirepass参数(去除注释和空行)
REDIS_PASSWORD=$(grep -E '^requirepass' $REDIS_CONF | awk '{print $2}')

# 设置Redis Exporter的监听地址
REDIS_EXPORTER_PORT="9121"

# 设置Redis Exporter的地址和密码
REDIS_EXPORTER_CMD="/usr/local/bin/redis_exporter"

# 获取IP地址
IP=$(hostname -I|awk '{print $1}')

# 启动redis_exporter,如果有密码则加上密码参数
if [ -z "$REDIS_PASSWORD" ]; then
  # 如果没有密码
  $REDIS_EXPORTER_CMD --redis.addr="tcp://$IP:6379" --web.listen-address=":$REDIS_EXPORTER_PORT"
else
  # 如果有密码
  $REDIS_EXPORTER_CMD --redis.addr="tcp://$IP:6379" --redis.password="$REDIS_PASSWORD" --web.listen-address=":$REDIS_EXPORTER_PORT"
fi
EOF

chmod +x /data/redis/redis_exporter.sh

# 创建 Redis Exporter systemd 服务文件
echo "创建 systemd 服务文件..."
cat << EOF > $REDIS_EXPORTER_SERVICE
[Unit]
Description=Redis Exporter for Prometheus
After=network.target

[Service]
Type=simple
User=root
ExecStart=/data/redis/redis_exporter.sh
Restart=always
WorkingDirectory=/data/redis
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target
EOF

# 重新加载 systemd 配置
echo "重新加载 systemd 配置..."
systemctl daemon-reload

# 启动 Redis Exporter 服务
echo "启动 Redis Exporter 服务..."
systemctl start redis_exporter

# 设置开机自启
echo "设置 Redis Exporter 开机自启..."
systemctl enable redis_exporter

# 验证 Redis Exporter 是否正常运行
echo "检查 Redis Exporter 服务状态..."
systemctl status redis_exporter

echo "Redis Exporter 安装并配置完成!"
相关推荐
IT大白鼠3 天前
Redis 系列 · 第 01 篇——认知入门:Redis 是什么
redis·nosql
彧azz3 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
lbb 小魔仙3 天前
OpenClaw + cpolar 实战:远程 NAS、分享小游戏、RDP,再配置公网 AI 入口
数据库·人工智能·redis·oracle·prometheus
夕除3 天前
redis--018
redis
两锭子3 天前
Redis基础
redis
烟沙九洲3 天前
Redis 缓存穿透、缓存击穿、缓存雪崩
数据库·redis
字节探索3 天前
Redis 只会当缓存用?这 10 大实战场景,让你的系统快到飞起
redis·后端
于樱花森上飞舞3 天前
【Redis】哨兵详解
java·开发语言·数据库·redis
Rain的Java大神之路3 天前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
Wang's Blog3 天前
Java 接入Redis: 通用命令与键管理
java·服务器·redis