サーバ構築 ミドルウェア

Redisをソースからインストール

更新日:

必要なパッケージをインストール

jemallocがepelにあるので先にepelリポジトリインストールする

# yum install epel-release

コンパイル等々に必要なものインストール

# yum install gcc jemalloc-devel tcl

Downloadしてコンパイルする

# curl -o /usr/local/src/redis-stable.tar.gz http://download.redis.io/redis-stable.tar.gz
# cd /usr/local/src
# tar xzf redis-stable.tar.gz
# cd redis-stable
# make
ry)
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/src/redis-stable/src'
  • 下記のようなエラーでmake失敗する場合は make distclean して再度 make する
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory
cc: error: ../deps/lua/src/liblua.a: No such file or directory
cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/root/redis-stable/src'
make: *** [all] Error 2
# make distclean
# make

make test して installする

make testして動作問題ないか確認

# make test
ry)
101 seconds - unit/hyperloglog
213 seconds - integration/replication
126 seconds - unit/geo
169 seconds - unit/obuf-limits

\o/ All tests passed without errors!

Cleanup: may take some time… OK
make[1]: Leaving directory `/usr/local/src/redis-stable/src'

make installで実行ファイル配置

# make install
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-stable/src'

Hint: It's a good idea to run 'make test' ;)
<pre><code>INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install</code></pre>
make[1]: Leaving directory `/usr/local/src/redis-stable/src'

パスが通る事を確認しとく

# which redis-server
/usr/local/bin/redis-server
# which redis-cli
/usr/local/bin/redis-cli

version確認

# redis-server --version
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=7a82f14a6151b75e

redisユーザ作成

# useradd redis -c "Redis Database Server" -d "/var/lib/redis" -s /sbin/nologin -M

必要なディレクトリ作成

# mkdir /etc/redis
# mkdir /var/log/redis
# mkdir /var/lib/redis
# chown redis:redis /etc/redis
# chown redis:redis /var/log/redis
# chown redis:redis /var/lib/redis

サンプルの設定ファイルコピー

# sed -e 's/^logfile .*$/logfile \"\/var\/log\/redis\/redis.log\"/' -e 's/^dir .*$/dir \/var\/lib\/redis\//' ./redis.conf > /etc/redis/redis.conf
# chown redis:redis /etc/redis/redis.conf

自動起動用の設定

systemd用の設定ファイル作成

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf --daemonize no
ExecStop=/usr/local/bin/redis-shutdown
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

停止用のコマンド作成

#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x &amp;&amp; set -x

REDIS_CLI=/usr/local/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
  SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/etc/redis/$SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file
HOST=<code>awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1</code>
PORT=<code>awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1</code>
PASS=<code>awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1</code>
SOCK=<code>awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1</code>

# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
  PORT=${PORT:-6379}
else
  PORT=${PORT:-26739}
fi

# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly
if [ -e "$SOCK" ] ; then
  $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
  $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS 
  shutdown
fi
# chmod 755 /usr/local/bin/redis-shutdown

systemd設定再読み込み

# systemctl daemon-reload

起動してみる

# systemctl start redis
# ps -ef | grep redis
# redis-cli info

停止してみる

# systemctl stop redis
# ps -ef | grep redis

デフォルトだとWarningでるので修正する

ログ見るとWarningでてるので対応していく

# cat /var/log/redis/redis.log 
11146:C 02 Apr 15:53:25.643 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11146:C 02 Apr 15:53:25.643 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=11146, just started
11146:C 02 Apr 15:53:25.643 # Configuration loaded
11146:M 02 Apr 15:53:25.644 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
11146:M 02 Apr 15:53:25.644 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
11146:M 02 Apr 15:53:25.644 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 11146
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

11146:M 02 Apr 15:53:25.647 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11146:M 02 Apr 15:53:25.647 # Server initialized
11146:M 02 Apr 15:53:25.647 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
11146:M 02 Apr 15:53:25.647 * Ready to accept connections
11146:M 02 Apr 15:53:35.713 # User requested shutdown...
11146:M 02 Apr 15:53:35.713 * Saving the final RDB snapshot before exiting.
11146:M 02 Apr 15:53:35.715 * DB saved on disk
11146:M 02 Apr 15:53:35.715 * Removing the pid file.
11146:M 02 Apr 15:53:35.715 # Redis is now ready to exit, bye bye...

ulimit修正

redis.confでmaxclientsを10000にしてるので少なくとも10032個ファイルオープンできるようにしておく必要がある。
だけど、10032のファイルを開くことができない。4096のファイルしか開けるようになってないから。
要はulimitの max open files の設定変更をしろとのこと。

11146:M 02 Apr 15:53:25.644 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
11146:M 02 Apr 15:53:25.644 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
11146:M 02 Apr 15:53:25.644 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.

現状、Redisプロセスの設定確認

# cat /proc/$(pidof redis-server)/limits | grep 'Max open files'
Max open files            4096                 4096                 files   

上限をあげる、systemdの設定ファイルの[service]セクションに LimitNOFILE を追記

LimitNOFILE=65536

再起動して確認

# systemctl daemon-reload
# systemctl restart redis
# cat /proc/$(pidof redis-server)/limits | grep 'Max open files'
Max open files            65536                65536                files

TCP backlog修正

redis.confでtcp-backlogの値を511にしてるけど、somaxconnの設定はそれより低い128になってる。
somaxconnの方が低いのでそっちに合わせられるらしい。

11393:M 02 Apr 16:17:44.186 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

現状を確認

# cat /proc/sys/net/core/somaxconn
128

設定値をあげる(tcp接続の待受キューの最大値を引き上げる)

net.core.somaxconn = 12800

sysctlの設定反映

# sysctl --system</h1>
# cat /proc/sys/net/core/somaxconn</h1>
12800

overcommit修正

overcommit_memoryに0が設定されてる。
Background save(bgsave?)が低メモリの場合失敗するかも。
overcommit_memoryを1にして再起動しろって。

11590:M 02 Apr 16:35:03.199 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

現状のovercommit設定

# cat /proc/sys/vm/overcommit_memory
0

変更する

vm.overcommit_memory = 1

sysctlの設定反映

# sysctl --system
# cat /proc/sys/vm/overcommit_memory
1

vagrant/ansible化してみた

github

-サーバ構築, ミドルウェア
-

Copyright© 明日から頑張ります。 , 2025 All Rights Reserved Powered by STINGER.