一、redis6 叢集部署(新特性瞭解)
Redis 6.0 introduces SSL, the new RESP3 protocol, ACLs, client side caching, diskless replicas, I/O threads, faster RDB loading, new modules APIs and many more improvements.
【Redis 6.0引入了SSL,新的RESP3協議,acl,客戶端快取,無磁碟副本,I/O執行緒,更快的RDB載入,新的模組api和更多的改進。】
一、redis6.2.6偽叢集部署
環境:
2臺虛擬機器:192.168.109.137、192.168.109.138
系統:CentOS Linux release 7.6.1810 (Core)
官網下載:redis-6.2.6.tar
安裝過程:
1.建立普通使用者,設定
[root@localhost ~]# useradd -d /home/apprun apprun
sudo vi /etc/sudoers
%apprun ALL=NOPASSWD: /apprun/*,/bin/systemctl *,/sbin/service *,/bin/mv /tmp/* /apprun/*,/usr/bin/vi *,/usr/bin/yum install *,/usr/bin/rpm *,/usr/bin/find *,/usr/sbin/lsof *,/usr/sbin/tcpdump *,/usr/bin/chown * /apprun/*,/usr/bin/chmod * /apprun/*,/usr/bin/kill
locale -a 列出所有已安裝的語言包
vim /etc/locale.conf 修改成如下字符集
LANG=en_US.UTF-8
source /etc/locale.conf 使配置生效
mkdir -p /apprun && chown -R apprun:apprun /apprun
2.安裝支援的庫檔案:
[root@localhost ~]# su - apprun
[apprun@localhost ~]$ cd /apprun/
[apprun@localhost ~]$ sudo yum install -y gcc automake autoconf libtool make
3.上傳解壓檔案:
[apprun@localhost apprun]$ mkdir -p /apprun/soft
[apprun@localhost apprun]$ cd /apprun/soft/
[apprun@localhost soft]$ tar -zxf redis-6.2.6.tar.gz
[apprun@localhost soft]$ mkdir -pv /apprun/redis
4.編譯與安裝:
[apprun@localhost soft]$ cd /apprun/soft/redis-6.2.6
[apprun@localhost redis-6.2.6]$ make PREFIX=/apprun/redis install
將 redis-trib.rb 複製到 /apprun/redis/bin 目錄下:
[apprun@localhost redis-6.2.6]$ cd src/
[apprun@localhost src]$ cp redis-trib.rb /apprun/redis/bin/
5.建立 Redis 節點:
[apprun@localhost apprun]$ mkdir -pv /apprun/redis_cluster
在 redis_cluster 目錄下,建立名為7000、7001、7002的目錄,並將 redis.conf 拷貝到這三個目錄中
[apprun@localhost apprun]$ mkdir -pv /apprun/redis_cluster/logs/
[apprun@localhost apprun]$ mkdir -pv /apprun/redis_cluster/data/{redis_7000,redis_7001,redis_7002}
[apprun@localhost apprun]$ cd /apprun/redis_cluster/
[apprun@localhost redis_cluster]$ mkdir 7000 7001 7002
[apprun@localhost redis_cluster]$ cp /apprun/soft/redis-6.2.6/redis.conf /apprun/redis_cluster/
[apprun@localhost redis_cluster]$ vim /apprun/redis_cluster/redis.conf
port 6379 //埠7000,7002,7003 bind 192.168.109.137 requirepass wang!321 masterauth wang!321 timeout 60 //在客戶端空閒N秒後關閉連線 daemonize yes //redis後臺執行 pidfile /var/run/redis_6379.pid //pidfile檔案對應7000,7001,7002 logfile "/apprun/redis_cluster/logs/redis_6379.log" always-show-logo yes cluster-enabled yes //開啟叢集 把註釋#去掉 cluster-config-file nodes_7000.conf //叢集的配置 配置檔案首次啟動自動生成 7000,7001,7002 把註釋#去掉 cluster-node-timeout 15000 //請求超時 預設15秒,可自行設定 把註釋#去掉 save 3600 1 save 300 100 save 60 10000 appendonly yes //aof日誌開啟 有需要就開啟,它會每次寫操作都記錄一條日誌 dir /apprun/redis_cluster/data/redis_6379配置參考
[apprun@localhost redis_cluster]$ cp /apprun/redis_cluster/redis.conf /apprun/redis_cluster/7000/
[apprun@localhost redis_cluster]$ cp /apprun/redis_cluster/redis.conf /apprun/redis_cluster/7001/
[apprun@localhost redis_cluster]$ cp /apprun/redis_cluster/redis.conf /apprun/redis_cluster/7002/
[apprun@localhost redis_cluster]$ sed -i 's#6379#7000#g' /apprun/redis_cluster/7000/redis.conf
[apprun@localhost redis_cluster]$ sed -i 's#6379#7001#g' /apprun/redis_cluster/7001/redis.conf
[apprun@localhost redis_cluster]$ sed -i 's#6379#7002#g' /apprun/redis_cluster/7002/redis.conf
接著在另外一臺機器上(192.168.109.138),的操作重複以上步驟
6.啟動各個節點:
[apprun@localhost apprun]$ /apprun/redis/bin/redis-server /apprun/redis_cluster/7000/redis.conf
[apprun@localhost apprun]$ /apprun/redis/bin/redis-server /apprun/redis_cluster/7001/redis.conf
[apprun@localhost apprun]$ /apprun/redis/bin/redis-server /apprun/redis_cluster/7002/redis.conf
檢查 redis 啟動情況:
[apprun@localhost apprun]$ ps aux | grep redis
開啟兩臺機器的防火牆:(實驗關閉了防火牆)
firewall-cmd --zone=public --add-port=7000-7002/tcp --permanent
firewall-cmd --zone=public --add-port=17000-17002/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
7.建立叢集:
[apprun@localhost apprun]$ /apprun/redis/bin/redis-cli --cluster create \
192.168.109.137:7000 192.168.109.137:7001 192.168.109.137:7002 192.168.109.138:7000 192.168.109.138:7001 192.168.109.138:7002 \
--cluster-replicas 1 -a 'wang!321'
8.叢集客戶端命令(redis-cli -c -p port)[redis-cli --cluster]:
列: [apprun@localhost apprun]$ /apprun/redis/bin/redis-cli -c -h 192.168.109.137 -p 7000 -a 'wang!321' cluster info 叢集 cluster info :列印叢集的資訊 cluster nodes :列出叢集當前已知的所有節點( node),以及這些節點的相關資訊。 節點 cluster meet <ip> <port> :將 ip 和 port 所指定的節點新增到叢集當中,讓它成為叢集的一份子。 cluster forget <node_id> :從叢集中移除 node_id 指定的節點。 cluster replicate <node_id> :將當前節點設定為 node_id 指定的節點的從節點。 cluster saveconfig :將節點的配置檔案儲存到硬盤裡面 槽(slot) cluster addslots <slot> [slot ...] :將一個或多個槽( slot)指派( assign)給當前節點。 cluster delslots <slot> [slot ...] :移除一個或多個槽對當前節點的指派。 cluster flushslots :移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。 cluster setslot <slot> node <node_id> :將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給 另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。 cluster setslot <slot> migrating <node_id> :將本節點的槽 slot 遷移到 node_id 指定的節點中。 cluster setslot <slot> importing <node_id> :從 node_id 指定的節點中匯入槽 slot 到本節點。 cluster setslot <slot> stable :取消對槽 slot 的匯入( import)或者遷移( migrate)。 鍵 cluster keyslot <key> :計算鍵 key 應該被放置在哪個槽上。 cluster countkeysinslot <slot> :返回槽 slot 目前包含的鍵值對數量。 cluster getkeysinslot <slot> <count> :返回 count 個 slot 槽中的鍵常用命令