将 kubernetes 部署部署到不同环境并在配置中处理内部版本号的最佳实践
Best practice deploying kubernetes deployment to different environments and handling buildnumbers in the config
每个微服务都有一个部署配置。这看起来像这样:
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
我们有以下环境开发、阶段、生产。如您所见,图像参数包含服务、分支和内部版本号。我有几个想法可以使它动态化并能够在开发环境中部署例如 service_x:development_190 并在舞台上进行不同的构建。但在我开始——也许——发明新的轮子之前,我想知道其他人是如何解决这个挑战的……
顺便提一句。我们使用 CircleCI 构建 docker 个图像。
我现在的问题是;在不同环境中部署构建的最佳实践是什么?
- 为每个构建构建 deployment.yml?
- 使用 variables/templates?
- 还有我不知道的其他解决方案吗?
- 也许让 kubernetes 文件与微服务放在一起不是最好的主意?
有很多方法可以做你想做的事,比如 helm 图表、更新模板等。
我所做的是,像这样构建代码:
├── .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 中设置这些变量。
每个微服务都有一个部署配置。这看起来像这样:
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
我们有以下环境开发、阶段、生产。如您所见,图像参数包含服务、分支和内部版本号。我有几个想法可以使它动态化并能够在开发环境中部署例如 service_x:development_190 并在舞台上进行不同的构建。但在我开始——也许——发明新的轮子之前,我想知道其他人是如何解决这个挑战的…… 顺便提一句。我们使用 CircleCI 构建 docker 个图像。
我现在的问题是;在不同环境中部署构建的最佳实践是什么?
- 为每个构建构建 deployment.yml?
- 使用 variables/templates?
- 还有我不知道的其他解决方案吗?
- 也许让 kubernetes 文件与微服务放在一起不是最好的主意?
有很多方法可以做你想做的事,比如 helm 图表、更新模板等。
我所做的是,像这样构建代码:
├── .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 中设置这些变量。