【问题标题】:How to access kubernetes service on localhost with Docker for Windows如何使用 Docker for Windows 访问 localhost 上的 kubernetes 服务
【发布时间】:2019-03-02 04:12:00
【问题描述】:

我有 Windows 10 Pro 和 Docker for Windows v18.06.1-ce 并启用了 kubernetes。

使用kubectl create -f,我创建了rc.yml:

apiVersion: v1
kind: ReplicationController
metadata:
  name: hello-rc
spec:
  replicas: 9
  selector:
    app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-ctr
        image: nigelpoulton/pluralsight-docker-ci:latest
        ports:
        - containerPort: 8080

svc.yml

apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels:
    app: hello-world
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30001
    protocol: TCP
  selector:
    app: hello-world

如何访问服务背后的网站?

我希望 localhost:8080 可以正常工作,但实际上并没有,10.108.96.27:8080 也没有

> kubectl describe service/hello-svc
Name:                     hello-svc
Namespace:                default
Labels:                   app=hello-world
Annotations:              <none>
Selector:                 app=hello-world
Type:                     NodePort
IP:                       10.108.96.27
LoadBalancer Ingress:     localhost
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                10.1.0.10:8080,10.1.0.11:8080,10.1.0.12:8080 + 6 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

【问题讨论】:

    标签: docker kubernetes kubectl docker-for-windows


    【解决方案1】:

    有两种方法可以从 Kubernetes 集群向外部世界公开服务:

    1. 类型:LoadBalancer。但是,它仅适用于云提供商。

    2. 类型:NodePort。正如你在这种情况下使用的那样。现在,要访问 Kubernetes 集群内的服务,您需要使用其中一个节点的 IP 地址和字段 nodePort 中的端口 例如,12.34.56.78:30001

    更多信息,请浏览official documentation

    【讨论】:

    • 使用 NodePort localhost:30001 有效,但 12.34.56.78:30001 仍然无效。
    【解决方案2】:

    本地开发:

    kubectl port-forward <my-pod-name> 8080:8080
    

    您的 pod 可通过 localhost:8080 访问。

    更多关于端口转发here

    【讨论】:

      猜你喜欢
      • 2015-03-04
      • 2021-04-13
      • 1970-01-01
      • 2020-07-28
      • 2020-11-23
      • 1970-01-01
      • 2020-04-22
      • 2018-05-09
      • 2019-05-07
      相关资源
      最近更新 更多