ConfigMap还用于Kubernetes配置吗?

时间:2019-08-06 11:50:34

标签: kubernetes

我有2个kubernetes描述符文件(yml):一个用于prod,一个用于测试。 它们之间的唯一区别是,在生产环境中,我最多需要4个副本来授予水平缩放比例,而在测试中,我很好。 因此,生产yaml具有以下功能:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: prodAutoScaling
spec:
  maxReplicas: 4
 minReplicas: 1
 scaleTargetRef:
  apiVersion: extensions/v1beta1
kind: Deployment
name: myapp
 targetCPUUtilizationPercentage: 80

如果可以将此配置放入配置映射中,则可以有两个相同的描述符,并避免同时维护两个描述符。 这可能吗?

2 个答案:

答案 0 :(得分:2)

检查kustomize可能仅对一个文件来说就算过高了,但是稍后当文件数量增加时,这会有所帮助。

  1. kustomize

$ kubectl kustomize dev/

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: dev-AutoScaling
spec:
  maxReplicas: 1
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: myapp
  targetCPUUtilizationPercentage: 80

$ kubectl kustomize prod/

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: prod-AutoScaling
spec:
  maxReplicas: 4
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: myapp
  targetCPUUtilizationPercentage: 80
.
├── base
│   ├── HorizontalPodAutoscaler.yaml
│   └── kustomization.yaml
├── dev
│   ├── kustomization.yaml
│   └── map.yaml
└── prod
    ├── kustomization.yaml
    └── map.yaml

$cat base/HorizontalPodAutoscaler.yaml

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: AutoScaling
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: myapp
  targetCPUUtilizationPercentage: 80
  maxReplicas: 0
  minReplicas: 1

$cat base/kustomization.yaml

resources:
- HorizontalPodAutoscaler.yaml
`$cat dev/kustomization.yaml`
bases:
- ../base
namePrefix: dev-
patchesStrategicMerge:
- map.yaml

$ cat dev/map.yaml

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: AutoScaling
spec:
  maxReplicas: 1

$ cat prod/kustomization.yaml

bases:
- ../base
namePrefix: prod-
patchesStrategicMerge:
- map.yaml

$ cat prod/map.yaml

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: AutoScaling
spec:
  maxReplicas: 4
  1. ytt

您还可以使用ytt进行模板制作。

答案 1 :(得分:2)

使用ytt进行此操作非常简单(总共6行)

./config/autoscaler.yml

#@ load("@ytt:data", "data")
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: prodAutoScaling
spec:
  maxReplicas: #@ data.values.replicas
 minReplicas: 1
 scaleTargetRef:
   apiVersion: extensions/v1beta1
   kind: Deployment
   name: myapp
   targetCPUUtilizationPercentage: 80

./config/values.yml

#@data/values
replicas: 1

./prod/values.yml

#@data/values
replicas: 4

要部署以进行测试:

$ ytt -f config/

要部署到产品:

$ ytt -f config/ -f prod/