【问题标题】:accessing services outside kubernetes访问 Kubernetes 之外的服务
【发布时间】:2017-03-29 18:27:54
【问题描述】:

我想访问外部服务/API

喜欢调用维基百科,所以我需要端口 80 http://en.wikipedia.org/w/api.php?action=opensearch&search=bee&limit=1&format=json

我的应用程序位于容器内的 pod 中,我暴露了端口 8000 并将其绑定到服务类型 loadbalncer 中的 300。

还可以访问托管在 kubernetes 之外的外部数据库,比如 mysql,所以我需要端口 3306,如何做到这一点。

这些都是部署文件和服务文件

https://github.com/hadyrashwan/request-wiki/blob/feature/open-internal-80/wiki-request-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: request-wiki-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: request-wiki
    spec:
      containers:
      - name: request-wiki
        image: hadyrashwan/request-wiki:0.0.4
        imagePullPolicy: Always
        ports:
        - containerPort: 8000
        - containerPort: 80

https://github.com/hadyrashwan/request-wiki/blob/feature/open-internal-80/wiki-request-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: request-wiki
  name:  request-wiki-service
spec:
  selector:
    app: request-wiki
  ports:
#  - name: app
  - port: 3000
    protocol: TCP
    targetPort: 8000
#  - name: app
#    protocol: TCP
#    targetPort: 80
  type: LoadBalancer

仍然没有使用配置/秘密或 tls

我在 2 台主机上使用 Rancher,用于 Kubernetes 环境,一台在 GCP 上,另一台在 AWS 上

【问题讨论】:

  • 问题是什么?您无法从 pod 内部访问外部服务?你遇到了什么错误?
  • yes { 错误:getaddrinfo EAI_AGAIN en.wikipedia.org:443 at Object.exports._errnoException (util.js:1034:11) at errnoException (dns.js:33:15) at GetAddrInfoReqWrap。 onlookup [as oncomplete] (dns.js:73:26) 代码:'EAI_AGAIN',errno:'EAI_AGAIN',系统调用:'getaddrinfo',主机名:'en.wikipedia.org',主机:'en.wikipedia.org ',端口:443 }

标签: mysql docker kubernetes rancher


【解决方案1】:

默认情况下,您的 pod 将使用它们所在节点的 docker 网桥进行出口连接。

尝试测试来自您将用于部署 pod 的节点的连接,如果您可以从节点连接,那么您的 pod 也应该能够做到这一点。通过这种方式,您将能够轻松跟踪与出口连接相关的任何问题(检查防火墙规则、正确配置的接口等)。

您在 yaml 中配置的这些端口选项仅用于入口流量。

【讨论】:

  • 我确认我能够从 ssh 获得结果到节点 h2rashwan@instance-2:~$ curl -X GET -H "Cache-Control: no-cache" -H "邮递员令牌:8decd972-20dc-8d4b-087b-901dc7d1697f" "en.wikipedia.org/w/api.php?format=json& action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow" {"batchcomplete":"","query":{"pages":{ “21721040”:......我也更新了yaml文件以包含端口80但仍然无法连接
  • 与yaml文件中的端口配置无关,只针对入口流量。您可以使用名为busybox的基本docker在节点级别测试容器与外部的连接性:docker pull busybox,然后docker run busybox sh -c "ping -c 5 en.wikipedia.org"如果它有效,您的容器应该能够连接,问题可能出在您的应用程序中,如果没有,你必须检查 docker 网络。
  • busybox 和我的应用程序都在 2 个节点上运行 $ sudo docker run -it -p 8000:8000 hadyrashwan/request-wiki:0.0.8
【解决方案2】:

如果我没记错的话,那么您想从 kubernetes 外部访问您的 kube 服务。 您可以使用服务类型 NodePort

键入节点端口

master 将从标志配置的范围内分配一个端口(默认值: 30000-32767),每个节点将代理该端口(相同的端口号 在每个节点上)进入您的服务。该端口将在您的 服务的 spec.ports[*].nodePort 字段。如果将 type 字段设置为“NodePort”,则 Kubernetes

你可以这样定义kube服务

{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "my-service"
    },
    "spec": {
        "selector": {
            "app": "MyApp"
        },
        "ports": [
            {
                "protocol": "TCP",
                "port": 80,
                "targetPort": 9376,
                "nodePort": 30061
            }
        ],
        "type": "LoadBalancer"
    },
    }
}

或者你也可以使用 kubectl

kubectl expose rc example-rc --type=NodePort --port=9000 --target-port=8080 --node-port=32001

以上也适用于部署

所以最后,如果您想获得服务,请点击anynode:nodeport

【讨论】:

  • 不,我希望 pod 节点服务能够通过 443 80 3306 等端口与外部世界通信
猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2021-05-18
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
相关资源
最近更新 更多