Dry, memcached+keepalived realize master replication and high availability, detailed!

Source: Internet
Author: User
Tags memcached

Keepalived+memcached Primary master replication High-availability Description:

The client connects to the VIP address between two servers if the backend server fails to automatically switch.

KeepAlive principle: 1. Fault detection:

The role of keepalived is to detect whether the state of the memcached server is normal.

2. Master-Slave Switching:

Keepalived The VIP can be moved from the primary server to the slave server if the memcached service is detected as down or crashed

3.Keepalived How to discover memcached exceptions:

1) keepalived generates a virtual IP on the memcached master server

2) keepalived can be continuously detected memcached the main server 11211 port is working properly, if found memcached down, virtual IP from the primary server to move from the server

High-availability Architecture scenarios:

If you have more memcached distributed nodes, you do not need to do a highly available replication-based architecture at all.

Highly available replication-based architectures are typically used in memcached single nodes for caching or session.

Introduction to the deployment environment:
操作系统:Centos 7 64位Node1:192.168.100.12  主节点Node2:192.168.100.13  备节点
1. Environment deployment:

As with two server deployments, you can deploy as follows.

(1)安装memcached依赖libevent[[email protected] ~]# yum -y install libevent libevent-devel(2)安装c语言编译环境[[email protected] ~]# yum -y install c++ gcc(3)安装具有复制功能的memcached版本wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gztar zxvf memcached-1.2.8-repcached-2.2.tar.gzcd memcached-1.2.8-repcached-2.2./configure  --prefix=/usr/local/memcached_replication  --enable-replicationmakemake install

Note: Compile Error resolution

To modify the Memcached.c file:

Before modification

[[email protected] memcached-1.2.8-repcached-2.2]# vim memcached.c  56 #ifndef IOV_MAX  57 #if defined(__FreeBSD__) || defined(__APPLE__)  58 # define IOV_MAX 1024  59 #endif  60 #endif

After modification

56 #ifndef IOV_MAX57 # define IOV_MAX 102458 #endif

The installation complete directory structure is as follows:

[[email protected] ~]# tree /usr/local/memcached_replication//usr/local/memcached_replication/├── bin│   ├── memcached│   └── memcached-debug└── share    └── man        └── man1            └── memcached.1

Query memcached copy version of the command help is just more than the normal memcached one-x option, that is to specify the replication of the MEMCACHED server hostname or IP address

[[email protected] ~]# /usr/local/memcached_replication/bin/memcached -h-x <ip_addr>  hostname or IP address of peer repcached
2. Start memcached with master-slave replication architecture

1) Create memcached user

[[email protected] ~]# useradd -r -s /sbin/nologin memcached

2) Start memcached with copy function

NODE1 node Start:

[[email protected] ~]# cd /usr/local/memcached_replication/bin/[[email protected] bin]# ./memcached -d -u memcached -m 512 -x 192.168.100.13[[email protected] bin]# ps aux | grep memmemcach+  20208  0.0  0.0  11704   872 ?        Ss   23:14   0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.13

Node2 node Start:

[[email protected] ~]# cd /usr/local/memcached_replication/bin/[[email protected] bin]# ./memcached -d -u memcached -m 512 -x 192.168.100.12[[email protected] bin]# ps aux | grep memcachedmemcach+  12162  0.0  0.0  11648   872 ?        Ss   18:22   0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.12
To verify the replication schema:

Inserting data on a node1 node

[[email protected] ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is ‘^]‘.set username 0 0 9node1xmfbSTOREDget usernameVALUE username 0 9node1xmfbEND

Node2 node verifies that data is present

[[email protected] ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is ‘^]‘.get usernameVALUE username 0 9node1xmfbEND

If the node 1 is written to the data node 2 can see that the memcached master-slave replication architecture is not a problem, you can use the memcached master-slave replication based on keepalived to generate VIP.

3.keepalived+memcached High-availability architecture deployment and configuration 1) keepalived package Installation
yum install keepalived* -y
2) configuration keepalived

The keepalived master node configuration file settings are as follows:

