使用kompose 快速转换dokcer-compose 文件为k8s deploy 文件
kompose 是一个不错的快速转换docker-compose 文件为k8s 部署yaml文件的工具,使用次工具我们
可以将简单的docker-compose文件,转换为复杂的yaml文件,对于使用者来说可以减少yaml的错误,
同时可以实现docker-compose 向k8s 的快速迁移
简单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 文件不是很复杂
安装kompose
brew install kompose
生成k8s yaml 部署文件
kompose convert
效果如下,文件很简单,包含了deploy 以及pvc 的定义,ingress 是我自己添加的
生成的deploy(gogs)
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: {}
service
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 删除文件夹
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应用迁移
参考架构图
参考资料
https://www.ctyun.cn/portal/link.html?target=https%3A%2F%2Fgithub.com%2Fkubernetes%2Fkompose%2F