使用kompose 快速轉換dokcer-compose 文件爲k8s deploy 文件

kompose 是一個不錯的快速轉換docker-compose 文件爲k8s 部署yaml文件的工具,使用次工具咱們
能夠將簡單的docker-compose文件,轉換爲複雜的yaml文件,對於使用者來講能夠減小yaml的錯誤,
同時能夠實現docker-compose 向k8s 的快速遷移mysql

簡單docker-compose 文件

version: '3'
services:
  gogs-service:
    image: gogs/gogs
    restart: always
    ports:
      - "10022:22"
      - "10080:3000"
    volumes:
      - ./data/gogs:/data
    depends_on:
      - mysql-gogs
  mysql-gogs:
    image: mysql:5.7.16
    restart: 'always'
    volumes:
      - ./gogs/mysql:/var/lib/mysql
    ports:
      - 3308:3306
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: dalongrong
      MYSQL_DATABASE: gogs
      MYSQL_USER: gogs
      MYSQL_PASSWORD: dalongrong
      TZ: Asia/Shanghai

說明,這是一個gogs git 工具的安裝,docker-compose 文件不是很複雜git

安裝kompose

brew install kompose

生成k8s yaml 部署文件

kompose convert

效果以下,文件很簡單,包含了deploy 以及pvc 的定義,ingress 是我本身添加的

生成的deploy(gogs)github

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: gogs-service
  name: gogs-service
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: gogs-service
    spec:
      containers:
      - image: gogs/gogs
        name: gogs-service
        ports:
        - containerPort: 22
        - containerPort: 3000
        resources: {}
        volumeMounts:
        - mountPath: /data
          name: gogs-service-claim0
      restartPolicy: Always
      volumes:
      - name: gogs-service-claim0
        persistentVolumeClaim:
          claimName: gogs-service-claim0
status: {}

servicesql

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: gogs-service
  name: gogs-service
spec:
  ports:
  - name: "10022"
    port: 10022
    targetPort: 22
  - name: "10080"
    port: 10080
    targetPort: 3000
  selector:
    io.kompose.service: gogs-service
status:
  loadBalancer: {}

說明,mysql 運行須要修改下,使用init container 刪除文件夾docker

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: mysql-gogs
  name: mysql-gogs
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: mysql-gogs
    spec:
      initContainers:
      - name: "remove-lost-found"
        image: "busybox:1.25.0"
        command: ["rm", "-fr", "/var/lib/mysql/lost+found"]
        volumeMounts:
        - name: mysql-gogs-claim0
          mountPath: /var/lib/mysql
      containers:
      - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        env:
        - name: MYSQL_DATABASE
          value: gogs
        - name: MYSQL_PASSWORD
          value: dalongrong
        - name: MYSQL_ROOT_PASSWORD
          value: dalongrong
        - name: MYSQL_USER
          value: gogs
        - name: TZ
          value: Asia/Shanghai
        image: mysql:5.7.16
        name: mysql-gogs
        ports:
        - containerPort: 3306
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-gogs-claim0
      restartPolicy: Always
      volumes:
      - name: mysql-gogs-claim0
        persistentVolumeClaim:
          claimName: mysql-gogs-claim0
status: {}

說明

使用起來仍是比較方便的,能夠減小好多代碼,目前來講,k8s api 的支持感受不是很好,並且還有一些bug,可是不影響
使用,結合這個工具,咱們能夠快速的向k8s應用遷移api

參考架構圖

參考資料

https://github.com/kubernetes/kompose/架構

相關文章
相關標籤/搜索