Memcached practice-master-slave based on repcached
Memcached does not prevent spof, because to ensure the high availability of Memcached services, we need to use external tools to implement high availability. This article introduces the Repcached tool, which enables you to complete the master and slave functions of the Memcached service.
Repcached is a tool developed by the Japanese to implement the Memcached replication function. The master-slave solution is a single-master, single-slave solution, which does not support multiple-master, multiple-slave. However, it features that the master and slave nodes can read and write each other to achieve mutual synchronization.
If the master node breaks down, the slave node will soon detect the disconnection, and then it will automatically switch to the listening status (listen) to become the master node, and wait for the new slave node to join.
Assuming that after the master node is restored, we can only manually start it from the node. The original master node cannot be a new master node unless the new master node fails. This means that Memcached master-slave based on Repcached does not have the preemption function for the master node.
If the slave node breaks down, the master node will soon detect the disconnection, and then it will switch to the listening status (listen) Again, and wait for the new slave node to join.
If both the master and slave nodes are down, the data will be lost! Therefore, this is a short board of Repcached, but later we can use other tools to make up for this shortcoming.
OK. Here is a brief introduction. The following experiment shows how to deploy the Memcached master-slave architecture based on Repcached and how to test and manage it in the future.
Previous Article: Memcached standalone deployment-Single Instance/multiple instances
Environment:
- CentOS 6.5 x86_64-bit installation is minimized, and the system has been optimized.
- Selinux is disabled, and iptables is in unrestricted mode.
- Host Name: nolinux
- Source code package storage location:/usr/local/src
- Libevent version: 2.0.21
- Memcached version: 1.4.20
Memcached installation and startup script
Performance problems of using Memcached in PHP
Install Memcached in Ubuntu and its command explanation
Install and apply Memcached
Use Nginx + Memcached's small image storage solution
Getting started with Memcached
I. Basic Environment preparation
[Root @ master ~] # Yum-y install gcc-c ++
Ii. Install Memcached
1. Install libevent
[Root @ master ~] # Cd/usr/local/src
[Root @ master src] # wget http://code.taobao.org/p/nolinux/src/memcached/src/libevent-2.0.21-stable.tar.gz? Orig
[Root @ master src] # tar zxvf libevent-2.0.21-stable.tar.gz
[Root @ master src] # cd libevent-2.0.21-stable
[Root @ master libevent-2.0.21-stable] #./configure -- prefix =/usr
[Root @ master libevent-2.0.21-stable] # make
[Root @ master libevent-2.0.21-stable] # make install
[Root @ master libevent-2.0.21-stable] # ll/usr/lib/libevent * # After libevent is installed, the following content will appear in the/usr/lib directory
Lrwxrwxrwx 1 root 21 Aug 11/usr/lib/libevent-2.0.so.5-> libevent-2.0.so.5.1.9
-Rwxr-xr-x 1 root 968690 Aug 11/usr/lib/libevent-2.0.so.5.1.9
-Rw-r -- 1 root 1571802 Aug 11 13:49/usr/lib/libevent.
Lrwxrwxrwx 1 root 26 Aug 11/usr/lib/libevent_core-2.0.so.5-> libevent_core-2.0.so.5.1.9
-Rwxr-xr-x 1 root 585225 Aug 11/usr/lib/libevent_core-2.0.so.5.1.9
-Rw-r -- 1 root 978482 Aug 11 13:49/usr/lib/libevent_core.a
-Rwxr-xr-x 1 root 970 Aug 11/usr/lib/libevent_core.la
Lrwxrwxrwx 1 root 26 Aug 11 13:49/usr/lib/libevent_core.so-> libevent_core-2.0.so.5.1.9
Lrwxrwxrwx 1 root 27 Aug 11/usr/lib/libevent_extra-2.0.so.5-> libevent_extra-2.0.so.5.1.9
-Rwxr-xr-x 1 root 404852 Aug 11/usr/lib/libevent_extra-2.0.so.5.1.9
-Rw-r -- 1 root 593392 Aug 11 :49/usr/lib/libevent_extra.a
-Rwxr-xr-x 1 root 977 Aug 11/usr/lib/libevent_extra.la
Lrwxrwxrwx 1 root 27 Aug 11 13:49/usr/lib/libevent_extra.so-> libevent_extra-2.0.so.5.1.9
-Rwxr-xr-x 1 root 935 Aug 11/usr/lib/libevent. la
Lrwxrwxrwx 1 root 30 Aug 11/usr/lib/libevent_pthreads-2.0.so.5-> libevent_pthreads-2.0.so.5.1.9
-Rwxr-xr-x 1 root 18430 Aug 11/usr/lib/libevent_pthreads-2.0.so.5.1.9
-Rw-r -- 1 root 18670 Aug 11 :49/usr/lib/libevent_pthreads.a
-Rwxr-xr-x 1 root 998 Aug 11/usr/lib/libevent_pthreads.la
Lrwxrwxrwx 1 root 30 Aug 11 13:49/usr/lib/libevent_pthreads.so-> libevent_pthreads-2.0.so.5.1.9
Lrwxrwxrwx 1 root 21 Aug 11/usr/lib/libevent. so-> libevent-2.0.so.5.1.9
[Root @ master libevent-2.0.21-stable] # cd ..
2. Install memcached
[Root @ master src] # tar zxvf memcached-1.4.20.tar.gz
[Root @ master src] # cd memcached-1.4.20
[Root @ master memcached-1.4.20] #./configure -- with-libevent =/usr
[Root @ master memcached-1.4.20] # wget http://code.taobao.org/p/nolinux/src/memcached/src/memcached-1.4.20.tar.gz? Orig
[Root @ master memcached-1.4.20] # make
[Root @ master memcached-1.4.20] # make install
[Root @ master memcached-1.4.20] # cd ..
[Root @ master src] # ll/usr/local/bin/memcached # Put memcached to/usr/local/bin/memcached after the installation is complete
-Rwxr-xr-x 1 root 341907 Aug 11/usr/local/bin/memcached
Note: if an error is reported in the middle, check the error information carefully and configure or add the corresponding library or path according to the error information.
Iii. repcached Installation
Method 1: Use repcached
[Root @ master src] # wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
[Root @ master src] # tar zxf memcached-1.2.8-repcached-2.2.tar.gz
[Root @ master src] # cd memcached-1.2.8-repcached-2.2
Method 2: Use the patch version
[Root @ master memcached-1.2.8-repcached-2.2] # wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
[Root @ master memcached-1.2.8-repcached-2.2] # gzip-cd ../repcached-2.2-1.2.8.patch.gz | patch-p1
[Root @ master memcached-1.2.8-repcached-2.2] #./configure -- enable-replication
[Root @ master memcached-1.2.8-repcached-2.2] # make
[Root @ master memcached-1.2.8-repcached-2.2] # make install
[Root @ master memcached-1.2.8-repcached-2.2] # cd ..
For the above operations, we need to operate on both the master and slave nodes! Here I only use the deployment of the master node as an example! Remember!
Iv. STARTUP configuration
1. Start the master
[Root @ master ~] # Memcached-v-d-p 11211-l 192.168.0.102-u root-P/tmp/memcached1.pid
[Root @ master ~] # Replication: listen
[Root @ master ~] # Replication: accept
2. Start salve
[Root @ slave src] # memcached-v-d-p 11211-l 192.168.0.103-u root-x 192.168.0.102-P/tmp/memcached1.pid
[Root @ slave src] # replication: connect (peer = 192.168.0.102: 11212)
Replication: marugoto copying
Replication: start
[Root @ slave src] #
3. Return to the master node
[Root @ master ~] # Replication: accept # after the master starts normally
V. Test
Because our master node and slave node's memcached service are both mounted and the listening is normal, all of the following test operations are performed on the master node.
[Root @ master ~] # Telnet 192.168.0.102 11211 # connect to our master node and add a record
Trying 192.168.0.102...
Connected to 192.168.0.102.
Escape character is '^]'.
Set key 0 0 6
Sunsky
STORED
Quit
Connection closed by foreign host.
[Root @ master ~] # Telnet 192.168.0.103 11211 # connect to our slave node and check whether the records of the master node are synchronized.
Trying 192.168.0.103...
Connected to 192.168.0.103.
Escape character is '^]'.
Get key
VALUE key 0 6
Sunsky
END
Quit
Connection closed by foreign host.
[Root @ master ~] # Pkill memcached # Now, kill the memcached process on our master node
Replication: cleanup start
Replication: close
Replication: cleanup complete
[Root @ slave src] # replication: close # The standby node changes to the listening status, that is, it changes to the master node.
Replication: listen
[Root @ master ~] # Telnet 192.168.0.103 11211 # Check whether the data on the slave node still exists
Trying 192.168.0.103...
Connected to 192.168.0.103.
Escape character is '^]'.
Get key
VALUE key 0 6
Sunsky
END
Quit
Connection closed by foreign host.
[Root @ master ~] # Memcache-v-d-p 11211-l 192.168.0.102-u root-x 192.168.0.103-P/tmp/memcached. pid # Because memcached master/Slave has no preemption function, after the master recovers, it can only serve as the slave node of the existing master node [root @ master ~] # Replication: connect (peer = 192.168.0.103: 11212)
Replication: marugoto copying
Replication: start
[Root @ slave src] # replication: accept # After the above is added, the following input will be generated from the slave node, indicating that synchronization is enabled.
Replication: marugoto start
Replication: marugoto 1
Replication: marugoto owari
[Root @ master ~] # Telnet 192.168.0.102 11211 # connect to the recovered node and we can see that the data is back.
Trying 192.168.0.102...
Connected to 192.168.0.102.
Escape character is '^]'.
Get key
VALUE key 0 6
Sunsky
END
Quit
Connection closed by foreign host.
The above is our master-slave replication experiment on memcached Based on repcached. Through the experiment, we can see that we have achieved any downtime in the master and slave databases without affecting the data on the other machine.
At the end of the article, we will summarize the advantages and disadvantages of Memcached master-slave based on Repcached:
Advantages:
1. Implement cache Redundancy
2. The master and slave nodes can read and write to each other.
Disadvantages:
1. One master, one slave, one-to-one
This article permanently updates the link address: