CentOS 7.4でのRedisおよびクラスタのインストールおよび構成


(一)Redis概要Redisは、オープンソース、サポートネットワーク、メモリベース、キー値ペアのKey-Valueデータベースであり、ANSI Cを使用して作成され、多くの言語のAPIを提供しています.これはほとんど難しいことではありません.数分でインストール作業を完了し、アプリケーションとスムーズに連携することができます.言い換えれば、わずかな時間と労力を費やすだけで、すぐに効果的で効果的なパフォーマンスが向上します.つまり、非常に簡単なキャッシュソリューションです.格納するValueタイプは文字列に限らず、主従同期、データ永続化などをサポートする.Redisはオープンソースのkey-valueストレージシステムであり、優れたパフォーマンスのため、ほとんどのインターネット企業がサーバ側キャッシュに使用されています.Redisは3.0バージョンまでは単一インスタンスモデルのみをサポートしており、主従モデル、哨兵モデルの配置をサポートして単一障害を解決していたが、現在、インターネット企業の数百Gのデータは、業務のニーズを満たすことができないため、3.0バージョン以降にクラスタモデルを発売した.Redis 3.0クラスタはP 2 Pのモードを採用し,完全に中心化されていない.RedisはすべてのKeyを16384個のslotに分割し,各Redisインスタンスはslotの一部を担当した.クラスタ内のすべての情報(ノード、ポート、slotなど)は、ノード間の定期的なデータ交換によって更新されます.Redisクライアントは、任意のRedisインスタンスで要求を発行し、必要なデータがインスタンスにない場合は、リダイレクトコマンドによってクライアントが必要なインスタンスにアクセスするように起動します.
総じて言えば、複数のインスタンスがクラスタを構成し、データが異なるインスタンスに分散し、どのインスタンスに行ってもデータを読むことができます.また、高可用性を保証するために、スレーブノードも構成します.
(二)設置及び配置
[root@ELK-Redis2 ~]#yum install -y gcc c++ jemalloc
[root@ELK-Redis2 ~]#wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@ELK-Redis2 ~]#tar xf redis-4.0.10.tar.gz
[root@ELK-Redis2 ~]#cd redis-4.0.10
[root@ELK-Redis2 ~]#make && make install
[root@ELK-Redis2 ~]#cp utils/redis_init_script /etc/init.d/redis
[root@ELK-Redis2 ~]#chmod +x /etc/init.d/redis
[root@ELK-Redis2 ~]#mkdir -p /etc/redis/ && mkdir -p /var/log/redis/ && mkdir -p /data/redis_db
[root@ELK-Redis2 ~]#cp redis.conf /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/daemonize no/s/no/yes/' /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/^pidfile/s/redis.pid/redis_6379.pid/' /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/^logfile/s/""/"\/var\/log\/redis\/redis.log"/' /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/^dir/s/.\//\/data\/redis_db/' /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/^slave-read-only/s/yes/no/' /etc/redis/6379.conf
[root@ELK-Redis2 ~]#sed -i '/^# maxclients/s/# //;/^maxclients/s/10000/100000/' /etc/redis/6379.conf
          “
18180:M 20 Sep 18:10:35.061 * 10 changes in 300 seconds. Saving...
18180:M 20 Sep 18:10:35.061 # Can't save in background: fork: Cannot al”
        :
1、vim /etc/sysctl.conf
2、   vm.overcommit_memory=1
3、sysctl -p
vm.overcommit_memory            ?
Linux              "yes",           。       ,         ,                     ,        ,      Overcommit。     ,                    ,       ,          ,         ,           ,        ,        , linux       ,   OOM killer(OOM=out-of-memory)。          (     ,      ,        ,                ),      。

