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 deploy Redis High availability Cluster on Kubernetes

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

Share

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

How to deploy Redis high availability cluster on Kubernetes, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Redis introduction

Redis stands for REmote DIctionary Server is an open source in-memory data store, usually used as a database, cache, or message proxy. It can store and manipulate advanced data types, such as lists, maps, collections, and sort collections. Because Redis accepts keys in multiple formats, operations can be performed on the server, reducing the workload of the client. It only uses the disk for persistence and keeps the database completely in memory. Redis is a popular data storage solution and is used by technology giants such as GitHub,Pinterest,Snapchat,Twitter,StackOverflow,Flickr.

Why use Redis

It's very fast. It is written in ANSI C and can be run on POSIX systems such as Linux,Mac OS X and Solaris.

Redis is generally ranked as the most popular key / value database and the most popular NoSQL database used with containers.

Its caching solution reduces the number of calls to the cloud database backend.

An application can access it through its client-side API library.

All popular programming languages support Redis.

It is open source and stable.

What is a Redis cluster

Redis Cluster is a set of Redis instances designed to extend the database by partitioning it to make it more resilient. Each member of the cluster, whether primary or secondary, manages a subset of the hash slot. If the host is unreachable, its slave will be upgraded to the host. In the smallest Redis cluster consisting of three master nodes, each master node has a slave node (for minimal failover), and each master node is assigned a hash slot range between 0 and 16383. Node A contains hash slots from 0 to 5000, node B from 5001 to 10000, and node C from 10001 to 16383. Communication within the cluster is carried out through the internal bus, using protocols to propagate information about the cluster or to discover new nodes.

Deploy Redis clusters in Kubernetes

Deploying Redis clusters in Kubernetes is challenging because each Redis instance relies on a configuration file that keeps track of other cluster instances and their roles. To do this, we need to use Kubernetes StatefulSets in conjunction with PersistentVolumes.

Clone deployment files

Git clone https://github.com/llmgo/redis-sts.git

Create a resource of type statefulset

[root@node01 redis-sts] # cat redis-sts.yml-apiVersion: V1 kind: ConfigMap metadata: name: redis-cluster data: update-node.sh: | #! / bin/sh REDIS_NODES= "/ data/nodes.conf" sed-I-e "/ myself/ s / [0-9]\ {1Magol 3\}\. [0-9]\ {1Med 3\}\. [0-9]\ {1jue 3\}\. [0 -9]\ {1apps/v1 kind 3\} / ${POD_IP} / "${REDIS_NODES} exec" $@ "redis.conf: | + cluster-enabled yes cluster-require-full-coverage no cluster-node-timeout 15000 cluster-config-file / data/nodes.conf cluster-migration-barrier 1 appendonly yes protected-mode no-apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-cluster spec: serviceName : redis-cluster replicas: 6 selector: matchLabels: app: redis-cluster template: metadata: labels: app: redis-cluster spec: containers:-name: redis image: redis:5.0.5-alpine ports:-containerPort: 6379 name: client-containerPort: 16379 name : gossip command: ["/ conf/update-node.sh" "redis-server" "/ conf/redis.conf"] env:-name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP volumeMounts:-name: conf mountPath: / conf readOnly: false-name: data mountPath: / data readOnly: false Volumes:-name: conf configMap: name: redis-cluster defaultMode: 0755 volumeClaimTemplates:-metadata: name: data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 5Gi storageClassName: standard $kubectl apply-f redis-sts.yml configmap/redis-cluster created statefulset.apps/redis-cluster created Kubectl get pods-l app=redis-cluster NAME READY STATUS RESTARTS AGE redis-cluster-0 1 Running 0 53s redis-cluster-1 1 Running 0 49s redis-cluster-2 1 Running 0 46s redis-cluster-3 1 redis-cluster-4 1 Running 0 42s redis- 1 Running 038s redis- Cluster-5 1/1 Running 0 34s

Create service

[root@node01 redis-sts] # cat redis-svc.yml-apiVersion: v1 kind: Service metadata: name: redis-cluster spec: type: ClusterIP clusterIP: 10.96.0.100 ports:-port: 6379 targetPort: 6379 name: client-port: 16379 targetPort: 16379 name: gossip selector: app: redis-cluster $kubectl apply-f redis-svc.yml service/redis-cluster created $kubectl get svc redis- Cluster NAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGE redis-cluster ClusterIP 10.96.0.100 6379/TCP 16379/TCP 35s

Initialize redis cluster

The next step is to form a Redis cluster. To do this, we run the following command and type yes to accept the configuration. The first three nodes become master nodes, and the last three nodes become slave nodes.

