kubernetes环境部署单节点redis数据库的方法

时间:2021-05-02

kubernetes部署redis数据库(单节点)

redis简介

Redis 是我们常用的非关系型数据库,在项目开发、测试、部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存。这里介绍下如何在 Kubernetes 环境中部署用于开发、测试的环境的 Redis 数据库,当然,部署的是单节点模式,并非用于生产环境的主从、哨兵或集群模式。单节点的 Redis 部署简单,且配置存活探针,能保证快速检测 Redis 是否可用,当不可用时快速进行重启。

redis 参数配置

在使用 Kubernetes 部署应用后,一般会习惯与将应用的配置文件外置,用 ConfigMap 存储,然后挂载进入镜像内部。这样,只要修改 ConfigMap 里面的配置,再重启应用就能很方便就能够使应用重新加载新的配置,很方便。

部署redis

创建configmap存储redis配置文件

redis-config.yaml

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 kind: ConfigMap apiVersion: v1 metadata: name: redis-config namespace: zisefeizhu labels: app: redis data: redis.conf: |- dir /data port 6379 bind 0.0.0.0 appendonly yes protected-mode no requirepass zisefeizhu pidfile /data/redis-6379.pid

Redis 数据存储

Kubernetes 部署的应用一般都是无状态应用,部署后下次重启很可能会漂移到不同节点上,所以不能使用节点上的本地存储,而是使用网络存储对应用数据持久化,PV 和 PVC 是 Kubernetes 用于与储空关联的资源,可与不同的存储驱动建立连接,存储应用数据,所以接下来我们要创建 Kubernetes PV、PVC 资源。

创建 Deployment 部署 Redis

创建用于 Kubernetes Deployment 来配置部署 Redis 的参数,需要配置 Redis 的镜像地址、名称、版本号,还要配置其 CPU 与 Memory 资源的占用,配置探针监测应用可用性,配置 Volume 挂载之前创建的 PV、PVC、ConfigMap 资源等等,内容如下:
redis-deployment.yaml

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 --- apiVersion: v1 kind: Service metadata: name: redis labels: app: redis spec: type: ClusterIP ports: - name: redis port: 6379 selector: app: redis --- apiVersion: apps/v1 kind: Deployment metadata: name: redis namespace: production-pppharmapack labels: app: redis spec: replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis spec: # 进行初始化操作,修改系统配置,解决 Redis 启动时提示的警告信息 initContainers: - name: system-init image: busybox:1.32 imagePullPolicy: IfNotPresent command: - "sh" - "-c" - "echo 2048 > /proc/sys/net/core/somaxconn && echo never > /sys/kernel/mm/transparent_hugepage/enabled" securityContext: privileged: true runAsUser: 0 volumeMounts: - name: sys mountPath: /sys containers: - name: redis image: redis:5.0.8 command: - "sh" - "-c" - "redis-server /usr/local/etc/redis/redis.conf" ports: - containerPort: 6379 resources: limits: cpu: 1000m memory: 1024Mi requests: cpu: 1000m memory: 1024Mi livenessProbe: tcpSocket: port: 6379 initialDelaySeconds: 300 timeoutSeconds: 1 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 readinessProbe: tcpSocket: port: 6379 initialDelaySeconds: 5 timeoutSeconds: 1 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 volumeMounts: - name: data mountPath: /data - name: config mountPath: /usr/local/etc/redis/redis.conf subPath: redis.conf volumes: - name: data persistentVolumeClaim: claimName: zisefeizhu - name: config configMap: name: redis-config - name: sys hostPath: path: /sys

测试redis是否可以正常使用

? 1 2 3 4 5 6 7 8 9 # ctl get pod -n production-pppharmapack | grep redis redis-7768dc9c56-4kp8l 1/1 Running 0 8m43s ctl exec -it redis-7768dc9c56-4kp8l -n production-pppharmapack -- /bin/sh # redis-cli 127.0.0.1:6379> auth zisefeizhu OK 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "zisefeizhu"

到此这篇关于kubernetes环境部署单节点redis数据库的方法的文章就介绍到这了,更多相关kubernetes部署redis数据库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/zisefeizhu/archive/2021/01/15/14282299.html

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章