如何将NodePort与KIND一起使用? [英] How to use NodePort with kind?

查看:58
本文介绍了如何将NodePort与KIND一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NodePortkind一起使用,但不知何故它不起作用。

我已成功部署以下群集:

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 80
    hostPort: 30000
    listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
    protocol: tcp # Optional, defaults to tcp
- role: worker

然后是一个非常简单的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostname-deployment
  labels:
    app: hostname
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hostname
  template:
    metadata:
      labels:
        app: hostname
    spec:
      containers:
      - name: hostname
        image: hostname:0.1
        ports:
        - containerPort: 80

和服务:

apiVersion: v1
kind: Service
metadata:
  name: hostname-service
spec:
  type: NodePort
  selector:
    app: hostname
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30000

我可以通过例如

连接到服务
(in one terminal)
k port-forward service/hostname-service 8080:80
Forwarding from 127.0.0.1:8080 -> 80

(another one)
curl localhost:8080
hostname: hostname-deployment-75c9fd6584-ddc59 at Wed, 17 Jun 2020 15:38:33 UTC

但我无法通过公开的NodePort连接到服务

curl -v localhost:30000
* Rebuilt URL to: localhost:30000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 30000 (#0)
> GET / HTTP/1.1
> Host: localhost:30000
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

kubectl get all输出:

NAME                                       READY   STATUS    RESTARTS   AGE
pod/hostname-deployment-75c9fd6584-ddc59   1/1     Running   0          34m
pod/hostname-deployment-75c9fd6584-tg8db   1/1     Running   0          34m

NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/hostname-service   NodePort    10.107.104.231   <none>        80:30000/TCP   34m
service/kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP        35m

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hostname-deployment   2/2     2            2           34m

NAME                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/hostname-deployment-75c9fd6584   2         2         2       34m

推荐答案

类集群配置需要如下

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30000
    hostPort: 30000
    listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
    protocol: tcp # Optional, defaults to tcp
- role: worker

然后将此文件作为kind create cluster --config=config.yaml(according to docs)传递给您的创建命令。

这篇关于如何将NodePort与KIND一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