之前的项目在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/