前言

很多时候,因为开发环境的资源问题,我们不得不将项目的环境分为 Docker Compose 和 Kubernetes,这样在本地开发环境则不需要运行完整的 Kubernetes 服务!但同时也产生了一些而外的工作量,例如要同时维护 docker-compose.yaml 和 Kubernetes 的 manifests.yaml 文件,甚至是 HELM 的配置文件等等。

Score 的诞生就是为了解决这个问题,他通过自定义的 YAML Specification,来描述容器、服务、资源等配置,然后通过对应的 CLI 将 score.yaml 的配置转换为对应平台的 YAML 配置!

Score 的 Slogan:

Configure once. Deploy anywhere. From local to prod.

安装

brew install score-spec/tap/score-k8s
brew install score-spec/tap/score-helm
brew install score-spec/tap/score-compose

其他操作系统或平台可以参考官方文档

试用

初始化 Score 项目:

score-k8s init
score-compose init
tree -a
.
├── .score-compose
│   ├── mounts
│   ├── state.yaml
│   └── zz-default.provisioners.yaml
├── .score-k8s
│   ├── state.yaml
│   └── zz-default.provisioners.yaml
└── score.yaml

4 directories, 5 files

编排

编辑 Score CLI 生成的 score.yaml 文件,例如我这边写了一个 Nginx 的示例:

apiVersion: score.dev/v1b1

metadata:
  name: ects

containers:
  nginx:
    image: nginx:latest
    variables:
      TZ: Asia/Shanghai

service:
  ports:
    http:
      port: 80
      targetPort: 80
    https:
      port: 443
      targetPort: 443

resources: {}

更多 score.yaml 的 Specification 可以参考官方的文档

生成配置

score-k8s generate score.yaml
INFO: Added score file to project
INFO: Primed resources
INFO: Loaded provisioners
INFO: Persisted state file
INFO: Wrote 2 manifests to manifests buffer for workload 'ects'
INFO: Wrote manifests to 'manifests.yaml'

score-compose generate score.yaml
INFO: Loaded state directory with docker compose project 'nginx'
INFO: Validated workload 'ects'
INFO: Successfully loaded 12 resource provisioners
INFO: Converting workload 'ects' to Docker compose

tree -a
.
├── .score-compose
│   ├── mounts
│   ├── state.yaml
│   └── zz-default.provisioners.yaml
├── .score-k8s
│   ├── state.yaml
│   └── zz-default.provisioners.yaml
├── compose.yaml
├── manifests.yaml
└── score.yaml

4 directories, 7 files

最终生成了 compose.yamlmanifests.yaml 两个 YAML 配置文件,分别如下:

compose.yaml 文件内容如下:

name: nginx
services:
    ects-nginx:
        annotations:
            compose.score.dev/workload-name: ects
        environment:
            TZ: Asia/Shanghai
        hostname: ects
        image: nginx:latest

manifests.yaml 文件内容如下:

---
apiVersion: v1
kind: Service
metadata:
    annotations:
        k8s.score.dev/workload-name: ects
    creationTimestamp: null
    labels:
        app.kubernetes.io/instance: ects-38d609b839
        app.kubernetes.io/managed-by: score-k8s
        app.kubernetes.io/name: ects
    name: ects-svc
spec:
    ports:
        - name: http
          port: 80
          protocol: TCP
          targetPort: 80
        - name: https
          port: 443
          protocol: TCP
          targetPort: 443
    selector:
        app.kubernetes.io/instance: ects-38d609b839
status:
    loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
    annotations:
        k8s.score.dev/workload-name: ects
    creationTimestamp: null
    labels:
        app.kubernetes.io/instance: ects-38d609b839
        app.kubernetes.io/managed-by: score-k8s
        app.kubernetes.io/name: ects
    name: ects
spec:
    replicas: 1
    selector:
        matchLabels:
            app.kubernetes.io/instance: ects-38d609b839
    strategy: {}
    template:
        metadata:
            annotations:
                k8s.score.dev/workload-name: ects
            creationTimestamp: null
            labels:
                app.kubernetes.io/instance: ects-38d609b839
                app.kubernetes.io/managed-by: score-k8s
                app.kubernetes.io/name: ects
        spec:
            containers:
                - env:
                    - name: TZ
                      value: Asia/Shanghai
                  image: nginx:latest
                  name: nginx
                  resources: {}
status: {}

总结

目前来说 Score 还处于早期阶段,支持的配置相较于 docker-compose.yaml 和 Kubernetes 的 manifests.yaml,其描述能力还比较弱,无法完全去描述个平台所支持的一些特性!

但该项目已经进入 CNCN 的沙盒进行孵化,相信未来会越来越完善,能为大幅提高广大 YAML 开发者的工作效率!

I hope this is helpful, Happy hacking…