UbuntuServer16.04LTSでRedisプロセスを複数立てる

テストサーバ(UbuntuLTS16.04)上に複数のRedisを立てたかったのでメモ
※ここでは標準の6379ポートの設定をもとに、6380ポートで立てている

# redis-6380用ディレクトリの作成
```bash
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ファイル作成
```bash
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
```

# 起動ファイル作成 
```bash
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
```

# サービスファイル作成
```bash
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でやりたかったけど力尽きた

```shell-session
[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ファイルのディレクトリ自動作成設定 
```bash
$ sudo vim /etc/tmpfiles.d/redis-6380.conf
```
以下を反映

```shell-session
d /var/run/redis-6380 0775 redis redis
```
※Linuxカーネルのあるバージョンから/var/runがtmpfs上に置かれる設定になったので必要
https://blog.cles.jp/item/9051

# サービス起動
```bash
sudo service redis-server-6380 start
```

# プロセス確認
```bash
ps aux | grep 6380
```

# アクセス確認
```bash
redis-cli -p 6380
```


# 参考サイト
http://infra.logdown.com/posts/175191-centos6-multiple-redis