使用 ConfigMap 配置 Redis
本頁面提供了一個如何使用 ConfigMap 配置 Redis 的實際示例,並建立在配置 Pod 使用 ConfigMap 任務之上。
目標
- 使用 Redis 配置值建立 ConfigMap
- 建立一個掛載並使用所建立的 ConfigMap 的 Redis Pod
- 驗證配置是否正確應用。
準備工作
你必須擁有一個 Kubernetes 叢集,並且 kubectl 命令列工具必須配置為與你的叢集通訊。建議在至少有兩個不作為控制平面主機的節點的叢集上執行本教程。如果你還沒有叢集,可以使用 minikube 建立一個叢集,或者你可以使用這些 Kubernetes 操場中的一個。
要檢查版本,請輸入 kubectl version
。
- 本頁面所示示例適用於
kubectl
1.14 及更高版本。 - 理解配置 Pod 使用 ConfigMap。
實際示例:使用 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 清單的內容並注意以下事項
- 由
spec.volumes[1]
建立了一個名為config
的卷。 spec.volumes[1].configMap.items[0]
下的key
和path
將example-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:8.0.2
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
再次使用 redis-cli
透過 kubectl exec
檢查 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
下一步
- 瞭解更多關於ConfigMaps的資訊。
- 檢視透過 ConfigMap 更新配置的示例。