1. 程式人生 > >kubernetes環境部署單節點redis

kubernetes環境部署單節點redis

# kubernetes部署redis資料庫(單節點) ## redis簡介 Redis 是我們常用的非關係型資料庫,在專案開發、測試、部署到生成環境時,經常需要部署一套 Redis 來對資料進行快取。這裡介紹下如何在 Kubernetes 環境中部署用於開發、測試的環境的 Redis 資料庫,當然,部署的是單節點模式,並非用於生產環境的主從、哨兵或叢集模式。單節點的 Redis 部署簡單,且配置存活探針,能保證快速檢測 Redis 是否可用,當不可用時快速進行重啟。 ### redis 引數配置 在使用 Kubernetes 部署應用後,一般會習慣與將應用的配置檔案外接,用 ConfigMap 儲存,然後掛載進入映象內部。這樣,只要修改 ConfigMap 裡面的配置,再重啟應用就能很方便就能夠使應用重新載入新的配置,很方便。 ## 部署redis ### 建立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 資料儲存 Kubernetes 部署的應用一般都是無狀態應用,部署後下次重啟很可能會漂移到不同節點上,所以不能使用節點上的本地儲存,而是使用網路儲存對應用資料持久化,PV 和 PVC 是 Kubernetes 用於與儲空關聯的資源,可與不同的儲存驅動建立連線,儲存應用資料,所以接下來我們要建立 Kubernetes PV、PVC 資源。 請參考:https://www.cnblogs.com/zisefeizhu/p/13564547.html ### 建立 Deployment 部署 Redis 建立用於 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 ``` ## 測試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) "zisefei