文章目录
- 前言
- 部署Redis Cluster
- 安装Redis Insight
- 写在最后
前言
在Web服务的开发过程中,Redis一直以来都有着举足轻重的作用。基本上所有的后端服务都会用这个中间件实现具体的业务场景,比如常作为系统缓存、分布式锁,也可以实现排名、定位以及发布订阅队列等等。当然,在k8s平台我们也能够部署Redis集群,今天就以Helm快速部署Redis集群。
部署Redis Cluster
一般情况下我们优先采用哨兵模式、cluster模式,这样才能保障高可用。今天作为演示,我们就采用普通的伪集群进行安装测试,当然其他的集群模式也是有helm charts的。
#添加 bitnami仓库
[root@master k8s]# helm repo add bitnami https://charts.bitnami.com/bitnami
# 查看仓库列表
[root@master k8s]# helm repo list
NAME URL
bitnami https://charts.bitnami.com/bitnami
ingress-nginx https://kubernetes.github.io/ingress-nginx
#拉取安装包
[root@master k8s]# helm pull bitnami/redis-cluster --version 8.6.6
#解压缩
[root@master k8s]# tar -zxvf redis-cluster-8.6.6.tgz
#进入解压后的目录
[root@master k8s]# cd redis-cluster
#修改value.yaml
[root@master redis-cluster]# vim value.yaml
# 主要编辑values.yaml如下内容
#储存配置
global.storageClass: "nfs-storage"
#密码
global.redis.password: "123456"
#创建命名空间
[root@master redis-cluster]# kubectl create ns redis-cluster
#安装redis-master
[root@master redis-cluster]# helm install redis-cluster . -n redis-cluster
验证redis集群
[root@master redis-cluster]# kubectl get pods,svc -n redis-cluster | grep redis
pod/redis-cluster-0 1/1 Running 1 (78s ago) 2m21s
pod/redis-cluster-1 1/1 Running 1 (78s ago) 2m21s
pod/redis-cluster-2 1/1 Running 1 (78s ago) 2m21s
pod/redis-cluster-3 1/1 Running 1 (78s ago) 2m21s
pod/redis-cluster-4 1/1 Running 1 (78s ago) 2m21s
pod/redis-cluster-5 1/1 Running 1 (78s ago) 2m21s
service/redis-cluster ClusterIP 10.97.55.190 6379/TCP 2m21s
service/redis-cluster-headless ClusterIP None 6379/TCP,16379/TCP 2m21s
#进入节点1
[root@node2 ~]# kubectl exec -it redis-cluster-0 -n redis-cluster bash
I have no name!@redis-cluster-0:/$ redis-cli -c -h 10.97.55.190 -p 6379
10.97.55.190:6379> auth 123456
OK
10.97.55.190:6379> set name senfel
OK
10.97.55.190:6379> get name
“senfel”
10.97.55.190:6379> set age 10
-> Redirected to slot [741] located at 10.244.2.53:6379
OK
10.244.2.53:6379> get age
“10”
卸载Redis Cluster
#查看helm
[root@master redis-cluster]# helm list -n redis-cluster
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
redis-cluster redis-cluster 1 2023-08-29 15:26:02.082652495 +0800 CST deployed redis-cluster-8.6.6 7.0.11
#卸载安装
[root@master redis-cluster]# helm uninsatll redis-cluster
安装Redis Insight
RedisInsight 是一个直观且高效的 Redis GUI,允许您与数据库交互并管理数据 - 内置对 Redis 模块的支持。
创建k8s执行文件redis-insight.yaml
cat >>redis-insight.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata: name: redis-insightnamespace: redis-cluster
spec:replicas: 1selector: matchLabels:app: redis-insighttemplate: metadata: labels: app: redis-insightspec:containers:- name: redis-insightimage: redislabs/redisinsight:1.12.1imagePullPolicy: IfNotPresentports: - containerPort: 8001volumeMounts: - name: dbmountPath: /dbvolumes:- name: dbemptyDir: {}
---
apiVersion: v1
kind: Service
metadata:name: redis-insight-servicenamespace: redis-cluster
spec:type: NodePortports:- port: 80targetPort: 8001nodePort: 31888selector:app: redis-insight
EOF
#创建redisinsight
[root@master k8s]# kubectl apply -f redis-insight.yaml
deployment.apps/redis-insight created
service/redis-insight-service created
#获取服务
[root@master k8s]# kubectl get svc -n redis-cluster
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-cluster ClusterIP 10.97.55.190 6379/TCP 45m
redis-cluster-headless ClusterIP None 6379/TCP,16379/TCP 45m
redis-insight-service NodePort 10.102.211.215 80:31888/TCP 21s
#移除服务
[root@master k8s]# kubctl delete -f redis-insight.yaml
浏览器登录
k8s-node-ip:31888
查看服务名称
[root@node1 ~]# kubectl get svc -n redis-cluster
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-cluster ClusterIP 10.97.55.190 6379/TCP 53m
redis-cluster-headless ClusterIP None 6379/TCP,16379/TCP 53m
redis-insight-service NodePort 10.102.211.215 80:31888/TCP 7m53s
访问格式是:
host: redis-cluster-headless.redis-cluster.svc.cluster.local svc名称. ns.svc.cluster.local
port: 6379
name: redis-cluster-headless svc名称
查看已有的数据
控制台交互
写在最后
运用helm部署Redes集群较为简单,相比于普通的文件部署省去了编写文件的过程,可直接使用charts完成部署。