将Kubernetes部署到不同环境并在配置中处理内部版本号的最佳实践

时间:2019-06-27 07:03:04

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

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

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

我们有以下环境开发,阶段,生产。如您所见,image参数包含服务,分支和内部版本号。我有几个想法可以使之动态化,并能够在开发环境和舞台上的不同构建中部署例如service_x:development_190。但是在我开始-也许-发明新的轮子之前,我想知道其他人如何解决这个挑战... 顺便说一句。我们使用CircleCI来构建docker映像。

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

  • 为每个构建构建Deployment.yml吗?
  • 使用变量/模板吗?
  • 我不知道的其他解决方案吗?
  • 也许将kubernetes文件放在微服务中不是最好的主意吗?

1 个答案:

答案 0 :(得分:2)

有很多方法可以完成您想做的事情,例如舵图,更新模板等。

我要做的是,像这样构建代码:

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