华为认证hcia hcip hcie云计算网络工程师在线培训
0 人在学
Redis有三种集群模式:主从模式,Sentinel(哨兵)模式,Cluster模式,这三种模式环境编排部署都会在本文章介绍与实战操作。
一、概述
REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库。
Redis有三种集群模式:主从模式,Sentinel(哨兵)模式,Cluster模式,这三种模式环境编排部署都会在本文章介绍与实战操作。
二、redis 主从模式编排部署实战操作
地址:https://artifacthub.io/packages/helm/bitnami/redis
1)下载chart 包
复制
1. helm repo add bitnami https://charts.bitnami.com/bitnami
2.
3. helm pull bitnami/redis --version 17.3.7
4.
5. tar -xf redis-17.3.7.tgz
2)构建镜像
这里就不重新构建镜像了,只是把远程镜像tag一下,推到本地harbor仓库加速下载镜像。有不清楚怎么构建镜像的小伙伴,可以私信或者留言。
复制
1. docker pull docker.io/bitnami/redis:7.0.5-debian-11-r7
2.
3. # tag
4. docker tag docker.io/bitnami/redis:7.0.5-debian-11-r7 myharbor.com/bigdata/redis:7.0.5-debian-11-r7
5.
6. # 推送镜像到本地harbor仓库
7. docker push myharbor.com/bigdata/redis:7.0.5-debian-11-r7
3)修改yaml编排
新增pv.yaml文件,内容如下:
复制
1. {{- range .Values.master.persistence.local }}
2. ---
3. apiVersion: v1
4. kind: PersistentVolume
5. metadata:
6. name: {{ .name }}
7. labels:
8. name: {{ .name }}
9. spec:
10. storageClassName: {{ $.Values.master.persistence.storageClass }}
11. capacity:
12. storage: {{ $.Values.master.persistence.size }}
13. accessModes:
14. - ReadWriteOnce
15. local:
16. path: {{ .path }}
17. nodeAffinity:
18. required:
19. nodeSelectorTerms:
20. - matchExpressions:
21. - key: kubernetes.io/hostname
22. operator: In
23. values:
24. - {{ .host }}
25. ---
26. {{- end }}
新增pv.yaml文件,内容如下:
复制
1. {{- range .Values.replica.persistence.local }}
2. ---
3. apiVersion: v1
4. kind: PersistentVolume
5. metadata:
6. name: {{ .name }}
7. labels:
8. name: {{ .name }}
9. spec:
10. storageClassName: {{ $.Values.replica.persistence.storageClass }}
11. capacity:
12. storage: {{ $.Values.replica.persistence.size }}
13. accessModes:
14. - ReadWriteOnce
15. local:
16. path: {{ .path }}
17. nodeAffinity:
18. required:
19. nodeSelectorTerms:
20. - matchExpressions:
21. - key: kubernetes.io/hostname
22. operator: In
23. values:
24. - {{ .host }}
25. ---
26. {{- end }}
复制
1. global:
2. redis:
3. password: "123456"
4.
5. ...
6.
7. image:
8. registry: myharbor.com
9. repository: bigdata/redis
10. tag: 7.0.5-debian-11-r7
11.
12. master:
13. count: 1
14. persistence:
15. enabled: true
16. size: 8Gi
17. storageClass: "local-redis-storage"
18. local:
19. - name: redis-0
20. host: "local-168-182-110"
21. path: "/opt/bigdata/servers/redis/data/data1"
22.
23. replica:
24. replicaCount: 2
25. persistence:
26. enabled: true
27. size: 8Gi
28. storageClass: "local-redis-storage"
29. local:
30. - name: redis-1
31. host: "local-168-182-111"
32. path: "/opt/bigdata/servers/redis/data/data1"
33. - name: redis-2
34. host: "local-168-182-112"
35. path: "/opt/bigdata/servers/redis/data/data1"
4)开始部署
复制
1. # 创建存储目录
2. mkdir /opt/bigdata/servers/redis/data/data1
3.
4. # 先检查语法
5. helm lint ./redis
6.
7. # 开始安装
8. helm install redis ./redis -n redis --create-namespace
NOTES
复制
1. REVISION: 1
2. TEST SUITE: None
3. NOTES:
4. CHART NAME: redis
5. CHART VERSION: 17.3.7
6. APP VERSION: 7.0.5
7.
8. ** Please be patient while the chart is being deployed **
9.
10. Redis® can be accessed on the following DNS names from within your cluster:
11.
12. redis-master.redis.svc.cluster.local for read/write operations (port 6379)
13. redis-replicas.redis.svc.cluster.local for read-only operations (port 6379)
14.
15.
16.
17. To get your password run:
18.
19. export REDIS_PASSWORD=$(kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d)
20.
21. To connect to your Redis® server:
22.
23. 1. Run a Redis® pod that you can use as a client:
24.
25. 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
26.
27. Use the following command to attach to the pod:
28.
29. kubectl exec --tty -i redis-client \\
30. --namespace redis -- bash
31.
32. 2. Connect using the Redis® CLI:
33. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-master
34. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-replicas
35.
36. To connect to your database from outside the cluster execute the following commands:
37.
38. kubectl port-forward --namespace redis svc/redis-master 6379:6379 &
39. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379
5)测试验证
复制
1. kubectl get pods,svc -n redis -owide
复制
1. # 登录master,可读可写
2. 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)
3.
4. # 登录slave,只读
5. 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)
6)卸载
复制
1. helm uninstall redis-sentinel -n redis-sentinel
2. # delete ns
3. kubectl delete ns redis-sentinel --force
4. # delete pv
5. kubectl delete pv `kubectl get pv|grep ^redis-|awk '{print $1}'` --force
6.
7. rm -fr /opt/bigdata/servers/redis/data/data1/*
三、redis 哨兵模式编排部署实战操作
主从模式的弊端就是不具备高可用性,当master挂掉以后,Redis将不能再对外提供写入操作,因此sentinel应运而生。
1)构建镜像
这里也重新构建镜像了,有不懂构建镜像的小伙伴可以在评论下方留言。这里也只是把远程的镜像推送到本地harbor。
复制
1. docker pull docker.io/bitnami/redis-sentinel:7.0.5-debian-11-r6
2. # tag
3. docker tag docker.io/bitnami/redis-sentinel:7.0.5-debian-11-r6 myharbor.com/bigdata/redis-sentinel:7.0.5-debian-11-r6
4. # push
5. docker push myharbor.com/bigdata/redis-sentinel:7.0.5-debian-11-r6
2)修改yaml编排
复制
1. replica:
2. # replica.replicaCount与sentinel.quorum值一样
3. replicaCount: 3
4. storageClass: "local-redis-storage"
5. local:
6. - name: redis-0
7. host: "local-168-182-110"
8. path: "/opt/bigdata/servers/redis/data/data1"
9. - name: redis-1
10. host: "local-168-182-111"
11. path: "/opt/bigdata/servers/redis/data/data1"
12. - name: redis-2
13. host: "local-168-182-112"
14. path: "/opt/bigdata/servers/redis/data/data1"
15.
16. sentinel:
17. enabled: true
18. image:
19. registry: myharbor.com
20. repository: bigdata/redis-sentinel
21. tag: 7.0.5-debian-11-r6
22. quorum: 3
新增pv.yaml文件,内容如下:
复制
1. {{- range .Values.sentinel.persistence.local }}
2. ---
3. apiVersion: v1
4. kind: PersistentVolume
5. metadata:
6. name: {{ .name }}
7. labels:
8. name: {{ .name }}
9. spec:
10. storageClassName: {{ $.Values.sentinel.persistence.storageClass }}
11. capacity:
12. storage: {{ $.Values.sentinel.persistence.size }}
13. accessModes:
14. - ReadWriteOnce
15. local:
16. path: {{ .path }}
17. nodeAffinity:
18. required:
19. nodeSelectorTerms:
20. - matchExpressions:
21. - key: kubernetes.io/hostname
22. operator: In
23. values:
24. - {{ .host }}
25. ---
26. {{- end }}
3)开始部署
复制
1. # 创建存储目录
2. mkdir -p /opt/bigdata/servers/redis/data/data1
3.
4. helm install redis-sentinel ./redis-sentinel -n redis-sentinel --create-namespace
NOTES
复制
1. NAME: redis-sentinel
2. LAST DEPLOYED: Fri Nov 4 22:42:52 2022
3. NAMESPACE: redis-sentinel
4. STATUS: deployed
5. REVISION: 1
6. TEST SUITE: None
7. NOTES:
8. CHART NAME: redis
9. CHART VERSION: 17.3.7
10. APP VERSION: 7.0.5
11.
12. ** Please be patient while the chart is being deployed **
13.
14. Redis® can be accessed via port 6379 on the following DNS name from within your cluster:
15.
16. redis-sentinel.redis-sentinel.svc.cluster.local for read only operations
17.
18. For read/write operations, first access the Redis® Sentinel cluster, which is available in port 26379 using the same domain name above.
19.
20.
21.
22. To get your password run:
23.
24. export REDIS_PASSWORD=$(kubectl get secret --namespace redis-sentinel redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d)
25.
26. To connect to your Redis® server:
27.
28. 1. Run a Redis® pod that you can use as a client:
29.
30. 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
31.
32. Use the following command to attach to the pod:
33.
34. kubectl exec --tty -i redis-client \\
35. --namespace redis-sentinel -- bash
36.
37. 2. Connect using the Redis® CLI:
38. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-sentinel -p 6379 # Read only operations
39. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-sentinel -p 26379 # Sentinel access
40.
41. To connect to your database from outside the cluster execute the following commands:
42.
43. kubectl port-forward --namespace redis-sentinel svc/redis-sentinel 6379:6379 &
44. REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379
查看
复制
1. kubectl get pods,svc -n redis-sentinel -owide
4)模拟故障测试
复制
1. # 查看
2. 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
模拟故障,kill master pod
复制
1. kubectl delete pod redis-sentinel-node-0 -n redis-sentinel
再次查看master所在节点,master节点已经切换到其它节点了。
再测试读写
复制
1. # 登录master节点
2. 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)
3.
4. # 登录slave节点
5. 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)
5)卸载
复制
1. helm uninstall redis-sentinel -n redis
2. # delete ns
3. kubectl delete ns redis --force
4. # delete pv
5. kubectl delete pv `kubectl get pv|grep ^redis-|awk '{print $1}'` --force
6.
7. rm -fr /opt/bigdata/servers/redis/data/data1/*
四、redis 集群模式编排部署实战操作
集群模式可以说是sentinel+主从模式的结合体,通过cluster可以实现主从和master重选功能,所以如果配置两个副本三个分片的话,就需要六个Redis实例。因为Redis的数据是根据一定规则分配到cluster的不同机器的,当数据量过大时,可以新增机器进行扩容。
1)下载chart 包
复制
1. helm repo add bitnami https://charts.bitnami.com/bitnami
2.
3. helm pull bitnami/redis-cluster --version 8.2.7
4.
5. tar -xf redis-cluster-8.2.7.tgz
2)构建镜像
这里就不重新构建镜像了,只是把远程镜像tag一下,推到本地harbor仓库加速下载镜像。有不清楚怎么构建镜像的小伙伴,可以私信或者留言。
复制
1. docker pull docker.io/bitnami/redis-cluster:7.0.5-debian-11-r9
2.
3. # tag
4. docker tag docker.io/bitnami/redis-cluster:7.0.5-debian-11-r9 myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9
5.
6. # 推送镜像到本地harbor仓库
7. docker push myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9
3)修改yaml编排
新增pv.yaml文件,内容如下:
复制
1. {{- range .Values.persistence.local }}
2. ---
3. apiVersion: v1
4. kind: PersistentVolume
5. metadata:
6. name: {{ .name }}
7. labels:
8. name: {{ .name }}
9. spec:
10. storageClassName: {{ $.Values.persistence.storageClass }}
11. capacity:
12. storage: {{ $.Values.persistence.size }}
13. accessModes:
14. - ReadWriteOnce
15. local:
16. path: {{ .path }}
17. nodeAffinity:
18. required:
19. nodeSelectorTerms:
20. - matchExpressions:
21. - key: kubernetes.io/hostname
22. operator: In
23. values:
24. - {{ .host }}
25. ---
26. {{- end }}
复制
1. password: "123456"
2.
3. ...
4.
5. image:
6. registry: myharbor.com
7. repository: bigdata/redis-cluster
8. tag: 7.0.5-debian-11-r9
9.
10. ...
11.
12. persistence:
13. storageClass: "local-redis-cluster-storage"
14. local:
15. - name: redis-cluster-0
16. host: "local-168-182-110"
17. path: "/opt/bigdata/servers/redis-cluster/data/data1"
18. - name: redis-cluster-1
19. host: "local-168-182-110"
20. path: "/opt/bigdata/servers/redis-cluster/data/data2"
21. - name: redis-cluster-2
22. host: "local-168-182-110"
23. path: "/opt/bigdata/servers/redis-cluster/data/data3"
24. - name: redis-cluster-3
25. host: "local-168-182-111"
26. path: "/opt/bigdata/servers/redis-cluster/data/data1"
27. - name: redis-cluster-4
28. host: "local-168-182-111"
29. path: "/opt/bigdata/servers/redis-cluster/data/data2"
30. - name: redis-cluster-5
31. host: "local-168-182-111"
32. path: "/opt/bigdata/servers/redis-cluster/data/data3"
33. - name: redis-cluster-6
34. host: "local-168-182-112"
35. path: "/opt/bigdata/servers/redis-cluster/data/data1"
36. - name: redis-cluster-7
37. host: "local-168-182-112"
38. path: "/opt/bigdata/servers/redis-cluster/data/data2"
39. - name: redis-cluster-8
40. host: "local-168-182-112"
41. path: "/opt/bigdata/servers/redis-cluster/data/data3"
42.
43. cluster:
44. init: true
45. # 一主两从(三组)
46. nodes: 9
47. replicas: 2
4)开始部署
复制
1. # 创建存储目录
2. mkdir -p /opt/bigdata/servers/redis-cluster/data/data{1..3}
3.
4. helm install redis-cluster ./redis-cluster -n redis-cluster --create-namespace
NOTES
复制
1. NOTES:
2. CHART NAME: redis-cluster
3. CHART VERSION: 8.2.7
4. APP VERSION: 7.0.5** Please be patient while the chart is being deployed **
5.
6.
7. To get your password run:
8. export REDIS_PASSWORD=$(kubectl get secret --namespace "redis-cluster" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d)
9.
10. 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:
11.
12. 1. Run a Redis® pod that you can use as a client:
13. kubectl run --namespace redis-cluster redis-cluster-client --rm --tty -i --restart='Never' \\
14. --env REDIS_PASSWORD=$REDIS_PASSWORD \\
15. --image myharbor.com/bigdata/redis-cluster:7.0.5-debian-11-r9 -- bash
16.
17. 2. Connect using the Redis® CLI:
18.
19. redis-cli -c -h redis-cluster -a $REDIS_PASSWORD
查看
复制
1. kubectl get pods,svc -n redis-cluster -owide
5)故障模拟测试
复制
1. 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 jsnotallow="{.data.redis-password}" | base64 -d) CLUSTER INFO
2.
3. 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
删除其中一个master节点
复制
1. kubectl delete pod redis-cluster-1 -n redis-cluster
2.
3. # 再查看节点情况
4. 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
6)卸载
复制
1. helm uninstall redis-cluster -n redis-cluster
2. # delete ns
3. kubectl delete ns redis-cluster --force
4. # delete pv
5. kubectl delete pv `kubectl get pv|grep ^redis-cluster-|awk '{print $1}'` --force
6.
7. rm -fr /opt/bigdata/servers/redis-cluster/data/data{1..3}/*
git地址:https://gitee.com/hadoop-bigdata/redis-on-k8s
来源: 今日头条
>>>>>>点击进入云计算专题
共19节 · 4小时29分钟
¥199.0011838人在学
共116节 · 0分钟
华为认证hcia hcip hcie云计算网络工程师在线培训
¥10500.000人在学
共15节 · 5小时20分钟
云计算架构师快速入门(AWS入门篇):“身份和权限”:IAM视频教程
¥2000.005414人在学
共10节 · 3小时25分钟
¥2000.004406人在学