乐趣区

关于运维:K8S-实用工具之五kompose

开篇

📜 引言

  • 磨刀不误砍柴工
  • 工欲善其事必先利其器
  • 第一篇:《K8S 实用工具之一 – 如何合并多个 kubeconfig?》
  • 第二篇:《K8S 实用工具之二 – 终端 UI K9S》
  • 第三篇:《K8S 实用工具之三 – 图形化 UI Lens》
  • 第四篇:《K8S 实用工具之四 – kubectl 实用插件》

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

🎉🎉🎉

参考链接

  • Kubernetes + Compose = Kompose

三人行, 必有我师; 常识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

退出移动版