当前位置:   article > 正文

基于k8s集群的redis-cluster集群

基于k8s集群的redis-cluster集群

一、提前准备好nfs存储

  1. [root@k8s-master ~]# yum -y install nfs-utils nfs-common rpcbind
  2. [root@k8s-master ~]# mkdir /nfsdata
  3. [root@k8s-master ~]# chmod -R 777 /nfsdata
  4. [root@k8s-master ~]# vim /etc/exports
  5. /nfsdata *(rw,no_root_squash,sync,no_all_squash)
  6. [root@k8s-master ~]# systemctl start rpcbind nfs
  7. [root@k8s-master ~]# systemctl status nfs
  8. # node节点
  9. yum -y install nfs-utils nfs-common

二、制作动态存储

1.安装helm工具
  1. [root@k8s-master ~]# wget https://get.helm.sh/helm-v3.10.1-linux-amd64.tar.gz
  2. --2024-08-06 15:53:55-- https://get.helm.sh/helm-v3.10.1-linux-amd64.tar.gz
  3. 正在解析主机 get.helm.sh (get.helm.sh)... 152.199.39.108, 2606:2800:247:1cb7:261b:1f9c:2074:3c
  4. 正在连接 get.helm.sh (get.helm.sh)|152.199.39.108|:443... 已连接。
  5. 已发出 HTTP 请求,正在等待回应... 200 OK
  6. 长度:14565908 (14M) [application/x-tar]
  7. 正在保存至: “helm-v3.10.1-linux-amd64.tar.gz”
  8. 100%[=======================================================================>] 14,565,908 157KB/s 用时 46s
  9. 2024-08-06 15:54:43 (312 KB/s) - 已保存 “helm-v3.10.1-linux-amd64.tar.gz” [14565908/14565908])
  10. [root@k8s-master ~]# tar -xzvf helm-v3.10.1-linux-amd64.tar.gz
  11. linux-amd64/
  12. linux-amd64/helm
  13. linux-amd64/LICENSE
  14. linux-amd64/README.md
  15. [root@k8s-master ~]# cp linux-amd64/helm /usr/bin/
  16. [root@k8s-master ~]# helm version
  17. version.BuildInfo{Version:"v3.10.1", GitCommit:"9f88ccb6aee40b9a0535fcc7efea6055e1ef72c9", GitTreeState:"clean", GoVersion:"go1.18.7"}
