Kubernetes 可以像 Docker Compose 一样使用吗?

Can Kubernetes be used like Docker Compose?

我已经研究了 Kubernetes 文档好几个小时了。我了解核心设计,以及服务、控制器、pods 等概念。

但是,我不明白的是我可以以声明方式配置集群的过程。也就是说,我可以编写一个配置文件(或一组配置文件)来定义云部署的构成和扩展选项。我希望能够在没有 运行 大量 cli 命令的情况下声明我想要哪些容器 pods、它们将如何通信、它们将如何扩展等。

Kubernetes 有 docker-compose 功能吗?

我希望我的应用程序在 git 中定义——受版本控制——而不依赖于手动 cli 交互。

能不能简明扼要一点?有比官方文档更清楚的参考吗?

Kubernetes当然有自己的yaml(如“Deploying Applications”所示)

但是作为“Docker Clustering Tools Compared: Kubernetes vs Docker Swarm”,它不是(仅仅)为Docker写的,它有自己的系统。

您可以使用 docker-compose 来启动 Kubernetes,如“vyshane/kid": that does mask some of the kubectl commands cli in scripts(可以进行版本控制)所示。

如果您仍在寻找,也许这个工具可以提供帮助:https://github.com/kelseyhightower/compose2kube

您可以创建一个合成文件:

# sample compose file with 3 services
web:
  image: nginx
  ports:
    - "80"
    - "443"
database:
  image: postgres
  ports:
    - "5432"
cache:
  image: memcached
  ports:
    - "11211"

然后使用工具将其转换为kubernetes对象:

compose2kube -compose-file docker-compose.yml -output-dir output

这将创建这些文件:

output/cache-rc.yaml
output/database-rc.yaml
output/web-rc.yaml

然后你可以使用kubectl将它们应用到kubernetes。

如果您已有 Docker 个 Composer 文件,您可以查看 Kompose project

kompose is a tool to help users who are familiar with docker-compose move to Kubernetes. kompose takes a Docker Compose file and translates it into Kubernetes resources.

kompose is a convenience tool to go from local Docker development to managing your application with Kubernetes. Transformation of the Docker Compose format to Kubernetes resources manifest may not be exact, but it helps tremendously when first deploying an application on Kubernetes.

到运行docker-compose.yaml文件或你自己的,运行:

kompose up

使用一个简单的命令将 docker-compose.yaml 转换为 Kubernetes 部署和服务:

$ kompose convert -f docker-compose.yaml
INFO 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

有关更多信息,请查看:http://kompose.io/

Docker 正式宣布了 kubernetes 集群的 docker-compose 功能。因此,从现在开始,您可以将 kubernetes 资源组合在一个文件中,并使用该文件应用它们。

首先,我们需要将 Compose on Kubernetes 控制器安装到您的 Kubernetes 集群中。此控制器使用标准 Kubernetes 扩展点将 Stack 引入 Kubernetes API。查看完整文档以安装 docker 组合控制器:

https://github.com/docker/compose-on-kubernetes

让我们编写一个简单的 compose yaml 文件:

version: "3.7"
services:
  web:
    image: dockerdemos/lab-web
    ports:
     - "33000:80"
  words:
    image: dockerdemos/lab-words
    deploy:
      replicas: 3
      endpoint_mode: dnsrr
  db:
    image: dockerdemos/lab-db

然后我们将使用 docker 客户端将其部署到 Kubernetes 集群 运行 控制器:

$ docker stack deploy --orchestrator=kubernetes -c docker-compose.yml words
Waiting for the stack to be stable and running...
db: Ready       [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
web: Ready      [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
words: Ready    [pod status: 1/3 ready, 2/3 pending, 0/3 failed]
Stack words is stable and running

然后我们可以通过 Kubernetes API 与这些对象进行交互。在这里您可以看到我们已经自动创建了较低级别的对象,如服务、Pods、部署和副本集:

$ kubectl get deployments
NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/db      1         1         1            1           57s
deployment.apps/web     1         1         1            1           57s
deployment.apps/words   3         3         3            3           57s    

请务必注意,这不是一次性转化。 Compose on Kubernetes API 服务器将 Stack 资源引入 Kubernetes API。因此,我们可以在构建应用程序时在同一抽象级别查询和管理所有内容。这使得深入研究上述细节有助于理解事物的工作原理或调试问题,但大多数时候不需要:

$ kubectl get stack
NAME      STATUS      PUBLISHED PORTS   PODS     AGE      
words     Running     33000             5/5      4m