II. Cài đặt và cấu hình Redis Cluster
2.1. Mô tả và chuẩn bị hệ thống
– Số lượng server cần cài Redis: 5 server
192.168.105.208
192.168.105.209
192.168.105.210

– Các server chạy 2 instance Redis tương ứng với 2 port 30001 và 30002
– Replicate: 1
– Hệ điều hành: Centos 7.3 64bit.
– Version Redis: Redis-4.0.1
– Version Ruby: Ruby-2.3.0
Cài đặt các phần mềm cần thiết trên server 192.168.105.208, sử dụng user có quyền Root.
• Cài đặt Ruby 2.3.0
– Download Ruby 2.3.0 theo link: https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
#Cd /root
#wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
– Giải nén và cài đặt ruby:
#tar -xvf ruby-2.3.0.tar.bz2
#cd ruby-2.3.0
#./configure
#make
#make install
– Đặt biến PATH, để server nhận ruby 2.3.0.
export PATH=/usr/local/bin:$PATH
Echo ‘export PATH=”/usr/local/bin:$PATH”‘ >> ~/.bash_profile
• Cài đặt rubygems
– Download theo link: https://rubygems.org/rubygems/rubygems-2.6.13.tgz
#Cd /root
#wget https://rubygems.org/rubygems/rubygems-2.6.13.tgz
– Giải nén và cài đặt Rubygems:
#tar -xvf rubygems-2.6.13.tgz
#cd rubygems-2.6.13
#ruby setup.rb install
• Cài đặt Redis Gem:
– Download theo link: https://rubygems.org/downloads/redis-4.0.0.gem
#Cd /root
#wget https://rubygems.org/downloads/redis-4.0.0.gem
– Cài đặt:
#gem install redis-4.0.0.gem
Output là cài đặt thành công:
[root@lab01 rubygems-2.6.13]# ruby setup.rb install
RubyGems 2.6.13 installed
Parsing documentation for rubygems-2.6.13
Installing ri documentation for rubygems-2.6.13
…….
RubyGems installed the following executables:
/usr/local/bin/gem

Ruby Interactive (ri) documentation was installed. ri is kind of like man
pages for ruby libraries. You may access it like this:
ri Classname
ri Classname.class_method
ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
–no-document flag, or set it as the default in your ~/.gemrc file. See
‘gem help env’ for details.
2.2. Hướng dẫn cài đặt Redis Cluster
• Cài đặt Redis
Ta sẽ thực hiện cài đặt Redis software trên tất cả các server đã quy hoạch cho Redis cluster bằng user non-root (marshall)
– Grant user marshall có quyền sudo
#visudo
Add line:
marshall ALL=(ALL) NOPASSWD:ALL
Vào cuối file.
– Download Redis theo link: https://redis.io/download
$cd ~
$wget http://download.redis.io/releases/redis-4.0.1.tar.gz
– Giải nén redis
$ tar –xvf redis-4.0.1.tar.gz
– Move thư mục vừa giải nén đến thư mục sử dụng để lưu trữ Redis
$ sudo mv redis-4.0.1 /data01
$ cd /data01/redis-4.0.1
– Cài đặt Redis
$ sudo make
$ sudo make install

• Start instance Redis trên mỗi server
Theo như yêu cầu đặt ra, chạy 2 instance Redis trên mỗi server. Ta edit file create-cluster như sau:
$cd /data01/redis-4.0.1/utils/create-cluster
– Tạo file config chung cho redis.
$vim /data01/redis-4.0.1/utils/create-cluster/redis.conf
Import nội dung sau vào file redis.conf:
protected-mode no
timeout 600
tcp-keepalive 300

daemonize yes
maxclients 100000
maxmemory 4g
appendonly yes
cluster-enabled yes
cluster-node-timeout 5000
– Tiếp theo, edit file create-cluster:
Thay đổi các tham số như sau (các dòng chữ đỏ là đã thay đổi so với file default):
#!/bin/bash

# Settings
PORT=30000
TIMEOUT=2000
NODES=2
REPLICAS=1

# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.

if [ -a config.sh ]
then
source “config.sh”
fi

# Computed vars
ENDPORT=$((PORT+NODES))

if [ “$1” == “start” ]
then
while [ $((PORT < ENDPORT)) != “0” ]; do
PORT=$((PORT+1))
echo “Starting $PORT”
../../src/redis-server ./redis.conf –port $PORT –cluster-config-file nodes-${PORT}.conf –appendfilename appendonly-${PORT}.aof –dbfilename dump-${PORT}.rdb –logfile ${PORT}.log
done
exit 0
fi

if [ “$1” == “create” ]
then
HOSTS=””
while [ $((PORT < ENDPORT)) != “0” ]; do
PORT=$((PORT+1))
HOSTS=”192.168.105.208:30001 192.168.105.208:30002 192.168.105.209:30001 192.168.105.209:30002 192.168.105.210:30001 192.168.105.210:30002″
done
../../src/redis-trib.rb create –replicas $REPLICAS $HOSTS
exit 0
fi

if [ “$1” == “stop” ]
then
while [ $((PORT < ENDPORT)) != “0” ]; do PORT=$((PORT+1)) echo “Stopping $PORT” ../../src/redis-cli -p $PORT shutdown nosave done exit 0 fi ………………….. – Cấu hình các server Redis còn lại như trên. Rồi start các instances redis $ ./create-cluster start     – Kiểm tra xem các Redis đã start lên chưa $ netstat –lntp | grep 3000   Và check log: • Create cluster $ cd /data01/redis-4.0.1/utils/create-cluster/ $ ./create-cluster create   Chọn “yes”.   – Kết thúc quá trình cài đặt Redis Cluster. 2.3. Một số câu lệnh với Redis cluster – Kiểm tra trạng thái cluster $cd /data01/redis-4.0.1 $./src/redis-trib.rb check 192.168.105.208:30001   – Connect vào cluster từ redis-cli [redis@redis-01 bin]$ ./redis-cli -h 192.168.105.208 -p 30001 -c 192.168.105.208:30001>
– Kiểm tra các thông tin của cluster
192.168.105.208:30001> info server

– Kiểm tra thông tin Memory của cluster
192.168.105.208:30001> info memory

– Kiểm tra trạng thái replication của server
192.168.105.208:30001> info replication
– Kiểm tra số lượng client connect tới server
192.168.105.208:30001> info clients

Leave a Reply

Your email address will not be published. Required fields are marked *