Premise: Before because Redis-cluster does not have PHP client + mouse, so direct one vote veto, the current situation uses Redis high available is through Sentinel + script VIP switch, but currently Redis support PHP client, I am ready to test under Redis cluster.
Reference official documents: Http://redis.io/topics/cluster-tutorial
Usage version: 3.0.7:http://redis.io/download
Server environment: 10.1.3.191
Objective: To build a clustered environment of 6 master and 6 slave
1. Download
cd/opt/soft/
wget http://219.239.26.10/files/10300000079CAD74/120.52.72.46/download.redis.io/c3pr90ntcsf0/releases/redis-3.0.7.tar.gz
2. Unzip the installation
TAR-ZXF redis-3.0.7.tar.gz
CD redis-3.0.7
Make && make install
3. Unified configuration File
cd/opt/soft/redis-3.0.7/
CP redis.conf Redis.conf_bak
The key parameters are as follows:
MaxMemory 5Gbport 7000cluster-enabled Yespidfile/var/run/redis_7000.pidcluster-config-file Nodes-7000.confcluster-node-timeout 5000appendonly Yesdir/u01/redis-cluster-test/7000/data/logfile "/u01/ Redis-cluster-test/log/redis_7000.log "
4. System Configuration
echo >/proc/sys/net/core/somaxconnsysctl Vm.overcommit_memory=1echo never >/sys/kernel/mm/transparent_ Hugepage/enabledyum install ruby Rubygems-ygem install Redis
5. Unified Script Configuration
function create_dir{#创建数据文件和配置文件目录port = "7001 7002 7003 7004 7005 8000 8001 8002 8003 8004 8005 "for i in $port; domkdir -p /etc/redis/$ i/mkdir -p /u01/redis-cluster-test/$i/data/donemkdir -p /u01/redis-cluster-test/log} function conf{# Modify configuration file port= "7001 7002 7003 7004 7005 8000 8001 8002 8003 8004 8005 "for i in $port;d ocp /etc/redis/7000/redis.conf /etc/redis/$i/ #copy redis.confcd /etc/redis/$i/sed -i "s/7000/$i/g" redis.conf done}function start_redis{/opt/soft/redis-3.0.7/src/redis-server /etc/redis/ 7000/redis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/7001/redis.conf &/ opt/soft/redis-3.0.7/src/redis-server /etc/redis/7002/redis.conf &/opt/soft/redis-3.0.7/src/ Redis-server /etc/redis/7003/rEdis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/7004/redis.conf &/opt/soft /redis-3.0.7/src/redis-server /etc/redis/7005/redis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/8000/redis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/8001/ redis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/8002/redis.conf &/opt/ soft/redis-3.0.7/src/redis-server /etc/redis/8003/redis.conf &/opt/soft/redis-3.0.7/src/ redis-server /etc/redis/8004/redis.conf &/opt/soft/redis-3.0.7/src/redis-server /etc/redis/ 8005/redis.conf &}create_dirconfstart_redis
After the script is started, the following shows
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7C/DB/wKiom1bZThfBySM_AATuhKx2eJA978.png "title=" Redis4.png "alt=" Wkiom1bzthfbysm_aatuhkx2eja978.png "/>
6. Create a cluster
#建立12个实例的集群
/OPT/SOFT/REDIS-3.0.7/SRC/REDIS-TRIB.RB Create--replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/7C/DB/wKiom1bZTQ6CD231AA33rCHw3D4348.jpg "style=" float: Left; "title=" redis3.jpg "alt=" Wkiom1bztq6cd231aa33rchw3d4348.jpg "/>
7. Cluster Management commands
Cluster CLUSTER INFO print cluster information CLUSTER NODES list all nodes currently known to the cluster (node), and information about these nodes. nodes CLUSTER MEET <ip> <port> will ip and port The node specified is added to the cluster, making it part of the cluster. CLUSTER FORGET <node_id> Remove node_id specified nodes from the cluster. CLUSTER REPLICATE <node_id> sets the current node to the from node of the node_id specified node. CLUSTER SAVECONFIG Save the node's configuration file to the hard drive. Slots (Slots) cluster addslots <slot> [slot  ...] assigns one or more slots (slots) to the current node (assign). &NBSP;&NBSP;CLUSTER&NBSP;DELSLOTS&NBSP;<SLOT>&NBSP;[SLOT&NBSP, ...] Removes the assignment of one or more slots to the current node. CLUSTER FLUSHSLOTS removes all slots assigned to the current node, making the current node a node without any slots assigned. CLUSTER SETSLOT <slot> NODE <node_id> Assign Slots slot to node_id The specified node, if the slot is already assigned to another node, let the other node delete the slot first, and then assign it. cluster setslot <slot> migrating <node_id> Migrate the slot slot of this node to the node node_id specified. CLUSTER SETSLOT <slot> IMPORTING <node_id> from node_id Import slots slot to this node in the specified node. CLUSTER SETSLOT <slot> STABLE Cancel the slot slot (import) or migration (migrate). key CLUSTER KEYSLOT <key> calculate key key which slot should be placed. CLUSTER COUNTKEYSINSLOT <slot> returns the number of key-value pairs that the slot slot currently contains. CLUSTER GETKEYSINSLOT <slot> <count> back count The key in the slot slot.
8. Cluster simple test
Test 1: View cluster status, etc.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/DB/wKiom1bZT2yD5wltAAdZZTdJZRk287.png "title=" Redis5.png "alt=" Wkiom1bzt2yd5wltaadzztdjzrk287.png "/>
Test 2: Simulate an outage switch
Note that the cluster client connection method needs to use: Redis-cli-c-P 7000 to log on, or when the write will be executed error
Attached:-C Enable cluster mode (follow-ask and-moved redirections)
127.0.0.1:7000> Set Tiger Bear
(Error) MOVED 2772 127.0.0.1:7001
127.0.0.1:7000> get Tiger
(Error) MOVED 2772 127.0.0.1:7001
Redis-cli-c-P 7000
127.0.0.1:7000> Set Tiger Bear
-Redirected to Slots [2772] located at 127.0.0.1:7001
Ok
127.0.0.1:7001> get Tiger
"Bear"
127.0.0.1:7001>
Highly available Test 1: instance downtime for simulated dumps
The instructions above indicate that the key that was inserted in the 7000 instance is backed up on 7001 instances, and the key (Tiger) is accessed through the other nodes, and is also prompted for distribution to Port 7001.
# redis-cli-c-P 7001127.0.0.1:7001> shutdownnot connected># redis-cli-c-P 7004127.0.0.1:7004> get tiger-> Redirected to slot [2772] located @ 127.0.0.1:8001 "Bear"
Description: After active shutdown7001 port, go to other node to access key (Tiger), Will forward 8001 (7001 of slave)
The slave here is different from the slave concept in the slaveof, which is the location of the cluster to the instance, as the cut-off point of M, so when actually used, it is not put on a machine as an instance of ' master and prepare '.
OK, what happens if we start the 7001 instance again?
#/opt/soft/redis-3.0.7/src/redis-server/etc/redis/7001/redis.conf &
The result is to revisit or access 8001 instances, we drop the 8001, and the test results are as follows
# redis-cli-c-P 7004127.0.0.1:7004> get tiger-> redirected to slot [2772] located @ 127.0.0.1:8001 "Bear" # Redis-c Li-c-P 8001127.0.0.1:8001> shutdownnot connected># redis-cli-c-P 7004127.0.0.1:7004> get Tiger (Error) CLUSTER Down the cluster are down127.0.0.1:7004> get tiger-> redirected to Slots [2772] located at 127.0.0.1:7001 ' Bear '
When the 8001 down, the immediate access will prompt the cluster outage, is because the cluster switchover also takes time, short-term can not be used, quickly switched back to 7001 nodes.
Write this today and continue adding content .....
This article is from "Wang Tiger" blog, please be sure to keep this source http://5434718.blog.51cto.com/5424718/1747629
Build Redis-cluster Environment