写在最前

根据你自身的需求选择任意一种部署方式,如果是新人可以先看之前的篇章来完成基础环境的安装与配置。

1. 前置要求

  1. 二进制部署任意版本docker

2. docker 部署

https://hub.docker.com/_/redis

mkdir -p /data/redis/conf /data/redis/data

# 生成配置文件
cat > /data/redis/conf/redis.conf <<EOF
appendonly yes
port 6379
bind 0.0.0.0
# 开启密码访问
requirepass 123456
EOF

docker run -d -p 6379:6379 --restart=always \
-v /data/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /data/redis/data:/data \
--name redis redis:6.2.5 \
redis-server /etc/redis/redis.conf

3. Kubernetes 部署

namespace 我统一设置为 basic ,自行修改存储类相关配置

3.1 configmap

kind: ConfigMap
apiVersion: v1
metadata:
  name: redis-conf
  namespace: basic
  annotations:
    kubesphere.io/creator: admin
data:
  redis.conf: |-
    appendonly yes
    port 6379
    bind 0.0.0.0
    # 开启密码访问
    requirepass 123456

3.2 deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: redis
  namespace: basic
  labels:
    app: redis
  annotations:
    kubesphere.io/creator: admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: redis
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: redis-data
          persistentVolumeClaim:
            claimName: redis-data
        - name: redis-conf
          configMap:
            name: redis-conf
            items:
              - key: redis.conf
                path: redis.conf
            defaultMode: 420
      containers:
        - name: container-rfvam5
          image: 'redis:6.2.5'
          command:
            - redis-server
          args:
            - /etc/redis/redis.conf
          ports:
            - name: http-6379
              containerPort: 6379
              protocol: TCP
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: redis-data
              mountPath: /data
            - name: redis-conf
              readOnly: true
              mountPath: /etc/redis/redis.conf
              subPath: redis.conf
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

3.3 service

使用nodeport方式连接

kind: Service
apiVersion: v1
metadata:
  name: redis
  namespace: basic
  labels:
    app: redis
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: http-6379
      protocol: TCP
      port: 6379
      targetPort: 6379
      nodePort: 31590
  selector:
    app: redis
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

4. 操作总结