Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memcached master master replication + Keepalived high availability architecture (included software package)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Introduction to Memcached

Memcached master replication means that the modified data in any Memcached server will be synchronized to another, but the Memcached API client cannot determine which Memcached server is connected to, so it is necessary to set the VIP (virtual IP) address to provide the Memcached API client with a connection. You can use Keepalived to generate VIP addresses to connect to the primary Memcached server and provide a highly available architecture.

The replication feature of Memcached allows multiple Memcached to replicate each other (bidirectional replication, both master and slave are readable and writable), which can solve the disaster recovery problem of Memcached.

Keepalive constantly detects port 11211 of the Memcached master server. If it detects downtime or panic of the Memcached server, it will move the VIP from the master server to the slave server, thus achieving high availability of Memcached.

Memcached High availability Architecture

Package connection: link: https://pan.baidu.com/s/10yic_9NDmhBbWCVhERlgPw password: gf1l

Lab environment: two Memcached servers and one client, as shown below.

Name

Main software packages of IP address operating system

Memached 1 (Master)

192.168.91.148

VIP: 192.168.91.188

Centos 7libevent-2.1.8-stable.tar.gz

Memcached-1.5.6.tar.gz

Magent-0.5.tar.gzMemached 2 (from) 192.168.91.150

VIP: 192.168.91.188Centos 7libevent-2.1.8-stable.tar.gz

Memcached-1.5.6.tar.gz client 192.168.91.149Centos 7

Build Memcached master master replication architecture

1. First install the required compilation environment and turn off the firewall

[root@localhost ~] # systemctl stop firewalld.service

[root@localhost ~] # setenforce 0

[root@master memcached] # yum install gcc gcc-c++ make-y

two。 Decompression package

[root@master memcached] # tar zxvf libevent-2.1.8-stable.tar.gz-C / opt/ libevent event Notification Library

[root@master memcached] # tar zxvf memcached-1.5.6.tar.gz-C / opt/

[root@master memcached] # mkdir / opt/magent / / create a magent directory

[root@master memcached] # tar zxvf magent-0.5.tar.gz-C / opt/magent/

Ketama.c

Magent.c

Ketama.h / / magent proxy plug-in

Makefile

3. Compile and install Libevent first, and then install Memcached

[root@master memcached] # cd / opt/libevent-2.1.8-stable/

[root@master libevent-2.1.8-stable] #. / configure-prefix=/usr

[root@master libevent-2.1.8-stable] # make & & make install

[root@master libevent-2.1.8-stable] # cd / opt/memcached-1.5.6/

[root@master memcached-1.5.6] #. / configure-with-libevent=/usr

[root@master memcached-1.5.6] # make & & make install

For the installation of the above steps, the master-slave Memcached is basically the same, except that the slave server does not need to decompress and install the Magent agent.

4. Modify the magent agent plug-in in the Memcached master server

[root@master memcached-1.5.6] # cd / opt/magent/

[root@master magent] # ls

Ketama.c ketama.h magent.c Makefile

[root@master magent] # vim ketama.h

# ifndef SSIZE_MAX / / add at the beginning of the file

# define SSIZE_MAX 32767

[root@master magent] # vim Makefile

LIBS =-levent-lm / / add-lm at the end of the first line (not the number 1)

After modifying the agent plug-in, directly make, you will see an executable file magent

[root@master magent] # make

Gcc-Wall-O2-g-c-o magent.o magent.c

Gcc-Wall-O2-g-c-o ketama.o ketama.c

Gcc-Wall-O2-g-o magent magent.o ketama.o-levent-lm

[root@master magent] # ls

Ketama.c ketama.h ketama.o magent magent.c magent.o Makefile

5. By installing openssh-clients service on the Memcached master server, you can copy the configuration of magent on the master server to the slave server

[root@master magent] # yum install openssh-clients-y

[root@master magent] # cp magent / usr/bin/ copy the magent configuration file on the master server to / usr/bin/ first

[root@master magent] # scp magent root@192.168.91.150:/usr/bin/ slave server address and directory

The authenticity of host '192.168.91.150 (192.168.91.150)' can't be established.

ECDSA key fingerprint is SHA256:ABSTPGOHvqKvUsfwD/uf5ESPpd × × RjvucRpzMqcUuzI.

ECDSA key fingerprint is MD5:f5:3a:8c:8b:1e:d5:a3:33:24:32:03:2d:4d:3e:e8:68.

Are you sure you want to continue connecting (yes/no)? Yes

Warning: Permanently added '192.168.91.150' (ECDSA) to the list of known hosts.

Root@192.168.91.150's password: / / login password from the server

Magent 100% 112KB 4.3MB/s 00:00

6. Install and configure Keepalive (both master and slave servers must be installed)

[root@master magent] # yum install keepalived-y

(1)。 Configure the primary Keepalived

[root@master magent] # vim / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Vrrp_script magent {/ / add function, named magent, called with magnt

Script "/ opt/shell/magent.sh" / / script location

Interval 2 / / the interval between testing scripts is 2s

}

Global_defs {/ / Global Settings

Notification_email {

Acassen@firewall.loc

Failover@firewall.loc

Sysadmin@firewall.loc

}

Notification_email_from Alexandre.Cassen@firewall.loc

Smtp_server 192.168.200.1

Smtp_connect_timeout 30

Router_id MAGENT_HA / / routing means that the host can never be the same

}

