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 安装并配置完成!"