本文详细介绍了如何在Kubernetes中使用StatefulSet创建并管理有状态的RedisCluster实例,包括配置文件、服务、持久卷和Pod的部署,以及集群初始化、验证和维护操作。
摘要由CSDN通过智能技术生成
1.创建statefulsets有状态应用
创建redis-cluster-sts.yaml 

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
  namespace: redis-cluster
data:
  redis-cluster.conf: |
    daemonize no
    supervised no
    protected-mode no
    bind 0.0.0.0
    port 6379
    cluster-announce-bus-port 16379
    cluster-enabled yes
    appendonly yes
    cluster-node-timeout 5000
    dir /data
    cluster-config-file /data/nodes.conf
    requirepass gNY7VRyxeIZqLLao
    masterauth  gNY7VRyxeIZqLLao
    pidfile /data/redis.pid
    loglevel notice
    logfile /data/redis_log
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service
  namespace: redis-cluster
spec:
  selector:
    app: redis-cluster
  clusterIP: None
  ports:
    - name: redis-6379
      port: 6379
    - name: redis-16379
      port: 16379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service-access
  namespace: redis-cluster
spec:
  selector:
    app: redis-cluster
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-6379
      port: 6379
      targetPort: 6379
      nodePort: 30202
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: redis-cluster
  name: redis-cluster
  namespace: redis-cluster
spec:
  serviceName: redis-cluster-service
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      labels:
        app: redis-cluster
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: redis
          image: redis:6.2.6
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "redis-server", "/etc/redis/redis-cluster.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(POD_IP)"
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
            - name: cluster
              containerPort: 16379
              protocol: TCP
          volumeMounts:
            - name: redis-conf
              mountPath: /etc/redis
            - name: pvc-data
              mountPath: /data
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: redis-conf
          configMap:
            name: redis-cluster-config
            items:
              - key: redis-cluster.conf
                path: redis-cluster.conf
  volumeClaimTemplates:
    - metadata:
        name: pvc-data
        namespace: redis-cluster
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 5G
        accessModes:
        - ReadWriteOnce                           #访问模式为RWO
        storageClassName: "nfs-storage"      #指定使用的存储类,实现动态分配pv
 kubectl apply -f redis-cluster-sts.yaml 

2.查看运行情况

[root@master-data-1 redis]# kubectl get pvc  -n  redis-cluster
NAME                       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-data-redis-cluster-0   Bound    pvc-e8860210-4605-4793-a8e7-ccb6c16858cc   5G         RWO            nfs-storage    56s
pvc-data-redis-cluster-1   Bound    pvc-fe4bb150-b74d-4962-84c3-e2d4062ac2ff   5G         RWO            nfs-storage    41s
pvc-data-redis-cluster-2   Bound    pvc-ea5316a3-b72b-42ea-8e18-0d101d0504c5   5G         RWO            nfs-storage    27s
pvc-data-redis-cluster-3   Bound    pvc-8a68b296-b71c-4686-9b72-1f8093f6cb0f   5G         RWO            nfs-storage    24s
pvc-data-redis-cluster-4   Bound    pvc-7b0bcacf-a0fd-4237-848e-f88cb263bad3   5G         RWO            nfs-storage    19s
pvc-data-redis-cluster-5   Bound    pvc-c3f8134c-2050-49b8-8f9e-6c989b4fdaa4   5G         RWO            nfs-storage    5s


3.查看pvc

[root@master-data-1 redis]# kubectl get pvc  -n  redis-cluster
NAME                       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-data-redis-cluster-0   Bound    pvc-e8860210-4605-4793-a8e7-ccb6c16858cc   5G         RWO            nfs-storage    56s
pvc-data-redis-cluster-1   Bound    pvc-fe4bb150-b74d-4962-84c3-e2d4062ac2ff   5G         RWO            nfs-storage    41s
pvc-data-redis-cluster-2   Bound    pvc-ea5316a3-b72b-42ea-8e18-0d101d0504c5   5G         RWO            nfs-storage    27s
pvc-data-redis-cluster-3   Bound    pvc-8a68b296-b71c-4686-9b72-1f8093f6cb0f   5G         RWO            nfs-storage    24s
pvc-data-redis-cluster-4   Bound    pvc-7b0bcacf-a0fd-4237-848e-f88cb263bad3   5G         RWO            nfs-storage    19s
pvc-data-redis-cluster-5   Bound    pvc-c3f8134c-2050-49b8-8f9e-6c989b4fdaa4   5G         RWO            nfs-storage    5s