2.配置helm下载源
  1. [root@k8s-master ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts
  2. "stable" has been added to your repositories
  3. [root@k8s-master ~]# helm repo list
  4. NAME URL
  5. stable http://mirror.azure.cn/kubernetes/charts
3.创建storageclass与nfs的目录相关联
  1. [root@k8s-master ~]# helm install nfs-redis stable/nfs-client-provisioner --set nfs.server=192.168.22.134 --set nfs.path=/nfsdata
  2. WARNING: This chart is deprecated
  3. NAME: nfs-redis
  4. LAST DEPLOYED: Tue Aug 6 16:01:14 2024
  5. NAMESPACE: default
  6. STATUS: deployed
  7. REVISION: 1
  8. TEST SUITE: None
  9. [root@k8s-master ~]# helm list
  10. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
  11. nfs-redis default 1 2024-08-06 16:01:14.424540014 +0800 CST deployed nfs-client-provisioner-1.2.11 3.1.0
  12. [root@k8s-master ~]# kubectl get pod
  13. NAME READY STATUS RESTARTS AGE
  14. nfs-redis-nfs-client-provisioner-7bd85b55f4-p26ks 1/1 Running 0 39s
  15. [root@k8s-master ~]# kubectl get sc
  16. NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
  17. nfs-client cluster.local/nfs-redis-nfs-client-provisioner Delete Immediate true 53s
三、redis配置文件configmap
  1. [root@k8s-master redis]# vim redis.conf
  2. [root@k8s-master redis]# cat redis.conf
  3. appendonly yes
  4. cluster-enabled yes
  5. cluster-config-file /var/lib/redis/nodes.conf
  6. cluster-node-timeout 5000
  7. dir /var/lib/redis
  8. port 6379
  9. [root@k8s-master redis]# kubectl create configmap redis-conf --from-file=redis.conf
  10. configmap/redis-conf created
  11. [root@k8s-master redis]# kubectl get cf
  12. error: the server doesn't have a resource type "cf"
  13. [root@k8s-master redis]# kubectl get cm
  14. NAME DATA AGE
  15. redis-conf 1 19s

四、创建Headless  service

  1. [root@k8s-master redis]# vim headless-service.yaml
  2. [root@k8s-master redis]# cat headless-service.yaml
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: redis-service
  7. labels:
  8. app: redis
  9. spec:
  10. ports:
  11. - name: redis-port
  12. port: 6379
  13. clusterIP: None
  14. selector:
  15. app: redis
  16. appCluster: redis-cluster
  17. [root@k8s-master redis]# kubectl apply -f headless-service.yaml
  18. service/redis-service created
  19. [root@k8s-master redis]# kubectl get svc
  20. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  21. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 40h
  22. redis-service ClusterIP None <none> 6379/TCP 9s

五、statefulSet运行redis实例

  1. [root@k8s-master redis]# vim statefulset-redis.yaml
  2. [root@k8s-master redis]# cat statefulset-redis.yaml
  3. apiVersion: apps/v1
  4. kind: StatefulSet
  5. metadata:
  6. name: redis-app
  7. spec:
  8. serviceName: "redis-service"
  9. replicas: 6
  10. selector:
  11. matchLabels:
  12. app: redis
  13. appCluster: redis-cluster
  14. template:
  15. metadata:
  16. labels:
  17. app: redis
  18. appCluster: redis-cluster
  19. spec:
  20. terminationGracePeriodSeconds: 20
  21. affinity:
  22. podAntiAffinity:
  23. preferredDuringSchedulingIgnoredDuringExecution:
  24. - weight: 100
  25. podAffinityTerm:
  26. labelSelector:
  27. matchExpressions:
  28. - key: app
  29. operator: In
  30. values:
  31. - redis
  32. topologyKey: kubernetes.io/hostname
  33. containers:
  34. - name: redis
  35. image: daocloud.io/library/redis:6-alpine3.12
  36. command:
  37. - "redis-server"
  38. args:
  39. - "/etc/redis/redis.conf"
  40. - "--protected-mode"
  41. - "no"
  42. resources:
  43. requests:
  44. cpu: "100m"
  45. memory: "100Mi"
  46. ports:
  47. - name: redis
  48. containerPort: 6379
  49. protocol: "TCP"
  50. - name: cluster
  51. containerPort: 16379
  52. protocol: "TCP"
  53. volumeMounts:
  54. - name: "redis-conf"
  55. mountPath: "/etc/redis"
  56. - name: "redis-data"
  57. mountPath: "/var/lib/redis"
  58. volumes:
  59. - name: "redis-conf"
  60. configMap:
  61. name: "redis-conf"
  62. items:
  63. - key: "redis.conf"
  64. path: "redis.conf"
  65. volumeClaimTemplates:
  66. - metadata:
  67. name: redis-data
  68. spec:
  69. accessModes: [ "ReadWriteMany" ]
  70. storageClassName: "nfs-client"
  71. resources:
  72. requests:
  73. storage: 200M
  74. [root@k8s-master redis]# kubectl apply -f statefulset-redis.yaml
  75. statefulset.apps/redis-app created
  76. [root@k8s-master redis]# kubectl get pod
  77. NAME READY STATUS RESTARTS AGE
  78. nfs-redis-nfs-client-provisioner-7bd85b55f4-p26ks 1/1 Running 0 31m
  79. redis-app-0 1/1 Running 0 29s
  80. redis-app-1 1/1 Running 0 26s
  81. redis-app-2 1/1 Running 0 21s
  82. redis-app-3 1/1 Running 0 17s
  83. redis-app-4 1/1 Running 0 12s
  84. redis-app-5 1/1 Running 0 8s
  85. [root@k8s-master redis]# kubectl get statefulset
  86. NAME READY AGE
  87. redis-app 6/6 68s

六、验证唯一访问标识可用性

        每个Pod都会得到集群内的⼀个DNS域名,格式为
(podname).(servicename).(namespace).svc.cluster.local
例如: redis-app-0.redis-service.default.svc.cluster.local
           redis-app-1.redis-service.default.svc.cluster.local
  1. # 创建一个pod用于测试,测试完即可删除
  2. [root@k8s-master redis]# vim busybox.yaml
  3. [root@k8s-master redis]# cat busybox.yaml
  4. ---
  5. apiVersion: v1
  6. kind: Pod
  7. metadata:
  8. name: busybox
  9. spec:
  10. containers:
  11. - name: busybox
  12. image: daocloud.io/library/busybox
  13. stdin: true
  14. tty: true
  15. [root@k8s-master redis]# kubectl apply -f busybox.yaml
  16. pod/busybox created
  17. [root@k8s-master redis]# kubectl get pod -o wide
  18. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  19. busybox 1/1 Running 0 2m56s 10.244.1.9 k8s-node1 <none> <none>
  20. nfs-redis-nfs-client-provisioner-7bd85b55f4-p26ks 1/1 Running 0 51m 10.244.2.4 k8s-node2 <none> <none>
  21. redis-app-0 1/1 Running 0 20m 10.244.1.4 k8s-node1 <none> <none>
  22. redis-app-1 1/1 Running 0 20m 10.244.2.5 k8s-node2 <none> <none>
  23. redis-app-2 1/1 Running 0 20m 10.244.1.5 k8s-node1 <none> <none>
  24. redis-app-3 1/1 Running 0 19m 10.244.2.6 k8s-node2 <none> <none>
  25. redis-app-4 1/1 Running 0 19m 10.244.1.6 k8s-node1 <none> <none>
  26. redis-app-5 1/1 Running 0 19m 10.244.2.7 k8s-node2 <none> <none>
  27. [root@k8s-master redis]# kubectl exec -it busybox /bin/sh
  28. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  29. / # ping redis-app-0.redis-service.default.svc.cluster.local
  30. PING redis-app-0.redis-service.default.svc.cluster.local (10.244.1.4): 56 data bytes
  31. 64 bytes from 10.244.1.4: seq=0 ttl=64 time=0.132 ms
  32. 64 bytes from 10.244.1.4: seq=1 ttl=64 time=0.062 ms
  33. 64 bytes from 10.244.1.4: seq=2 ttl=64 time=0.074 ms
  34. ^C
  35. --- redis-app-0.redis-service.default.svc.cluster.local ping statistics ---
  36. 3 packets transmitted, 3 packets received, 0% packet loss
  37. round-trip min/avg/max = 0.062/0.089/0.132 ms
  38. / # ping redis-app-1.redis-service.default.svc.cluster.local
  39. PING redis-app-1.redis-service.default.svc.cluster.local (10.244.2.5): 56 data bytes
  40. 64 bytes from 10.244.2.5: seq=0 ttl=62 time=2.076 ms
  41. 64 bytes from 10.244.2.5: seq=1 ttl=62 time=0.796 ms
  42. ^C
  43. --- redis-app-1.redis-service.default.svc.cluster.local ping statistics ---
  44. 2 packets transmitted, 2 packets received, 0% packet loss
  45. round-trip min/avg/max = 0.796/1.456/2.108 ms
  46. / # ping redis-app-2.redis-service.default.svc.cluster.local
  47. PING redis-app-2.redis-service.default.svc.cluster.local (10.244.1.5): 56 data bytes
  48. 64 bytes from 10.244.1.5: seq=0 ttl=64 time=0.167 ms
  49. 64 bytes from 10.244.1.5: seq=1 ttl=64 time=0.067 ms
  50. 64 bytes from 10.244.1.5: seq=2 ttl=64 time=0.061 ms
  51. ^C
  52. --- redis-app-2.redis-service.default.svc.cluster.local ping statistics ---
  53. 3 packets transmitted, 3 packets received, 0% packet loss
  54. round-trip min/avg/max = 0.061/0.098/0.167 ms
  55. / #

 查看PV和PVC

  1. [root@k8s-master redis]# kubectl get pv
  2. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  3. pvc-04c6e1bc-709f-4a77-bd36-7cd8c23d4ca1 200M RWX Delete Bound default/redis-data-redis-app-1 nfs-client 21m
  4. pvc-1650521c-ba23-4502-b8e8-c260359a2d27 200M RWX Delete Bound default/redis-data-redis-app-0 nfs-client 21m
  5. pvc-63d54f9b-7be4-4ce7-a72b-caa7bedde507 200M RWX Delete Bound default/redis-data-redis-app-2 nfs-client 21m
  6. pvc-652e836c-f426-4629-8600-d542f4269a52 200M RWX Delete Bound default/redis-data-redis-app-3 nfs-client 21m
  7. pvc-b1eee630-b2fc-4423-be6c-32278d6a8018 200M RWX Delete Bound default/redis-data-redis-app-5 nfs-client 21m
  8. pvc-efc54629-d01d-45ac-9c42-37ecbe34189a 200M RWX Delete Bound default/redis-data-redis-app-4 nfs-client 21m
  9. [root@k8s-master redis]# kubectl get pvc
  10. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  11. redis-data-redis-app-0 Bound pvc-1650521c-ba23-4502-b8e8-c260359a2d27 200M RWX nfs-client 21m
  12. redis-data-redis-app-1 Bound pvc-04c6e1bc-709f-4a77-bd36-7cd8c23d4ca1 200M RWX nfs-client 21m
  13. redis-data-redis-app-2 Bound pvc-63d54f9b-7be4-4ce7-a72b-caa7bedde507 200M RWX nfs-client 21m
  14. redis-data-redis-app-3 Bound pvc-652e836c-f426-4629-8600-d542f4269a52 200M RWX nfs-client 21m
  15. redis-data-redis-app-4 Bound pvc-efc54629-d01d-45ac-9c42-37ecbe34189a 200M RWX nfs-client 21m
  16. redis-data-redis-app-5 Bound pvc-b1eee630-b2fc-4423-be6c-32278d6a8018 200M RWX nfs-client 21m

七、redis集群初始化

        由于Redis集群必须在所有节点启动后才能进⾏初始化,这⾥,我们专⻔启动⼀个Ubuntu的容器,可以在该容器中安装Redis-tribe,进⽽初始化Redis集群;

1)创建Ubuntu容器并配置源
  1. [root@k8s-master redis]# kubectl run -it ubuntu --image=daocloud.io/library/ubuntu:artful-20170619 --restart=Never /bin/bash
  2. If you don't see a command prompt, try pressing enter.
  3. root@ubuntu:/#
  4. # 添加阿里云的Ubuntu源,并安装基本的软件环境
  5. root@ubuntu:/# root@ubuntu:/# cat > /etc/apt/sources.list << EOF
  6. > deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
  7. > deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
  8. > deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
  9. > deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
  10. > deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
  11. > deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
  12. > deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
  13. > deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
  14. > deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  15. > deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  16. > EOF
  17. root@ubuntu:/# apt-get update
  18. ......
  19. root@ubuntu:/# echo $?
  20. 0
  21. root@ubuntu:/# apt-get install -y vim wget python2.7 python-pip redis-tools dnsutils
