我使用 Docker 镜像在 K8s 中做了一个小型部署,但它没有显示在部署中,而是只显示在 pod 中。 原因:它没有在部署中创建任何默认命名空间。
请建议:
以下是我使用的命令。
$ 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
最佳答案
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
。
作为使用命令式命令创建Deployments
或Pod
的替代方法,您可以使用声明式方法:
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 list 中扩展环境变量实际上不起作用,因此不能使用上述部署示例中的以下行:
image: gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0
最简单的解决方法是一个相当简单的 sed
“技巧”。
首先,我们需要在部署定义 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 -
上面的命令告诉 sed
在 deployment.yaml
文档中搜索 {{DEVSHELL_PROJECT_ID}}
字符串,每次出现这个字符串时,将其替换为 $DEVSHELL_PROJECT_ID
环境变量的实际值。
关于kubernetes - [云运行容器] : No resources found in default namespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62336763/