Redis 是咱們經常使用的非關係型數據庫,在項目開發、測試、部署到生成環境時,常常須要部署一套 Redis 來對數據進行緩存。這裏介紹下如何在 Kubernetes 環境中部署用於開發、測試的環境的 Redis 數據庫,固然,部署的是單節點模式,並不是用於生產環境的主從、哨兵或集羣模式。單節點的 Redis 部署簡單,且配置存活探針,能保證快速檢測 Redis 是否可用,當不可用時快速進行重啓。html
在使用 Kubernetes 部署應用後,通常會習慣與將應用的配置文件外置,用 ConfigMap 存儲,而後掛載進入鏡像內部。這樣,只要修改 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
Kubernetes 部署的應用通常都是無狀態應用,部署後下次重啓極可能會漂移到不一樣節點上,因此不能使用節點上的本地存儲,而是使用網絡存儲對應用數據持久化,PV 和 PVC 是 Kubernetes 用於與儲空關聯的資源,可與不一樣的存儲驅動創建鏈接,存儲應用數據,因此接下來咱們要建立 Kubernetes PV、PVC 資源。api
請參考:https://www.cnblogs.com/zisefeizhu/p/13564547.html緩存
建立用於 Kubernetes Deployment 來配置部署 Redis 的參數,須要配置 Redis 的鏡像地址、名稱、版本號,還要配置其 CPU 與 Memory 資源的佔用,配置探針監測應用可用性,配置 Volume 掛載以前建立的 PV、PVC、ConfigMap 資源等等,內容以下:
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: # 進行初始化操做,修改系統配置,解決 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
# 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"