redis cluster簡介:node
3.0之後版本加入,去中心化設計,不一樣redis節點均可接收用戶請求並持有不一樣數據,每節點共享節點innode信息方便找到數據。redis
實驗準備:vim
試驗目標:3臺機器,每機器2節點共6個節點組成完整高可用的集羣。 centos
測試環境:ruby
HOST | IP | software | PORT | SYSTEM | |
node1 | 192.168.23.128 | redis-4.0.10 | 7001 | 7004 | CentOS 7.4 |
node2 | 192.168.23.129 | redis-4.0.10 | 7002 | 7005 | CentOS 7.4 |
node3 | 192.168.23.131 | redis-4.0.10 | 7003 | 7006 | CentOS 7.4 |
操做步驟:bash
1.軟件下載到/usr/local:wget http://download.redis.io/releases/redis-4.0.10.tar.gz 服務器
2.編譯安裝app
tar xzf redis-4.0.10.tar.gz cd redis-4.0.10 make &&make install #若有軟件依賴安裝依賴包 #複製redis 集羣管理命令 cp /usr/local/redis-4.0.10/src/redis-trib.rb /usr/local/bin/
3.配置節點(以node1節點7001爲例)
ide
mkdir -p /data/redis-cluster/{7001,7004} cp /usr/local/redis-4.0.10/redis.conf /data/redis-cluster/7001/redis-7001.conf
需修改配置文件:工具
#vim /data/redis-cluster/7001/redis-7001.conf bind 0.0.0.0 port 7001 daemonize yes pidfile /var/run/redis_7001.pid dir /data/redis-cluster/7001 logfile "/var/log/redis/7001.log" cluster-enabled yes cluster-node-timeout 2000 appendonly yes cluster-config-file nodes-7001.conf
##新建日誌目錄及文件 mkdir /var/log/redis ##啓動各redis 實例 cd 7001 redis-server ./redis7001.conf
將配置文件複製到其餘節點並分別啓動:
grep -v "^#" redis-7001.conf|grep -v "^$" >redis.conf.tem cp redis.conf.tem /data/redis-cluser/700X/redis-700X.conf vim修改複製過來的配置文件: :%s/7001/700X/g
4.配置集羣redis-trib.rb工具環境(ruby語言編寫)
redis集羣管理工具須要ruby2.2以上版本,查看版本ruby -v
推薦yum安裝方式(只在一臺服務器上安裝便可):
yum install centos-release-scl-rh yum install rh-ruby23 -y scl enable rh-ruby23 bash ruby -v 加入到環境變量: echo "source /opt/rh/rh-ruby23/enable" >> /etc/profile source /etc/profile gem install redis #安裝redis插件,以下載較慢可以下切換到國內gem源 #gem sources –l #gem sources --remove https://rubygems.org/ #gem source -a https://gems.ruby-china.com
初始化redis集羣
#注意建立集羣時 對主機名和DNS支持很差,最好使用 IP:port的方式
建立方法一:先在三臺主機上分別啓動一個Master ,在啓動slave交叉加入各主機Master,0,1表示slave節點的數量
#設計端口單數爲master 雙數爲slave redis-trib.rb create --replicas 0 192.168.23.128:7001 192.168.23.129:7002 192.168.23.131:7003 redis-trib.rb check 192.168.23.128:7001 #檢查任意主機節點,並記錄master-id ###啓動slave加入各Master redis-trib.rb add-node --slave --master-id e3933a2276962abb7180ebe7767a544bdd539f27 192.168.23.128:7004(新節點) 192.168.23.128:7001(任意舊節點) redis-trib.rb add-node --slave --master-id cee9b75e480a3d707e7079631a280e732e0264b7 192.168.23.129:7005 192.168.23.128:7001 redis-trib.rb add-node --slave --master-id c61feb0e6f33d467758e8278436c03ce265c44b1 192.168.23.131:7006 192.168.23.128:7001
建立方法二(推薦):
redis-trib.rb create --replicas 1 192.168.23.128:7001 192.168.23.129:7002 192.168.23.131:7003 192.168.23.128:7004 192.168.23.129:7005 192.168.23.131:7006 ##檢查對應關係 redis-trib.rb check 192.168.23.128:7001
附:redis-trib.rb工具使用方法示例:
https://www.jianshu.com/p/21f67bd739cc
5. 內存優化相關
#修改系統參數 echo never > /sys/kernel/mm/transparent_hugepage/enabled echo 511 > /proc/sys/net/core/somaxconn 並加到 /etc/rc.local
vim /etc/sysctl.conf vm.overcommit_memory=1 net.core.somaxconn = 511 kernel.pid_max=1000000 sysctl -p
vim /etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535