K8S 實用工具之五-kompose

東風微鳴發表於2023-03-07

開篇

? 引言

  • 磨刀不誤砍柴工
  • 工欲善其事必先利其器

Kubernetes + Compose = Kompose

從 Docker Compose 到 Kubernetes 的轉換工具

什麼是 Kompose?

Kompose 是 dockercompose 到 Kubernetes (或 OpenShift) 等容器編排器的轉換工具。

為什麼開發者喜歡它?

  • 使用 Docker Compose 簡化開發過程,然後將容器部署到生產叢集
  • 轉換你的 docker-compose.yaml 需要一個簡單的命令 kompose convert

易如反掌

  1. 找一個 docker-compose.yaml 檔案;
  2. 執行:kompose convert
  3. 執行 kubectl apply 並檢查您的 k8s 叢集為您新部署的容器!
$ wget https://raw.githubusercontent.com/kubernetes/kompose/master/examples/docker-compose-v3.yaml -O docker-compose.yaml

$ kompose convert

$ kubectl apply -f .

$ kubectl get po
NAME                            READY     STATUS              RESTARTS   AGE
frontend-591253677-5t038        1/1       Running             0          10s
redis-master-2410703502-9hshf   1/1       Running             0          10s
redis-slave-4049176185-hr1lr    1/1       Running             0          10s

實戰

比如我要在 K8S 上安裝 RssHub,這是官方提供的 docker-compose.yml:

version: '3'
services:
    rsshub:
        image: diygod/rsshub
        restart: always
        ports:
            - '1200:1200'
        environment:
            NODE_ENV: production
            CACHE_TYPE: redis
            REDIS_URL: 'redis://redis:6379/'
            PUPPETEER_WS_ENDPOINT: 'ws://browserless:3000'
        depends_on:
            - redis
            - browserless
    browserless:
        # See issue 6680
        image: browserless/chrome:1.43-chrome-stable
        restart: always
        ulimits:
          core:
            hard: 0
            soft: 0
    redis:
        image: redis:alpine
        restart: always
        volumes:
            - redis-data:/data
volumes:
    redis-data:

執行 kompose convert 後,從 docker-compose.yml 生成以下檔案:

$ ll
.rw-r--r--  711 casey  1 Dec 21:20 browserless-deployment.yaml
.rw-r--r--  715 casey  1 Dec 21:20 docker-compose.yml
.rw-r--r--  243 casey  1 Dec 21:20 redis-data-persistentvolumeclaim.yaml
.rw-r--r--  867 casey  1 Dec 21:20 redis-deployment.yaml
.rw-r--r-- 1.0k casey  1 Dec 21:20 rsshub-deployment.yaml
.rw-r--r--  352 casey  1 Dec 21:20 rsshub-service.yaml

每個 docker-compose 容器,會生成為一個 deployment,併為你自動轉換好 label 和 env 等欄位,以 rsshub-deployment.yaml 為例:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: rsshub
  name: rsshub
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: rsshub
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.version: 1.26.0 (40646f47)
      creationTimestamp: null
      labels:
        io.kompose.service: rsshub
    spec:
      containers:
        - env:
            - name: CACHE_TYPE
              value: redis
            - name: NODE_ENV
              value: production
            - name: PUPPETEER_WS_ENDPOINT
              value: ws://browserless:3000
            - name: REDIS_URL
              value: redis://redis:6379/
          image: diygod/rsshub
          name: rsshub
          ports:
            - containerPort: 1200
          resources: {}
      restartPolicy: Always
status: {}

Docker compose 的 ports 部分,會轉換為 SVC,以 rsshub-service.yaml 為例:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: rsshub
  name: rsshub
spec:
  ports:
    - name: "1200"
      port: 1200
      targetPort: 1200
  selector:
    io.kompose.service: rsshub
status:
  loadBalancer: {}

Docker compose 的 volumes 欄位,會轉換為 PVC,以 redis-data-persistentvolumeclaim.yaml 為例:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: redis-data
  name: redis-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

安逸!

安裝

# Linux
curl -L https://github.com/kubernetes/kompose/releases/download/v1.25.0/kompose-linux-amd64 -o kompose

# macOS
curl -L https://github.com/kubernetes/kompose/releases/download/v1.25.0/kompose-darwin-amd64 -o kompose

chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

That's All

???

參考連結

三人行, 必有我師; 知識共享, 天下為公. 本文由東風微鳴技術部落格 EWhisper.cn 編寫.

相關文章