iT邦幫忙

2022 iThome 鐵人賽

DAY 8
0
DevOps

從零開始的Kubernetes轉換系列 第 8

Day 8 kompose 和 kustomize

  • 分享至 

  • xImage
  •  

我們昨天介紹了怎麼利用helm來安裝DB operator結合terraform 建立stateful sets,今天來講一下我如何使用kompose和kustomize 把docker轉換成k8s deploy的步驟。

收先我們先下載kompose這個程式,可以到下面網址https://kompose.io/ 下載。完成之後只要下kompose docker-compose.yml 就會幫我們轉換本來的docker-compose裡面的 service 變成deployment 並且還有port 轉換成service,volume mount轉換成 pvc

feedback-handler-claim0-persistentvolumeclaim.yaml
feedback-handler-claim1-persistentvolumeclaim.yaml
feedback-handler-claim2-persistentvolumeclaim.yaml
feedback-handler-claim3-persistentvolumeclaim.yaml
feedback-handler-claim4-persistentvolumeclaim.yaml
feedback-handler-deployment.yaml
feedback-handler-service.yaml

先進入deployment
把env的部分抓出來放成config
再把volume mount的pvc改一下
我們就可以來改寫kustomize

tree
.
├── base
│   ├── config.txt
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
    └── production
        ├── cpu_count.yaml
        ├── kustomization.yaml
        └── replica_count.yaml

kustomize的結構如以上
把基本的deployment放入base的deployment,基本的service放進去
再寫出kustomization.yaml
如下

commonLabels:
  app: bidagent
resources:
  - deployment.yaml
  - service.yaml
configMapGenerator:
  - name: bidagent-env-configmap
    files:
      - config.txt

然後我們可以寫production的overlay
kustomization.yaml

namePrefix: prod-
commonLabels:
  infra: prod
commonAnnotations:
  note: bidagent-prod-annotation
resources:
  - ../../base
patches:
- replica_count.yaml
- cpu_count.yaml

cpu_count.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bidagent
spec:
  template:
    spec:
      containers:
        - name: bidagent
          resources:
            limits:
              cpu: 1000m

replica_count.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bidagent
spec:
  replicas: 3

之後我們就可以利用kustomize去build我們的yaml檔啦

kustomize build ./overlays/production
apiVersion: v1
data:
  config.txt: |
    ADWIN_CAMP_LIMIT: "10"
kind: ConfigMap
metadata:
  annotations:
    note: bidagent-prod-annotation
  labels:
    app: bidagent
    infra: prod
  name: prod-bidagent-env-configmap-tt8d28f996
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    note: bidagent-prod-annotation
  labels:
    app: bidagent
    infra: prod
  name: prod-bidagent
spec:
  ports:
  - name: "1050"
    port: 1050
    targetPort: 8888
  selector:
    app: bidagent
    infra: prod
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    note: bidagent-prod-annotation
  labels:
    app: bidagent
    infra: prod
  name: prod-bidagent
spec:
  replicas: 3
  selector:
    matchLabels:
      app: bidagent
      infra: prod
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        note: bidagent-prod-annotation
      labels:
        app: bidagent
        infra: prod
    spec:
      containers:
      - args:
        - node
        - index.js
        envFrom:
        - configMapRef:
            name: prod-bidagent-env-configmap-tt8d28f996
        - secretRef:
            name: bidagent-env-secrets
        image: asia.gcr.io/mi-dsp-prod/ghtinc/bidagent:gcp
        name: bidagent
        ports:
        - containerPort: 8888
        resources:
          limits:
            cpu: 1000m
      restartPolicy: Always

這樣我們就可以依照overlay去創建不同的環境
但是基底只要寫一份yaml檔就好,個人是覺得頗方便的


上一篇
IaC Terraform provision(五) - 讓terraform與helm結合
下一篇
Day 9 使用 KMS 來幫 GKE 的 secret 加密
系列文
從零開始的Kubernetes轉換30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言