iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 22
0
DevOps

從題目中學習k8s系列 第 22

【從題目中學習k8s】-【Day22】第十四題 - ConfigMap

  • 分享至 

  • twitterImage
  •  

title: 【從題目中學習k8s】-【Day22】第十四題 - ConfigMap
description: 以無比的恆毅力堅持30天鍊成鐵人--連續30天,一天發表一篇IT技術文章

【從題目中學習k8s】-【Day22】第十四題 - ConfigMap

tags: DevOps CICD K8s Docker

Question

Create a configmap called cfgvolume with values var1=val1,
var2=val2 and create an nginx pod with volume nginx-volume which
reads data from this configmap cfgvolume and put it on the path
/etc/cfg


概念

這題是有關ConfigMap的題目,ConfigMap是甚麼呢?在介紹之前,我們先來了解一下 K8s 中使用環境變數的方式~
K8s 中要為 Pod 添加環境變數,基本的可以透過兩種方式,一種是在Pod YAML中直接加入env參數,範例YAML如下:

apiVersion: v1
kind: Pod
metadata: 
  name: myweb
spec:
  - name: myweb
    image: nginx
    ports:
      - containerPort: 8080
    env: 
      - name: color
        value: blue

env是array架構,所以每個環境變數要以-符號區隔

第二種方式是在創建Pod時在後面加上-e參數,例如:

$ kubectl run my-web -e color=blue

但是難道我們每個 Pod 都要這樣一個一個設定嗎?若我們有1000的 Pod 在集群中,那我們不是得設定1000次環境變數,如此不僅很難維護也很不make sense。聰明的K8s為此提供了一個好用的方式,就是ConfigMap啦~

ConfigMap是一種API object,用於將非機密的資料儲存在key:value pair中

Pod 若要使用ConfigMap可將ConfigMap視為以下方式加以使用

  • 環境變數
  • command參數
  • Volume中的配置文件來使用

有沒有發現ConfigMap和我們先前介紹過的Secret的使用方式很像?的確是,但是他們的差別是ConfigMap不提供保密或加密,若資料是機密的就必須使用Secret

要創建ConfigMap一樣有兩種方式:命令列方式和YAML檔方式

命令列方式

$ kubectl create configmap <configmap-name> --from-literal=<key>=<value>
## 例如
$ kubectl create configmap web-config --from-literal=color=blue \
--from-literal=mod=app

## 或是configMap路徑

$ kubectl create configmap <configmap-name> --from-literal=<config-path>
## 例如
$ kubectl create configmap web-config --from-literal=web-config.properties

YAML檔方式

$ cat config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
data:
  # property-like keys; each key maps to a simple value
  player_initial_lives: "3"
  ui_properties_file_name: "user-interface.properties"
  #
  # file-like keys
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5
  user-interface.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    
$ kubectl apply -f config.yaml

Pod 使用 ConfigMap,可以有環境變數方式及 Volume 掛載兩種方式

環境變數方式

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo
      image: nginx
      ports:
        - containerPort: 8080
      valueFrom:
        - configMapRef:
            name: web-config
              

Volume

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo
      image: nginx
      volumeMounts:
      - name: config
        mountPath: "/config"
        readOnly: true
  volumes:
    ## You set volumes at the Pod level, then mount them into containers inside that Pod
    - name: config
      configMap:
        ## Provide the name of the ConfigMap you want to mount.
        name: web-config
        ## An array of keys from the ConfigMap to create as files
        items:
        - key: "game.properties"
          path: "game.properties"
        - key: "user-interface.properties"
          path: "user-interface.properties"

回到題目,這題就是結合ConfigMapVolume的題目,將ConfigMap創建後以Volume形式掛載到Pod上就ok囉~


Answer

## 創建名為cfgvolume的configMap
$ kubectl create cm cfgvolume --from-literal=var1=val1 --fromliteral=var2=val2
## 確認是否創建成功
$ kubectl describe cm cfgvolume
$ vim nginx-configmap-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  volumes:
  - name: nginx-volume
    configMap:
      name: cfgvolume
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - name: nginx-volume
      mountPath: /etc/cfg

$ kubectl apply -f nginx-configmap-pod.yaml

驗證

## 進入Pod
kubectl exec -it nginx -- /bin/sh
## 確認路徑
cd /etc/cfg

結論

這是一題ConfigMap 的考題,值得一提的是他結合了Volume的觀念,算是一道綜合題,考前可以多複習這類題目。好啦,今天就到這囉~ 謝謝大家~

參考資料

Define Environment Variables for a Container
ConfigMaps
了解 K8S 的 ConfigMap & Secret

Thank you!

You can find me on


上一篇
【從題目中學習k8s】-【Day21】第十三題 - JSON PATH
下一篇
【從題目中學習k8s】-【Day23】TrobleShooting 概念
系列文
從題目中學習k8s31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言