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 安装并配置完成!"
相关推荐
库库林_沙琪马35 分钟前
Redis 持久化:从零到掌握
数据库·redis·缓存
去看日出7 小时前
CentOS 7 企业级Redis 7部署指南
linux·redis·centos
-$_$-9 小时前
【黑马点评优化】2-Canel实现多级缓存(Redis+Caffeine)同步
数据库·redis·缓存
天天向上vir9 小时前
缓存三大问题及其解决方案
java·redis·mysql
啥也不会的菜鸟·10 小时前
Redis7——基础篇(五)
redis·学习·缓存
LUCIAZZZ11 小时前
从Redis实现分布式锁的问题延伸到Redisson的使用入门
java·数据库·spring boot·redis·分布式·spring cloud
Foolforuuu12 小时前
redis 如何保证缓存和数据库一致性?解决策略如下
数据库·redis·缓存
别致的影分身18 小时前
Redis 客户端C++使用
数据库·redis·缓存
不良人天码星1 天前
Redis的简单使用
java·spring boot·redis·mybatis
qw9491 天前
Redis(高阶篇)02章——BigKey
数据库·redis·缓存