找不到命名空间

时间:2019-07-11 00:47:01

标签: kubernetes namespaces minikube

运行部署时看到以下错误:

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

我的n3deployment.yaml没有引用n2?

分步

  1. 确保所有内容均为空
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. 创建文件
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. 应用配置
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

我曾经有一个名为n2的命名空间,但是,正如您所看到的,它不再存在。

4 个答案:

答案 0 :(得分:1)

我以前创建了两个上下文,并且我的minikube集群设置为仍处于n2上下文中。我删除了上下文,重新运行了命令,它就起作用了。

答案 1 :(得分:1)

就我而言,我创建了上下文并将 --namespace 设置为不存在的上下文,解决方案是仅使用 kubectl 命令切换命名空间:

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

您可以使用以下方法检查您的情况是否存在相同的问题:

kubectl get svc -o wide
kubectl describe sa default | grep Namespace

答案 2 :(得分:0)

在部署规范中添加名称空间:n3

答案 3 :(得分:0)

我认为您可以使用 kubens 和 kubectx 来验证您在哪个命名空间和上下文之后验证您的 kubernetes 清单文件

相关问题