Kubernetes服务无法通过浏览器访问

kninwzqo  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(3)|浏览(164)

我是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文件,它的工作如预期。

vom3gejh

vom3gejh1#

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)。

minikubeLoadBalancer类型的服务

minikubeLoadbalancer类型Service的Angular 讲:
最简单的方法是用NodePort连接。由于minikube的本地特性,它不会获得分配的External IP,除非您选择一种变通方法,例如:

我没有亲自测试过,但你可以尝试一下:$ minikube addons enable metallb将其分配给CIDR中的IP地址与minikube示例相同,然后查询(curl)这个新分配的IP地址。
更多参考指南:

其他资源:

ztigrdn8

ztigrdn82#

您无法部署类型为loadbalance的服务,因为您的群集未设置loadbalancer。尝试使用NodePort或clusterIP类型部署服务

mrwjdhj3

mrwjdhj33#

我遇到了这个错误,花了一整天的时间才弄清楚,对我来说,无法从外部访问Pod的原因是因为运行“kubectl get svc”时EXTERNAL IP为none外部IP:是从外部访问群集所需的。而外部IP为none的原因是因为您的类型为type:NodePort,将其更改为类型:LoadBalancer.type Loadbalancer是将流量从集群外部(Internet)引导到集群中的Pod。我认为NodePort在集群内,所以你的service.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: LoadBalancer

字符串

相关问题