kubernetes環(huán)境部署單節(jié)點redis數(shù)據(jù)庫的方法
kubernetes部署redis數(shù)據(jù)庫(單節(jié)點)
redis簡介
Redis 是我們常用的非關(guān)系型數(shù)據(jù)庫,在項目開發(fā)、測試、部署到生成環(huán)境時,經(jīng)常需要部署一套 Redis 來對數(shù)據(jù)進行緩存。這里介紹下如何在 Kubernetes 環(huán)境中部署用于開發(fā)、測試的環(huán)境的 Redis 數(shù)據(jù)庫,當然,部署的是單節(jié)點模式,并非用于生產(chǎn)環(huán)境的主從、哨兵或集群模式。單節(jié)點的 Redis 部署簡單,且配置存活探針,能保證快速檢測 Redis 是否可用,當不可用時快速進行重啟。
redis 參數(shù)配置
在使用 Kubernetes 部署應用后,一般會習慣與將應用的配置文件外置,用 ConfigMap 存儲,然后掛載進入鏡像內(nèi)部。這樣,只要修改 ConfigMap 里面的配置,再重啟應用就能很方便就能夠使應用重新加載新的配置,很方便。
部署redis
創(chuàng)建configmap存儲redis配置文件
redis-config.yaml
kind: ConfigMap apiVersion: v1 metadata: name: redis-config namespace: zisefeizhu labels: app: redis data: redis.conf: |- dir /data port 6379 bind 0.0.0.0 appendonly yes protected-mode no requirepass zisefeizhu pidfile /data/redis-6379.pid
Redis 數(shù)據(jù)存儲
Kubernetes 部署的應用一般都是無狀態(tài)應用,部署后下次重啟很可能會漂移到不同節(jié)點上,所以不能使用節(jié)點上的本地存儲,而是使用網(wǎng)絡(luò)存儲對應用數(shù)據(jù)持久化,PV 和 PVC 是 Kubernetes 用于與儲空關(guān)聯(lián)的資源,可與不同的存儲驅(qū)動建立連接,存儲應用數(shù)據(jù),所以接下來我們要創(chuàng)建 Kubernetes PV、PVC 資源。
請參考:http://www.dhdzp.com/article/190491.htm
創(chuàng)建 Deployment 部署 Redis
創(chuàng)建用于 Kubernetes Deployment 來配置部署 Redis 的參數(shù),需要配置 Redis 的鏡像地址、名稱、版本號,還要配置其 CPU 與 Memory 資源的占用,配置探針監(jiān)測應用可用性,配置 Volume 掛載之前創(chuàng)建的 PV、PVC、ConfigMap 資源等等,內(nèi)容如下:
redis-deployment.yaml
---
apiVersion: v1
kind: Service
metadata:
name: redis
labels:
app: redis
spec:
type: ClusterIP
ports:
- name: redis
port: 6379
selector:
app: redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: production-pppharmapack
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
# 進行初始化操作,修改系統(tǒng)配置,解決 Redis 啟動時提示的警告信息
initContainers:
- name: system-init
image: busybox:1.32
imagePullPolicy: IfNotPresent
command:
- "sh"
- "-c"
- "echo 2048 > /proc/sys/net/core/somaxconn && echo never > /sys/kernel/mm/transparent_hugepage/enabled"
securityContext:
privileged: true
runAsUser: 0
volumeMounts:
- name: sys
mountPath: /sys
containers:
- name: redis
image: redis:5.0.8
command:
- "sh"
- "-c"
- "redis-server /usr/local/etc/redis/redis.conf"
ports:
- containerPort: 6379
resources:
limits:
cpu: 1000m
memory: 1024Mi
requests:
cpu: 1000m
memory: 1024Mi
livenessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 300
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 5
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
volumeMounts:
- name: data
mountPath: /data
- name: config
mountPath: /usr/local/etc/redis/redis.conf
subPath: redis.conf
volumes:
- name: data
persistentVolumeClaim:
claimName: zisefeizhu
- name: config
configMap:
name: redis-config
- name: sys
hostPath:
path: /sys
測試redis是否可以正常使用
# ctl get pod -n production-pppharmapack | grep redis redis-7768dc9c56-4kp8l 1/1 Running 0 8m43s ctl exec -it redis-7768dc9c56-4kp8l -n production-pppharmapack -- /bin/sh # redis-cli 127.0.0.1:6379> auth zisefeizhu OK 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "zisefeizhu"
到此這篇關(guān)于kubernetes環(huán)境部署單節(jié)點redis數(shù)據(jù)庫的方法的文章就介紹到這了,更多相關(guān)kubernetes部署redis數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis序列化反序列化不一致導致String類型值多了雙引號問題
這篇文章主要介紹了Redis序列化反序列化不一致導致String類型值多了雙引號問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