(3)redisクラスタの構築Redisサポートクラスタの最小単位は6つのインスタンス、3つのプライマリノード、3つのスレーブノードクラスタ構築:少なくとも3つのmasterの2つのノード:master:172.0.144.1105(172.0.144.1105:6377172.0.144.1105:6378172.2.0.144.1105:6379)slave:172.0.144.1106(172.0.144.1106:6377,172.0.144.166:6378,172.0.144.1106:6379)
(1)関連するredisのインスタンスを作成する関連構成1.1、redisインストールおよび関連構成
[root@ELK-Redis1 ~]#tar xf redis-4.0.10.tar.gz
[root@ELK-Redis1 ~]#cd redis-4.0.10
[root@ELK-Redis1 ~]#make && make install
[root@ELK-Redis1 ~]#cp utils/redis_init_script /etc/init.d/redis
[root@ELK-Redis1 ~]#chmod +x /etc/init.d/redis
[root@ELK-Redis1 ~]#mkdir -p /etc/redis/ && mkdir -p /var/log/redis/ && mkdir -p /data/redis_db
[root@ELK-Redis1 redis-4.0.10]#mkdir /etc/redis/{6377,6378,6379}
[root@ELK-Redis1 redis-4.0.10]# cp redis.conf /etc/redis/6377/redis.conf
[root@ELK-Redis1 redis-4.0.10]# cp redis.conf /etc/redis/6378/redis.conf
[root@ELK-Redis1 redis-4.0.10]# cp redis.conf /etc/redis/6379/redis.conf
[root@ELK-Redis1 redis-4.0.10]# sed -i '/^dir/s/.\//\/data\/redis_db/' /etc/redis/6377/redis.conf 
[root@ELK-Redis1 redis-4.0.10]# sed -i '/^dir/s/.\//\/data\/redis_db/' /etc/redis/6378/redis.conf 
[root@ELK-Redis1 redis-4.0.10]# sed -i '/^dir/s/.\//\/data\/redis_db/' /etc/redis/6379/redis.conf 

1.2、順次修正:172.0.144.1105:6377、172.0.144.1105:6378、172.0.144.1105:6379172.2.0.144.1106:6377、172.0.144.166:6378、172.0.144.166:6379の関連配置書類
[root@ELK-Redis1 redis-4.0.10]# grep '^[a-Z]' /etc/redis/6377/redis.conf 
bind 172.20.144.105
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis_db
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
         
port  7000                                        //  7000,7001,7002        
bind   ip                                       //  ip 127.0.0.1               ip                 ,      
daemonize    yes                               //redis    
pidfile  /var/run/redis_7000.pid          //pidfile    7000,7001,7002
cluster-enabled  yes                           //         #  
cluster-config-file  nodes_7000.conf   //                    7000,7001,7002
cluster-node-timeout  15000                //        15 ,     
appendonly  yes                           //aof            ,              

1.3、メインプロファイルを順次起動する
[root@ELK-Redis1 redis-4.0.10]# redis-server /etc/redis/6377/redis.conf 
1329:C 18 Sep 20:33:59.546 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1329:C 18 Sep 20:33:59.546 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1329, just started
1329:C 18 Sep 20:33:59.546 # Configuration loaded
[root@ELK-Redis1 redis-4.0.10]# redis-server /etc/redis/6378/redis.conf 
1340:C 18 Sep 20:34:06.981 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1340:C 18 Sep 20:34:06.981 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1340, just started
1340:C 18 Sep 20:34:06.981 # Configuration loaded
[root@ELK-Redis1 redis-4.0.10]# redis-server /etc/redis/6379/redis.conf 
1348:C 18 Sep 20:34:10.703 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1348:C 18 Sep 20:34:10.703 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1348, just started
1348:C 18 Sep 20:34:10.703 # Configuration loaded
[root@ELK-Redis1 redis-4.0.10]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.20.144.105:6379     0.0.0.0:*               LISTEN      1349/redis-server 1 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1189/sshd           
tcp        0      0 127.0.0.1:16377         0.0.0.0:*               LISTEN      1330/redis-server 1 
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1455/master         
tcp        0      0 172.20.144.105:16378    0.0.0.0:*               LISTEN      1341/redis-server 1 
tcp        0      0 172.20.144.105:16379    0.0.0.0:*               LISTEN      1349/redis-server 1 
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1249/zabbix_agentd  
tcp        0      0 127.0.0.1:6377          0.0.0.0:*               LISTEN      1330/redis-server 1 
tcp        0      0 172.20.144.105:6378     0.0.0.0:*               LISTEN      1341/redis-server 1 
tcp6       0      0 :::22                   :::*                    LISTEN      1189/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1455/master