Vrrp_instance VI_1 {

State MASTER / / Master server status is: MASTER

Change the name of the interface ens33 / / Nic to ens33 (centos 7)

Virtual_router_id 51 / / Virtual routing ID, master and slave cannot be the same

Priority 100 / / priority, master is higher than slave

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Track_script {

Magent / / call function name magent

}

Virtual_ipaddress {

192.168.91.188 / / define VIP address

}

}

(2) configure slave Keepalived and import the configuration file of keepalied into slave server on the master server.

[root@master magent] # cd / etc/keepalived/

[root@master keepalived] # scp keepalived.conf root@192.168.91.150:/etc/keepalived/

Root@192.168.91.150's password:

Keepalived.conf 100% 660 2.0KB/s 00:00

Back to modifying the configuration file of keepalived from the server, the content of the master-slave Keepalived configuration file is similar, which can be copied and modified directly. Here are only the differences sorted out

[root@localhost keepalived] # vim keepalived.conf

Router_id MAGENT_HB / / routing means different from the host

}

Vrrp_instance VI_1 {

State BACKUP / / status is BACKUP

Interface ens33

Virtual_router_id 52 / / Virtual routing ID is different from the host

Priority 90 / / priority is less than master

7. In the master-slave setup script, (a function named magent is added in the master-slave Keepalived)

[root@master keepalived] # mkdir / opt/shell

[root@master keepalived] # cd / opt/shell/

[root@master shell] # vim magent.sh

#! / bin/bash

K = `ps-ef | grep keepalived | grep-v grep | wc-l`

If [$k-gt 0]; then

Magent-u root-n 51200-l 192.168.91.188-p 12000-s 192.168.91.148purl 11211-b 192.168.91.150purl 11211

Else

Pkill-9 magent

Fi

The parameters are explained as follows

-n 51200 / / define the maximum number of connections for users

-l 192.168.91.188 / / specify virtual IP

-p 12000 / / designated port

-s / / specify the main cache server

-b / / specifies that from the cache server

8. Give the execution permission to start the keepalived service

[root@master shell] # chmod + x magent.sh

[root@master shell] # systemctl start keepalived.service / / start the service

[root@master shell] # netstat-ntap | grep 12000 / / View the port and confirm that magent is running

Tcp 00 192.168.91.188 Virgo 12000 0.0.0.0 * LISTEN 47938/magent

Check the log to verify the master and slave

Vim / var/log/messages

Use the ip addr command to confirm whether the drift address is in effect.

The steps from the server are the same.

[root@localhost shell] # netstat-ntap | grep 12000

Tcp 00 192.168.91.188 Virgo 12000 0.0.0.0 * LISTEN 66801/magent

9. Start memcached on the master and slave servers respectively

[root@master shell] # memcached-m 512k-u root-d-l 192.168.91.148-p 11211

[root@master shell] # netstat-ntap | grep 11211

Tcp 0 0 192.168.91.148 11211 0.0.0.0 * LISTEN 51398/memcached

From the server

[root@localhost shell] # memcached-m 512k-u root-d-l 192.168.91.150-p 11211

[root@localhost shell] # netstat-ntap | grep 11211

Tcp 0 0 192.168.91.150 11211 0.0.0.0 * LISTEN 54741/memcached

10. Test verification

(1) when the master server is in self-test to connect to the Memcached cache database, you need to install telnet

[root@master shell] # yum install telnet-y

[root@master shell] # telnet 192.168.91.148 11211 / / self-test connection

Trying 192.168.91.148...

Connected to 192.168.91.148.

Escape character is'^]'. / / enter the cache database

Quit

Connection closed by foreign host.

(2) testing on the slave server

[root@localhost shell] # telnet 192.168.91.150 11211

Trying 192.168.91.150...

Connected to 192.168.91.150.

Escape character is'^]'.

Quit

Connection closed by foreign host.

[root@localhost shell] #

(3) install telnet on the client, log in using virtual IP, and add statements to see if you can see it on the master-slave server.

[root@localhost ~] # systemctl stop firewalld.service

[root@localhost ~] # setenforce 0

[root@localhost ~] # yum install telnet-y

[root@localhost] # telnet 192.168.91.188 12000

Trying 192.168.91.188...

Connected to 192.168.91.188.

Escape character is'^]'.

Add username 0 07 / / add a key value data

1234567

STORED

Quit

Connection closed by foreign host.

[root@localhost ~] #

(4) distributed login master slave memcached to view the added content

[root@master shell] # telnet 192.168.91.148 11211

Trying 192.168.91.148...

Connected to 192.168.91.148.

Escape character is'^]'.

Get username / / query key value data

VALUE username 0 7

1234567

END

[root@localhost shell] # telnet 192.168.91.150 11211

Trying 192.168.91.150...

Connected to 192.168.91.150.

Escape character is'^]'.

Get username

VALUE username 0 7

1234567

END

Summary:

(1) Memcached is a distributed in-memory object caching system because all data is stored in memory, which is usually used for website acceleration.

(2) Memcached distributed implementation is not implemented on the server side but on the client side.

(3) Memcached can realize the high reliability of Memcached service through Keepalived.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report