• k8s部署redis哨兵


    一、准备redis镜像

    Dockerfile

    FROM redis:6.0
    MAINTAINER 运维@小兵
    
    COPY *.conf /opt/conf/
    COPY run.sh /opt/run.sh
    RUN apt update -y;apt-get install vim net-tools -y;apt-get clean && \
        chmod +x /opt/run.sh
    
    CMD /opt/run.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    redis配置文件redis.conf

    #绑定到哪台机器,0.0.0.0表示允许所有主机访问
    bind 0.0.0.0
    #redis3.2版本之后加入的特性,yes开启后,如果没有配置bind则默认只允许127.0.0.1访问
    protected-mode yes
    #对外暴露的访问端口
    port 6379
    #登录密码
    requirepass devops
    #主从同步认证密码
    masterauth devops
    #三次握手的时候server端接收到客户端 ack确认号之后的队列值
    tcp-backlog 511
    #服务端与客户端连接超时时间,0表示永不超时
    timeout 0
    #连接redis的时候的密码 hello
    #requirepass hello
    #tcp 保持会话时间是300s
    tcp-keepalive 300
    #redis是否以守护进程运行,如果是,会生成pid
    daemonize yes
    supervised no
    #pid文件路径
    pidfile /var/run/redis_6379.pid
    #日志级别
    loglevel notice
    logfile /var/log/redis.log
    #默认redis有几个db库
    databases 32
    #每间隔900秒,如果一个键值发生变化就触发快照机制
    save 900 1
    save 300 10
    save 60 10000
    #快照出错时,是否禁止redis写入
    stop-writes-on-bgsave-error no
    #持久化到rdb文件时,是否压缩文件
    rdbcompression no
    #持久化到rdb文件是,是否RC64开启验证
    rdbchecksum no
    #持久化输出的时候,rdb文件命名
    dbfilename dump.rdb
    #持久化文件路径
    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
    #是否开启aof备份
    appendonly yes
    #aof备份文件名称
    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
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    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
    #客户端最大连接数
    maxclients 20000
    lazyfree-lazy-eviction yes
    lazyfree-lazy-expire yes
    lazyfree-lazy-server-del yes
    slave-lazy-flush yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    redis哨兵配置文件sentinel.conf

    # 哨兵sentinel实例运行的端口 默认26379
    port 26379
    # 哨兵sentinel的工作目录
    dir "/tmp"
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster redis-0.redis 6379 2
    sentinel auth-pass mymaster devops
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 15000
    # 设定5秒内没有响应,说明服务器挂了,需要将配置放在sentinel monitor master 127.0.0.1 6379 下面
    sentinel parallel-syncs mymaster 2
    # 设定15秒内master没有活起来,就重新选举主
    sentinel config-epoch mymaster 3
    #.表示如果master重新选出来后,其它slave节点能同时并行从新master同步缓存的台数有多少个,显然该值越大,所有slave节点完成同步切换的整体速度越快,但如
    果此时正好有人在访问这些slave,可能造#成读取失败,影响面会更广。最保定的设置为1,只同一时间,只能有一台干这件事,这样其它slave还能继续服务,但是所
    有slave全部完成缓存更新同步的进程将变慢。
    sentinel leader-epoch mymaster 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    启动脚本run.sh

    #!/bin/bash
    
    pod_seq=$(echo $POD_NAME | awk -F"-" '{print $2}')
    if [[ ${pod_seq} -ne 0 ]];then    #为从机
        sed -i '/^slaveof /d' /opt/conf/redis.conf
        echo "slaveof redis-0.redis 6379" >> /opt/conf/redis.conf	#redis-0.redis代表第一个redis的访问地址
    fi
    /usr/local/bin/redis-server /opt/conf/redis.conf
    sleep 15    #如果redis-0没起来,它里面的哨兵也起不来,等待一段时间再启动哨兵
    /usr/local/bin/redis-sentinel /opt/conf/sentinel.conf &
    tail -f /var/log/redis.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    构建镜像

    docker build --pull -t 192.168.1.2/common/redis_sentinel:6.0 .
    docker push 192.168.1.2/common/redis_sentinel:6.0
    
    • 1
    • 2

    二、准备k8s yml—redis-sentinel.yml

    StatefulSet相关信息可以参考:K8S之StatefulSet有状态服务

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: redis
      namespace: redis-ns
    spec:
      serviceName: redis
      selector:
        matchLabels:
          app: redis
      replicas: 3
      template:
        metadata:
          labels:
            app: redis
        spec:
          nodeSelector:
            productLine: redis-ns
            area: wuhan
          restartPolicy: Always
          containers:
            - name: redis
              image: 192.168.1.2/common/redis_sentinel:6.0
              imagePullPolicy: Always
              env:
                - name: POD_NAME
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.name
              livenessProbe:
                tcpSocket:
                  port: 6379
                initialDelaySeconds: 3
                periodSeconds: 5
              readinessProbe:
                tcpSocket:
                  port: 6379
                initialDelaySeconds: 3
                periodSeconds: 5
              ports:
                - containerPort: 6379
              resources:
                requests:
                  memory: 256Mi
                  cpu: 50m
                limits:
                  memory: 256Mi
                  cpu: 200m
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis
      namespace: redis-ns
    spec:
      type: NodePort
      ports:
        - name: redis
          port: 6379
          targetPort: 6379
          nodePort: 26380
      selector:
        app: redis
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    kubectl apply -f redis-sentinel.yml

    会创建三个redis pod

    kubectl get pod -n redis-ns
    
    • 1

    在这里插入图片描述

    三、查看redis哨兵信息

    kubectl exec -it redis-0 -n redis-ns -- bash
    root@redis-0:/data# redis-cli
    127.0.0.1:6379> AUTH devops
    127.0.0.1:6379> info Replication	#查看主从信息
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    127.0.0.1:6379> exit
    root@redis-0:/data# redis-cli -p 26379
    127.0.0.1:26379> info sentinel		#查看哨兵信息
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    四、连接redis哨兵

    k8s其它命令空间的java进程连接redis哨兵

    spring:
      redis:
        database: 4
        # redis哨兵地址
        sentinel:
          master: mymaster
          nodes: redis-0.redis.redis-demo.svc.cluster.local:26379,redis-1.redis.redis-demo.svc.cluster.local:26379,redis-2.redis.redis-demo.svc.cluster.local:26379
        # redis访问密码(默认为空)
        password: devops
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    客户端连接redis

    node节点IP:26380 密码:devops

  • 相关阅读:
    后端返回图片流前端展示图片
    Oracle.xs.dll‘ for module DBD::Oracle: load_file:找不到指定的模块
    猿创征文|那些年我们追过的那些技术
    《深入浅出Spring》父子容器
    Openssl数据安全传输平台003:Protobuf - 部署
    三步写出一篇思路清晰的技术文章
    HTML做一个传统节日端午节 带设计报告4500字
    ES6面向对象
    VUE实现Office文档在线编辑,支持doc/docx、xls/xlsx、ppt/pptx、pdf等
    Apifox 更新|编排模式、Markdown 编辑器升级、自动申请 SSL 证书、用户反馈问题优化
  • 原文地址:https://blog.csdn.net/anqixiang/article/details/125483034