Kubernetes 集群部署 Redis + redis_exporter (单节点)

网友投稿 900 2022-05-30

简介

Redis 参数配置

创建 ConfigMap 存储 Redis 配置文件

Kubectl 部署 ConfigMap

简介

Redis 参数配置

创建 ConfigMap 存储 Redis 配置文件

Kubectl 部署 ConfigMap

Redis 数据存储

创建 PV

创建 PVC 绑定存储空间

通过 Kubectl 工具部署 PV、PVC

部署 Redis + redis_exporter

原理

redis_exporter

创建 Sidecar 部署 Redis

通过 Kubectl 工具部署 Redis

测试 Redis 是否能够正常使用

Redis 是否能够正常获取监控数据

系统环境:

Redis 版本:5.0.8

Kubernetes 版本:1.19.2

操作系统版本:CentOS 7.8

简介

Redis 是我们常用的非关系型数据库,在项目开发、测试、部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存。这里介绍下如何在 Kubernetes 环境中部署用于开发、测试的环境的 Redis 数据库,当然,部署的是单节点模式,并非用于生产环境的主从、哨兵或集群模式。

Redis 参数配置

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

创建 ConfigMap 存储 Redis 配置文件

创建 Kubernetes 的 ConfigMap 资源,用于存储 Redis 的配置文件 redis.conf 内容:

redis-config.yaml:

kind: ConfigMap apiVersion: v1 metadata: name: labels: app: redis data: redis.conf: |- dir /data port 6379 bind 0.0.0.0 appendonly yes protected-mode no pidfile /data/redis-6379.pid

Kubectl 部署 ConfigMap

通过 kubectl 工具部署 Kubernetes ConfigMap 资源,命令如下:

$ kubectl create -f redis-config.yaml

Redis 数据存储

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

创建 PV

PV 支持多种存储驱动,不同存储驱动的 PV 配置方式是不同的,需要根据你的存储系统来配置 PV 参数。这里用的是 NFS 存储(共享网络文件存储系统),直接使用前面创建的 StorageClass 即可 。

具体参考:

Kubernetes 集群部署 NFS-Subdir-External-Provisioner 存储插件

创建 PVC 绑定存储空间

redis-storage.yaml:

## PVC kind: PersistentVolumeClaim apiVersion: v1 metadata: name: redis spec: storageClassName: nfs-storage #---指定StorageClass resources: requests: storage: 5Gi #设置 pvc 存储资源大小 accessModes: - ReadWriteOnce

通过 Kubectl 工具部署 PV、PVC

通过 kubectl 工具部署 Kubernetes PV、PVC 资源,命令如下:

$ kubectl create -f redis-storage.yaml

部署 Redis + redis_exporter

原理

Prometheus 的数据指标是通过一个公开的 HTTP(S) 数据接口获取到的,我们不需要单独安装监控的 agent,只需要暴露一个 metrics 接口,Prometheus 就会定期去拉取数据;

对于一些普通的 HTTP 服务,我们完全可以直接重用这个服务,添加一个 /metrics 接口暴露给 Prometheus;而且获取到的指标数据格式是非常易懂的,不需要太高的学习成本。

同时现在很多服务从一开始就内置了一个/metrics 接口,比如 Kubernetes 的各个组件、istio 服务网格都直接提供了数据指标接口。有一些服务即使没有原生集成该接口,也完全可以使用一些 exporter 来获取到指标数据,比如今天介绍的 redis_exporter,而 exporter 就有点类似于传统监控服务中的 agent,作为服务一直存在,用来收集目标服务的指标数据然后直接暴露给 Prometheus。

redis_exporter

redis 没有自带 /metrics 接口供 Prometheus 使用,在这种情况下,我们就需要利用 exporter 服务来为 Prometheus 提供指标数据了。Prometheus 官方为许多应用就提供了对应的 exporter 应用,也有许多第三方的实现,我们可以前往官方网站进行查看:https://prometheus.io/docs/instrumenting/exporters/。

这里我们选择官方的 redis_exporter:https://github.com/oliver006/redis_exporter

创建 Sidecar 部署 Redis

创建用于 Kubernetes Deployment 来配置部署 Redis 的参数:

配置 Redis 的镜像地址、名称、版本号;

配置其 CPU 与 Memory 资源的占用;

配置 Volume 挂载之前创建的 PV、PVC、ConfigMap 资源等等;

sidecar 挂载 redis_exporter。

