这篇文章主要介绍“k8s怎么部署redis哨兵”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“k8s怎么部署redis哨兵”文章能帮助大家解决问题。
一、准备redis镜像
Dockerfile
FROM redis:6.0MAINTAINER 运维@小兵COPY *.conf /opt/conf/COPY run.sh /opt/run.shRUN apt update -y;apt-get install vim net-tools -y;apt-get clean && \ chmod +x /opt/run.shCMD /opt/run.sh
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 保持会话时间是300stcp-keepalive 300#redis是否以守护进程运行,如果是,会生成piddaemonize yessupervised no#pid文件路径pidfile /var/run/redis_6379.pid#日志级别loglevel noticelogfile /var/log/redis.log#默认redis有几个db库databases 32#每间隔900秒,如果一个键值发生变化就触发快照机制save 900 1save 300 10save 60 10000#快照出错时,是否禁止redis写入stop-writes-on-bgsave-error no#持久化到rdb文件时,是否压缩文件rdbcompression no#持久化到rdb文件是,是否RC64开启验证rdbchecksum no#持久化输出的时候,rdb文件命名dbfilename dump.rdb#持久化文件路径slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100#是否开启aof备份appendonly yes#aof备份文件名称appendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes#客户端最大连接数maxclients 20000lazyfree-lazy-eviction yeslazyfree-lazy-expire yeslazyfree-lazy-server-del yesslave-lazy-flush yes
redis哨兵配置文件sentinel.conf
# 哨兵sentinel实例运行的端口 默认26379port 26379# 哨兵sentinel的工作目录dir "/tmp"sentinel deny-scripts-reconfig yessentinel monitor mymaster redis-0.redis 6379 2sentinel auth-pass mymaster devopssentinel down-after-milliseconds mymaster 5000sentinel 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
启动脚本run.sh
#!/bin/bashpod_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.confsleep 15 #如果redis-0没起来,它里面的哨兵也起不来,等待一段时间再启动哨兵/usr/local/bin/redis-sentinel /opt/conf/sentinel.conf &tail -f /var/log/redis.log
构建镜像
docker build --pull -t 192.168.1.2/common/redis_sentinel:6.0 .docker push 192.168.1.2/common/redis_sentinel:6.0
二、准备k8s yml—redis-sentinel.yml
apiVersion: apps/v1kind: StatefulSetmetadata: name: redis namespace: redis-nsspec: 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: v1kind: Servicemetadata: name: redis namespace: redis-nsspec: type: NodePort ports: - name: redis port: 6379 targetPort: 6379 nodePort: 26380 selector: app: redis
kubectl apply -f redis-sentinel.yml
会创建三个redis pod
kubectl get pod -n redis-ns
三、查看redis哨兵信息
kubectl exec -it redis-0 -n redis-ns -- bashroot@redis-0:/data# redis-cli127.0.0.1:6379> AUTH devops127.0.0.1:6379> info Replication#查看主从信息
127.0.0.1:6379> exitroot@redis-0:/data# redis-cli -p 26379127.0.0.1:26379> info sentinel#查看哨兵信息
四、连接redis哨兵
k8s其它命令空间的java进程连接redis哨兵
127.0.0.1:6379> exitroot@redis-0:/data# redis-cli -p 26379127.0.0.1:26379> info sentinel#查看哨兵信息
客户端连接redis
node节点IP:26380 密码:devops
关于“k8s怎么部署redis哨兵”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。