[[email protected] bin]# cat/etc/keepalived/keepalived.confglobal_defs {router_id memcached_replication #路由标识, Master-Slave consistency}vrrp_instance mem_1 {#定义VRRP热备实例 state MASTER #MASTER表示主节点 interface eth0 #承              The physical interface of the VIP address virtual_router_id #虚拟路由器的ID号, the main and the same priority #优先级, the higher the value of the greater the precedence Advert_int 1 #通知间隔秒数 (Heartbeat) Nopreempt #不主动抢占资源, only the master or high priority server settings authentication {#认证信息配置, the master-slave server Consistent Auth_type PASS #设置认证类型为密码 auth_pass asdfgh #设置认证的密码}# define what a VIP is Virtual_ipaddres s {192.168.100.100}}# VIP fault detection virtual_server 192.168.100.100 11211 {deploy_loop 6 protocol TCP # Config main service Service address Port Real_server 192.168.100.10 11211 {weight 3 notify_down/shell/memcached.sh # when memcached, execute keep alived Service Stop} tcp_check {connect_timeout 3 #连接超时 nb_get_retry 3 #重试次数 delay_be    Fore_retry 3 #重试时间间隔    Connect_port 11211 #连接端口}} 

The

keepalived node configuration file settings are as follows:

[[email protected] bin]# cat/etc/keepalived/keepalived.confglobal_defs {router_id memcached_replication #路由标识,            Master-Slave consistency}vrrp_instance mem_1 {#定义VRRP热备实例 state BACKUP #MASTER表示主节点 interface eth0             #承载VIP地址的物理接口 virtual_router_id #虚拟路由器的ID号, the master has the same priority #优先级, the higher the value the greater the precedence Advert_int 1 #通知间隔秒数 (Heartbeat) Nopreempt #不主动抢占资源, only the master or high priority server settings authentication {#认证信息配置, the master-slave server maintains a To Auth_type PASS #设置认证类型为密码 auth_pass asdfgh #设置认证的密码}# define what the VIP is virtual_ipaddress {192.168.100.100}} Virtual_server 192.168.100.100 11211 {deploy_loop 6 protocol TCP # Configure Primary server address port Real_server 192.168.100.11 1121        1 {weight 3 notify_down/shell/memcached.sh} tcp_check {connect_timeout 3 #连接超时 Nb_get_retry 3 #重试次数 delay_before_retry 3 #重试时间间隔 connect_port 11211 #连接端口}}

Two nodes are added script: if the monitoring of the memcached fault, then the local keepalived, to achieve VIP drift

[[email protected] ~]# cat /shell/memcached.shsystemctl stop keepalived[[email protected] ~]# chmod +x /shell/memcached.sh
3) Two nodes start keepalived service
[[email protected] ~]# systemctl start keepalived.service
4. Highly Available validation

Verify that the VIP is started

[[email protected] ~]# ip address show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:85:36:db brd ff:ff:ff:ff:ff:ff    inet 192.168.100.12/24 brd 192.168.100.255 scope global dynamic eth0       valid_lft 20059sec preferred_lft 20059sec    inet 192.168.100.100/32 scope global eth0       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe85:36db/64 scope link       valid_lft forever preferred_lft foreverYou have new mail in /var/spool/mail/root

Force the primary node memcached process to be closed, you can see that the VIP address is missing

[[email protected] ~]# ps aux | grep keepliavedroot      45424  0.0  0.0 112648   980 pts/0    S+   11:24   0:00 grep --color=auto keepliaved[[email protected] ~]# ps aux | grep memmemcach+  45419  0.0  0.1  12672  1924 ?        Ss   11:22   0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.13root      45427  0.0  0.0 112648   976 pts/0    S+   11:24   0:00 grep --color=auto mem[[email protected] ~]# kill -9 45419[[email protected] ~]# ip addr show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:85:36:db brd ff:ff:ff:ff:ff:ff    inet 192.168.100.12/24 brd 192.168.100.255 scope global dynamic eth0       valid_lft 16561sec preferred_lft 16561sec    inet6 fe80::20c:29ff:fe85:36db/64 scope link       valid_lft forever preferred_lft forever

At the point of view from the node, the memcached address appears again.

[[email protected] ~]# ip addr show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:9e:47:61 brd ff:ff:ff:ff:ff:ff    inet 192.168.100.13/24 brd 192.168.100.255 scope global dynamic eth0       valid_lft 14759sec preferred_lft 14759sec    inet 192.168.100.100/32 scope global eth0       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe9e:4761/64 scope link       valid_lft forever preferred_lft forever

Operation success, thank you to watch!

Dry, memcached+keepalived realize master replication and high availability, detailed!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.