在Ubuntu Server 16.04 LTS上启动多个Redis进程

我想在测试服务器上(UbuntuLTS16.04)部署多个Redis实例,以下是笔记
※在这里,我是基于默认的6379端口配置,将Redis实例部署在6380端口上。

创建redis-6380目录

sudo install -o redis -g root -m 755 -d /var/lib/redis-6380
sudo install -o redis -g root -m 755 -d /var/log/redis-6380
sudo install -o redis -g root -m 755 -d /var/run/redis-6380
sudo chown -R redis:redis /var/lib/redis-6380
sudo chown -R redis:redis /var/log/redis-6380
sudo chown -R redis:redis /var/run/redis-6380

创建conf文件

sudo cp -rf /etc/redis /etc/redis-6380
sudo sed -i 's/^pidfile .*$/pidfile \/var\/run\/redis-6380\/redis.pid/' /etc/redis-6380/redis.conf
sudo sed -i 's/^port .*$/port 6380/' /etc/redis-6380/redis.conf
sudo sed -i 's/^logfile .*$/logfile \/var\/log\/redis-6380\/redis.log/' /etc/redis-6380/redis.conf
sudo chown -R redis:redis /etc/redis-6380/redis.conf

创建启动文件

sudo cp -p /etc/init.d/redis-server /etc/init.d/redis-server-6380
sudo sed -i 's/^DAEMON=.*$/DAEMON=redis-server-6380/' /etc/init.d/redis-server-6380
sudo sed -i 's/^DAEMON_ARGS=.*$/DAEMON_ARGS=\/etc\/redis-6380\/redis.conf/' /etc/init.d/redis-server-6380
sudo sed -i 's/^NAME=.*$/NAME=redis-server-6380/' /etc/init.d/redis-server-6380
sudo sed -i 's/^DESC=.*$/DESC=redis-server-6380/' /etc/init.d/redis-server-6380
sudo sed -i 's/^RUNDIR=.*$/RUNDIR=\/var\/run\/redis-6380/' /etc/init.d/redis-server-6380
sudo sed -i 's/^PIDFILE=.*$/PIDFILE=\$RUNDIR\/redis-server-6380.pid/' /etc/init.d/redis-server-6380
sudo ln -s /usr/bin/redis-server /usr/bin/redis-server-6380

创建服务文件

sudo cp -p /lib/systemd/system/redis-server.service /lib/systemd/system/redis-server-6380.service
sudo vi /lib/systemd/system/redis-server-6380.service

只需要一种选项,将以下内容以中文母语方式改写:

虽然我本想用 sed 命令来完成,但是我放弃了。

[Service]
Type=forking
ExecStart=/usr/bin/redis-server-6380 /etc/redis-6380/redis.conf
PIDFile=/var/run/redis-6380/redis-server-6380.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis

ExecStartPre=-/bin/run-parts --verbose /etc/redis-6380/redis-server-6380.pre-up.d
ExecStartPost=-/bin/run-parts --verbose /etc/redis-6380/redis-server-6380.post-up.d
ExecStop=-/bin/run-parts --verbose /etc/redis-6380/redis-server-6380.pre-down.d
ExecStop=/bin/kill -s TERM $MAINPID
ExecStopPost=-/bin/run-parts --verbose /etc/redis-6380/redis-server-6380.post-down.d

PrivateTmp=yes
PrivateDevices=yes
ProtectHome=yes
ReadOnlyDirectories=/
ReadWriteDirectories=-/var/lib/redis-6380
ReadWriteDirectories=-/var/log/redis-6380
ReadWriteDirectories=-/var/run/redis-6380
CapabilityBoundingSet=~CAP_SYS_PTRACE

# redis-server writes its own config file when in cluster mode so we allow
# writing there (NB. ProtectSystem=true over ProtectSystem=full)
ProtectSystem=true
ReadWriteDirectories=-/etc/redis-6380

[Install]
WantedBy=multi-user.target
Alias=redis-6380.service

自动创建PID文件目录的设置。

$ sudo vim /etc/tmpfiles.d/redis-6380.conf

反映以下内容

d /var/run/redis-6380 0775 redis redis

由于某个版本的Linux内核,将/var/run文件夹被放置在tmpfs上的设置中,因此需要该设置。

启动服务

sudo service redis-server-6380 start

确认流程

ps aux | grep 6380

确认访问

redis-cli -p 6380

请参考网站

广告
将在 10 秒后关闭
bannerAds