【问题标题】:Unable to access the nginx through Kubernetes LoadBalancer service无法通过 Kubernetes LoadBalancer 服务访问 nginx
【发布时间】:2021-03-16 07:44:48
【问题描述】:

我正在使用随 docker desktop (windows) 提供的 k8s。

我的 deployment.yml 文件是

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:stable-alpine
        ports:
        - containerPort: 80

我的服务 yml 文件是

apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: NodePort
  ports:
  - nodePort: 31000
    port: 80
    targetPort: 80

一切都已启动并正在运行,但我无法访问该应用程序。

>curl localhost:31000
curl: (7) Failed to connect to localhost port 31000: Connection refused

>kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-685658ccbf-g84w5   1/1     Running   0          8s

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP        14h
service/my-service   LoadBalancer   10.96.210.40   localhost     80:31000/TCP   4s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   1/1     1            1           8s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-685658ccbf   1         1         1       8s

注意:在 windows 防火墙中为这个 31000 端口创建了入站/出站规则,以确保它不会阻塞

【问题讨论】:

    标签: nginx kubernetes kubernetes-ingress kubernetes-pod


    【解决方案1】:

    有一些问题你可以尝试回答Debug Services:

    1. Does the Service exist?:在你的情况下,我们看到确实如此。

    2. Does the Service work by DNS name?:客户端使用服务的最常见方式之一是通过 DNS 名称。

    3. Does the Service work by IP?:假设您已经确认 DNS 工作正常,接下来要测试的是您的服务是否通过其 IP 地址工作。

    4. Is the Service defined correctly?: 你真的应该仔细检查你的服务是否正确并且匹配你的 Pod 的端口。另外:

    • 您尝试访问的服务端口是否列在spec.ports[] 中?

    • targetPort 是否适用于您的 Pod(某些 Pod 使用与服务不同的端口)?

    • 如果您打算使用数字端口,它是数字 (9376) 还是字符串“9376”?

    • 如果您打算使用命名端口,您的 Pod 是否会公开同名的端口?

    • 端口的protocol 是否适合您的 Pod?

    1. Does the Service have any Endpoints?: 检查你运行的 Pod 是否真的被 Service 选中。

    2. Are the Pods working?:再次检查 Pod 是否确实在工作。

    3. Is the kube-proxy working?:确认kube-proxy 正在您的节点上运行。

    完成上述步骤将帮助您找到此问题的原因以及未来可能出现的服务问题。

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2022-07-12
      • 2021-05-23
      • 2021-09-24
      • 1970-01-01
      • 2019-07-26
      相关资源
      最近更新 更多