[root@ELK-Redis2 redis-4.0.10]# redis-server /etc/redis/6377/redis.conf 
6675:C 18 Sep 21:03:59.530 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6675:C 18 Sep 21:03:59.530 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=6675, just started
6675:C 18 Sep 21:03:59.530 # Configuration loaded
[root@ELK-Redis2 redis-4.0.10]# redis-server /etc/redis/6378/redis.conf 
6685:C 18 Sep 21:04:05.334 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6685:C 18 Sep 21:04:05.334 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=6685, just started
6685:C 18 Sep 21:04:05.334 # Configuration loaded
[root@ELK-Redis2 redis-4.0.10]# redis-server /etc/redis/6379/redis.conf 
6722:C 18 Sep 21:04:39.916 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6722:C 18 Sep 21:04:39.916 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=6722, just started
6722:C 18 Sep 21:04:39.916 # Configuration loaded
[root@ELK-Redis2 redis-4.0.10]# netstat -lntp|grep 637*
tcp        0      0 172.20.144.106:6379     0.0.0.0:*               LISTEN      6723/redis-server 1 
tcp        0      0 172.20.144.106:16377    0.0.0.0:*               LISTEN      6676/redis-server 1 
tcp        0      0 172.20.144.106:16378    0.0.0.0:*               LISTEN      6686/redis-server 1 
tcp        0      0 172.20.144.106:16379    0.0.0.0:*               LISTEN      6723/redis-server 1 
tcp        0      0 172.20.144.106:6377     0.0.0.0:*               LISTEN      6676/redis-server 1 
tcp        0      0 172.20.144.106:6378     0.0.0.0:*               LISTEN      6686/redis-server 1 
[root@ELK-Redis2 redis-4.0.10]# ps -ef|grep redis
root      6676     1  0 21:03 ?        00:00:00 redis-server 172.20.144.106:6377 [cluster]
root      6686     1  0 21:04 ?        00:00:00 redis-server 172.20.144.106:6378 [cluster]
root      6723     1  0 21:04 ?        00:00:00 redis-server 172.20.144.106:6379 [cluster]
root      6791 24114  0 21:05 pts/0    00:00:00 grep --color=auto redis

これではクラスタではなく6つのredisインスタンスにすぎません.次にクラスタを作成します.
(2)クラスタ2.1の作成、rubyのインストール
[root@ELK-Redis2 redis-4.0.10]# yum -y install ruby ruby-devel rubygems rpm-build
[root@ELK-Redis2 redis-4.0.10]# gem install redis
ERROR:  Could not find a valid gem 'redis' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/latest_specs.4.8.gz)
        ,              ,    (https://rubygems.org/downloads/redis-3.3.5.gem)
       :https://rubygems.org/gems/redis/versions

[root@ELK-Redis2 opt]# gem install -l /opt/redis-3.3.5.gem 
Successfully installed redis-3.3.5
Parsing documentation for redis-3.3.5
Installing ri documentation for redis-3.3.5
1 gem installed

2.2、クラスタを作成する.redis-trib.rbはredisが公式に発表したredisクラスタを管理するツールであり、redisのソースコードsrcディレクトリの下に統合され、redisが提供するクラスタコマンドに基づいて簡単で、便利で、実用的な操作ツールにパッケージされている.redis-trib.rbはredisの著者がrubyで完成したものである.
[root@ELK-Redis2 opt]# cp /opt/redis-4.0.10/src/redis-trib.rb /usr/local/bin/     
[root@ELK-Redis1 src]# redis-trib.rb create  --replicas  1  172.20.144.105:6377 172.20.144.105:6378 172.20.144.105:6379 172.20.144.106:6377 172.20.144.106:6378 172.20.144.106:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.20.144.105:6377
172.20.144.106:6377
172.20.144.105:6378
Adding replica 172.20.144.106:6379 to 172.20.144.105:6377
Adding replica 172.20.144.105:6379 to 172.20.144.106:6377
Adding replica 172.20.144.106:6378 to 172.20.144.105:6378
M: 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c 172.20.144.105:6377
   slots:0-5460 (5461 slots) master
M: 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066 172.20.144.105:6378
   slots:10923-16383 (5461 slots) master
S: dc1b2174b49363d8cbd7276bb25bbcd663b358c7 172.20.144.105:6379
   replicates cb652e954181aeecb27bb2b1a6f06e8c31d49d4a
M: cb652e954181aeecb27bb2b1a6f06e8c31d49d4a 172.20.144.106:6377
   slots:5461-10922 (5462 slots) master
S: cb79c3c8520cfb6dbbd685e2289673d9425bd942 172.20.144.106:6378
   replicates 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066
S: a86950a77fd9fb387c246938c6c25d553cce4bcb 172.20.144.106:6379
   replicates 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 172.20.144.105:6377)
