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

<p>How do I do this?</p>

<p>namespace yamls:</p>

<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: n1

and

apiVersion: v1
kind: Namespace
metadata:
  name: n2
</code></pre>

<p>service yamls:</p>

<pre><code>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
</code></pre>

<p>deployment yamls:</p>

<pre><code>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
</code></pre>

<p><code>waiter:v1</code> corresponds to this repo: <a href="https://hub.docker.com/r/adamgardnerdt/waiter" rel="nofollow noreferrer">https://hub.docker.com/r/adamgardnerdt/waiter</a></p>

<p>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"?</p>

<p>For Service you have specified namespaces , that is correct.</p>

<p>For Deployments you should also specify <strong>namespaces othervise they will go to default namespace.</strong></p>

<p><strong>Example:</strong></p>

<pre><code>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
</code></pre>
