amazon-web-services - 将 Kubernetes 部署部署到不同环境并在配置中处理内部版本号的最佳实践
问题描述
每个微服务都有一个部署配置。这看起来像这样:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: service_x
name: service_x
spec:
replicas: 2
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: service_x
spec:
containers:
- env:
- name: FLASK_ENV
value: "production"
image: somewhere/service_x:master_179
name: service_x
ports:
- containerPort: 80
resources: {}
volumeMounts:
- mountPath: /app/service_x/config/deployed
name: volume-service_xproduction
restartPolicy: Always
volumes:
- name: volume-service_xproduction
configMap:
name: service_xproduction
items:
- key: production.py
path: production.py
我们有以下环境 Dev、Stage、Production。如您所见,图像参数包含服务、分支和内部版本号。我有几个想法可以使这种动态化并能够在开发环境中部署例如 service_x:development_190 并在舞台上进行不同的构建。但在我开始 - 也许 - 发明新的轮子之前,我想知道其他人如何解决这个挑战......顺便说一句。我们使用 CircleCI 来构建 docker 镜像。
我现在的问题是;在不同环境中部署构建的最佳实践是什么?
- 为每个构建构建deployment.yml?
- 使用变量/模板?
- 我不知道的任何其他解决方案?
- 也许将 kubernetes 文件放在微服务中不是最好的主意?
解决方案
有很多方法可以做你想做的事情,比如舵图、更新模板等。
我所做的是,像这样构造代码:
├── .git
├── .gitignore
├── .gitlab-ci.yml
├── LICENSE
├── Makefile
├── README.md
├── src
│ ├── Dockerfile
│ ├── index.html
└── templates
├── autoscaler.yml
├── deployment.yml
├── ingress.yml
├── sa.yml
├── sm.yml
└── svc.yml
Kubernetes 模板文件将具有以下内容:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
namespace: __NAMESPACE__
labels:
app: app
environment: __CI_COMMIT_REF_NAME__
commit: __CI_COMMIT_SHORT_SHA__
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
environment: __CI_COMMIT_REF_NAME__
commit: __CI_COMMIT_SHORT_SHA__
annotations:
"cluster-autoscaler.kubernetes.io/safe-to-evict": "true"
spec:
containers:
- name: app
image: <registry>/app:__CI_COMMIT_SHORT_SHA__
ports:
- containerPort: 80
因此,只要您更改src
.
然后在 CircleCI 配置中,你可以有步骤在应用之前更新模板:
- sed -i "s/__NAMESPACE__/${CI_COMMIT_REF_NAME}/" deployment.yml service.yml
- sed -i "s/__CI_COMMIT_SHORT_SHA__/${CI_COMMIT_SHORT_SHA}/" deployment.yml service.yml
- sed -i "s/__CI_COMMIT_REF_NAME__/${CI_COMMIT_REF_NAME}/" deployment.yml service.yml
- kubectl apply -f deployment.yml
- kubectl apply -f service.yml
这些变量将可供您使用或在 CircleCI 中设置。
推荐阅读
- linux-kernel - 在 Linux 内核中获取帧计数
- asp.net-mvc - 如何从 Identity Server 启动调用 API
- javascript - graphql 变异参数名称为 [Object: null prototype]
- highcharts - Highcharts:如何对齐饼图的文本标签中间中心
- javascript - 根据另一个数组中的值对数组进行排序 - Javascript
- angular - 为什么 ngAfterViewInit 中的 formGroup (formRef.form) 为空?
- javascript - 如何改进这个 Promise 链接并获得相同的行为
- erlang - CouchDB 2.3.1 make install 在 RHEL7.6 上失败
- python - Python - 遍历列表并从每个目录中提取路径
- php - apache和IIS上getttype()的PHP不同结果