[cloud-运行-a-container]:在默认命名空间中找不到资源

[cloud-running-a-container]: No resources found in default namespace

我使用 Docker 图片在 K8s 中做了一个小型部署,但它没有显示在部署中,只显示在 pods 中。 原因:它没有在部署中创建任何默认命名空间。

请推荐:

以下是我使用的命令。

$ kubectl run hello-node --image=gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0 --port=8080 --namespace=default
pod/hello-node created

$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
hello-node   1/1     Running   0          12s

$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                                       READY   STATUS    RESTARTS   AGE
default       hello-node                                                 1/1     Running   0          9m9s
kube-system   event-exporter-v0.2.5-599d65f456-4dnqw                     2/2     Running   0          23m
kube-system   kube-proxy-gke-hello-world-default-pool-c09f603f-3hq6      1/1     Running   0          23m

$ kubectl get deployments
**No resources found in default namespace.**

$ kubectl get deployments --all-namespaces
NAMESPACE     NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   event-exporter-v0.2.5                      1/1     1            1           170m
kube-system   fluentd-gcp-scaler                         1/1     1            1           170m
kube-system   heapster-gke                               1/1     1            1           170m
kube-system   kube-dns                                   2/2     2            2           170m
kube-system   kube-dns-autoscaler                        1/1     1            1           170m
kube-system   l7-default-backend                         1/1     1            1           170m
kube-system   metrics-server-v0.3.1                      1/1     1            1           170m

使用 kubectl version

检查 kubectl 版本

从 kubectl 1.18 版本开始 kubectl run 只创建 pod,没有其他任何东西。要创建部署,请使用 kubectl create deployment 或使用旧版本的 kubectl

Arghya Sadhu 的回答是正确的。过去 kubectl run 命令确实默认创建了 Deployment 而不是 Pod。实际上在过去,您可以将它与所谓的 generators 一起使用,并且您可以通过提供 --generator 标志和相应的值来准确指定要创建的资源类型。当前 --generator 标志已弃用并且无效。

请注意,在 运行 执行 kubectl run 命令后,您得到了非常明确的信息:

$ kubectl run hello-node --image=gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0 --port=8080 --namespace=default
pod/hello-node created

它清楚地表明 Pod hello-node 已创建。它没有在任何地方提到 Deployment

作为使用命令式命令创建DeploymentsPods的替代方法,您可以使用声明式方法:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-node
  namespace: default
  labels:
    app: hello-node
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-node
  template:
    metadata:
      labels:
        app: hello-node
    spec:
      containers:
      - name: hello-node-container
        image: gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0
        ports:
        - containerPort: 8080

在这种情况下可以省略 namespace 的声明,因为默认情况下所有资源都部署到 default 命名空间中。

保存文件后,例如作为 nginx-deployment.yaml 你只需要 运行:

kubectl apply -f nginx-deployment.yaml

更新:

yaml 清单中的环境变量扩展实际上不起作用,因此无法使用上述部署示例中的以下行:

image: gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0

最简单的解决方法是相当简单 sed "trick".

首先,我们需要在部署定义 yaml 中稍微更改项目 ID 的占位符。它可能看起来像这样:

image: gcr.io/{{DEVSHELL_PROJECT_ID}}/hello-node:1.0

然后在应用部署定义而不是简单的 kubectl apply -f deployment.yaml 运行 这个单行时:

sed "s/{{DEVSHELL_PROJECT_ID}}/$DEVSHELL_PROJECT_ID/g" deployment.yaml | kubectl apply -f -

上面的命令告诉 seddeployment.yaml 文档中搜索 {{DEVSHELL_PROJECT_ID}} 字符串,每次出现该字符串时,将其替换为 $DEVSHELL_PROJECT_ID 的实际值环境变量。