kubernetes 如何使用NodePort与kind?

x33g5p2x  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(8)|浏览(145)

我尝试在kind中使用NodePort,但不知何故它不想工作。
我已经成功部署了以下集群:

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
vu8f3i0k

vu8f3i0k1#

Kind集群配置需要如下所示

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.yamlaccording to docs)传递给您的创建命令。

gjmwrych

gjmwrych2#

实际上,做Arghya Sadhu建议的工作。不知道为什么答案被删除了。

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

sr4lhrrt3#

我将分享我的答案,因为我今天使用多节点集群尝试了这个,另一件重要的事情是需要注意Kubectl客户端和服务器版本。我相信这将帮助某人。
步骤1
kind-api-cluster.yml

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30950
    hostPort: 30950
    listenAddress: "127.0.0.1"
    protocol: TCP
- role: worker
- role: worker

步骤2 Kubernetes服务和部署文件。(如果你注意到这里,kind的控制平面hostPort和kubernetes服务的nodePort应该是相同的)
api.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  labels:
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: api
          imagePullPolicy: Never
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: api-svc
spec:
  selector:
    app: api
  type: NodePort
  ports:
    - name: http
      nodePort: 30950
      targetPort: 8080
      port: 8080

附加说明:-
kubectl版本

Client Version: version.Info{Major:"1", Minor:"20+", GitVersion:"v1.20.4-dirty", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"dirty", BuildDate:"2021-03-15T09:55:27Z", GoVersion:"go1.16.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-21T01:11:42Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

类-版本

kind version 0.10.0
qgelzfjb

qgelzfjb4#

你不应该使用本地IP的nodeport。相反,你应该:
1.使用命令:kubectl获取节点-o宽
1.记下节点的IP

  1. curl 度:30000
  2. docker exec -it /bin/bash
  3. curl localhost:30000
v64noz0r

v64noz0r5#

在kind config yaml中

containerPort必须与服务配置中的'NodePort'相同。在你的情况下是30000

提示:hostPort可以是任意端口,不需要与containerPort相同

zaqlnxep

zaqlnxep6#

kubectl port-forward svc/hostname-service 30000:80

现在您的服务将在主机端口30000上访问

okxuctiv

okxuctiv7#

我会尝试删除nodePort:30000,并检查新分配的nodePort是否与TCP接口一起工作。如果你做一个服务的更新,kube有一个问题与satically分配nodePorts,你必须自己manige端口冲突。尝试删除部署并重新部署也会有所帮助。

sdnqo3pr

sdnqo3pr8#

我有两个选择:
1.或者在创建服务时保持与创建kind集群时相同的nodePort配置。
1.第二种方法是使用portforward命令将任何nodePort保留在服务清单中
kubectl port-forward svc/hostname-service [assigned node port to service]:80
然后使用localhost:[nodePort]访问它

相关问题