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 安装并配置完成!"
相关推荐
柯34938 分钟前
Redis的过期删除策略和内存淘汰策略
数据库·redis·lfu·lru
bing_1582 小时前
Redis 的单线程模型对微服务意味着什么?需要注意哪些潜在瓶颈?
数据库·redis·微服务
小黑蛋学java2 小时前
Redis-cli常用参数及功能的详细说明
redis
听闻风很好吃3 小时前
Redis高级数据类型解析(二)——Set、Sorted Set与Geo实战指南
数据库·redis·缓存
YGGP3 小时前
【每日八股】复习 Redis Day2:Redis 的持久化(下)
redis
知初~3 小时前
java—11 Redis
redis
云攀登者-望正茂4 小时前
Redis 及其在系统设计中的作用
数据库·redis·缓存
冼紫菜5 小时前
基于Redis实现高并发抢券系统的数据同步方案详解
java·数据库·redis·后端·mysql·缓存·性能优化
搬砖天才、7 小时前
日常记录-redis主从复制(master-slave)+ Sentinel 哨兵(高可用)
数据库·redis·sentinel
ShAn DiAn7 小时前
实时步数统计系统 kafka + spark +redis
大数据·redis·分布式·spark·kafka