Monday, July 27, 2020

Configure Redis cluster - Cài đặt Redis Cluster



I. centos >7.3

 

1.install lib

 

yum -y install patch libyaml-devel libffi-devel glibc-headers autoconf gcc-c++ glibc-devel readline-devel zlib-devel bzip2 automake libtool bison

 

 

2. install rvm

curl -sSL https://rvm.io/mpapis.asc | sudo gpg2 --import -

curl -sSL https://rvm.io/pkuczynski.asc | sudo gpg2 --import -

curl -L get.rvm.io | bash -s stable

source /etc/profile.d/rvm.sh

rvm reload

 

3. install ruby

rvm install 2.3.0

 

4. install env redis

gem install redis

 

II. centos <7.3

1. install lib

yum -y install patch libyaml-devel libffi-devel glibc-headers autoconf gcc-c++ glibc-devel readline-devel zlib-devel bzip2 automake libtool bison

 

2. install ruby

 yum -y install ruby gcc ruby-devel rubygems

 

3. install rvm

curl -sSL https://rvm.io/mpapis.asc | gpg --import -

curl -L get.rvm.io | bash -s stable

source /etc/profile.d/rvm.sh

rvm reload

 

4. optimize system

echo never >/sys/kernel/mm/transparent_hugepage/enabled

vim /etc/sysctl.conf

vm.overcommit_memory = 1

 

---

sysctl -w net.core.somaxconn=512

 

----------

* soft nofile 196000

* hard nofile 196000

* hard nproc 65536

* soft nproc 65536

* soft stack 32768

zredis  soft    nproc   65536

zredis  hard    nproc   65536

 

------

 

B. Deploy

172.27.10.37 zpapp1

172.27.10.38 zpapp2

172.27.10.39 zpapp3

1. Thông tin cluster redis

Port Master: 8000

Port Slave: 8001,8002

Mo hinh shard:

Shard 1: 172.27.10.37:8000 - zpapp2:8001 - zpapp3:8002

Shard 2: zpapp2:8000 - zpapp3:8001 - 172.27.10.37:8002

Shard 3: zpapp3:8000 - 172.27.10.37:8001 - zpapp2:8002

 

2.tạo user zredis để start redis instance

useraddradd zredis

 

3. set quyền folder /zvccb/redis

chown -R zredis. /zvccb/redis

----

sysctl -w net.core.somaxconn=512

4. start all instance tren cac 172.27.10.37,zpapp2,zpapp3

/zvccb/redis/bin/redis-server /zvccb/redis/conf/8000.conf

/zvccb/redis/bin/redis-server /zvccb/redis/conf/8001.conf

/zvccb/redis/bin/redis-server /zvccb/redis/conf/8002.conf

 

5. Tạo cluster redis

/zvccb/redis/bin/redis-trib.rb create 172.27.10.37:8000 172.27.10.38:8000 172.27.10.39:8000

 

6. Check cluster states:

/zvccb/redis/bin/redis-cli -h 172.27.10.37 -p 8000 -c cluster nodes

/zvccb/redis/bin/redis-cli -h 172.27.10.38 -p 8000 -c cluster nodes

/zvccb/redis/bin/redis-cli -h 172.27.10.39 -p 8000 -c cluster nodes

 

61f8b86b5c875d50811a346c3b9f8652d21bda7d zpapp3:8000 master - 0 1578558765480 3 connected 10923-16383

2fb15487b6a95d4c43e4fda3dc67c785ada4e38b 172.27.10.37:8000 myself,master - 0 0 1 connected 0-5460

537f2ecb13452cd2c6bf6c1417687792c3d0e6ee zpapp2:8000 master - 0 1578558767484 2 connected 5461-10922

 

7. Add slave-node

---------

 

----------

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id f283e5fe9ec7128a2711e02e7ab9aa9410a65366 172.27.10.38:8001 172.27.10.37:8000

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id 9fe89332e3cbf87343a3361ec4b336ceb4a477a8 172.27.10.37:8001 172.27.10.39:8000

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id 3d42b987a7b342e3e46560ce70e4ebfd5b8e44b7 172.27.10.39:8001 172.27.10.38:8000

 

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id f283e5fe9ec7128a2711e02e7ab9aa9410a65366 172.27.10.38:8002 172.27.10.37:8000

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id 9fe89332e3cbf87343a3361ec4b336ceb4a477a8 172.27.10.37:8002 172.27.10.39:8000

/zvccb/redis/bin/redis-trib.rb add-node --slave --master-id 3d42b987a7b342e3e46560ce70e4ebfd5b8e44b7 172.27.10.39:8002 172.27.10.38:8000

==============

 

 

======

8. check cluster states 9 instance

/zvccb/redis/bin/redis-cli -h 172.27.10.38 -p 8000 -c cluster nodes

.

