Namespaces not found
Asked Answered
F

6

14

I see the following error when I run my deployment:

Error from server (NotFound): error when creating "n3deployment.yaml": namespaces "n2" not found

My n3deployment.yaml has no reference to n2?

Step By Step

  1. Ensure everything is empty
c:\temp\k8s>kubectl get pods
No resources found.

c:\temp\k8s>kubectl get svc
No resources found.

c:\temp\k8s>kubectl get deployments
No resources found.

c:\temp\k8s>kubectl get namespaces
NAME          STATUS    AGE
default       Active    20h
docker        Active    20h
kube-public   Active    20h
kube-system   Active    20h
  1. Create files
n3namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: n3

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

n3deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-n3
  labels:
    app: my-app-n3
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app-n3
  template:
    metadata:
      labels:
        app: my-app-n3
    spec:
      containers:
      - name: waiter
        image: waiter:v1
        ports:
        - containerPort: 80
  1. Apply configuration
c:\temp\k8s>kubectl apply -f n3namespace.yaml
namespace "n3" created

c:\temp\k8s>kubectl apply -f n3service.yaml
service "my-app-n3" created

c:\temp\k8s>kubectl apply -f n3deployment.yaml
Error from server (NotFound): error when creating "n3deployment.yaml": namespaces "n2" not found

I used to have a namespace called n2 but, as you can see, it no longer exists.

Fiorin answered 11/7, 2019 at 0:47 Comment(0)
S
10

In my case, I was missing to have executed before:

kubectl create namespace n3
Slake answered 9/12, 2022 at 10:52 Comment(1)
While this works, better approach is to update yaml file responsible for namespace creation. That way it is much more reproducible and ready for production use.Binocular
F
3

I had previously created two contexts and my minikube cluster was set to still be in the n2 context. I deleted the context, re-ran the command and it worked.

Fiorin answered 11/7, 2019 at 0:50 Comment(0)
F
2

Add namespace: n3 to deployment spec

Fard answered 11/7, 2019 at 3:25 Comment(0)
P
1

In my case I created context and set --namespace to not existing one, the solution was switch namespace only using the kubectl commands:

kubectl config set-context --current --namespace=<namespace>

You can check if in your situation is the same problem using:

kubectl get svc -o wide
kubectl describe sa default | grep Namespace
Petersburg answered 11/4, 2021 at 20:55 Comment(0)
S
0

I think you can use kubens and kubectx to verify on which namespace and context you are after that verify your kubernetes manifest files

Supervene answered 11/4, 2021 at 21:25 Comment(0)
H
0

In your n3deployment.yaml file add namespace as well under metadata section like below

n3deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-n3
  namespace: n3
  labels:
  app: my-app-n3
  ------
  -------
Havre answered 23/2, 2022 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.