本頁提供了一個實際範例,說明如何使用 ConfigMap 設定 Redis,並延伸自設定 Pod 以使用 ConfigMap 任務。
您需要有一個 Kubernetes 叢集,且必須設定 kubectl 命令列工具使其能與叢集通訊。 建議在至少有兩個未擔任控制平面主機之節點的叢集上執行本教學。 如果您還沒有叢集,可以使用 Minikube 建立一個, 或使用以下其中一個 Kubernetes 練習環境:
若要確認版本,請輸入 kubectl version.
kubectl 1.14 或以上版本。請按照以下步驟,使用 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 鍵,以名為 redis.conf 的檔案形式呈現在 config 卷中spec.containers[0].volumeMounts[1] 接著將 config 卷掛載至 /redis-master結果會將 example-redis-config ConfigMap 中 data.redis-config 的資料,以 /redis-master/redis.conf 的形式於 Pod 內呈現。
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 pod/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 pod/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 pod/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