指定命名空间时,Kubernetes Pod 无法联系另一个 pod

Kubernetes Pod not able to contact another pod when Namespace specified

我在 centOS 7 上使用 Kubernetes。

Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-13T11:23:11Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-13T11:13:49Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}

我确实通过使用以下 prod_www_pod.yaml:

在 "production" 命名空间上部署了一个 Nginx pod
apiVersion: v1
kind: Pod
metadata:
  name: www
  namespace: production
  labels:
    app: www
spec:
  containers:
  - name: nginx
    image: myrepo:5001/nginx

我已经部署了第二个 (prod_debug.yaml) 以检查是否一切正常 运行。

apiVersion: v1
kind: Pod
metadata:
  name: debug
  namespace: production
spec:
  containers:
  - name: debug
    image: myrepo:5001/debug:latest
    command:
    - "sleep"
    - "10000"

我可以在正确的命名空间中看到两者,

[plaurent@kubmaster deployment]$ kubectl get po
No resources found in default namespace.
[plaurent@kubmaster deployment]$ kubectl get po -n production
NAME    READY   STATUS    RESTARTS   AGE
debug   1/1     Running   0          94s
www     1/1     Running   0          3m57s

但是当尝试从调试中卷曲 www 时,我有:

[plaurent@kubmaster deployment]$ kubectl exec -it -n production debug -- sh
/ # curl www
Nothing there
/ # 

kubectl delete po debug www -n production 之后,我尝试安装相同的内容,只是删除了元数据上指定的命名空间。

[plaurent@kubmaster deployment]$ kubectl get po
NAME    READY   STATUS    RESTARTS   AGE
debug   1/1     Running   0          10s
www     1/1     Running   0          18s

当尝试从调试中联系 www 时,它运行良好。

[plaurent@kubmaster deployment]$ kubectl exec -it debug -- sh
/ # curl www
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ # 

有人能给我指出正确的方向吗?

此致,

皮埃尔

你无法通过dns到达一个pod,除非你已经为它配置了hostnamesubdomain,并且到达它的方式与你做的不一样。

可能您在 default 命名空间中有一个名为 www 的服务,它将请求转发到 pod,但是在您的 production 命名空间中您没有该服务。

为了证实我的意思,运行 kubectl get svc 在你的 defaultproduction 命名空间中。

如果我是对的,请通过 kubectl expose pod ... 公开您的 pod,或通过 yaml 文件创建服务。

现在请注意,创建一个 pod 不是一个好主意。最好用 1 个副本创建 e Deployment。

这适用于默认命名空间,而不适用于生产命名空间,因为默认情况下 kube-dns 是如何配置的。 default ndots config of the kube-dns recognises default.svc.cluster.local。因此,当使用默认命名空间时,默认值允许 kube-dns 定位 www。但是由于 production.svc.cluster.local 没有默认条目,名称解析失败。

您需要配置一项服务以在生产命名空间中公开 www pod(也可以使用 headless service)。