M: 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c 172.20.144.105:6377
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: cb79c3c8520cfb6dbbd685e2289673d9425bd942 172.20.144.106:6378
   slots: (0 slots) slave
   replicates 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066
S: a86950a77fd9fb387c246938c6c25d553cce4bcb 172.20.144.106:6379
   slots: (0 slots) slave
   replicates 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c
M: 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066 172.20.144.105:6378
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: dc1b2174b49363d8cbd7276bb25bbcd663b358c7 172.20.144.105:6379
   slots: (0 slots) slave
   replicates cb652e954181aeecb27bb2b1a6f06e8c31d49d4a
M: cb652e954181aeecb27bb2b1a6f06e8c31d49d4a 172.20.144.106:6377
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

備考:このコマンドを説明するだけで、rubyコマンドを呼び出してクラスタを作成します.replicas 1は、プライマリ・セカンダリ・レプリケーションの割合が1:1であることを示します.すなわち、プライマリ・ノードがセカンダリ・ノードに対応していることを示します.次に、Redisクラスタに16383個のsoltしかないため、デフォルトでは各プライマリノードと対応するスレーブノードサービス、およびsoltのサイズが割り当てられます.デフォルトでは平均的に割り当てられます.もちろん、後続の増減ノードも再割り当てできます.
2.3.クラスタノードの検証
[root@ELK-Redis1 src]# redis-trib.rb check 172.20.144.106:6379
>>> Performing Cluster Check (using node 172.20.144.106:6379)
S: a86950a77fd9fb387c246938c6c25d553cce4bcb 172.20.144.106:6379
   slots: (0 slots) slave
   replicates 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c
M: 0905078cce6cf980b9362e7d9b6cb7e6ad1d874c 172.20.144.105:6377
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066 172.20.144.105:6378
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: cb652e954181aeecb27bb2b1a6f06e8c31d49d4a 172.20.144.106:6377
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: cb79c3c8520cfb6dbbd685e2289673d9425bd942 172.20.144.106:6378
   slots: (0 slots) slave
   replicates 31ef8f27b2fb9fdebbbd562c2c4e589c42f83066
S: dc1b2174b49363d8cbd7276bb25bbcd663b358c7 172.20.144.105:6379
   slots: (0 slots) slave
   replicates cb652e954181aeecb27bb2b1a6f06e8c31d49d4a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

2.4、redis-trib.rb関連機能は、redisクラスタを作成する際にすでに使用されており、redis公式に与えられたクラスタ管理ツールです.次の機能があります.
1create:    
2check:    
3info:      
4fix:    
5reshard:    slot
6rebalance:      slot  
7add-node:        
8del-node:        
9set-timeout:                
10call:            
11import:   redis