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

How to build distributed Redis Cluster with Docker

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use Docker to build a distributed Redis cluster". In daily operation, I believe many people have doubts about how to use Docker to build a distributed Redis cluster. The editor consulted all kinds of materials and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to use Docker to build a distributed Redis cluster". Next, please follow the editor to study!

1. Download and configure the basic image

I use a very good phusion image as the base image, which adds a lot of features that Docker ignores, such as starting services in order, and so on. For more information about this image, you can click here.

First, let's use Docker to pullphusion the image (it is recommended to use a higher version of Docker download, but there will be problems with the lower version).

Root@server:/home/sam# docker pull phusion/baseimage

After the download is complete, you can see the newly downloaded image through the docker images command.

Root@server:/home/sam# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

Phusion/baseimage 0.9.15 cf39b476aeec 3 months ago 289.4 MB

Phusion/baseimage latest cf39b476aeec 3 months ago 289.4 MB

This image is very useful. You can specify the startup order of a specific service when the container starts. Here I want to add a SSH password login function to this image instead of using SSH key. Because it runs locally, you don't have to worry too much about security. We need to create an instance of phusion, then log in through SSH key, modify the configuration, restart SSH, and then we can log in using root.

First, create a new container with the phusion base image.

Root@server:/home/sam# docker run-d-name redis phusion/baseimage / sbin/my_init

-- enable-insecure-key

/ sbin/my_init allows phusion to start your service when the container starts. Enable-insecure-key allows us to use 'insecure key'ssh to enter new containers.

Now that we have deployed the container, we need to get its IP address, using the docker inspect command.

Root@server:/home/sam# docker inspect redis | grep IPA

"IPAddress": "172.17.0.46"

Next, download 'insecure key' and use it to log in to the container.

Root@server:/home/sam# curl-o insecure_key-fSL https://github.com/phusion/baseimage-docker/raw/master/image/insecure_key

Root@server:/home/sam# chmod 600 insecure_key

Root@server:/home/sam# ssh-I insecure_key root@

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

ECDSA key fingerprint is aa:bb:cc:xx:xx:xx:xx:xx:xx:xx:xx:yy:zz:04:bf:04.

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

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

Root@c36b4bba7dd4:~#

Congratulations, you are now SSH into the container! Next you need to modify the SSH so that we no longer need 'insecure key'. To do this, open / etc/ssh/sshd_config' to find the following line and remove the comment.

PermitRootLogin yes

Save the file, and then set the password for root.

Root@c36b4bba7dd4:~# passwd

Enter new UNIX password:

Retype new UNIX password:

Passwd: password updated successfully

Root@c36b4bba7dd4:~#

We have done some preparatory work above, and then we will install Redis in this container.

2 install Redis

Now that we have the configured image, we need to download and install Redis. You can download the Redis you want from here. In our example, 3.0.0 RC1 is used at the download address: https://github.com/antirez/red. Ar.gz .

Note: you also need to install wget, gcc, make and other tools. You can use the following command:

Apt-get update # update system

Apt-get upgrade # upgrade the system

Apt-get install wget gcc make# installs wget, gcc, make

First, we download Redis 3.0.0 RC1.

Root@e3919192d9e3:/home# wget https://github.com/antirez/redis/archive/3.0.0-rc1.tar.gz

Unpack the package and make it.

Root@e3919192d9e3:/home# tar-zxvf 3.0.0-rc1.tar.gz

Root@e3919192d9e3:/home# cd redis-3.0.0-rc1/

Root@e3919192d9e3:/home/redis-3.0.0-rc1# make

Finally, we move several executable commands from the compiled code to / usr/bin/

Root@e3919192d9e3:/home/redis-3.0.0-rc1# cd src

Root@e3919192d9e3:/home/redis-3.0.0-rc1/src# mv redis-cli redis-server redis-sentinel / usr/bin/

Root@e3919192d9e3:/home/redis-3.0.0-rc1/src# cd..

Root@e3919192d9e3:/home/redis-3.0.0-rc1# mkdir-p / etc/redis/

Root@e3919192d9e3:/home/redis-3.0.0-rc1# cp redis.conf / etc/redis/redis.conf

Now open the file / etc/redis/redis.conf, find the 'daemonize no'' change to 'daemonize yes',' and start it!

Root@e3919192d9e3:/home/redis-3.0.0-rc1/src# redis-server / etc/redis/redis.conf

Root@e3919192d9e3:/home/redis-3.0.0-rc1/src#

Well, Redis is now installed and running in the container, using the configuration file / etc/redis/redis.conf.

3. Let Docker start the Redis service when the container is started

