首页 > 解决方案 > Kubernetes,无法使用外部 IP 地址访问 Pod

问题描述

我是 Kubernetes 新手,遇到了一些问题。

我有一个 ubuntu 服务器,我正在处理它。我创建了 pod 和服务,还有一个 API-Gateway pod 和服务。我想用我的电脑上的 ubuntu 服务器 IP 地址访问这个 pod。

但我无法从服务器外部访问此 pod。我在 docker 映像上的应用程序在 80 端口上运行。

我的 api-gateway.yaml 文件是这样的:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api-gateway
  template:
    metadata:
      labels:
        app: api-gateway
    spec:
      containers:
        - name: api-gateway
          image: myapi/api-gateway
---
apiVersion: v1
kind: Service
metadata:
  name: api-gateway
spec:
  selector:
    app: api-gateway
  ports:
    - name: api-gateway
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30007
  type: NodePort
  externalIPs:
  - <My Ubuntu Server IP Adress>

当我输入kubectl get services api-gateway时,我得到

NAME           TYPE       CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
api-gateway   NodePort   10.104.42.32   <MyUbuntuS IP>   80:30007/TCP   131m

当我输入kubectl describe services api-gateway时,我得到

Name:                     api-gateway
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=api-gateway
Type:                     NodePort
IP Families:              <none>
IP:                       10.104.42.32
IPs:                      10.104.42.32
External IPs:             <My Ubuntu Server IP Adress>
Port:                     api-gateway  80/TCP
TargetPort:               80/TCP
NodePort:                 api-gateway  30007/TCP
Endpoints:                172.17.0.4:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason  Age                  From                Message
  ----    ------  ----                 ----                -------
  Normal  Type    30m                  service-controller  ClusterIP -> LoadBalancer
  Normal  Type    6m10s                service-controller  NodePort -> LoadBalancer
  Normal  Type    77s (x2 over 9m59s)  service-controller  LoadBalancer -> NodePort

那么,如何在我的 PC 浏览器或 Postman 上访问此 pod?

标签: dockerkuberneteskubectlkubeadm

解决方案


推荐阅读