網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、準(zhǔn)備redis鏡像
Dockerfile
FROM redis:6.0 MAINTAINER 運(yùn)維@小兵 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
redis配置文件redis.conf
#綁定到哪臺(tái)機(jī)器,0.0.0.0表示允許所有主機(jī)訪問(wèn) bind 0.0.0.0 #redis3.2版本之后加入的特性,yes開(kāi)啟后,如果沒(méi)有配置bind則默認(rèn)只允許127.0.0.1訪問(wèn) protected-mode yes #對(duì)外暴露的訪問(wèn)端口 port 6379 #登錄密碼 requirepass devops #主從同步認(rèn)證密碼 masterauth devops #三次握手的時(shí)候server端接收到客戶端 ack確認(rèn)號(hào)之后的隊(duì)列值 tcp-backlog 511 #服務(wù)端與客戶端連接超時(shí)時(shí)間,0表示永不超時(shí) timeout 0 #連接redis的時(shí)候的密碼 hello #requirepass hello #tcp 保持會(huì)話時(shí)間是300s tcp-keepalive 300 #redis是否以守護(hù)進(jìn)程運(yùn)行,如果是,會(huì)生成pid daemonize yes supervised no #pid文件路徑 pidfile /var/run/redis_6379.pid #日志級(jí)別 loglevel notice logfile /var/log/redis.log #默認(rèn)redis有幾個(gè)db庫(kù) databases 32 #每間隔900秒,如果一個(gè)鍵值發(fā)生變化就觸發(fā)快照機(jī)制 save 900 1 save 300 10 save 60 10000 #快照出錯(cuò)時(shí),是否禁止redis寫(xiě)入 stop-writes-on-bgsave-error no #持久化到rdb文件時(shí),是否壓縮文件 rdbcompression no #持久化到rdb文件是,是否RC64開(kāi)啟驗(yàn)證 rdbchecksum no #持久化輸出的時(shí)候,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 #是否開(kāi)啟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 #客戶端最大連接數(shù) maxclients 20000 lazyfree-lazy-eviction yes lazyfree-lazy-expire yes lazyfree-lazy-server-del yes slave-lazy-flush yes
redis哨兵配置文件sentinel.conf
# 哨兵sentinel實(shí)例運(yùn)行的端口 默認(rèn)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 # 設(shè)定5秒內(nèi)沒(méi)有響應(yīng),說(shuō)明服務(wù)器掛了,需要將配置放在sentinel monitor master 127.0.0.1 6379 下面 sentinel parallel-syncs mymaster 2 # 設(shè)定15秒內(nèi)master沒(méi)有活起來(lái),就重新選舉主 sentinel config-epoch mymaster 3 #.表示如果master重新選出來(lái)后,其它slave節(jié)點(diǎn)能同時(shí)并行從新master同步緩存的臺(tái)數(shù)有多少個(gè),顯然該值越大,所有slave節(jié)點(diǎn)完成同步切換的整體速度越快,但如 果此時(shí)正好有人在訪問(wèn)這些slave,可能造#成讀取失敗,影響面會(huì)更廣。最保定的設(shè)置為1,只同一時(shí)間,只能有一臺(tái)干這件事,這樣其它slave還能繼續(xù)服務(wù),但是所 有slave全部完成緩存更新同步的進(jìn)程將變慢。 sentinel leader-epoch mymaster 3
啟動(dòng)腳本run.sh
#!/bin/bash pod_seq=$(echo $POD_NAME | awk -F"-" '{print $2}') if [[ ${pod_seq} -ne 0 ]];then #為從機(jī) sed -i '/^slaveof /d' /opt/conf/redis.conf echo "slaveof redis-0.redis 6379" >> /opt/conf/redis.conf #redis-0.redis代表第一個(gè)redis的訪問(wèn)地址 fi /usr/local/bin/redis-server /opt/conf/redis.conf sleep 15 #如果redis-0沒(méi)起來(lái),它里面的哨兵也起不來(lái),等待一段時(shí)間再啟動(dòng)哨兵 /usr/local/bin/redis-sentinel /opt/conf/sentinel.conf & tail -f /var/log/redis.log
構(gòu)建鏡像
docker build --pull -t 192.168.1.2/common/redis_sentinel:6.0 . docker push 192.168.1.2/common/redis_sentinel:6.0
二、準(zhǔn)備k8s yml—redis-sentinel.yml
StatefulSet相關(guān)信息可以參考:K8S之StatefulSet有狀態(tài)服務(wù)
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
kubectl apply -f redis-sentinel.yml
會(huì)創(chuàng)建三個(gè)redis pod
kubectl get pod -n redis-ns
三、查看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 #查看主從信息
127.0.0.1:6379> exit root@redis-0:/data# redis-cli -p 26379 127.0.0.1:26379> info sentinel #查看哨兵信息
四、連接redis哨兵
k8s其它命令空間的java進(jìn)程連接redis哨兵
127.0.0.1:6379> exit root@redis-0:/data# redis-cli -p 26379 127.0.0.1:26379> info sentinel #查看哨兵信息
客戶端連接redis
node節(jié)點(diǎn)IP:26380 密碼:devops
原文鏈接:https://blog.csdn.net/anqixiang/article/details/125483034
相關(guān)推薦
- 2023-04-09 python實(shí)現(xiàn)數(shù)組平移K位問(wèn)題_python
- 2022-04-27 C語(yǔ)言陷阱與缺陷之?dāng)?shù)組越界訪問(wèn)詳解_C 語(yǔ)言
- 2022-02-07 SecureCRT連Linux服務(wù)器,提示The remote system refused the
- 2022-09-13 本地使用Docker搭建go開(kāi)發(fā)環(huán)境的全過(guò)程_Golang
- 2022-12-05 關(guān)于adfuller函數(shù)返回值的參數(shù)說(shuō)明與記錄_python
- 2022-09-27 Kotlin示例講解標(biāo)準(zhǔn)函數(shù)with與run和apply的使用_Android
- 2022-12-14 python矩陣的基本運(yùn)算及各種操作_python
- 2022-10-31 Python實(shí)現(xiàn)將DNA序列存儲(chǔ)為tfr文件并讀取流程介紹_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支