SoFunction
Updated on 2025-05-16

Project Practice Deployed by K8S redis

The steps to deploy a single Redis instance/cluster in Kubernetes 1.26.14 are as follows (combined with NFS persistent storage and authentication configuration):

1. Deploy Redis single instance (StatefulSet mode)

1. Create a configuration file

# ConfigMap storage Redis configurationapiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  : |
    bind 0.0.0.0
    port 6379
    requirepass your_secure_password  # Replace with the actual password    appendonly yes
    dir /data

---
# Password SecretapiVersion: v1
kind: Secret
metadata:
  name: redis-secret
type: Opaque
data:
  password: eW91cl9zZWN1cmVfcGFzc3dvcmQ=  # base64 encoded password
---
# StatefulSet DefinitionapiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis-headless
  replicas: 1  # Single instance  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7.0.12
        command: ["redis-server", "/etc/redis/"]
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: config
          mountPath: /etc/redis
        - name: data
          mountPath: /data
        env:
        - name: REDIS_PASSWORD  # Inject password from Secret          valueFrom:
            secretKeyRef:
              name: redis-secret
              key: password
      volumes:
      - name: config
        configMap:
          name: redis-config
  volumeClaimTemplates:  # Dynamic PVC  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "nfs-redis"  # NFS StorageClass needs to be created in advance      resources:
        requests:
          storage: 5Gi

---
# Headless Service (Internal DNS)apiVersion: v1
kind: Service
metadata:
  name: redis-headless
spec:
  clusterIP: None
  selector:
    app: redis
  ports:
  - port: 6379
    name: redis

---
# NodePort Service (external access)apiVersion: v1
kind: Service
metadata:
  name: redis-external
spec:
  type: NodePort
  selector:
    app: redis
  ports:
  - port: 6379
    targetPort: 6379
    nodePort: 31000  # Custom port range 30000-32767

2. Deploy resources

kubectl apply -f 

2. Deploy Redis cluster (6 nodes 3 masters 3 slaves)

1. Adjust StatefulSet configuration

# Modify the StatefulSet sectionspec:
  replicas: 6  #6 Node  template:
    spec:
      containers:
      - env:
        - name: REDIS_CLUSTER_ENABLED  # Enable cluster mode          value: "yes"
# Other parts are the same as single instance

2. Initialize the cluster

# Enter any Redis Pod to perform cluster initializationkubectl exec -it redis-0 -- redis-cli -a your_password \
  --cluster create \
  $(kubectl get pods -l app=redis -o jsonpath='{[*]}{.}:6379 ') \
  --cluster-replicas 1

3. Key configuration instructions

  • Persistent storage

    • Use NFS dynamic provisioning (StorageClass needs to be deployed in advance)
    • volumeClaimTemplatesAutomatically create PVC, data directory/dataPersistence
  • Safety certification

    • Manage passwords with Secret to avoid plain text exposure
    • requirepassConfigure mandatory authentication access
  • Service exposure

    • Headless Service provides internal DNS resolution (-headless
    • NodePort Service allows external access through node IP:31000
  • Cluster mode extension

    • The cluster needs to be manually initialized during the deployment of 6 nodes (redis-cli --cluster create
    • It is recommended to use Sentinel mode to achieve high availability in the production environment.

4. Verification and deployment

# Check resource statuskubectl get statefulset,pvc,svc -l app=redis

# Test single instance connectionkubectl exec -it redis-0 -- redis-cli -a your_password ping

# Check cluster status (cluster mode)kubectl exec -it redis-0 -- redis-cli -a your_password cluster nodes

This is the end of this article about the project practice of K8S redis deployment. For more related content on K8S redis deployment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!