ConfigMap also for kubernetes configurations?

8/6/2019

I have 2 kubernetes descriptor files (yml): one for prod and one for test. The only difference between them is the fact that in production I want up to 4 replicas to grant horizontal scaling, while in test I'm fine. So the production yaml has the following more:

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

If it was possible to put this configuration inside a config map I could have two identical descriptors and avoid maintaining both. Is this possible?

-- Phate
kubernetes

1 Answer

8/6/2019

Check kustomize it might be overkill just for one file, but later when a number of files increase it will be helpful.

  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

You can also use ytt for templating.

-- FL3SH
Source: StackOverflow