2)安装redis-trib

root@ubuntu:/# pip install redis-trib==0.5.1

3)创建只有master节点的集群

root@ubuntu:/# redis-trib.py create \
 `dig +short redis-app-0.redis-service.default.svc.cluster.local`:6379 \
 `dig +short redis-app-1.redis-service.default.svc.cluster.local`:6379 \
 `dig +short redis-app-2.redis-service.default.svc.cluster.local`:6379

4)为每个master添加slave

root@ubuntu:/# redis-trib.py replicate \
 --master-addr `dig +short redis-app-0.redis-service.default.svc.cluster.local`:6379 \
 --slave-addr `dig +short redis-app-3.redis-service.default.svc.cluster.local`:6379
root@ubuntu:/# redis-trib.py replicate \
 --master-addr `dig +short redis-app-1.redis-service.default.svc.cluster.local`:6379 \
 --slave-addr `dig +short redis-app-4.redis-service.default.svc.cluster.local`:6379
root@ubuntu:/# redis-trib.py replicate \
 --master-addr `dig +short redis-app-2.redis-service.default.svc.cluster.local`:6379 \
 --slave-addr `dig +short redis-app-5.redis-service.default.svc.cluster.local`:6379

5) 连接到任意一个redis pod中检验一下
  1. [root@k8s-master redis]# kubectl exec -it redis-app-5 /bin/sh
  2. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  3. /data # /usr/local/bin/redis-cli -c
  4. 127.0.0.1:6379> CLUSTER NODES
  5. 42beeed3837a809034874d7415c307e91e2daac8 10.244.2.5:6379@16379 master - 0 1722937557103 1 connected 0-5461
  6. c06e4e94182f72196bcf524e948c6265a8fcfb87 10.244.1.5:6379@16379 master - 0 1722937556000 4 connected 10923-16383
  7. 9bcbf04efa0705a10ab9c80b5379f346f2e32c38 10.244.2.7:6379@16379 myself,slave c06e4e94182f72196bcf524e948c6265a8fcfb87 0 1722937555000 4 connected
  8. 79ebf7ed87288fe7b23065c5e8911878b8dde0e4 10.244.2.6:6379@16379 slave 3413f32da2cb4e177b2a6c57ffd1171850c039c2 0 1722937556296 2 connected
  9. f62ab0f4d9e3ac82163d63310716cc460ac31898 10.244.1.6:6379@16379 slave 42beeed3837a809034874d7415c307e91e2daac8 0 1722937555792 1 connected
  10. 3413f32da2cb4e177b2a6c57ffd1171850c039c2 10.244.1.4:6379@16379 master - 0 1722937556802 2 connected 5462-10922
  11. 127.0.0.1:6379> cluster info
  12. cluster_state:ok
  13. cluster_slots_assigned:16384
  14. cluster_slots_ok:16384
  15. cluster_slots_pfail:0
  16. cluster_slots_fail:0
  17. cluster_known_nodes:6
  18. cluster_size:3
  19. cluster_current_epoch:4
  20. cluster_my_epoch:4
  21. cluster_stats_messages_ping_sent:654
  22. cluster_stats_messages_pong_sent:582
  23. cluster_stats_messages_sent:1236
  24. cluster_stats_messages_ping_received:581
  25. cluster_stats_messages_pong_received:654
  26. cluster_stats_messages_meet_received:1
  27. cluster_stats_messages_received:1236
  28. 127.0.0.1:6379>