4.初始化集群
使用初始化命令

kubectl exec -it redis-cluster-0 -n  redis-cluster   -- redis-cli -a gNY7VRyxeIZqLLao --cluster create --cluster-replicas 1 $(kubectl get pods -n redis-cluster  -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 {end}')

[root@master-data-1 redis]# kubectl exec -it redis-cluster-0 -n  redis-cluster   -- redis-cli -a gNY7VRyxeIZqLLao --cluster create --cluster-replicas 1 $(kubectl get pods -n redis-cluster  -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 {end}')
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.244.185.163:6379 to 10.244.79.58:6379
Adding replica 10.244.79.60:6379 to 10.244.217.230:6379
Adding replica 10.244.79.59:6379 to 10.244.217.231:6379
M: 112d3c4ce15c16b81c9a7219aa175600176f9116 10.244.79.58:6379
   slots:[0-5460] (5461 slots) master
M: 05db9150b8b3e8b4a044f2551d413d3cba8eb690 10.244.217.230:6379
   slots:[5461-10922] (5462 slots) master
M: cdf008e6af4e88a889d37b0bb865f7113e2bf285 10.244.217.231:6379
   slots:[10923-16383] (5461 slots) master
S: b63075e826fee169e01e6008f41a1120321a4be2 10.244.79.59:6379
   replicates cdf008e6af4e88a889d37b0bb865f7113e2bf285
S: 33dd308104c62fddcce2c0fc0f26a5b2b923d59b 10.244.185.163:6379
   replicates 112d3c4ce15c16b81c9a7219aa175600176f9116
S: c551b927f2b34979fa24100e7128e35bc38bd28b 10.244.79.60:6379
   replicates 05db9150b8b3e8b4a044f2551d413d3cba8eb690
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 10.244.79.58:6379)
M: 112d3c4ce15c16b81c9a7219aa175600176f9116 10.244.79.58:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: b63075e826fee169e01e6008f41a1120321a4be2 10.244.79.59:6379
   slots: (0 slots) slave
   replicates cdf008e6af4e88a889d37b0bb865f7113e2bf285
M: 05db9150b8b3e8b4a044f2551d413d3cba8eb690 10.244.217.230:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 33dd308104c62fddcce2c0fc0f26a5b2b923d59b 10.244.185.163:6379
   slots: (0 slots) slave
   replicates 112d3c4ce15c16b81c9a7219aa175600176f9116
S: c551b927f2b34979fa24100e7128e35bc38bd28b 10.244.79.60:6379
   slots: (0 slots) slave
   replicates 05db9150b8b3e8b4a044f2551d413d3cba8eb690
M: cdf008e6af4e88a889d37b0bb865f7113e2bf285 10.244.217.231:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


5.查看状态

[root@master-data-1 redis]# kubectl get pod -n  redis-cluster -o wide 
NAME              READY   STATUS    RESTARTS   AGE   IP               NODE          NOMINATED NODE   READINESS GATES
redis-cluster-0   1/1     Running   0          12m   10.244.79.58     node-data-2   <none>           <none>
redis-cluster-1   1/1     Running   0          11m   10.244.217.230   node-data-1   <none>           <none>
redis-cluster-2   1/1     Running   0          11m   10.244.217.231   node-data-1   <none>           <none>
redis-cluster-3   1/1     Running   0          11m   10.244.79.59     node-data-2   <none>           <none>
redis-cluster-4   1/1     Running   0          11m   10.244.185.163   node-data-3   <none>           <none>
redis-cluster-5   1/1     Running   0          11m   10.244.79.60     node-data-2   <none>           <none>


6.验证redis
#插入100条数据

for line in {1..100};do kubectl exec -it redis-cluster-0 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao -c set wacky_${line} ${line}; done


 
#读取100条数据

