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 docker redis5.0 cluster Cluster

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to build a docker redis5.0 cluster cluster. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

System environment: ubuntu16.04LTS

This article uses 6 docker containers to build a stand-alone cluster test. If there are multiple containers in the actual environment, you can modify the number of containers, port number and cluster ip address accordingly, and each machine can follow the same steps below.

Pull the official image of redis

Docker pull redis:5.0

Create configuration files and data directories

Create a directory

Mkdir ~ / redis_clustercd ~ / redis_cluster

Create a new template file, sudo vim redis_cluster.tmpl, and enter the following:

# redis port port ${PORT} # turn off protected mode protected-mode no# enable cluster cluster-enabled yes# cluster node configuration cluster-config-file nodes.conf# timeout cluster-node-timeout 500 cluster node IP host mode is host IPcluster-announce-ip 10.10.100.197# Cluster node port 7000-7005cluster-announce-port ${PORT} cluster-announce-bus-port 1 ${PORT} # enable appendonly backup Mode appendonly yes# every second backup appendfsync everysec# compresses the aof file Whether to perform a synchronous operation no-appendfsync-on-rewrite no# when the current aof file size exceeds 100% of the aof file size at the time of the last rewrite, the auto-aof-rewrite-percentage 10 file size minimum before AOF file rewrite defaults 64mbauto-aof-rewrite-min-size 5120mb# turns off snapshot backup save ""

Create configuration files and data directories in batch, and the terminal runs the following command:

For port in `seq 7000 7005`; do\ mkdir-p. / ${port} / conf\ & & PORT=$ {port} envsubst

< ./redis_cluster.tmpl >

. / ${port} / conf/redis.conf\ & & mkdir-p. / ${port} / data;\ done

Start redis containers in batch

The ip address of the container is in host mode:

For port in `seq 7000 7005`; do\ docker run-d-it-- memory=1G\-v ~ / redis_cluster/$ {port} / conf/redis.conf:/usr/local/etc/redis/redis.conf\-v ~ / redis_cluster/$ {port} / data:/data\-restart always-- name redis-$ {port}-net host\-sysctl net.core.somaxconn=1024 redis:5.0 redis-server / usr/local/etc/redis/redis.conf;\ done

Here-- memeory=1G limits the amount of memory occupied by a single docker container to 1G, which will be killed by the process. Memory limited without swap... may appear at run time. This warning can be ignored. If you don't need to limit memory, you can remove the-- memeory parameter.

Create a cluster

Casually enter one of the containers:

Docker exec-it redis-7000 bash

After entering, execute the following command to create a cluster:

Redis-cli-- cluster create 10.10.100.197Vl7000 10.10.100.197VOL7001 10.10.100.197VOL7002 10.10.100.197VOL7003 10.10.100.197VOL7004 10.10.100.197VOL7005-- cluster-replicas 1

Install the redis-cli command (skip this step if available):

Sudo apt install redis-tools

After entering yes, the cluster is created, and exit exits docker, then logs in to one of the nodes to verify the availability of the cluster:

Redis-cli-c-p 7000

Enter cluster nodes to view the cluster status

127.0.0.1 cluster nodes06851aa134d50096d82a434eced9194233b5204e 7003 "17003" slave 8b33f273386c9504ef8bd10b005e24825b3b9596 0 1567671901000 4 connecteda42297b65f7101fc9e4941ef8a0e65080d1b6338 10.10.100.197Vera 7005 "17005 slave 0aa20378d14e3ef0859815196cbafa67e1001d0e 01567671901581 6 connectede7b6a35b1e92f94c225c507ea19f7f0318f0d1c3 10.10.100.197Vera 7002" 17002 master-01567671902582 3 connected 10923-163830aa20378d14e3ef0859815196cbafa67e1001d0e 10.10.100.1977 connecteda42297b65f7101fc9e4941ef8a0e65080d1b6338 7000000 myself,master-015676719010001 connected 0-54608b33f273386c9504ef8bd10b005e24825b3b9596 10.10.100.19715 70017001 master-0 1567671902383 2 connected 5461-10922fe355eed99100197f43d1216d1de82643dd496a5 10.10.100.1970.1900717004 slave e7b6a35b1e92f94c225c507ea19f7f0318f0d1c3 01567671901380 connected

Set the cluster password

Why not set the password in the above step and write it in when you batch create the configuration file using the template file?

No matter in the redis5.x version or the previous redis version using ruby to create a cluster, there is no password parameter configuration in the process of creating a cluster in redis-cli-cluster create, so we need to set the password after creating the cluster.

We use config set to set the same password for each node (there is no need to restart redis, and it is still valid after restart). Before that, add w permissions to all redis configuration files, otherwise the password cannot be saved to the file.

Note that the current path is still in ~ / redis_cluster/:

For port in `seq 7000 7005`; do\ chmod astatw. / ${port} / conf/redis.conf;\ done

Let's use one as an example:

Log in to a node:

Redis-cli-c-p 7000

Set the password:

127.0.0.1 auth 123456OK127.0.0.1:7000 7000 > config set masterauth 123456OK127.0.0.1:7000 > config set requirepass 123456OK127.0.0.1:7000 > config rewriteOK

The next few can do the same thing.

Simple test of cluster writing data

Log in to any cluster node:

Redis-cli-c-p 7003-a 123456

Write data:

127.0.0.1located at 10.10.100.197:7001OK10.10.100.197:7001 7003 > set va 1-> Redirected to slot [7800] located at 10.10.100.197:7001OK10.10.100.197:7001 > get va "1" 10.10.100.1971 located at 10.10.100.197:7001OK10.10.100.197:7001 7001 > del va (integer) 1

As you can see, data is written by any node in the cluster and can be read at any other node.

At this point, the redis cluster has been built.

Other considerations

If the public network accesses the redis, the firewall may need to open the corresponding port.

If you need to delete the container, you can do it in batch:

For port in `seq 7000 7005`; do\ docker stop redis-$ {port}; docker rm redis-$ {port}; done thank you for reading! This is the end of this article on "how to build a docker redis5.0 cluster cluster". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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