Phate
Phate

Reputation: 6622

ConfigMap also for kubernetes configurations?

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?

Upvotes: 0

Views: 234

Answers (2)

JTigger
JTigger

Reputation: 424

It is incredibly straight-forward (6 lines total) to do this with ytt

./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

To deploy to test:

$ ytt -f config/

To deploy to prod:

$ ytt -f config/ -f prod/

Upvotes: 2

FL3SH
FL3SH

Reputation: 3328

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.

Upvotes: 2

Related Questions