参考Github:
https://github.com/kubernetes/kompose
kompose工具用于将docker-compose配置文件转换的k8s可识别的yaml文件
- 安装
CentOS安装
# yum install epel-release
# yum install kompose
使用源安装的版本较老可直接下载
# Linux
curl -L https://github.com/kubernetes/kompose/releases/download/v1.32.0/kompose-linux-amd64 -o kompose
# macOS
curl -L https://github.com/kubernetes/kompose/releases/download/v1.32.0/kompose-darwin-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose
查看版本
# kompose version
1.32.0 (765fde254)
- 转换和使用k8s启动
示例docker-compose.yml
# cat docker-compose.yml
version: "2.0"
services:
nginx:
hostname: nginx
image: nginx
ports:
- 83:80
volumes:
- ./html:/usr/share/nginx/html
该示例使用nginx镜像启动并且挂载一个首页index.htmt
# cat html/index.html
Hello Kompose
可以使用以下命令启动
# docker-compose up -d
访问
转换成k8s可识别的yaml文件
如果yml文件名不是docker-compose.yml则加参数-f 文件名进行转换
# kompose convert
WARN Volume mount on the host "/data/softs/kompose/html" isn't supported - ignoring path on the host
INFO Kubernetes file "nginx-service.yaml" created
INFO Kubernetes file "nginx-deployment.yaml" created
INFO Kubernetes file "nginx-claim0-persistentvolumeclaim.yaml" created
会在当前文件夹生成deployment,pvc,service文件
不能直接使用kubelet apply部署而是需要修改
pvc文件需要修改增加创建该pvc使用的storageclass
使用以下命令获取当时集群的storageclass,如果无storageclass则需要先创建
# kubectl get storageclass
NAME PROVISIONER AGE
gluster-heketi-storageclass kubernetes.io/glusterfs 66d
managed-nfs-storage fuseim.pri/ifs 76d
修改后的pvc文件如下
# cat nginx-claim0-persistentvolumeclaim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
io.kompose.service: nginx-claim0
name: nginx-claim0
spec:
accessModes:
- ReadWriteOnce
storageClassName: gluster-heketi-storageclass
resources:
requests:
storage: 100Mi
在原始内容基础新增一行指定pvc使用的stoageclass
先生成pvc
# kubectl apply -f nginx-claim0-persistentvolumeclaim.yaml
persistentvolumeclaim/nginx-claim0 created
查看
# kubectl get pvc nginx-claim0
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nginx-claim0 Bound pvc-2f0bd9e0-c562-11ee-a9a9-525400e71cb5 1Gi RWO gluster-heketi-storageclass 29s
应用deployment文件部署deployment
# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.32.0 (765fde254)
labels:
io.kompose.service: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: nginx
strategy:
type: Recreate
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.32.0 (765fde254)
labels:
io.kompose.network/kompose-default: "true"
io.kompose.service: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
hostPort: 83
protocol: TCP
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-claim0
hostname: nginx
restartPolicy: Always
volumes:
- name: nginx-claim0
persistentVolumeClaim:
claimName: nginx-claim0
# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx created
查看部署的pod
# kubectl get pod nginx-5d7ccd9d5-mqm92
NAME READY STATUS RESTARTS AGE
nginx-5d7ccd9d5-mqm92 1/1 Running 0 2m1s
把首页拷贝到对应文件夹即可在node节点使用pod地址访问
# kubectl cp html/index.html nginx-5d7ccd9d5-mqm92:/usr/share/nginx/html
部署服务
# kubectl apply -f nginx-service.yaml
service/nginx created
[root@CentOS7K8SMaster01063 kompose]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.32.0 (765fde254)
labels:
io.kompose.service: nginx
name: nginx
spec:
ports:
- name: "83"
port: 83
targetPort: 80
selector:
io.kompose.service: nginx
部署
# kubectl apply -f nginx-service.yaml
service/nginx created
查看
# kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.0.0.20 <none> 83/TCP 60s
默认生成的svc为ClusetrIP模式即只能在集群内访问
在node节点访问,注意本次把port修改为83了即集群ip的83端口对应pod的80端口
# curl 10.0.0.20:83
Hello Kompose
修改service配置文件修改成nodeport
修改后如下
# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.32.0 (765fde254)
labels:
io.kompose.service: nginx
name: nginx
spec:
ports:
- name: "83"
port: 80
targetPort: 80
type: NodePort
selector:
io.kompose.service: nginx
修改以下两处
应用svc文件
# kubectl apply -f nginx-service.yaml
service/nginx configured
查看svc
# kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.0.0.20 <none> 80:32858/TCP 7m23s
在Node内使用ClusterIP访问
# curl 10.0.0.20
在集群外使用任意nodeip+随机的nodeport端口访问
# curl 192.168.3.65:32858
实验完毕删除deployment pvc svc
# kubectl delete -f nginx-deployment.yaml
# kubectl delete -f nginx-claim0-persistentvolumeclaim.yaml
# kubectl delete -f nginx-service.yaml
注意事项
- 如果在docker-compose中有设置容器的重启策略为on-failer则需要修改成always否则转换后不是deployment而是pod
- 如果docker-compose中有挂载配置文件则也会生成对应的pvc配置文件,可以删除对应配置修改成configmap的挂载方式
- 默认转换的service为clusterip格式如有需要则修改成NodePort格式
- 默认转换后的pvc文件是没有stoeageclass信息,需要手动添加