Simple construction of redis+keepalived+haproxy cluster



#############################redis cluster
reference
http://www.cnblogs.com/wuxl360/p/5920330.html

redis -3.2.9
make PREFIX=/usr/local/redis install

cp redis-3.2.9/src/redis-trib.rb /usr/local/redis/bin/
cp redis-3.2.9/redis.conf to
a machine
/opt/redis_cluster/7000/redis.conf
/opt/redis_cluster/7001/redis.conf
/opt/redis_cluster/7002/redis.conf


another machine
/opt/redis_cluster/7003/redis.conf
/opt/redis_cluster/ 7004/redis.conf
/opt/redis_cluster/7005/redis.conf

Each configuration is as follows, the port is changed according to the number
#############
port 7000 //Port 7000,7002,7003       
bind local ip //The default ip is 127.0.0.1, and it needs to be changed to an ip that can be accessed by other node machines. Otherwise, the corresponding port cannot be accessed when the cluster is created, and the cluster cannot be created.
Daemonize yes //redis runs
pidfile /var/run/redis_7000 in the background. pid //The pidfile file corresponds to 7000,7001,7002
cluster-enabled yes //Open the cluster and remove the comment #
cluster-config-file nodes_7000.conf //Cluster configuration The configuration file is automatically generated for the first time when the 7000,7001,7002
cluster-node is started -timeout 15000 //The request timeout defaults to 15 seconds, you can set
appendonly yes //aof log is turned on if necessary, it will record a log for each write operation 
############## ########################

Start redis on each machine
#/bin/sh
/usr/local/redis/bin/redis-server /opt/ redis_cluster/7001/redis.conf
/usr/local/redis/bin/redis-server /opt/redis_cluster/7002/redis.conf
/usr/local/redis/bin/redis-server /opt/redis_cluster/7003/redis.conf

ps -ef|grep redis
3 processes
netstat -nltp|grep redis
6 ports

Install ruby
​​yum -y install ruby ​​ruby-devel rubygems rpm-build -y
gem install redis

associated
cluster./redis-trib.rb create --replicas 1 192.168.139.161:7000 192.168.139.161:7001 192.193:7003 192.193:7003 192.168.139.193:7004 192.168.139.193:7005

free node write any node received
./redis-cli -h 192.168.139.161 -c -p 7000
set hao ning
./redis-cli -h 192.168.139.193 -c -p 7005
get hao


When redis cluster is designed, decentralization and middleware are taken into account, that is to say, each node in the cluster is equal and equal, and each node saves its own data and the entire The state of the cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any node in the cluster to obtain data from other nodes.
Redis Cluster does not use traditional consistent hashing to distribute data, but uses another method called hash slot to distribute data. Redis cluster allocates 16384 slots by default. When we set a key, we will use the CRC16 algorithm to take the modulo to get the corresponding slot, and then assign the key to the nodes in the hash slot interval. The specific algorithm is: CRC16(key) %16384. So when we saw set and get during the test, we jumped directly to the node of port 7000.
The Redis cluster will store the data on a master node, and then synchronize the data between the master and its corresponding slave. When reading data, it also obtains data from the corresponding master node according to the consistent hash algorithm. Only when a master hangs up, will a corresponding salve node be started to act as the master.
It should be noted that there must be 3 or more master nodes, otherwise the cluster creation will fail, and when the number of surviving master nodes is less than half of the total number of nodes, the entire cluster will be unable to provide services.




######################keepalived############################################################################################################################################################################
Usually Dual-machine hot standby means that both machines are running, but not both machines are providing services at the same time.
When the one that provides the service fails, the other one will automatically take over and provide the service immediately, and the switching time is very short.
The working principle of keepalived is VRRP (Virtual Router Redundancy Protocol) virtual routing redundancy protocol.
There are two important concepts in VRRP: VRRP routers and virtual routers, master routers and backup routers.
A VRRP router is a router that runs VRRP and is a physical entity. A virtual router is created by the VRRP protocol and is a logical concept. A group of VRRP routers work together to form a virtual router. There is an election mechanism in Vrrp, which is used to elect the route that provides services, that is, the master route, and the others become backup routes. When the master route fails, a master route will be re-elected from the backup route to continue working to ensure uninterrupted service


wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
./configure --prefix=/usr/local/keepalived
make
make install
set virtual ip 192.168.139.201


cat /usr/local/keepalived/etc/keepalived/keepalived.conf
on one machine
######### ########
global_defs {
    router_id NodeA
}

vrrp_instance VI_1 {
    state MASTER #Set as the main server 
    interface br-ex #Monitor network interface 
    virtual_router_id 51 #Main and standby must have the same 
    priority 100 #(The main and standby machines take different priorities, the host value is larger, the backup machine value is smaller, the larger the value, the higher the priority) 
    advert_int 1 #VRRP Multicast broadcast cycle seconds
    authentication {
        auth_type PASS #VRRP authentication method, the master and backup must be the same 
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.139.201/20 #VRRP HA virtual address 
    }
}
###############

On another machine
#################
global_defs {
    router_id NodeB
}

vrrp_instance VI_1 {
    state BACKUP
    interface br-ex
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }  
    virtual_ipaddress {
        192.168.139.201/20
    }  
}
###############
Start
/usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived /etc/keepalived/keepalived.conf

ifconfig br-ex
ip a show br-ex
to see the ip address
ip link show br-ex
to see the mac address

ping 192.168.139.201
arp -a to see the mac address of 201

and then kill the keepalived on the master node
Then arp -a to see that the mac address of 201 has changed to the mac of the backup node, and tailf /var/log/message

on both machines to see the log changes


################## ########haproxy################
yum install haproxy -y

  haproxy The configuration is divided into five parts, as follows:
1. global: The parameter is process-level, usually related to the operating system. These parameters are generally only set once. If the configuration is correct, there is no need to modify them again.
2. defaults: configure the default parameters. These parameters can be used for frontend, backend, and Listen components
. 3. frontend: the front-end virtual node that receives the request. Frontend can More rules directly specify the backend that uses the backend. 4. Backend: The configuration
of the backend service cluster is a real server. One Backend corresponds to one or more entity servers .
/haproxy/haproxy.cfg Comment out frontend and backend first, use tcp ################### defaults     #mode http     mode tcp     log global # option httplog # option dontlognull # option http -server-close














#    option forwardfor       except 127.0.0.0/8
#    option                  redispatch
##############
最后加
##################
listen test1  0.0.0.0:6380
        server s1 192.168.139.161:7000  check inter 2000 rise 2 fall 5
        server s2 192.168.139.161:7001  check inter 2000 rise 2 fall 5
        server s3 192.168.139.161:7002  check inter 2000 rise 2 fall 5
        server s4 192.168.139.193:7003  check inter 2000 rise 2 fall 5
        server s5 192.168.139.193:7004  check inter 2000 rise 2 fall 5
        server s6 192.168.139.193:7005  check inter 2000 rise 2 fall 5
##############

./redis-cli -h 192.168.139.161 -c -p 6380

###################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
_
_ .139.201 -c -p 6380

hangs any node can be accessed normally











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326401514&siteId=291194637