#!/bin/sh
#install redis
. /etc/init.d/functions
#安装依赖包
yum -y -q install gcc jemalloc-devel make wget || { action "安装依赖包失败,请检查环境" false ;exit; }
#下载源码包并解压
wget http://download.redis.io/releases/redis-5.0.9.tar.gz || { action "源码包下载失败" false ;exit; }
tar xvf redis-5.0.9.tar.gz
#创建安装目录及用户
mkdir /apps
id redis >& /dev/null || { useradd -r -s /sbin/nologin redis; action "redis用户创建成功"; }
#编译安装
cd redis-5.0.9/
make PREFIX=/apps/redis install >& /dev/null && action "redis编译安装完成"
#配置环境变量
echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
source /etc/profile.d/redis.sh
ln -sv /apps/redis/bin/redis-* /usr/bin/
#准备相关目录和配置文件
mkdir -pv /apps/redis/{etc,log,data,run}
cp /root/redis-5.0.9/redis.conf /apps/redis/etc/
#三个告警处理
cat > /etc/sysctl.conf <<EOF
vm.overcommit_memory = 1
net.core.somaxconn = 1024
EOF
sysctl -p
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
source /etc/rc.d/rc.local
#服务启动文件
cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now redis &> /dev/null && action "redis服务启动成功" || { action "redis启动失败" false ;exit; }