为什么我无法从 kubernetes 集群访问我的 Web 服务?

Why I can't access my web service from the kubernetes cluster?

我正在尝试在 kubernetes 集群中执行应用程序。 我曾经使用 docker-compose 启动应用程序没有问题,但是当我创建 我的 kubernetes 部署文件,即使公开它们后我也无法访问集群内的服务。这是我的部署文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
      matchLabels:
        app: myapp
  # type: LoadBalancer
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: jksun12/vdsaipro
       # command: ["/run.sh"]
        ports:
        - containerPort: 80
        - containerPort: 3306
   # volumeMounts:
   #     - name: myapp-pv-claim
   #       mountPath: /var/lib/mysql
   #   volumes:
   #   - name: myapp-pv-claim
   #     persistentVolumeClaim:
   #       claimName: myapp-pv-claim
---
apiVersion: apps/v1
kind: PersistentVolumeClaim
metadata:
  name: myapp-pv-claim
  labels:
    app: myapp
spec:
  accesModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 4Gi

这是

的结果

kubectl describe service myapp-service

:

Name:                     myapp-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=myapp
Type:                     NodePort
IP:                       10.109.12.113
Port:                     port-1  80/TCP
TargetPort:               80/TCP
NodePort:                 port-1  31892/TCP
Endpoints:                172.18.0.5:80,172.18.0.8:80,172.18.0.9:80
Port:                     port-2  3306/TCP
TargetPort:               3306/TCP
NodePort:                 port-2  32393/TCP
Endpoints:                172.18.0.5:3306,172.18.0.8:3306,172.18.0.9:3306
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

下面是我尝试访问它们时遇到的错误:

curl 172.17.0.2:32393

curl: (1) Received HTTP/0.9 when not allowed

这是我尝试访问其他端口时的下一个结果

curl 172.17.0.2:31892

curl: (7) 无法连接到 172.17.0.2 端口 31892:连接被拒绝 curl: (7) 无法连接到 172.17.0.2 端口 31892:连接被拒绝

我是 运行 ubuntu 服务器 20.04.1 LTS。 manip 在 minikube 之上。 感谢您的帮助。

如果您从集群内部访问服务,请使用 ClusterIP 作为 IP。所以 curl 应该是 10.109.12.113:8010.109.12.113:3306

如果从集群外部访问它,则使用 NODEIPNODEPORT。所以卷曲应该在 <NODEIP>:32393<NODEIP>:31892

在集群内部,我还会直接使用 POD IP 来了解问题是在服务级别还是 Pod 级别。

您需要确保应用程序正在侦听端口 80 和端口 3306。仅将 containerPort 提及为 803306 不会使应用程序侦听这些端口。

还要确保 pod 内的应用程序代码正在侦听 0.0.0.0 而不是 127.0.0.1