看尽浮华

导航

 

一、准备redis镜像

1.Dockerfile

FROM redis:5.0.5

COPY *.conf /opt/conf/

COPY run.sh /opt/run.sh

RUN chmod +x /opt/run.sh

CMD /opt/run.sh

2.redis配置文件redis.conf

bind 0.0.0.0

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile "/var/run/redis_6379.pid"

loglevel notice

logfile "/var/log/redis.log"

databases 16

always-show-logo yes

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename "dump.rdb"

masterauth "foobared"

replica-serve-stale-data yes

replica-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

replica-priority 100

requirepass "foobared"

maxmemory 15gb

maxmemory-policy volatile-lru

lazyfree-lazy-eviction no

lazyfree-lazy-expire no

lazyfree-lazy-server-del no

replica-lazy-flush no

appendonly no

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 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

stream-node-max-bytes 4096

stream-node-max-entries 100

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit replica 256mb 64mb 60

hz 10

dynamic-hz yes

aof-rewrite-incremental-fsync yes

rdb-save-incremental-fsync yes

3.redis哨兵配置文件sentinel.conf

port 26379

dir "/tmp"

logfile "/var/log/sentinel_log.log"

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster redis-0.redis 6379 2

#使用pod服务名

sentinel auth-pass mymaster foobared

sentinel down-after-milliseconds mymaster 5000

# 设定5秒内没有响应,说明服务器挂了,需要将配置放在sentinel monitor master 127.0.0.1 6379 下面

sentinel failover-timeout mymaster 15000

sentinel parallel-syncs mymaster 2

# 设定15秒内master没有活起来,就重新选举主

sentinel config-epoch mymaster 3

sentinel leader-epoch mymaster 3

4.启动脚本run.sh(使用固定的id号,在重启sentinel时可以重新加入集群中)

#!/bin/bash

pod_seq=`echo $POD_NAME | awk -F"-" '{print $2}'`

if [[ $pod_seq -ne 0 ]];then    #取pod服务名后面的数字,不是0则为从机

    sed -i '/^slaveof /d' /opt/conf/redis.conf

    echo "slaveof redis-0.redis 6379" >> /opt/conf/redis.conf

#redis-0.redis代表第一个redis的访问地址

fi

#固定myid

if [[ $pod_seq -eq 0 ]];then    #为从机

    echo "sentinel myid 1b3512500a23699add9466858bf440102ec3eb54" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-1.redis 26379 5dff85ad8c80e9865464bf629c712dd139cb552f" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-2.redis 26379 b0ccc7d27eb00516c382f008458d92017f28b899" >> /opt/conf/sentinel.conf

fi

if [[ $pod_seq -eq 1 ]];then

    echo "sentinel myid 5dff85ad8c80e9865464bf629c712dd139cb552f" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-0.redis 26379 1b3512500a23699add9466858bf440102ec3eb54" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-2.redis 26379 b0ccc7d27eb00516c382f008458d92017f28b899" >> /opt/conf/sentinel.conf

fi

if [[ $pod_seq -eq 2 ]];then

    echo "sentinel myid b0ccc7d27eb00516c382f008458d92017f28b899" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-0.redis 26379 1b3512500a23699add9466858bf440102ec3eb54" >> /opt/conf/sentinel.conf

    echo "sentinel known-sentinel mymaster redis-1.redis 26379 b0ccc7d27eb00516c382f008458d92017f28b899" >> /opt/conf/sentinel.conf

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

构建镜像

docker build -t BJ03000048.hnapay.com/software/redis-sentine:v2 .

docker push BJ03000048.hnapay.com/software/redis-sentine:v2

 

二、准备K8S yml,redis.yaml

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: redis

  namespace: ykt-dev1

spec:

  serviceName: redis

  selector:

    matchLabels:

      app: redis

  replicas: 3

  template:

    metadata:

      labels:

        app: redis

    spec:

      restartPolicy: Always

      containers:

        - name: redis

          image: redis-sentine:v1

          imagePullPolicy: Always

          env:

            - name: POD_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: ykt-dev1

spec:

  type: NodePort # 配置为NodePort,外部可以访问 可以不用

  ports:

    - name: redis

      port: 6379 # 容器间,服务调用的端口

      targetPort: 6379 # 容器暴露的端口,与Dockerfile暴露端口保持一致

      nodePort: 26380 # NodePort,外部访问的端口  可以不用

  selector:

app: redis

 

java进程连接redis哨兵:

 

连接格式:podname.(headless server name).namespace.svc.cluster.local

参数详解:

imagePullPolicy: [Always | Never | IfNotPresent] # 【String】 每次都尝试重新拉取镜像 | 仅使用本地镜像 | 如果本地有镜像则使用,没有则拉取

livenessProbe: # 【Object】 对Pod内各容器健康检查的设置,当探测无响应几次之后,系统将自动重启该容器。可以设置的方法包括:exec、httpGet和tcpSocket。对一个容器只需要设置一种健康检查的方法

restartPolicy: [Always | Never | OnFailure] # Pod的重启策略 一旦终止运行,都将重启 | 终止后kubelet将报告给master,不会重启 | 只有Pod以非零退出码终止时,kubelet才会重启该容器。如果容器正常终止(退出码为0),则不会重启。

readinessProbe 就绪性探针,用于判断容器内的程序是否存活(或者说是否健康),只有程序(服务)正常, 容器开始对外提供网络访问(启动完成并就绪)

 

posted on 2022-08-03 13:39  看尽浮华  阅读(615)  评论(0编辑  收藏  举报