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.
Environment: centos 6.5 x86_64-bit installation is minimized. The SELinux is disabled after basic optimization, and iptables is in unrestricted mode. Host Name: nolinux source code package storage location:/usr/local/srclibevent version: 2.0.21memcached version: 1.4.20
I. Basic Environment preparation
[[email protected] ~]# yum -y install gcc gcc-c++
Ii. Install memcached
1. Install libevent
[[Email protected] ~] # Cd/usr/local/src [[email protected] SRC] # wget http://code.taobao.org/p/nolinux/src/memcached/src/libevent-2.0.21-stable.tar.gz? Orig [[email protected] SRC] # tar zxvf libevent-2.0.21-stable.tar.gz [[email protected] SRC] # cd libevent-2.0.21-stable [[email protected] libevent-2.0.21-stable] #. /configure -- prefix =/usr [[[email protected] libevent-2.0.21-stable] # Make [[[email protected] libevent-2.0.21-stable] # make install [[email protected] 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/usr/lib/libevent. A 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/usr/lib/libevent_core.a-rwxr-XR-x 1 Root 970 Aug 11/usr/lib/libevent_core.la lrwxrwxrwx 1 Root 26 Aug 11/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/usr/lib/libevent_extra.a-rwxr-XR-x 1 Root 977 Aug 11/usr/lib/libevent_extra.la lrwxrwxrwx 1 Root 27 Aug 11/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/usr/lib/libevent_pthreads.a-rwxr-XR-x 1 Root 998 Aug 11/usr/lib/libevent_pthreads.lrla wxrwxrwx 1 Root 30 Aug 11/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 [[email protected] libevent-2.0.21-stable] # CD ..
2. Install memcached
[[Email protected] SRC] # tar zxvf memcached-1.4.20.tar.gz [[email protected] SRC] # cd memcached-1.4.20 [[email protected] memcached-1.4.20] #. /configure -- With-libevent =/usr [[email protected] memcached-1.4.20] # wget http://code.taobao.org/p/nolinux/src/memcached/src/memcached-1.4.20.tar.gz? Orig [[email protected] memcached-1.4.20] # Make [[[email protected] memcached-1.4.20] # make install [[[email protected] memcached-1.4.20] # CD .. [[email protected] SRC] # ll/usr/local/bin/memcached # After the installation is complete, put memcached to/usr/local/bin/memcached-rwxr-XR-x 1. root 341907 Aug 11 :52/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
[[email protected] src]# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz[[email protected] src]# tar zxf memcached-1.2.8-repcached-2.2.tar.gz[[email protected] src]# cd memcached-1.2.8-repcached-2.2
Method 2: Use the patch version
[[email protected] memcached-1.2.8-repcached-2.2]# wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz[[email protected] memcached-1.2.8-repcached-2.2]# gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1[[email protected] memcached-1.2.8-repcached-2.2]# ./configure --enable-replication[[email protected] memcached-1.2.8-repcached-2.2]# make[[email protected] memcached-1.2.8-repcached-2.2]# make install[[email protected] 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
[[email protected] ~]# memcached -v -d -p 11211 -l 192.168.0.102 -u root -P /tmp/memcached1.pid [[email protected] ~]# replication: listen [[email protected] ~]# replication: accept
2. Start salve
[[email protected] src]# memcached -v -d -p 11211 -l 192.168.0.103 -u root -x 192.168.0.102 -P /tmp/memcached1.pid [[email protected] src]# replication: connect (peer=192.168.0.102:11212) replication: marugoto copying replication: start [[email protected] src]#
3. Return to the master node
[[Email protected] ~] # 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.
[[Email protected] ~] # 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. [[email protected] ~] # Telnet 192.168.0.103 11211 # connect to our slave node and check whether the records of the master node are synchronized to 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. [[email protected] ~] # Pkill memcached # Now, kill the memcached process replication: cleanup start replication: Close replication: cleanup complete [[email protected] SRC] # replication: close # The standby node changes to the listening status, that is, the master node replication: Listen [[email protected] ~] # Telnet 192.168.0.103 11211 # Check whether the data on the slave node still has 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. [[email protected] ~] # 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 [[email protected] ~] # Replication: connect (peer = 192.168.0.103: 11212) replication: marugoto copying replication: Start [[email protected] SRC] # replication: accept # after adding the above, the following input is generated from the slave node, indicating that synchronization replication is Enabled: marugoto start replication: marugoto 1 replication: marugoto Owari [[email protected] ~] # Telnet 192.168.0.102 11211 # connect to the recovered node. You can see that the data returned is 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. the cache redundancy function can be realized. 2. The master and slave nodes can read and write each other. Disadvantages: 1. One master and one slave can be used as much as possible.
This article is from the not only Linux blog, please be sure to keep this source http://nolinux.blog.51cto.com/4824967/1544931
Memcached practice-master-slave based on repcached