这里通过 redis_exporter 的服务来监控 redis 服务,我们以 sidecar 的形式和主应用部署在同一个 Pod 中,比如我们这里来部署一个 redis,并用 redis_exporter 的方式来采集监控数据供 Prometheus 使用,这里通过 redis_exporter 的服务来监控 redis 服务,我们以 sidecar 的形式和主应用部署在同一个 Pod 中,比如我们这里来部署一个 redis,并用 redis_exporter 的方式来采集监控数据供 Prometheus 使用。

如下资源清单文件:promethues-redis-deploy.yaml:

## Service apiVersion: v1 kind: Service metadata: name: cloud-redis labels: app: redis spec: selector: app: redis ports: - name: redis port: 6379 targetPort: 6379 - name: prom port: 9121 targetPort: 9121 --- ## Deployment apiVersion: apps/v1 kind: Deployment metadata: name: cloud-redis labels: app: redis spec: replicas: 1 selector: matchLabels: app: redis template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "9121" labels: app: redis spec: initContainers: - name: system-init image: busybox:1.32 imagePullPolicy: IfNotPresent command: - "sh" - "-c" - "echo 2000 > /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-exporter image: oliver006/redis_exporter:latest resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 9121 - 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 volumeMounts: - name: data mountPath: /data - name: config mountPath: /usr/local/etc/redis/redis.conf subPath: redis.conf volumes: - name: data persistentVolumeClaim: claimName: redis - name: config configMap: name: redis-config - name: sys hostPath: path: /sys

参数简介:

ports: 配置镜像映射端口。

Kubernetes 集群部署 Redis + redis_exporter (单节点)

resources: 配置 CPU、Memory 资源限制,可以通过配置该值来配置 Pod 的 QoS 级别。

volumeMounts: 存储卷挂载配置,用于镜像内存储的挂载配置,与 volumes 中对于的 name 进行绑定。

volumes: 存储卷配置,可配置使用 pvc、hostPath、emptyDir、nfs 等存储,需要配置 name 值与 -VolumeMounts 进行绑定。

通过 Kubectl 工具部署 Redis

通过 kubectl 工具部署 Deployment 来创建 Redis,命令如下:

# -n:指定部署应用的 Namespace 命名空间 $ kubectl create -f redis-deploy.yaml -n mall

测试 Redis 是否能够正常使用

进入Redis 使用 redis-cli 命令进行连接:

$ kubectl exec -ti cloud-redis-79b69db657-vjh8r --container redis -n mall -- /bin/bash root@cloud-redis-79b69db657-vjh8r:/data# redis-cli 127.0.0.1:6379> set a "100" OK 127.0.0.1:6379> get a "100" 127.0.0.1:6379>

可以看到,已经成功连接并进入 Redis 命令行界面,说明数据库能正常使用。

注意:

pod 里有多个容器,需要加上 --container or -c 参数,指定进入的容器。

Redis 是否能够正常获取监控数据

创建完成后,我们可以看到 redis 的 Pod 里面包含有两个容器:

$ kubectl get pods -n mall NAME READY STATUS RESTARTS AGE cloud-redis-79b69db657-vjh8r 2/2 Running 0 5d4h $ kubectl get svc -n mall NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE cloud-redis ClusterIP 10.96.243.202 6379/TCP,9121/TCP 5d4h

我们可以通过 9121 端口来校验是否能够采集到数据:

$ curl 10.96.243.202:9121/metrics # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 4.6144e-05 go_gc_duration_seconds{quantile="0.25"} 9.4462e-05 go_gc_duration_seconds{quantile="0.5"} 0.000122258 go_gc_duration_seconds{quantile="0.75"} 0.000168729 go_gc_duration_seconds{quantile="1"} 0.009149671 go_gc_duration_seconds_sum 1.531426455 go_gc_duration_seconds_count 6342 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge go_goroutines 10 # HELP go_info Information about the Go environment. # TYPE go_info gauge go_info{version="go1.15"} 1 # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use. # TYPE go_memstats_alloc_bytes gauge go_memstats_alloc_bytes 4.304504e+06 # HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed. # TYPE go_memstats_alloc_bytes_total counter go_memstats_alloc_bytes_total 1.6514546264e+10 # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table. # TYPE go_memstats_buck_hash_sys_bytes gauge go_memstats_buck_hash_sys_bytes 1.581302e+06 # HELP go_memstats_frees_total Total number of frees. # TYPE go_memstats_frees_total counter go_memstats_frees_total 1.45861236e+08 ......

源码地址:

https://github.com/zuozewei/blog-example/tree/master/Kubernetes/k8s-redis-exporter

Kubernetes Redis

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ReentrantReadWriteLock源码解析
下一篇:浅谈NB-IoT行业发展
相关文章