Now that our container is running Redis and can log in using SSH, we also need to make it start the Redis service automatically when the container starts, which is quite easy to do with the phusion base image. First, because we started the container with / sibn/my_init, it will run any program we put under / etc/service/*. So, for us, all we have to do is create a directory and create a file called run in that directory, like this:

Root@e3919192d9e3:/etc/service# cd / etc/service

Root@e3919192d9e3:/etc/service# mkdir redis

Root@e3919192d9e3:/etc/service# cd redis

Root@e3919192d9e3:/etc/service/redis# nano run

In this run file, we add the following:

#! / bin/sh

Set-e

Exec / usr/bin/redis-server / etc/redis/redis.conf

Finally, remember to add executable permissions to the run file.

4. Submit the image for reuse

Now that our redis container is working well, we want to save it as a pseudo template so that we can deploy it repeatedly on Docker. It's very simple to do this, all we have to do is use 'docker commit.' This image can be mirrored to our local library, like this:

Root@server:/home/sam# docker ps-as

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE

E3919192d9e3 phusion/baseimage:0.9.15 "/ sbin/my_init-- ena 3 hours ago Up 3 hours redis 164.9 MB

Root@server:/home/sam# docker commit redis redis-cluster-node

Root@server:/home/sam# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

Redis-cluster-node latest babfb02edf4d 5 hours ago 561.2 MB

5. Deploy 3 instances of the new image

In order to explain, I will delete the previously created container so as not to get confused later. Use docker stop redis first, and then docker rm redis:

Root@server:/home/sam# docker stop redis

Redis

Root@server:/home/sam# docker rm redis

Redis

Root@server:/home/sam#

Quite simply, let's deploy three instances of the new image. We use Docker's port mapping mechanism so that we can access these instances using the IP of the Host server. The ports we associate to these instances are as follows:

Node1-hostip:7001

Node2-hostip:7002

Node3-hostip:7003

So we run redis-cli-h 192.168.0.2-p 7001, which will redirect to 172.17.0.x-p 6379, for example, we have deployed three instances:

Root@server:/home/sam# docker run-d-name node1-p 7001 redis-cluster-node / sbin/my_init

Cd1c1f96346bdf9c1cec04333c2e849992ecbc4375dcea6b30902dd9842d8c99

Root@server:/home/sam# docker run-d-name node2-p 7002 redis-cluster-node / sbin/my_init

Cd1c1f96346bdf9c1cec04333c2e849992ecbc4375dcea6b30902dd9842d8c99

Root@server:/home/sam# docker run-d-name node3-p 7003purl 6379 redis-cluster-node / sbin/my_init

Cd1c1f96346bdf9c1cec04333c2e849992ecbc4375dcea6b30902dd9842d8c99

Root@server:/home/sam#

Now we can run docker ps-as to see the three Redis containers we are running:

Root@server:/home/sam# docker ps-as

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE

A532b4ac60d9 redis-cluster-node:latest "/ sbin/my_init" 5 hours ago Up 5 hours 0.0.0.0 hours ago Up 7003-> 6379/tcp node3 2.267 MB

6c8a87a0a76a redis-cluster-node:latest "/ sbin/my_init" 5 hours ago Up 5 hours 0.0.0.0 hours ago Up 7002-> 6379/tcp node2 2.318 MB

39e02633ccf8 redis-cluster-node:latest "/ sbin/my_init" 5 hours ago Up 5 hours 0.0.0.0 hours ago Up 7001-> 6379/tcp node1 2.334 MB

Root@server:/home/sam#

Now we see three containers, named node1, node2, and node3, with a dedicated port that maps to the port of the Redis service. To test that this mapping is correct, you can log in to each Redis server using redis-cli on another machine:

Root@server:/home/sam# redis-cli-h 192.168.0.2-p 7001

Redis 192.168.0.16VR 7001 >

Root@server:/home/sam# redis-cli-h 192.168.0.2-p 7002

Redis 192.168.0.16 purl 7002 >

Root@server:/home/sam# redis-cli-h 192.168.0.2-p 7003

Redis 192.168.0.16 purl 7003 >

Root@server:/home/sam# redis-cli-h 192.168.0.2-p 7005

Could not connect to Redis at 192.168.0.2:7005: Connection refused

Not connected >

As you can see, we can log in to the corresponding Redis server using hostip+port, but not when we try an incorrect port.

Translator added: if the verification fails here, you can log in to the container to check whether the Redis service is up:

Docker exec-t-i redis bash-l

Netstat-anp | grep redis # to see if there is any network monitoring. If you do not execute the following command to start the redis service

Redis-server / etc/redis/redis.conf

6. Configure the slave node of Redis

We have three separate Redis servers, and now we want to connect them together so that we can test the scalability of the cluster, monitor the cluster, or do something else.

We use node1 as the master node and node2 and node3 as its slave nodes, which can be achieved simply by modifying the / etc/redis/redis.conf file. SSH enters the node2 and node3 nodes, modifies the configuration file, and then restarts the container:

Root@server:/home/sam# docker inspect node1 | grep IPA

"IPAddress": "172.17.0.46"

Root@server:/home/sam# docker inspect node2 | grep IPA

"IPAddress": "172.17.0.47"

Root@server:/home/sam# ssh root@172.17.0.47

Root@172.17.0.47's password:

Last login: Tue Jan 13 11:47:31 2015 from 172.17.42.1

Root@6c8a87a0a76a:~# nano / etc/redis/redis.conf

In this configuration file, all we have to do is find the line 'salveof'', uncomment it, and change it to the ip address of node1, like this:

Root@6c8a87a0a76a:~# cat / etc/redis/redis.conf | grep slaveof

# Master-Slave replication. Use slaveof to make a Redis instance a copy of

Slaveof 172.17.0.46 6379

Root@6c8a87a0a76a:~#

Finally, restart the container with the command docker restart node2, which is now a slave node of node1. To verify this, log in to node1 and node2 using redis-cli, run the command set hello world on node1, and then run get hello on node2. If configured correctly, you will get the following results:

Root@server:/home/sam# redis-cli-h 192.168.0.16-p 7001

Redis 192.168.0.12 7001 > set hello world

OK

Redis 192.168.0.12 7001 > exit

Root@server:/home/sam# redis-cli-h 192.168.0.16-p 7002

Redis 192.168.0.12 7002 > get hello

"world"

Redis 192.168.0.12 7002 > exit

Root@server:/home/sam#

You can do the same for node3.

At this point, the study on "how to build a distributed Redis cluster with Docker" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report