【问题标题】:Best practice deploying kubernetes deployment to different environments and handling buildnumbers in the config将 Kubernetes 部署部署到不同环境并在配置中处理内部版本号的最佳实践
【发布时间】:2019-06-27 07:03:04
【问题描述】:

每个微服务都有一个部署配置。这看起来像这样:

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 文件放在微服务中不是最好的主意?

【问题讨论】:

    标签: amazon-web-services kubernetes continuous-deployment circleci amazon-eks


    【解决方案1】:

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

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

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

    【讨论】:

    • 这正是我打算做的。感谢您的详细描述,它肯定会在一段时间内保护我;)
    • 我现在已经按照您的建议实施了,我对解决方案非常满意。谢谢!
    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 2020-04-06
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2010-11-25
    相关资源
    最近更新 更多