首页 > 解决方案 > 可以从节点访问 Kubernetes 服务,但不能从我的机器访问

问题描述

我的站点托管在 DigitalOcean 提供的 Kubernetes 集群上时出现超时问题。

u@macbook$ curl -L fork.example.com
curl: (7) Failed to connect to fork.example.com port 80: Operation timed out

我已经尝试了调试服务页面上列出的所有内容。我使用一个名为df-stats-site.

u@pod$ nslookup df-stats-site
Server:     10.245.0.10
Address:    10.245.0.10#53

Name:   df-stats-site.deepfork.svc.cluster.local
Address: 10.245.16.96

当我从节点执行此操作时,它会给出相同的输出:

u@node$ nslookup df-stats-site.deepfork.svc.cluster.local 10.245.0.10
Server:     10.245.0.10
Address:    10.245.0.10#53

Name:   df-stats-site.deepfork.svc.cluster.local
Address: 10.245.16.96

借助该服务是否通过 IP 工作?页面的一部分,我尝试了以下命令并获得了预期的输出。

u@node$ curl 10.245.16.96
*correct response*

这应该意味着 DNS 和服务一切正常。我确认kube-proxy正在使用以下命令运行:

u@node$ ps auxw | grep kube-proxy
root  4194  0.4  0.1 101864 17696 ?    Sl Jul04  13:56 /hyperkube proxy  --config=...

但是我的iptables规则有问题:

u@node$ iptables-save | grep df-stats-site
(unfortunately, I was not able to copy the output from node, see the screenshot below)

截屏

建议kube-proxy使用-v标志设置为 4 重新启动,但我不知道如何使用 DigitalOcean 提供的集群来做到这一点。

这就是我使用的配置:

apiVersion: v1
kind: Service
metadata:
  name: df-stats-site
spec:
  ports:
  - port: 80
    targetPort: 8002
  selector:
    app: df-stats-site

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: df-stats-site
  annotations:
    kubernetes.io/ingress.class: nginx
    certmanager.k8s.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
  - hosts:
    - fork.example.com
    secretName: letsencrypt-prod
  rules:
  - host: fork.example.com
    http:
      paths:
      - backend:
          serviceName: df-stats-site
          servicePort: 80

另外,我在这个答案的帮助下设置了一个 NGINX 入口控制器。

我必须注意它以前工作得很好。我不确定是什么原因造成的,但是重新启动集群会很棒,尽管我不知道如何在不删除所有资源的情况下做到这一点。

标签: kubernetes

解决方案


ClusterIP 服务只能从集群内部访问。如果要从集群外部访问它,则需要将其配置为NodePort 或 LoadBalancer

如果你只是想在本地测试一些东西,你可以使用kubectl port-forward将本地机器上的端口转发到远程集群上的 ClusterIP 服务。下面是一个从镜像创建部署的示例,将其公开为 ClusterIP 服务,然后通过 kubectl port-forward 访问它:

$ kubectl run --image=rancher/hello-world hello-world --replicas 2
$ kubectl expose deployment hello-world --type=ClusterIP --port=8080 --target-port=80
$ kubectl port-forward svc/hello-world 8080:8080

现在可以从我的本地计算机访问此服务,网址为http://127.0.0.1:8080


推荐阅读