$kubectl exec-it redis-cluster-0-- redis-cli-- cluster create-- cluster-replicas 1 $(kubectl get pods-l app=redis-cluster-o jsonpath=' {range.items [*]} {.status.podIP}: 6379') > Performing hash slots allocation on 6 nodes... Master [0]-> Slots 0-5460 Master [1]-> Slots 5461-10922 Master [2]-> Slots 10923-16383 Adding replica 10.244.2.11 Master 10.244.9.19 Master 6379 Adding replica 10.244.9.20 Adding replica 10.244.6.10 Adding replica 10.244.8.156379 to 10.244.8 6379 M: 00721c43db194c8f2cacbafd01fd2be6a2fede28 10.244.9.19lun 6379 slots: [0-5460] (5461) Slots) master M: 9c36053912dec8cb20a599bda202a654f241484f 10.244.6.10 master 6379 slots: [5461-10922] (5462 slots) master M: 2850f24ea6367de58fb50e632fc56fe4ba5ef016 10.244.7.8 slots: [10923-16383] (5461 slots) master S: 554a58762e3dce23ca5a75886d0ccebd2d582502 10.244.8.15 slots 6379 replicates 00721c43db194c8f2cacbafd01fd2be6a2fede28 S: 20028fd0b79045489824eda71fac9898f17af896 10.244.2.116379 replicates 00721c43db194c8f2cacbafd01fd2be6a2fede28 S: 87e8987e314e4e5d4736e5818651abc1ed6ddcd9 10.244.9.20 replicates 9c36053912dec8cb20a599bda202a654f241484f Can I set the above configuration? (type 'yes' to accept): yes > > Nodes configuration updated > > Assign a different config epoch to each node > > Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join. > > Performing Cluster Check (using node 10.244.9.19 master 6379) M: 00721c43db194c8f2cacbafd01fd2be6a2fede28 10.244.9.19 master 6379 slots: [0-5460] (5461 slots) master 1 additional replica (s) M: 9c36053912dec8cb20a599bda202a654f241484f 10.244.6.140 106379 slots: [5461-10922] ( 5462 slots) master 1 additional replica (s) S: 87e8987e314e4e5d4736e5818651abc1ed6ddcd9 10.244.9.20 slots 6379 slots: (0 slots) slave replicates 9c36053912dec8cb20a599bda202a654f241484f S: 554a58762e3dce23ca5a75886d0ccebd2d582502 10.244.8.15 slots 6379 slots: (0 slots) slave replicates 2850f24ea6367de58fb50e632fc56fe4ba5ef016 S: 20028fd0b79045489824eda71fac9898f17af896 10.244.2.11 slave replicates 2850f24ea6367de58fb50e632fc56fe4ba5ef016 6379 slots: (0 slots) slave replicates 00721c43db194c8f2cacbafd01fd2be6a2fede28 M: 2850f24ea6367de58fb50e632fc56fe4ba5ef016 10.244.7.8 slave replicates 2850f24ea6367de58fb50e632fc56fe4ba5ef016 6379 slots: [10923-16383] (5461 slots) master 1 Additional replica (s) [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.

Verify the cluster

[root@node01 redis-sts] # kubectl exec-it redis-cluster-0-redis-cli cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:16 cluster_stats_messages_pong_sent:22 cluster_stats_messages_sent:38 cluster_stats_messages _ ping_received:17 cluster_stats_messages_pong_received:16 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:38 [root@node01 redis-sts] # for x in $(seq 0 5) Do echo "redis-cluster-$x"; kubectl exec redis-cluster-$x-- redis-cli role; echo; done redis-cluster-0 master 14 10.244.2.11 6379 14 redis-cluster-1 master 28 10.244.9.20 6379 28 redis-cluster-2 master 28 10.244.8.15 6379 28 redis-cluster-3 slave 10.244.7.8 6379 connected 28 redis-cluster-4 slave 10.244.9.19 6379 connected 14 redis-cluster-5 slave 10.244.6.10 6379 connected 28

Test cluster

We want to use clustering and then simulate node failures. For the previous task, we will deploy a simple Python application, while for the latter, we will delete a node and observe the cluster behavior.

Deploy Click counter application

We deploy a simple application to the cluster and place a load balancer in front of it. The purpose of this application is to increment the counter and store it in the Redis cluster before returning the counter value as a HTTP response.

$kubectl apply-f app-deployment-service.yml service/hit-counter-lb created deployment.apps/hit-counter-app created

In the process, if we continue to load the page, the counter will continue to increase, and after deleting the Pod, we see no data loss.

$curl `kubectl get svc hit-counter-lb-o json | jq-r .spec.clusterIP` I have been hit 20 times since deployment. $curl `kubectl get svc hit-counter-lb-o json | jq-r .spec.clusterIP` I have been hit 21 times since deployment. $curl `kubectl get svc hit-counter-lb-o json | jq-r .spec.clusterIP` I have been hit 22 times since deployment. $kubectl delete pods redis-cluster-0 pod "redis-cluster-0" deleted $kubectl delete pods redis-cluster-1 pod "redis-cluster-1" deleted $curl `kubectl get svc hit-counter-lb-o json | jq-r .spec. ClusterIP` I have been hit 23 times since deployment. Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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