Kubernetes set deploment number of replicas based on namespace

3/29/2018

I've split our Kubernetes cluster into two different namespaces; staging and production, aiming to have production deployments having two replicas (for rolling deployments, autoscaling comes later) and staging having one single replica.

Other than having one deployment configuration per namespace, I was wondering whether or not we could set the default number of replicas per deployment, per namespace?

When creating the deployment config, if you don't specify the number of replicas, it will default to one. Is there a way of defaulting it to two on the production namespace?

If not, is there a recommended approach for this which will prevent the need to have a deployment config per namespace?

One way of doing this would be to scale the deployment up to two replicas, manually, in the production namespace, once it has been created for the first time, but I would prefer to skip any manual steps.

-- max_
kubernetes

3 Answers

3/31/2018

When creating the deployment config, if you don't specify the number of replicas, it will default to one. Is there a way of defaulting it to two on the production namespace?

Actually, there are two ways to do it, but both of them involved coding.

  1. Admission Controllers:

This is the recommended way of assigning default values to fields.

While creating objects in Kubernetes, it passes through some admission controllers and one of them is MutatingWebhook.

MutatingWebhook has been upgraded to beta version since v1.9+. This admission controller modifies (mutates) the object before actully created (or modified/deleted), say, assigning default values of some fields and some similar task. You can change the minimum replicas number here.

User Have to implement a admission server to receive requests from kubernetes and give modified object as response accordingly.

Here is a sample admission server implemented by Openshift kubernetes-namespace-reservation.

  1. Deployment Controller:

This is comparatively easier but kind of hacking the deployment procedure.

You can write a Deployment controller which will watch for deployment and if there is any deployment made, it will do some task. Here, you can update the deployment with some minimum values you wish. You can see the official Sample Pod Controller.

If both of them seems lots to do, it is better to assign fields more carefully each time for each deployment.

-- Abdullah Al Maruf - Tuhin
Source: StackOverflow

3/29/2018

It is not possible to set different number of replicas per namespace in one deployment.

But you can have 2 different deployment files 1 per each namespace, i.e. <your-app>-production.yaml and <your-app>-staging.yaml.

In these descriptions you can determine any custom values and settings that you need.

For an example:

<your-app>-production.yaml:

apiVersion: v1
kind: Deployment
metadata:
  name: <your-app>
  namespace: production #Here is namespace
...
spec:
  replicas: 2 #Here is the count of replicas of your application
  template:
    spec:
      containers:
      - name: <your-app-pod-name>
        image: <your-app-image>
...

<your-app>-staging.yaml:

apiVersion: v1
kind: Deployment
metadata:
  name: <your-app>
  namespace: staging #Here is namespace
...
spec:
  replicas: 1 #Here is the count of replicas of your application
  template:
    spec:
      containers:
      - name: <your-app-pod-name>
        image: <your-app-image>
...
-- Artem Golenyaev
Source: StackOverflow

3/29/2018

I don't think you can avoid having two deployments, but you can get rid of the duplicated code by using helm templates (https://docs.helm.sh/chart_template_guide). Then you can define a single deployment yaml and substitute different values when you deploy with an if statement.

-- ewramner
Source: StackOverflow