将 docker compose的yml文件转换为 Kubernetes的yaml资源清单

之前的项目在docker中部署,利用的是docker-compose部署的,现在需要迁移到K8S集群中? 因为docker-compose文件是yml格式,k8s是yaml文件格式,他们的格式是不一样的,需要我们把yml文件编写成yaml文件,才能在K8S中部署。 这个便宜就特别麻烦了,我们该怎么办?这里告诉你一个利用软件转换的方法。

docker-compose.yml文件转换为kubernetes的yaml文件

第一:安装 Kompose

根据不同的系统安装 kompose这个软件,我这里为红帽和centos系统为例

 [root@k8s-master ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@k8s-master ~]# sudo yum -y install kompose #安装kompose

第二:上传docker-compose.yml文件

[root@k8s-master ~]# mkdir  mqq
[root@k8s-master ~]# cd mqq/
[root@k8s-master mqq]# ls  #注意文件名称必须是docker-compose.yml
docker-compose.yml
[root@k8s-master mqq]# cat docker-compose.yml 
version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: nginx-latest
    restart: always
    ports:
      - 80:80
    volumes:
       - /data/nginx/html:/usr/share/nginx/html
       - /data/download:/data/download
       - /data/nginx/logs:/var/log/nginx
       - /conf/nginx/nginx.conf:/etc/nginx/nginx.conf
       - /app/web/dist:/app/web/dist
  mysql:
    image: mysql:5.7
    container_name: mysql-5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3306:3306
    volumes:
       - /data/mysql/db:/var/lib/mysql
       - /conf/mysql:/etc/mysql/conf.d
       - /data/mysql/log:/var/log/mysql
       - /data/mysql/log/mysqld.log:/var/log/mysqld.log
       - /docker/tsdb.sql:/tsdb.sql
  redis:
    image: redis:5.0.5
    container_name: redis-5.0.5
    restart: always
    command: redis-server --appendonly yes --requirepass redis@Agilor2021 --protected-mode no 
    volumes:
      - /data/redis:/data
      - /conf/redis/redis.conf:/etc/redis.conf
    ports:
      - 6379:6379

[root@k8s-master mqq]# 

第三:将docker-compose.yml文件转换为yaml文件

[root@k8s-master mqq]# ls
docker-compose.yml
[root@k8s-master mqq]# kompose convert
WARN Volume mount on the host "/data/mysql/db" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/conf/mysql" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/mysql/log" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/mysql/log/mysqld.log" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/docker/tsdb.sql" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/nginx/html" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/download" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/nginx/logs" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/conf/nginx/nginx.conf" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/app/web/dist" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/data/redis" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/conf/redis/redis.conf" isn't supported - ignoring path on the host 
INFO Kubernetes file "mysql-service.yaml" created 
INFO Kubernetes file "nginx-service.yaml" created 
INFO Kubernetes file "redis-service.yaml" created 
INFO Kubernetes file "mysql-deployment.yaml" created 
INFO Kubernetes file "mysql-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "mysql-claim1-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "mysql-claim2-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "mysql-claim3-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "mysql-claim4-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "nginx-deployment.yaml" created 
INFO Kubernetes file "nginx-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "nginx-claim1-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "nginx-claim2-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "nginx-claim3-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "nginx-claim4-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "redis-deployment.yaml" created 
INFO Kubernetes file "redis-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "redis-claim1-persistentvolumeclaim.yaml" created 
[root@k8s-master mqq]# ls
docker-compose.yml                       mysql-claim4-persistentvolumeclaim.yaml  nginx-claim2-persistentvolumeclaim.yaml  redis-claim0-persistentvolumeclaim.yaml
mysql-claim0-persistentvolumeclaim.yaml  mysql-deployment.yaml                    nginx-claim3-persistentvolumeclaim.yaml  redis-claim1-persistentvolumeclaim.yaml
mysql-claim1-persistentvolumeclaim.yaml  mysql-service.yaml                       nginx-claim4-persistentvolumeclaim.yaml  redis-deployment.yaml
mysql-claim2-persistentvolumeclaim.yaml  nginx-claim0-persistentvolumeclaim.yaml  nginx-deployment.yaml                    redis-service.yaml
mysql-claim3-persistentvolumeclaim.yaml  nginx-claim1-persistentvolumeclaim.yaml  nginx-service.yaml
[root@k8s-master mqq]# 

参考其他系统安装 kompose的方法:
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/translate-compose-kubernetes/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_14910065

你的支持是我最大的努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值