我使用kompose将docker-compose.yml转换为Azure Kubernetes Service(AKS)来运行

简述

使用kcompose convert命令将此文章中的docker-compose.yml文件转换为Kubernetes的清单,并确保在AKS上可以顺利运行容器。

本地环境

macOS Big Sur 11.3 (macOS 大 Sur 11.3)
python 3.8.3 (Python 3.8.3)

预备工作

按照这篇文章的参考,已经构建了AKS和ACR,并完成了在AKS集群上运行的节点的确认。


创建宣言

安装Kompose

$ curl -L https://github.com/kubernetes/kompose/releases/download/v1.22.0/kompose-darwin-amd64 -o kompose
$ chmod +x kompose
$ sudo mv ./kompose /usr/local/bin/kompose      

将 Docker Compose 配置文件 docker-compose.yml 进行转换

我们将使用以下的docker-compose.yml文件作为目标。

version: "2"

services:

  redis-master:
    image: k8s.gcr.io/redis:e2e
    ports:
      - "6379"

  redis-slave:
    image: gcr.io/google_samples/gb-redisslave:v3
    ports:
      - "6379"
    environment:
      - GET_HOSTS_FROM=dns

  frontend:
    image: gcr.io/google-samples/gb-frontend:v4
    ports:
      - "80:80"
    environment:
      - GET_HOSTS_FROM=dns
    labels:
      kompose.service.type: LoadBalancer

执行将转换到 Kubernetes 的操作

$ kompose convert -f docker-compose.yml   

IINFO Kubernetes file "frontend-service.yaml" created 
INFO Kubernetes file "redis-master-service.yaml" created 
INFO Kubernetes file "redis-slave-service.yaml" created 
INFO Kubernetes file "frontend-deployment.yaml" created 
INFO Kubernetes file "redis-master-deployment.yaml" created 
INFO Kubernetes file "redis-slave-deployment.yaml" created 

在AKS上启动Pod

确认在集群上运行的节点。

$ kubectl get node

创建Pod

$ kubectl apply -f frontend-service.yaml,redis-master-service.yaml,redis-slave-service.yaml,frontend-deployment.yaml,redis-master-deployment.yaml,redis-slave-deployment.yaml

service/frontend created
service/redis-master created
service/redis-slave created
deployment.apps/frontend created
deployment.apps/redis-master created
deployment.apps/redis-slave created

确认Pod

$ kubectl get pods

NAME                           READY   STATUS    RESTARTS   AGE
frontend-6bd559fc9d-8r75p      1/1     Running   0          47s
redis-master-77b6f7887-bmjg7   1/1     Running   0          47s
redis-slave-7d89659bdc-nd7kj   1/1     Running   0          47s

确认应用程序

确认服务

$ kubectl describe svc frontend

Name:                     frontend
Namespace:                default
Labels:                   io.kompose.service=frontend
Annotations:              kompose.cmd: kompose convert -f docker-compose.yml
                          kompose.service.type: LoadBalancer
                          kompose.version: 1.22.0 (955b78124)
Selector:                 io.kompose.service=frontend
Type:                     LoadBalancer
IP Families:              <none>
IP:                       10.0.7.99
IPs:                      <none>
LoadBalancer Ingress:     20.89.87.139
Port:                     80  80/TCP
TargetPort:               80/TCP
NodePort:                 80  31064/TCP
Endpoints:                10.244.0.7:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  110s  service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   105s  service-controller  Ensured load balancer

访问应用程序

$ curl http://20.89.87.139

<html ng-app="redis">
  <head>
    <title>Guestbook</title>
        :
  </head>
  <body ng-controller="RedisCtrl">
        :
  </body>
</html>

删除Pod

$ kubectl delete -f frontend-service.yaml,redis-master-service.yaml,redis-slave-service.yaml,frontend-deployment.yaml,redis-master-deployment.yaml,redis-slave-deployment.yaml

最终

通过将docker-compose.yml转换为kompose生成部署和服务清单,然后使用kubectl apply将它们部署到AKS,确认应用程序正常运行。

广告
将在 10 秒后关闭
bannerAds