溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在kubernetes環境中部署一個單節點redis數據庫

發布時間:2021-01-18 15:15:44 來源:億速云 閱讀:259 作者:Leah 欄目:開發技術

怎么在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.jb51.net/article/190491.htm

創建 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"

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女