for line in {1..100};do kubectl exec -it redis-cluster-1 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao -c get wacky_${line}; done


 
#登入redis

kubectl exec -it redis-cluster-0 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao


 

#查看redis信息
127.0.0.1:6379> cluster info
 
#检查redis集群信息
127.0.0.1:6379> cluster nodes
 
#遍历所有的key    
127.0.0.1:6379> keys *
登录redis-0

kubectl exec -it redis-cluster-0 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao
 
#遍历所有的key    
127.0.0.1:6379> keys *
登录redis-1

kubectl exec -it redis-cluster-1 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao
 
#遍历所有的key    
127.0.0.1:6379> keys *
登录redis-2

kubectl exec -it redis-cluster-2 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao
 
#遍历所有的key    
127.0.0.1:6379> keys *


7.删除pod进行验证
#检查redis集群信息

 
[root@master-data-1 redis]# kubectl exec -it redis-cluster-0 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:98822
cluster_stats_messages_pong_sent:100212
cluster_stats_messages_sent:199034
cluster_stats_messages_ping_received:100207
cluster_stats_messages_pong_received:98822
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:199034
127.0.0.1:6379> cluster nodes
112d3c4ce15c16b81c9a7219aa175600176f9116 10.244.79.58:6379@16379 myself,master - 0 1713832730000 1 connected 0-5460
b63075e826fee169e01e6008f41a1120321a4be2 10.244.79.59:6379@16379 slave cdf008e6af4e88a889d37b0bb865f7113e2bf285 0 1713832732000 3 connected
05db9150b8b3e8b4a044f2551d413d3cba8eb690 10.244.217.230:6379@16379 master - 0 1713832731565 2 connected 5461-10922
33dd308104c62fddcce2c0fc0f26a5b2b923d59b 10.244.185.163:6379@16379 slave 112d3c4ce15c16b81c9a7219aa175600176f9116 0 1713832732972 1 connected
c551b927f2b34979fa24100e7128e35bc38bd28b 10.244.79.60:6379@16379 slave 05db9150b8b3e8b4a044f2551d413d3cba8eb690 0 1713832731000 2 connected
cdf008e6af4e88a889d37b0bb865f7113e2bf285 10.244.217.231:6379@16379 master - 0 1713832732469 3 connected 10923-16383


 删除pod进行验证

kubectl delete  pod redis-cluster-5   -n redis-cluster

[root@master-data-1 redis]# kubectl delete  pod redis-cluster-5   -n redis-cluster
pod "redis-cluster-5" deleted
[root@master-data-1 redis]# kubectl get pod -n redis-cluster
NAME              READY   STATUS    RESTARTS   AGE
redis-cluster-0   1/1     Running   0          15h
redis-cluster-1   1/1     Running   0          15h
redis-cluster-2   1/1     Running   0          15h
redis-cluster-3   1/1     Running   0          15h
redis-cluster-4   1/1     Running   0          15h
redis-cluster-5   1/1     Running   0          25s
 
[root@master-data-1 redis]# kubectl exec -it redis-cluster-1 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster nodes
b63075e826fee169e01e6008f41a1120321a4be2 10.244.79.59:6379@16379 slave cdf008e6af4e88a889d37b0bb865f7113e2bf285 0 1713833362458 3 connected
05db9150b8b3e8b4a044f2551d413d3cba8eb690 10.244.217.230:6379@16379 myself,master - 0 1713833361000 2 connected 5461-10922
112d3c4ce15c16b81c9a7219aa175600176f9116 10.244.79.58:6379@16379 master - 0 1713833361454 1 connected 0-5460
cdf008e6af4e88a889d37b0bb865f7113e2bf285 10.244.217.231:6379@16379 master - 0 1713833361000 3 connected 10923-16383
33dd308104c62fddcce2c0fc0f26a5b2b923d59b 10.244.185.163:6379@16379 slave 112d3c4ce15c16b81c9a7219aa175600176f9116 0 1713833361000 1 connected
c551b927f2b34979fa24100e7128e35bc38bd28b 10.244.217.232:6379@16379 slave 05db9150b8b3e8b4a044f2551d413d3cba8eb690 0 1713833360450 2 connected
127.0.0.1:6379> 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