八、创建用于访问Service

        还需要创建⼀个Service,专⽤于为Redis集群提供访问和负载均衡;

  1. [root@k8s-master redis]# vim redis-access-service.yaml
  2. [root@k8s-master redis]# cat redis-access-service.yaml
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: redis-access-service
  7. labels:
  8. app: redis
  9. spec:
  10. ports:
  11. - name: redis-port
  12. protocol: "TCP"
  13. port: 6379
  14. targetPort: 6379
  15. selector:
  16. app: redis
  17. appCluster: redis-cluster
  18. [root@k8s-master redis]# kubectl apply -f redis-access-service.yaml
  19. service/redis-access-service created
  20. [root@k8s-master redis]# kubectl get svc
  21. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  22. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
  23. redis-access-service ClusterIP 10.103.21.164 <none> 6379/TCP 11s
  24. redis-service ClusterIP None <none> 6379/TCP 103m
  25. [root@k8s-master redis]# kubectl get ep
  26. NAME ENDPOINTS AGE
  27. cluster.local-nfs-redis-nfs-client-provisioner <none> 116m
  28. kubernetes 192.168.22.134:6443 42h
  29. redis-access-service 10.244.1.4:6379,10.244.1.5:6379,10.244.1.6:6379 + 3 more... 25s
  30. redis-service 10.244.1.4:6379,10.244.1.5:6379,10.244.1.6:6379 + 3 more... 103m

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/948800
推荐阅读
相关标签
  

闽ICP备14008679号