Redis

Redisのインストール CentOS6.8

優 Redis
Redis インストール
Redis https://redis.io/

バックナンバー

  1. Redisのインストール CentOS6.8
  2. Redisの冗長化
  3. Redisクラスタ フェイルオーバ Redis Sentinel

ニコニコ動画やソーシャルゲーム等で大活躍のKVS型インメモリデータベース、Redisのインストールするチップスです。セッション管理やキャッシュに使ってみてはいかがでしょうか。

KVS

キーバリューストア(Kye-Value Store)の略で、膨大な情報を保存するのに適したデータ保存形式のひとつ。特に、複数のサーバーに分散して KVS方式でデータを保存すること、あるいはそうした情報管理システムを分散KVSという。

@see http://www.nttpc.co.jp/yougo/%E3%82%AD%E3%83%BC%E3%83%90%E3%83%AA%E3%83%A5%E3%83%BC%E3%82%B9%E3%83%88%E3%82%A2.html

KVSって何もの??

コード(主キー) なまえ しょくぎょう  レベル  とくぎ
1 ほらりん まもの 99 ほのお
2 ぶっくる まもの 32 噛み付き
3 びえーる 近衛兵 70 回復魔法

キーバリューはそのままのキーと値です。単純にキーと値の集まりで構成されたデータベース。

Redisのメリット

  • 分散化、冗長化しやすい。
  • KVSだけど集合やソートが扱える。
    頻繁に更新されるリストデータやリアルタイムランキング、カウンタなどに向いている。
  • インメモリなので高速
  • キャッシュ用途で活躍
  • セッションも標準で入れられる
  • 揮発性だが、永続化もできる
  • データの保存期限を設定できる。
  • sentinelと組み合わせると、Redisのフェイルオーバーは簡単

消えても困らない系データのキャッシュやセッション管理用途に使われることが多いです。そんなRedisをインストールするチップスです。

リポジトリのインストール

EPEL

# yum install epel-release

Remi

# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

基本モジュールインストール

ソースから入れる為の準備を行います。

# yum groupinstall "Development Tools"
# yum groupinstall "Base"
# yum install wget gcc gcc-c++ pcre-devel zlib-devel make openssl-devel libxml2 libxml2-devel libxslt-devel libxslt libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools-devel flex jemalloc
# yum install --enablerepo=epel gperftools

カーネルパラメータチューニング

vm.overcommit_memory, somaxconnをRedis用に設定します。

# vi /etc/sysctl.conf


※下記を追記

#Redis用設定
vm.overcommit_memory = 1
net.core.somaxconn = 1024

反映させましょう。

# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
vm.overcommit_memory = 1 ←確認
net.core.somaxconn = 1024 ←確認

Transparent Huge Pagesの無効化

# vi /etc/rc.local

# that this script will be executed during boot.

・
・
touch /var/lock/subsys/local


※以下を追記

#Redis用設定
echo never > /sys/kernel/mm/transparent_hugepage/enabled

実行権限付与

# chmod +x /etc/rc.d/rc.local

再起動

# reboot now

Redisのインストール

面倒だったらyumで安定版がストンと入ります。

# yum install redis

ソースからのインストールはこちら

この手のエッジの効いたミドルウェアは新バージョンで魅力的な機能が出てきたりすることが多く、そんな時にバージョン変更をさくっと行いたいのでソースから入れるのが私は好みです。

ソースダウンロードディレクトリに移動します。

# cd /usr/local/src

redisの公式(https://redis.io/download)からダウンロードしましょう。

# wget http://download.redis.io/releases/redis-3.2.8.tar.gz

解凍します。

# tar xvzf redis-*.tar.gz

作業ディレクトリに移動します。

# cd redis-*

コンパイルとインストールを行います。

redis-3.2.8]# make && make install


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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-3.2.8/src'

インストール完了

# mkdir /etc/redis

redisディレクトリの作成

# mkdir -p /var/redis/6379

ソースから基本設定ファイルを複製します。

# cp utils/redis_init_script /etc/init.d/redis_6379
# cp redis.conf /etc/redis/6379.conf

設定ファイルを編集します。

# vi /etc/redis/6379.conf


daemonize no

↓変更

#daemonize no
daemonize yes


pidfile /var/run/redis_6379.pid



logfile ""

↓変更

#logfile ""
logfile /var/log/redis/redis_6379.log



dir ./

↓変更

#dir ./
dir /var/redis/6379

ログディレクトリ作成

# mkdir /var/log/redis

起動スクリプトの作成

# vi /etc/init.d/redis_6379


#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

※2行を追加する

# chkconfig: - 58 74
# description: redis_6379 is the redis daemon

好みでrestart機能もつけています。

# cat /etc/init.d/redis_6379

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

start(){
    if [ -f $PIDFILE ]
    then
            echo "$PIDFILE exists, process is already running or crashed"
    else
            echo "Starting Redis server..."
            $EXEC $CONF
    fi
}
stop(){
    if [ ! -f $PIDFILE ]
    then
            echo "$PIDFILE does not exist, process is not running"
    else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
    fi
}

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

自動起動設定を行います。

# chkconfig --add redis_6379
# chkconfig redis_6379 on

起動させます。

# /etc/init.d/redis_6379 start

Starting Redis server...

プロセスを確認しましょう。

# netstat -an | grep 6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN
# redis-cli ping
PONG
バージョン確認
# redis-cli info | grep redis_version
redis_version:3.2.8

ログから動いているか確認します。

# tail -n 25 /var/log/redis/redis_6379.log

6037:M 13 Feb 19:08:55.807 * DB saved on disk
6037:M 13 Feb 19:08:55.807 * Removing the pid file.
6037:M 13 Feb 19:08:55.807 # Redis is now ready to exit, bye bye...
1483:M 13 Feb 19:14:22.924 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1483
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1483:M 13 Feb 19:14:22.927 # Server started, Redis version 3.2.8
1483:M 13 Feb 19:14:22.927 * DB loaded from disk: 0.001 seconds
1483:M 13 Feb 19:14:22.928 * The server is now ready to accept connections on port 6379

アスキーアートが可愛らしいですね。

お疲れ様です。

>> Redisの冗長化(https://sys-guard.com/post-12899/)

Redis
最新情報をチェックしよう!
>システム構築・保守に特化した会社です。

システム構築・保守に特化した会社です。

システムの構築・保守運用「システムガーディアン」 社内システム担当が欲しいが、専属で雇うほどの仕事量はない。 必要な時に必要なだけ頼りたいというお悩みを持つ企業様へ専門知識を持って対応を行っております。 サーバから各種システムまで自社・他社で構築されたシステムに対してサポートを行っております。

CTR IMG