zoukankan      html  css  js  c++  java
  • Kompose: Docker-compose 到 Kubernetes 的迁移工具

    Docker 让每个人都能够从 Docker Registry 启动一个打包好的 Docker 应用。Docker-Compose在Docker基础上解决了多容器应用之间的依赖启动问题。 

    Docker Compose 借助 yaml 格式的描述文件来定义一个多容器应用,然后就可以用一个简单的 docker-compose up来启动这一应用中的多个容器。然而,Compose 只能够在本地或者 Docker Swarm 集群中运行。

    如果我们需要在 Swarm 之外运行怎么办?比如 Kubernetes?Compose 格式并非为分布式而诞生的,所以我们只能为选择的容器编排工具重新编写应用描述文件。

    现在,在 Kubernetes Incubator 可以找到 Kompose。有了 Kompose,我们能够简单实现从 Docker Swarm 到 Kubernetes 的转换过程,这样就为 Docker 用户敞开了 Kubernetes 的大门。

    Kompose 目前支持 Docker-compose v2 格式,最近还加入了持久卷所有权(PVC)、以及多容器 Pod 的支持。除了缺省的 Kubernetes 之外,我们还支持 Openshift 的发布能力。Kompose 现在还出现在了 Fedora 包中,未来也会进入 CentOS 中去。

    Kompose 是一个 Golang 应用,可以从 Github 上获取。下面让我们跳过 Build 环节直接进入实例。

    Kompose工具能够自动把 Docker Compose 应用转换为 Kubernetes 描述文件。利用简单的 kompose up 命令,可以在 Kubernetes 集群上启动 Compose 应用。

    Docker 的留言板应用

    留言板应用是 Kubernetes 的权威示例。如果要用 Docker Compose 来实现留言板,可以用下面的代码:

    version: "2"
    
    services:
      redis-master:
        image: gcr.io/google_containers/redis:e2e
        ports:
          - "6379"
      redis-slave:
        image: gcr.io/google_samples/gb-redisslave:v1
        ports:
          - "6379"
        environment:
          - GET_HOSTS_FROM=dns
      frontend:
        image: gcr.io/google-samples/gb-frontend:v4
        ports:
          - "80:80"
        environment:
          - GET_HOSTS_FROM=dns

    其中包含了三个服务:

    • 一个 Redis 主节点;
    • 一组能够横向扩展并借助 DNS 找到 Master 的 Redis 从节点;
    • 暴露于 80 端口的 PHP 前端。

    这些组合在一起,让用户可以发表留言,并保存在 Redis 集群中。

    要启动这个应用:

    $ docker-compose -f docker-guestbook.yml up -d
    Creating network "examples_default" with the default driver
    Creating examples_redis-slave_1
    Creating examples_frontend_1
    Creating examples_redis-master_1

    这就是一个简单的 Docker 用法,下面我肯看看如何在不重写任何东西的情况下,让这些工作在 Kubernetes 上完成。

    Kompose 的留言板应用

    Kompose 目前有三个主要的命令:up、down 以及 convert。为了行文方便,我们只简单说一下留言吧应用的启动。

    跟 docker-compose 类似,我们可以用 kompose up 命令处理 Docker compose 文件,来启动应用:

    $ kompose -f ./examples/docker-guestbook.yml up
    We are going to create Kubernetes deployment and service for your dockerized application.
    If you need more kind of controllers, use 'kompose convert' and 'kubectl create -f' instead.
    
    INFO[0000] Successfully created service: redis-master
    INFO[0000] Successfully created service: redis-slave
    INFO[0000] Successfully created service: frontend
    INFO[0000] Successfully created deployment: redis-master
    INFO[0000] Successfully created deployment: redis-slave
    INFO[0000] Successfully created deployment: frontend
    
    Application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc' for details.

    Kompose 自动把 Docker-compose 文件转为 Kuberntes 对象。缺省情况下,他会为一个 Compose 服务创建一个 Deployment 以及一个服务。另外还能自动检测当前的 Kuberntes 端点,并在上面创建对象。可以通过一系列的选项来创建 Replication Controller、Replica Set 或者 Daemon Set。

    就这样完成了自动转换,如果你了解一些 Kubernetes 的话,可以用 kubectl 命令来看看集群上运行的留言板。

    $ kubectl get pods,svc,deployments
    NAME                             READY        STATUS        RESTARTS     AGE
    frontend-3780173733-0ayyx        1/1          Running       0            1m
    redis-master-3028862641-8miqn    1/1          Running       0            1m
    redis-slave-3788432149-t3ejp     1/1          Running       0            1m
    NAME                             CLUSTER-IP   EXTERNAL-IP   PORT(S)      AGE
    frontend                         10.0.0.34    <none>        80/TCP       1m
    redis-master                     10.0.0.219   <none>        6379/TCP     1m
    redis-slave                      10.0.0.84    <none>        6379/TCP     1m
    NAME                             DESIRED      CURRENT       UP-TO-DATE 
    
    AVAILABLE   AGE
    frontend                         1            1             1            1           1m
    redis-master                     1            1             1            1           1m
    redis-slave                      1            1             1            1           1m

    看到了三个服务、三个 Deployment 以及三个 Pod。可以通过 frontend 服务来访问留言板应用。只不过这次的留言板,是从 Docker-Compose 文件启动的。

    20161228141423

    以上给读者快速的介绍了一下 kompose。还有很多激动人心的特性,例如创建不同类型的资源、创建 Helm Chars,甚至可以使用试验性的 Docker bundle 格式进行输入(Lachlan Evenson 的博客:using a Docker bundle with Kubernetes)。可以在我们的 KubeCon 上的视频 中看到完整的演示。

    前往 Kubernetes incubator 获取 Kompose,可以帮助你轻松地把应用从 Docker Compose 迁移为 Kubernetes 集群应用。

  • 相关阅读:
    清除System.Web.HttpRuntime.Cache缓存
    C# 自动添加文件头注释的方法
    MVC中为站点添加是否开启过滤器
    css 设置页面打印分页
    MVC core TempData Session has not been configured for this application or request.
    abp .net core hangfire JobStorage.Current property value has not been initialized
    数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份
    无法执行程序。所执行的命令为 "C:WindowsMicrosoft.NETFramework64v4.0.30319csc.exe" /noconfig /fullpaths @"C:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files oot411ea3248a9fbaun5r0xd.c
    ABP select2 在模态框中搜索框无法输入
    asp access 数据库连接失败(未指定的错误)
  • 原文地址:https://www.cnblogs.com/lexiaofei/p/8041384.html
Copyright © 2011-2022 走看看