Deploy same image two different namespaces same port

7/11/2019

I have a single node k8s cluster. I have two namespaces, call them n1 and n2. I want to deploy the same image, on the same port but in different namespaces.

How do I do this?

namespace yamls:

apiVersion: v1
kind: Namespace
metadata:
  name: n1

and

apiVersion: v1
kind: Namespace
metadata:
  name: n2

service yamls:

apiVersion: v1
kind: Service
metadata:
  name: my-app-n1
  namespace: n1
  labels:
    app: my-app-n1
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: http
    protocol: TCP
  selector:
    app: my-app-n1

and

apiVersion: v1
kind: Service
metadata:
  name: my-app-n2
  namespace: n2
  labels:
    app: my-app-n2
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: http
    protocol: TCP
  selector:
    app: my-app-n2

deployment yamls:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-n1
  labels:
    app: my-app-n1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app-n1
  template:
    metadata:
      labels:
        app: my-app-n1
    spec:
      containers:
      - name: waiter
        image: waiter:v1
        ports:
        - containerPort: 80

and

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-n2
  labels:
    app: my-app-n2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app-n2
  template:
    metadata:
      labels:
        app: my-app-n2
    spec:
      containers:
      - name: waiter
        image: waiter:v1
        ports:
        - containerPort: 80

waiter:v1 corresponds to this repo: https://hub.docker.com/r/adamgardnerdt/waiter

Surely I can do this as namespaces are supposed to represent different environments? eg. nonprod vs. prod. So surely I can deploy identically into two different "environments" aka "namespaces"?

-- A. Gardner
kubernetes

2 Answers

7/11/2019

For Service you have specified namespaces , that is correct.

For Deployments you should also specify namespaces othervise they will go to default namespace.

Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-n1
  namespace: n1
  labels:
    app: my-app-n1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app-n1
  template:
    metadata:
      labels:
        app: my-app-n1
    spec:
      containers:
      - name: waiter
        image: waiter:v1
        ports:
        - containerPort: 80
-- Ijaz Ahmad Khan
Source: StackOverflow

7/11/2019

I want to deploy the same image, on the same port but in different namespaces.

You are already doing that with your configs, except for deployment objects, that should refer to correct namespaces (as mentioned by answer from Ijaz Ahmad Khan), available to other services in the namespaces using DNS names my-app-n1 and my-app-n2 respectively.

Because waiter is a web server, I assume you would like to access both instances of it from the internet. Hence, you should:

  • change the type of both services to ClusterIP,
  • add ingress object, one per each namespace, containing a host name, e.g. myapp.com and staging.myapp.com respectively),
  • put a load balancer in front of your cluster: the load balancer will use ingress objects to know which hostname matches which service (your cloud provider should create a load balancer automatically).
-- gmile
Source: StackOverflow