Redis on K8s 编排部署讲解与实战操作

news/2024/6/3 17:57:43 标签: redis, kubernetes, 数据库, 缓存, 容器

f2dd8134730d346c616205ecdbe3036d.gif

本文转自大数据老司机,原文:https://www.cnblogs.com/liugp/p/16859528.html,版权归原作者所有。欢迎投稿,投稿请添加微信好友:cloud-native-yang

概述

REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库

Redis有三种集群模式:主从模式,Sentinel(哨兵)模式,Cluster模式,这三种模式环境编排部署都会在本文章介绍与实战操作。

想了解更多关于redis概念与原理介绍,可参考我这篇文章:Redis原理介绍与环境部署(主从模式、哨兵模式、集群模式)[1]

redis 主从模式编排部署实战操作

d999827ad9eba14987d302db73a2e775.png

地址:https://artifacthub.io/packages/helm/bitnami/redis

1)下载chart 包

$ helm repo add bitnami https://charts.bitnami.com/bitnami

$ helm pull bitnami/redis --version 17.3.7

$ tar -xf redis-17.3.7.tgz

2)构建镜像

这里就不重新构建镜像了,只是把远程镜像tag一下,推到本地harbor仓库加速下载镜像。有不清楚怎么构建镜像的小伙伴,可以私信或者留言。

$ docker pull docker.io/bitnami/redis:7.0.5-debian-11-r7

# tag
$ docker tag docker.io/bitnami/redis:7.0.5-debian-11-r7 myharbor.com/bigdata/redis:7.0.5-debian-11-r7

# 推送镜像到本地harbor仓库
$ docker push myharbor.com/bigdata/redis:7.0.5-debian-11-r7

3)修改yaml编排

  • redis/templates/master/pv.yaml

新增pv.yaml文件,内容如下:

{{- range .Values.master.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.master.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.master.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}
  • redis/templates/replicas/pv.yaml

新增pv.yaml文件,内容如下:

{{- range .Values.replica.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.replica.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.replica.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}
global:
  redis:
    password: "123456"

...

image:
  registry: myharbor.com
  repository: bigdata/redis
  tag: 7.0.5-debian-11-r7

master:
  count: 1
  persistence:
    enabled: true
    size: 8Gi
    storageClass: "local-redis-storage"
    local:
    - name: redis-0
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/redis/data/data1"

replica:
  replicaCount: 2
  persistence:
    enabled: true
    size: 8Gi
    storageClass: "local-redis-storage"
    local:
    - name: redis-1
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/redis/data/data1"
    - name: redis-2
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/redis/data/data1"

4)开始部署

# 创建存储目录
$ mkdir /opt/bigdata/servers/redis/data/data1

# 先检查语法
$ helm lint ./redis

# 开始安装
$ helm install redis ./redis -n redis --create-namespace

NOTES

REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 17.3.7
APP VERSION: 7.0.5

** Please be patient while the chart is being deployed **

Redis® can be accessed on the following DNS names from within your cluster:

    redis-master.redis.svc.cluster.local for read/write operations (port 6379)
    redis-replicas.redis.svc.cluster.local for read-only operations (port 6379)



To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d)

To connect to your Redis® server:

1. Run a Redis® pod that you can use as a client:

   kubectl run --namespace redis redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image myharbor.com/bigdata/redis:7.0.5-debian-11-r7 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace redis -- bash

2. Connect using the Redis® CLI:
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-master
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-replicas

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace redis svc/redis-master 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379
86f53053575fa3ffeb1ae677c2612a14.png

5)测试验证

$ kubectl get pods,svc -n redis -owide
1090cb1315a55a59c11dac69b4206624.png
# 登录master,可读可写
$ kubectl exec -it redis-master-0 -n redis -- redis-cli -h redis-master -a $(kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d)

# 登录slave,只读
$ kubectl exec -it redis-master-0 -n redis -- redis-cli -h redis-replicas -a $(kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d)
a08a8d534f66eee5cebefaa636fb0c83.png

6)卸载

$ helm uninstall redis-sentinel -n redis-sentinel
# delete ns 
$ kubectl delete ns redis-sentinel --force
# delete pv
$ kubectl delete pv `kubectl get pv|grep ^redis-|awk '{print $1}'` --force

