使用 ConfigMap 設定 Redis

本頁提供如何使用 ConfigMap 配置 Redis 的實際範例,並以設定 Pod 以使用 ConfigMap 工作為基礎。

目標

  • 建立具有 Redis 組態值的 ConfigMap
  • 建立掛載並使用已建立 ConfigMap 的 Redis Pod
  • 驗證組態是否已正確套用。

開始之前

您需要有一個 Kubernetes 叢集,而且必須設定 kubectl 命令列工具以與您的叢集通訊。建議在至少有兩個節點且未充當控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用這些 Kubernetes 實驗環境之一

若要檢查版本,請輸入 kubectl version

實際範例:使用 ConfigMap 配置 Redis

依照下列步驟使用儲存在 ConfigMap 中的資料配置 Redis 快取。

首先,建立具有空白組態區塊的 ConfigMap

cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
EOF

套用上面建立的 ConfigMap,以及 Redis pod 清單

kubectl apply -f example-redis-config.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

檢查 Redis pod 清單的內容,並注意下列事項

  • 名為 config 的磁碟區由 spec.volumes[1] 建立
  • spec.volumes[1].configMap.items[0] 下的 keypathexample-redis-config ConfigMap 中的 redis-config 鍵公開為 config 磁碟區上名為 redis.conf 的檔案。
  • 然後,config 磁碟區由 spec.containers[0].volumeMounts[1] 掛載在 /redis-master

這具有將上述 example-redis-config ConfigMap 中的 data.redis-config 資料公開為 Pod 內部的 /redis-master/redis.conf 的淨效果。

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/redis.conf"
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf

檢查已建立的物件

kubectl get pod/redis configmap/example-redis-config 

您應該會看到下列輸出

NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          8s

NAME                             DATA   AGE
configmap/example-redis-config   1      14s

回想一下,我們將 example-redis-config ConfigMap 中的 redis-config 鍵保留空白

kubectl describe configmap/example-redis-config

您應該會看到空白的 redis-config

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:

使用 kubectl exec 進入 pod 並執行 redis-cli 工具以檢查目前組態

kubectl exec -it redis -- redis-cli

檢查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它應該會顯示預設值 0

1) "maxmemory"
2) "0"

同樣地,檢查 maxmemory-policy

127.0.0.1:6379> CONFIG GET maxmemory-policy

這也應該產生其預設值 noeviction

1) "maxmemory-policy"
2) "noeviction"

現在,讓我們將一些組態值新增至 example-redis-config ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru    

套用更新的 ConfigMap

kubectl apply -f example-redis-config.yaml

確認 ConfigMap 已更新

kubectl describe configmap/example-redis-config

您應該會看到我們剛新增的組態值

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru

再次透過 kubectl exec 使用 redis-cli 檢查 Redis Pod,以查看組態是否已套用

kubectl exec -it redis -- redis-cli

檢查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它仍然是預設值 0

1) "maxmemory"
2) "0"

同樣地,maxmemory-policy 仍然是 noeviction 預設設定

127.0.0.1:6379> CONFIG GET maxmemory-policy

傳回

1) "maxmemory-policy"
2) "noeviction"

組態值尚未變更,因為 Pod 需要重新啟動才能從相關聯的 ConfigMap 擷取更新的值。讓我們刪除並重新建立 Pod

kubectl delete pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

現在最後一次重新檢查組態值

kubectl exec -it redis -- redis-cli

檢查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它現在應該傳回更新的值 2097152

1) "maxmemory"
2) "2097152"

同樣地,maxmemory-policy 也已更新

127.0.0.1:6379> CONFIG GET maxmemory-policy

它現在反映所需的 allkeys-lru

1) "maxmemory-policy"
2) "allkeys-lru"

透過刪除已建立的資源來清除您的工作

kubectl delete pod/redis configmap/example-redis-config

下一步

上次修改時間為 2023 年 12 月 27 日 PST 下午 3:37:透過 Configmap 更新配置 (3efc5cde2f)