9. force failover: khi xay ra tinh trang nhay states cluster tu master -> slave, slave ->master tien hanh force lai theo dung mo hinh ban dau

/zvccb/redis/bin/redis-cli -h 172.27.10.37 -p 8000 -c CLUSTER FAILOVER

 

Check lai sau khi failover

 

 

10. restart lai cac services chay redis

 

 

-----------

 systemctl daemon-reload

 

 @reboot sleep 60 && /zvccb/redis/zredis_fo.sh

 

1 ---auto

 redis start tự động = script /etc/init.d/redis với user zredis

 sau 60s thì lấy lại Master = command trong crontab của root " @reboot sleep 60 && /zvccb/redis/zredis_fo.sh "

2 -- manual

 su - root

 pkill -u zredis # kill redis process

 su - zredis

 sh /zvccb/redis/zredis.sh # start redis

 sh /zvccb/redis/zredis_fo.sh # lấy lại master

Disable Transparent Huge Pages (THP) — MongoDB


#Centos , Redhat


vi /etc/init.d/disable-transparent-hugepages

 

#!/bin/bash

### BEGIN INIT INFO

# Provides: disable-transparent-hugepages

# Required-Start: $local_fs

# Required-Stop:

# X-Start-Before: mongod mongodb-mms-automation-agent

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: Disable Linux transparent huge pages

# Description: Disable Linux transparent huge pages, to improve

# database performance.

### END INIT INFO

 

case $1 in

start)

if [ -d /sys/kernel/mm/transparent_hugepage ]; then

thp_path=/sys/kernel/mm/transparent_hugepage

elif [ -d /sys/kernel/mm/transparent_hugepage ]; then

thp_path=/sys/kernel/mm/transparent_hugepage

else

return 0

fi

 

echo 'never' > ${thp_path}/enabled

echo 'never' > ${thp_path}/defrag

 

re='^[0-1]+$'

if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]

then

# RHEL 7

echo 0 > ${thp_path}/khugepaged/defrag

else

# RHEL 6

echo 'no' > ${thp_path}/khugepaged/defrag

fi

 

unset re

unset thp_path

;;

esac

####################

chmod 755 /etc/init.d/disable-transparent-hugepages

chkconfig --add disable-transparent-hugepages

chkconfig disable-transparent-hugepages on

Configure SSL for MongoDB and BI Connector - Cấu hình SSL MongoDB và BI Connector




 #### Tạo key cho MongoDB
mkdir /opt/ssl/
cd /op/ssl
openssl req -nodes -newkey rsa:2048 -keyout mongo.key -out mongo.crt -x509 -days 1000 -subj "/C=VN/ST=HCM/L=HCM/O=IT/OU=IT/CN=trueid05-worker03"
cat mongo.crt mongo.key > mongo.pem

########## Cấu hình SSL MongoDB
vi /etc/mongod.conf
systemLog:
   destination: file
   path: '/data/mongod.log'
   logAppend: true
processManagement:
   fork: true
   pidFilePath: '/data/mongod.pid'
net:
   bindIp: localhost
   port: 27017
   ssl:
      mode: requireSSL
      PEMKeyFile: '/opt/ssl/mongo.pem'
storage:
   dbPath: '/data
######## Tạo User cho BI connect vào MonggoDB
mongo
use admin
db.createUser({
user: "hoanc",
pwd: "password",
roles:[{role: "userAdminAnyDatabase" , db:"admin"}]})
###### Start dịch vụ MongoDB
mongod -f /etc/mongod.conf --fork
############ Tạo Key cho BI
cd /op/ssl
openssl req -nodes -newkey rsa:2048 -keyout bi.key -out bi.crt -x509 -days 1000 -subj "/C=VN/ST=HCM/L=HCM/O=IT/OU=IT/CN=172.27.5.13"
cat bi.crt bi.key > bi.pem
/######## Cấu hình SSL cho BI
vi /etc/mongosqld.conf
systemLog:
  logAppend: false
  path: '/data/mongosqld.log'
  verbosity: 2
security:
  enabled: true
mongodb:
  net:
    uri: trueid05-worker03
    auth:
      username: "hoanc"
      password: "password"
    ssl:
      enabled: true
      PEMKeyFile: "/opt/ssl/mongo.pem"
      CAFile: "/opt/ssl/mongo.crt"
net:
  bindIp: 172.27.5.13
  port: 3307
  ssl:
    mode: "allowSSL"
    PEMKeyFile: "/opt/ssl/bi.pem"
############### Start dịch vụ BI
mongosqld --config /etc/mongosqld.conf --sslAllowInvalidCertificates &

######## kiểm tra kết nối SLL
mysql --ssl-mode REQUIRED --ssl-ca=/opt/ssl/bi.crt --enable-cleartext-plugin --port 3307 -u hoanc -p -h 172.27.5.13