【问题标题】:kubernetes service not accessible through browser无法通过浏览器访问 kubernetes 服务
【发布时间】:2021-05-23 02:56:18
【问题描述】:

我是 kubernetes 的新手,正在尝试部署一个简单的 hello-world 应用程序。我正在使用 Ubuntu 20.04 并在 VMware 工作站上运行它。我已经安装了 minikube 并尝试部署。但是,已部署 pod,但无法通过浏览器访问服务。

下面是我的deployment.yaml文件:

---
kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    name: myapp
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8081
      # Port to forward to inside the pod
      targetPort: 8080
      # Port accessible outside cluster
      nodePort: 30000
  type: NodePort
 
 
 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myappdeployment
spec:
  replicas: 5
  selector:
    matchLabels:
        name: myapp
  template:
    metadata:
      labels:
        name: myapp
    spec:
      containers:
        - name: myapp
          image: pritishkapli/example:v1.0.0
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: 512Mi
              cpu: "1"
            requests:
              memory: 256Mi
              cpu: "0.2"

下面是kubernetes服务:

pritish@ubuntu:~$ kubectl get svc
NAME             TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
exampleservice   LoadBalancer   10.101.149.155   <pending>     8081:30000/TCP   12m
kubernetes       ClusterIP      10.96.0.1        <none>        443/TCP          9h

下面是正在运行的 pod:

pritish@ubuntu:~$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
myappdeployment-b85b56d64-knhhc   1/1     Running   0          17m
myappdeployment-b85b56d64-m4vbg   1/1     Running   0          17m
myappdeployment-b85b56d64-qss4l   1/1     Running   0          17m
myappdeployment-b85b56d64-r2jq4   1/1     Running   0          17m
myappdeployment-b85b56d64-tflnz   1/1     Running   0          17m

请帮忙!

PS:我已经更新了deployment.yaml 文件,它按预期工作。

【问题讨论】:

标签: kubernetes yaml minikube


【解决方案1】:

TL;DR

这不是LoadBalancer 类型的Service 的问题,而是service.spec.selector 值和deployment.spec.selector.matchLabels 值之间的不匹配 值。


如何修复您的设置?

要修复您的设置,您可以使用来自ServiceDeployment 的相同值。

请仅选择以下选项之一:

  • deployment.yaml 变化:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myappdeployment
spec:
  replicas: 5
  selector:
    matchLabels:
        app: myapp # <-- CHANGES
  template:
    metadata:
      labels:
        app: myapp # <-- CHANGES
    spec:
      containers:
        - name: myapp
          image: pritishkapli/example:v1.0.0
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: 512Mi
              cpu: "1"
            requests:
              memory: 256Mi
              cpu: "0.2"
  • service.yaml 变化:
kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app.kubernetes.io/name: myapp # <-- CHANGES
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8081
      # Port to forward to inside the pod
      targetPort: 8080
      # Port accessible outside cluster
      nodePort: 30000
  type: LoadBalancer

你怎么知道这是selector 的问题?

这归结为使用 Kubernetes 的经验,但最简单的检查方法是(使用旧示例):

  • kubectl describe service exampleservice(由于可读性原因,此输出的部分内容已编辑)
Name:                     exampleservice
Namespace:                default
Labels:                   <none>
Selector:                 app=myapp
Type:                     LoadBalancer
IP:                       10.8.10.103
Port:                     <unset>  8081/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30000/TCP
Endpoints:                <none> # <-- LOOK HERE!
  • kubectl get endpoints exampleservice
NAME             ENDPOINTS   AGE
exampleservice   <none>      2m3s

正如您在上面的输出中看到的那样,没有 endpoints 可以将流量发送到 (Pods)。


LoadBalancer 类型的服务minikube

minikubeLoadbalancer类型的Service的角度谈:

如前所述,最简单的方法是通过NodePort 进行连接。由于minikube 的本地性质,除非您选择解决方法,否则它不会分配External IP,例如:

  • minikube tunnel

tunnel - 创建到使用 LoadBalancer 类型部署的服务的路由,并将它们的 Ingress 设置为它们的 ClusterIP。详细示例见https://minikube.sigs.k8s.io/docs/tasks/loadbalancer

Minikube.sigs.k8s.io: Docs: Commands: Tunnel


附注!

我没有亲自测试过,但您可以尝试使用:$ minikube addons enable metallb 为其分配与您的minikube 实例相同的CIDR 中的IP 地址,然后查询(curl)这个新分配的知识产权。

更多参考指南:


其他资源:

【讨论】:

  • 您好,感谢您的详细解释。我正在使用type: NodePort。我已经更新了我的问题部分中的 yaml 文件。请确认我是否做得对。
  • @Pritish 我可以看到您包含的 YAML 清单是正确的。您应该能够通过以下方式与您的应用程序通信:minikube_ip:nodeport
  • 谢谢大卫。我现在可以与应用程序通信了。
【解决方案2】:

您无法使用类型负载平衡部署您的服务,因为您的集群没有设置负载平衡器。尝试使用 NodePort 或 clusterIP 类型部署您的服务

【讨论】:

  • 谢谢。但是,我仍然无法在我的浏览器中访问。小魔方 IP:192.168.49.2。我正在浏览器中尝试这个网址:192.168.49.2:30000。我做得对吗?
  • 更多信息publish service
  • 对不起。看了链接后不太明白。在我的 yaml 文件中有什么事情要做吗?得到以下输出:pritish@ubuntu:~/kubernetes/exampleapp$ curl http://192.168.49.2:30000curl: (7) Failed to connect to 192.168.49.2 port 30000: Connection refused
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 2021-10-12
  • 2013-02-14
  • 2015-06-27
  • 2016-07-16
  • 1970-01-01
  • 2021-03-16
  • 2017-10-02
相关资源
最近更新 更多