如何使用 Docker for Windows 访问本地主机上的 kubernetes 服务

Lie*_*ero 6 docker kubernetes kubectl docker-for-windows

我有 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

我希望 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>
Run Code Online (Sandbox Code Playgroud)

Art*_*aev 5

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

  1. 类型:负载均衡器。但是,它仅适用于云提供商。

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

有关更多信息,请查看官方文档