使用单个deploy.json文件构建、推送和部署k8s服务,为多个生产服务提供通用约定。
项目描述
deploy
使用单个deploy.json文件构建、推送和部署k8s服务,为多个生产服务提供通用约定。
pip install python-deploy
使您的Docker设置可部署
首先,在deploy/deploy.json
路径下创建一个文件,指向您的Dockerfile。
建议将您的Dockerfile放在
deploy/docker
子目录下。
{
"fancy-service": {
"docker": {
"image": "certpl/fancy-service",
"dockerfile": "deploy/docker/Dockerfile",
"dir": "."
}
}
}
然后,您可以使用deploy build
来构建您的Docker镜像。您的镜像将使用Git提交哈希进行标记。
$ deploy build
[INFO] Building image for fancy-service
[INFO] Building certpl/fancy-service:cb85eb9d38c407e462a6681351dfd36331635329
如果您想提供替代版本标记,请使用--version
。您可以提供任何您想要的标记,但其中一些是特殊的
date
使用当前时间戳(例如v20200302170007
)commit
是默认的- 其他字符串按原样使用
如果您想将(并构建)镜像推送到Docker Registry或Docker Hub,请使用deploy push
$ deploy push
[INFO] Building image for fancy-service
[INFO] Building certpl/fancy-service:cb85eb9d38c407e462a6681351dfd36331635329
[INFO] Pushing image for fancy-service
[INFO] Pushing certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
使您的k8s部署可部署
通过配置k8s部署增强您的deploy/deploy.json
Deploy支持两个环境:k8s
(生产)和k8s-staging
(预发布)。
以下是一个完整的deploy.json
文件示例
{
"fancy-service": {
"docker": {
"image": "certpl/fancy-service",
"dockerfile": "deploy/docker/Dockerfile",
"dir": "."
},
"k8s": {
"namespace": "service-prod",
"deployment": "deployment-fancy-service",
"container": "container-fancy-service"
},
"k8s-staging": {
"namespace": "service-st",
"deployment": "deployment-fancy-service",
"container": "container-fancy-service"
}
}
}
从v4.0.0版本开始,您可以提供cronjob
而不是deployment
。如果它使用相同的镜像,也支持init-container
。
建议将您的Kubernetes配置文件放在
deploy/k8s
和deploy/k8s-staging
子目录中。
这使得您可以使用deploy staging
和deploy production
命令。
-
执行
deploy staging
会构建并推送带有版本标签和latest
标签的 Docker 镜像。之后,更新用于测试环境的容器所使用的镜像名为新版本。 -
执行
deploy production
做同样的事情,但镜像还会被标记为master
。然后更新生产容器。
$ deploy production
[INFO] Building image for fancy-service
[INFO] Building certpl/fancy-service:cb85eb9d38c407e462a6681351dfd36331635329
[INFO] Pushing image for fancy-service
[INFO] Pushing certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
[INFO] Deploying image to k8s
[INFO] Tagging certpl/fancy-service:e12840da50a9426b36de7c0be6dc553cde9923e8 as certpl/fancy-service::latest
[INFO] Pushing certpl/fancy-service:latest
如果您不想重新构建 Docker 镜像,只需从 Docker 仓库中拉取,可以使用 deploy pull
和 deploy production --deploy-only
开关。
$ docker pull
[INFO] Pulling image for fancy-service
[INFO] Pulling certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
$ deploy production --deploy-only
[INFO] Deploying image to k8s
[INFO] Tagging certpl/fancy-service:e12840da50a9426b36de7c0be6dc553cde9923e8 as certpl/fancy-service::latest
[INFO] Pushing certpl/fancy-service:latest
支持多个服务
如果您的应用程序使用多个容器构建,只需在 deploy.json
的顶层指定更多服务作为键。
{
"fancy-service": {
...
},
"fancy-service-web": {
...
}
}
Deploy 会构建、推送和部署它们全部(除非您使用 --service/-s
选项显式选择服务)。
使 Gitlab CI 启用部署功能
您可以使用 CI/CD 自动化所有这些步骤。以下是一个示例 .gitlab-ci.yml
文件。
image: certpl/deploy:v4.0.0
services:
- docker:dind
stages:
- build
- test
- deploy
build:
stage: build
script:
- git submodule init
- git submodule update --recursive
# Build and push images to Docker Registry
- deploy push
test:
stage: test
script:
# Pull images and run unit tests
- deploy pull
- deploy run -- python -m unittest discover
deploy:
stage: deploy
only:
- master
script:
# Set default token, pull images and deploy them
- kubernetes_use_token https://kapi.example.com "$KUBE_TOKEN"
- deploy pull
- deploy production --deploy-only
如果您没有测试并且只想一步构建和部署所有内容,请调用 deploy production
。
完整用法
请注意,某些可选参数仅适用于某些命令。
usage: deploy [-h] {build,push,pull,staging,production,image,run} ...
Deploy the application.
positional arguments:
{build,push,pull,staging,production,image,run}
Deploy commands
build Only build images
push Build and push images
pull Pull images from registry
staging Build, push and deploy images to the staging
environment
production Build, push and deploy images to the PRODUCTION
environment
image Show image names
run Run interactive command for service images
optional arguments:
-h, --help show this help message and exit
--tag TAG, -t TAG Alternative tags for image
--no-cache Pass --no-cache to docker build
--service SERVICE, -s SERVICE
Specify services to perform action (default: all)
--force, -f Don't perform check-ups, force deploy (not
recommended)
--verbose, -v Print spawned subcommands and their outputs
--version {commit,date,latest}
Alternative version tag
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。