$ rm -fr /opt/bigdata/servers/redis/data/data1/*

redis 哨兵模式编排部署实战操作

主从模式的弊端就是不具备高可用性,当master挂掉以后,Redis将不能再对外提供写入操作,因此sentinel应运而生。

6526d86e6f04bc1343361b4cd89d5865.png

1)构建镜像

这里也重新构建镜像了,有不懂构建镜像的小伙伴可以在评论下方留言。这里也只是把远程的镜像推送到本地harbor。

$ docker pull docker.io/bitnami/redis-sentinel:7.0.5-debian-11-r6
# tag
$ docker tag docker.io/bitnami/redis-sentinel:7.0.5-debian-11-r6 myharbor.com/bigdata/redis-sentinel:7.0.5-debian-11-r6
# push
$ docker push  myharbor.com/bigdata/redis-sentinel:7.0.5-debian-11-r6

2)修改yaml编排

  • redis-sentinel/values.yaml

replica:
  # replica.replicaCount与sentinel.quorum值一样
  replicaCount: 3
  storageClass: "local-redis-storage"
    local:
    - name: redis-0
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/redis/data/data1"
    - name: redis-1
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/redis/data/data1"
    - name: redis-2
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/redis/data/data1"

sentinel:
  enabled: true
  image:
    registry: myharbor.com
    repository: bigdata/redis-sentinel
    tag: 7.0.5-debian-11-r6
  quorum: 3
  • redis-sentinel/templates/replicas/pv.yaml

新增pv.yaml文件,内容如下:

{{- range .Values.sentinel.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.sentinel.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.sentinel.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}

3)开始部署

# 创建存储目录
$ mkdir -p /opt/bigdata/servers/redis/data/data1

$ helm install redis-sentinel ./redis-sentinel -n redis-sentinel --create-namespace

NOTES

NAME: redis-sentinel
LAST DEPLOYED: Fri Nov  4 22:42:52 2022
NAMESPACE: redis-sentinel
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 17.3.7
APP VERSION: 7.0.5

** Please be patient while the chart is being deployed **

Redis® can be accessed via port 6379 on the following DNS name from within your cluster:

    redis-sentinel.redis-sentinel.svc.cluster.local for read only operations

For read/write operations, first access the Redis® Sentinel cluster, which is available in port 26379 using the same domain name above.



To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace redis-sentinel redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d)

To connect to your Redis® server:

1. Run a Redis® pod that you can use as a client:

   kubectl run --namespace redis-sentinel redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image myharbor.com/bigdata/redis:7.0.5-debian-11-r7 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace redis-sentinel -- bash

2. Connect using the Redis® CLI:
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-sentinel -p 6379 # Read only operations
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-sentinel -p 26379 # Sentinel access

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace redis-sentinel svc/redis-sentinel 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379
9f571196fd9eda99b0cb49780ceda346.png
# 查看
$ kubectl get pods,svc -n redis-sentinel -owide
f383f3fc3c908d48cbca787ffbfe637a.png

4)模拟故障测试

# 查看
$ kubectl exec -it redis-sentinel-node-0 -n redis-sentinel -- redis-cli -h redis-sentinel -a $(kubectl get secret --namespace redis-sentinel redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d) info replication
00104f4ffc44477ce76e69234f002f59.png

模拟故障,kill  master pod

$ kubectl delete pod redis-sentinel-node-0 -n redis-sentinel

再次查看master所在节点,master节点已经切换到其它节点了。69ae2354ada2d1e41e706daf58fd7aa9.png

再测试读写

# 登录master节点
kubectl exec -it redis-sentinel-node-0 -n redis-sentinel -- redis-cli -h redis-sentinel-node-2.redis-sentinel-headless -a $(kubectl get secret --namespace redis-sentinel redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d)

# 登录slave节点
kubectl exec -it redis-sentinel-node-0 -n redis-sentinel -- redis-cli -h redis-sentinel-node-0.redis-sentinel-headless -a $(kubectl get secret --namespace redis-sentinel redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d)
d554eacecde6f2f523590fba5262aac1.png

5)卸载

$ helm uninstall redis-sentinel -n redis
# delete ns 
$ kubectl delete ns redis --force
# delete pv
$ kubectl delete pv `kubectl get pv|grep ^redis-|awk '{print $1}'` --force

$ rm -fr /opt/bigdata/servers/redis/data/data1/*

redis 集群模式编排部署实战操作

集群模式可以说是sentinel+主从模式的结合体,通过cluster可以实现主从和master重选功能,所以如果配置两个副本三个分片的话,就需要六个Redis实例。因为Redis的数据是根据一定规则分配到cluster的不同机器的,当数据量过大时,可以新增机器进行扩容。

b2c5cc831f6d6753b80e39e3ad3fd837.png

1)下载chart 包

$ helm repo add bitnami https://charts.bitnami.com/bitnami

$ helm pull bitnami/redis-cluster --version 8.2.7

$ tar -xf redis-cluster-8.2.7.tgz

2)构建镜像

这里就不重新构建镜像了,只是把远程镜像tag一下,推到本地harbor仓库加速下载镜像。有不清楚怎么构建镜像的小伙伴,可以私信或者留言。

$ docker pull docker.io/bitnami/redis-cluster:7.0.5-debian-11-r9

# tag
$ docker tag docker.io/bitnami/redis-cluster:7.0.5-debian-11-r9 myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9

# 推送镜像到本地harbor仓库
$ docker push myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9

3)修改yaml编排

  • redis-cluster/templates/pv.yaml

新增pv.yaml文件,内容如下:

{{- range .Values.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .name }}
  labels:
    name: {{ .name }}
spec:
  storageClassName: {{ $.Values.persistence.storageClass }}
  capacity:
    storage: {{ $.Values.persistence.size }}
  accessModes:
    - ReadWriteOnce
  local:
    path: {{ .path }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - {{ .host }}
---
{{- end }}
password: "123456"

...

image:
  registry: myharbor.com
  repository: bigdata/redis-cluster
  tag: 7.0.5-debian-11-r9

...

persistence:
  storageClass: "local-redis-cluster-storage"
  local:
    - name: redis-cluster-0
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/redis-cluster/data/data1"
    - name: redis-cluster-1
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/redis-cluster/data/data2"
    - name: redis-cluster-2
      host: "local-168-182-110"
      path: "/opt/bigdata/servers/redis-cluster/data/data3"
    - name: redis-cluster-3
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/redis-cluster/data/data1"
    - name: redis-cluster-4
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/redis-cluster/data/data2"
    - name: redis-cluster-5
      host: "local-168-182-111"
      path: "/opt/bigdata/servers/redis-cluster/data/data3"
    - name: redis-cluster-6
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/redis-cluster/data/data1"
    - name: redis-cluster-7
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/redis-cluster/data/data2"
    - name: redis-cluster-8
      host: "local-168-182-112"
      path: "/opt/bigdata/servers/redis-cluster/data/data3"
  
cluster:
  init: true
  # 一主两从(三组)
  nodes: 9
  replicas: 2

4)开始部署

# 创建存储目录
$ mkdir -p /opt/bigdata/servers/redis-cluster/data/data{1..3}

$ helm install redis-cluster ./redis-cluster -n redis-cluster --create-namespace

NOTES

NOTES:
CHART NAME: redis-cluster
CHART VERSION: 8.2.7
APP VERSION: 7.0.5** Please be patient while the chart is being deployed **


To get your password run:
    export REDIS_PASSWORD=$(kubectl get secret --namespace "redis-cluster" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d)

You have deployed a Redis® Cluster accessible only from within you Kubernetes Cluster.INFO: The Job to create the cluster will be created.To connect to your Redis® cluster:

1. Run a Redis® pod that you can use as a client:
kubectl run --namespace redis-cluster redis-cluster-client --rm --tty -i --restart='Never' \
 --env REDIS_PASSWORD=$REDIS_PASSWORD \
--image myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9 -- bash

2. Connect using the Redis® CLI:

redis-cli -c -h redis-cluster -a $REDIS_PASSWORD
b4ffd8d9176682e26bdc376f3810c60f.png

查看

$ kubectl get pods,svc -n redis-cluster -owide
d439bd8a51406fdca19443753f5d95aa.png

5)故障模拟测试

$ kubectl exec -it redis-cluster-0 -n redis-cluster -- redis-cli -c -h redis-cluster -a $(kubectl get secret --namespace "redis-cluster" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d) CLUSTER INFO

$ kubectl exec -it redis-cluster-0 -n redis-cluster -- redis-cli -c -h redis-cluster -a $(kubectl get secret --namespace "redis-cluster" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d) CLUSTER NODES
3c55bd7a39316a629594d084bd3e92ea.png

删除其中一个master节点

$ kubectl delete pod redis-cluster-1 -n redis-cluster

# 再查看节点情况
$ kubectl exec -it redis-cluster-0 -n redis-cluster -- redis-cli -c -h redis-cluster -a $(kubectl get secret --namespace "redis-cluster" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d) CLUSTER NODES
cfb700a81507f4d45c398b3e779e251b.png

6)卸载

$ helm uninstall redis-cluster -n redis-cluster
# delete ns 
$ kubectl delete ns redis-cluster --force
# delete pv
$ kubectl delete pv `kubectl get pv|grep ^redis-cluster-|awk '{print $1}'` --force

$ rm -fr /opt/bigdata/servers/redis-cluster/data/data{1..3}/*

git地址:https://gitee.com/hadoop-bigdata/redis-on-k8s

Redis on K8s 三种模式的编排部署就先到这里了,小伙伴有任何疑问,欢迎给我留言哦,后续会持续更新【大数据+云原生】相关的问题~

引用链接

[1]

Redis原理介绍与环境部署(主从模式、哨兵模式、集群模式): https://www.cnblogs.com/liugp/p/16487671.html

47605d72c01bd2dff086fa50032ace45.gif

21b636d08b840daa3145c7c61a5e798f.png

你可能还喜欢

点击下方图片即可阅读

Cilium 未来数据平面:支撑 100Gbit/s k8s 集群

2022-11-14

baf39c02336f9753cd60846c0174032d.jpeg

Prometheus 官方记录片(中英双语),带你了解 Prometheus 的前世今生

2022-11-11

715505c7ff149c6170d7ed655f2e1e9c.jpeg

GraalVM 加持 Java 容器化,速度起飞!

2022-11-09

102879c0ab46c6f96270a876fd844b43.jpeg

eBPF 技术实践:加速容器网络转发,耗时降低60%+!

2022-11-08

86bf4fecd0f341da56244cd57bb016b4.jpeg

83b33e9ee98bd4d94d3e87849ba8b5fa.gif

云原生是一种信仰 🤘

关注公众号

后台回复◉k8s◉获取史上最方便快捷的 Kubernetes 高可用部署工具,只需一条命令,连 ssh 都不需要!

e066eb576d600d3d3104975b8615f141.gif

6d676cee1399a3252800ad80c0e74d4e.gif

点击 "阅读原文" 获取更好的阅读体验!

发现朋友圈变“安静”了吗?

c007e6a09344414bad79260b0159f2e2.gif


http://www.niftyadmin.cn/n/1749006.html

相关文章

asmlib的安装

目前只有Linux 操作系统上有asmlib 包,其他的系统暂时还没有。asmlib 简化磁盘管理,取代原来我们在linux 上常用rawdevices 服务。(也就是说,asmlib是由几个os库文件组成的,用来简化管理磁盘,安装了它&…

云原生时代的企业级容器云平台架构实践

作为云原生的关键基础,近年来容器技术在企业的应用规模不断提升,云原生理念也相应地在生产环境中得到了越来越广泛的应用实践,加速企业的数字化转型效率。与此同时,企业也面临着云原生技术的学习和研发投入等成本过高的挑战&#…

Moving Away from Python 2

2019独角兽企业重金招聘Python工程师标准>>> For those of you who dont know, Python 2.7 is slated to reach end-of-life in 2020 (originally, it was slated to end in 2015, but it was extended in 2014, due to the extraordinary difficulty of moving to …

Karmada 如何跨集群实现完整的自定义资源分发能力?

❝本文转自徐信钊的博客,原文:https://xinzhao.me/posts/guide-to-karmada-resource-interpreter-webhook/,版权归原作者所有。欢迎投稿,投稿请添加微信好友:cloud-native-yangKarmada 介绍在开始讲 Resource Interpre…

CallerInformation

http://www.cnblogs.com/henryzhu/archive/2013/01/27/csharp-5-new-callerinformation.html 去年8月,Visual Studio 2012和.NET Framework 4.5已经完成了,在.NET Framework 4.5 的C# 5.0的新特性中,其中之一就是CallerInformation&#xff0…

彻底解决 K8s 节点本地存储被撑爆的问题

现在云原生越来越流行,很多企业都上马了 K8s,但是这里边也有很多的坑要填,这篇文章就聊一下 K8s 节点本地存储被撑爆的问题,也就是磁盘被占满的问题。存储的内容要解决存储使用过多的问题,就得先了解存储中都保存了些什…

.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过,用过简单的例子,不过…

Linux平台上配置Oracle ASMLib和磁盘多路径

配置Oracle ASMLIB和多路径磁盘 以下文档描述如何在linux的平台下使用Oracle的asmlib来访问多路径的磁盘,无论您使用哪种多路径的软件,该文档是建立在已经创建好了多路径磁盘的基础上的。这个文档给出的多路径磁盘的名称是" multipatha"&#…