目录

一、配置单节点redis以及持久化

1.编写redis的配置文件

2、创建对应pv-pvc的yaml

3、编写deployment.yaml

4、service.yaml

5.测试持久化 是否生效

二、集群安装(主从)

三、多主多从


一、配置单节点redis以及持久化

这种方式建议开发环境使用

1.编写redis的配置文件

####configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-conf-new
data:
  redis.conf: |+
    requirepass dkongjian20211117
    protected-mode no
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    slave-lazy-flush no
    appendonly yes
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble no
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events Ex
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes

2、创建对应pv-pvc的yaml

此处也可以采用动态挂载,但不好的是,集群挂掉恢复后,不好对应pvc

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wms-redis-data
#  namespace: magi
  labels:
    pv: wms-redis-data
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    # FIXME: use the right IP
    server: 192.168.50.25
    path: "/data/nfsboot/wms-redis-1/"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wms-redis-data
 # namespace: magi
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      pv: wms-redis-data

3、编写deployment.yaml

apiVersion: apps/v1  #  for k8s versions before 1.9.0 use apps/v1beta2  and before 1.8.0 use extensions/v1beta1
kind: Deployment
#kind: StatefulSet
metadata:
  name: redis-master
spec:
 # serviceName: redis-master
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
#        image: redis:3.2.9  # or just image: redis
        image: redis:4.0.0
        command: ["redis-server","/etc/redis/redis.conf"]
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
          - name: REDIS_PASSWORD
            value: dkongjian_20211117
          - name: REDIS_REPLICATION_MODE
            value: master
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: redis-config
          mountPath: /etc/redis/redis.conf
          subPath: redis.conf
        - name: wms-redis-data
          mountPath: /data
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf-new
          items:
          - key: redis.conf
            path: redis.conf
      - name: wms-redis-data
        persistentVolumeClaim:
          claimName: wms-redis-data
#####动态pvc挂载

#  volumeClaimTemplates:
#  - metadata:
#      name: redis
#    spec:
#      accessModes: [ "ReadWriteOnce" ]
#      storageClassName: "nfs"
#      resources:
#        requests:
#          storage: 1Gi

4、service.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

5.测试持久化 是否生效

进入pod内,登录redis后,随便创建几个key,删除pod,等pod启动过后在进入,查看之前创建的key是不是还在

二、集群安装(主从)

以上是redis-master,这里精简点,只写redis-slave的deployment部分,其余照着上一步抄过来就好了

apiVersion: apps/v1  #  for k8s versions before 1.9.0 use apps/v1beta2  and before 1.8.0 use extensions/v1beta1
kind: Deployment
metadata:
  name: redis-slave
spec:
  selector:
    matchLabels:
      app: redis
      role: slave
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: slave
        tier: backend
    spec:
      containers:
      - name: slave
       # image: mirrorgooglecontainers/redis-slave:v2
        image: redis:4.0.0
        command: ["redis-server","/etc/redis/redis.conf"]
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
          - name: REDIS_REPLICATION_MODE
            value: slave
          - name: REDIS_MASTER_HOST
            value: redis
          - name: REDIS_MASTER_PASSWORD
            value: dkongjian20211117
          - name: REDIS_PASSWORD
            value: dkongjian20211117
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: redis-config
          mountPath: /etc/redis/redis.conf
          subPath: redis.conf
        - name: wms-redis-slave-data
          mountPath: /data
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf-new
          items:
          - key: redis.conf
            path: redis.conf
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf-new
          items:
          - key: redis.conf
            path: redis.conf
      - name: wms-redis-slave-data
        persistentVolumeClaim:
          claimName: wms-redis-slave-data

测试,略

三、多主多从

参考:K8S部署Redis Cluster集群(三主三从模式) - 部署笔记 - 散尽浮华 - 博客园

Logo

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

更多推荐