[cloud-running-a-container]:在默认名称空间中找不到资源 [英] [cloud-running-a-container]: No resources found in default namespace
问题描述
我使用Docker映像在K8中进行了一次小型部署,但未在部署中显示,而仅在pod中显示.原因:它没有在部署中创建任何默认名称空间.
I did a small deployment in K8s using Docker image but it is not showing in deployment but only showing in pods. Reason: It is not creating any default namespace in deployments.
请建议:
以下是我使用的命令.
$ 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
标志,该标志无效.
Arghya Sadhu's answer is correct. In the past kubectl run
command indeed created by default a Deployment
instead of a Pod
. Actually in the past you could use it with so called generators and you were able to specify exactly what kind of resource you want to create by providing --generator
flag followed by corresponding value. Currently --generator
flag is deprecated and has no effect.
请注意,在运行 kubectl run
命令后,您会获得非常清晰的消息:
Note that you've got quite clear message after running your kubectl run
command:
$ 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
.
It clearly says that the Pod
hello-node
was created. It doesn't mention about a Deployment
anywhere.
除了使用命令式命令创建 Deployment
或 Pods
之外,还可以使用声明性方法:
As an alternative to using imperative commands for creating either Deployments
or Pods
you can use declarative approach:
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
命名空间中.
Declaration of namespace
can be ommitted in this case as by default all resources are deployed into the default
namespace.
保存文件后,例如作为 nginx-deployment.yaml
,您只需运行:
After saving the file e.g. as nginx-deployment.yaml
you just need to run:
kubectl apply -f nginx-deployment.yaml
更新:
在yaml清单中扩展环境变量实际上是行不通的,因此无法使用上述部署示例中的以下行:
Update:
Expansion of the environment variables within the yaml manifest actually doesn't work so the following line from the above deployment example cannot be used:
image: gcr.io/$DEVSHELL_PROJECT_ID/hello-node:1.0
最简单的解决方法是一个相当简单的 sed
技巧".
The simplest workaround is a fairly simple sed
"trick".
首先,我们需要在部署定义yaml中更改项目ID的占位符.可能看起来像这样:
First we need to change a bit our project id's placeholder in our deployment definition yaml. It may look like this:
image: gcr.io/{{DEVSHELL_PROJECT_ID}}/hello-node:1.0
然后,当应用部署定义而不是简单的 kubectl apply -f deployment.yaml
时,运行此单行代码:
Then when applying the deployment definition instead of simple kubectl apply -f deployment.yaml
run this one-liner:
sed "s/{{DEVSHELL_PROJECT_ID}}/$DEVSHELL_PROJECT_ID/g" deployment.yaml | kubectl apply -f -
上面的命令告诉 sed
在 deployment.yaml
文档中搜索 {{DEVSHELL_PROJECT_ID}}
字符串,并且每次出现此字符串时,将其替换为 $ DEVSHELL_PROJECT_ID
环境变量的实际值.
The above command tells sed
to search through deployment.yaml
document for {{DEVSHELL_PROJECT_ID}}
string and each time this string occurs, to substitute it with the actual value of $DEVSHELL_PROJECT_ID
environment variable.
这篇关于[cloud-running-a-container]:在默认名称空间中找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!