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) "zisefeizhu"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章