首页 > 解决方案 > 将 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 镜像。

我现在的问题是;在不同环境中部署构建的最佳实践是什么?

标签: amazon-web-serviceskubernetescontinuous-deploymentcircleciamazon-eks

解决方案


有很多方法可以做你想做的事情,比如舵图、更新模板等。

我所做的是,像这样构造代码:

├── .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 中设置。


推荐阅读