gpt4 book ai didi

How to use NodePort with kind?(如何将NodePort与Kind一起使用?)

翻译 作者:bug小助手 更新时间:2023-10-26 21:47:22 46 4
gpt4 key购买 nike



I'm trying to use NodePort with kind but somehow it doesn't want to work.

我正试着用Kind来使用NodePort,但不知何故它不想工作。



I've successfully deployed the following cluster:

我已经成功部署了以下集群:



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


and then a very simple deployment:

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



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


and a service:

和一项服务:



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


and I can connect to the service via e.g.

并且我可以通过例如



(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


But I cannot connect to the service via the exposed NodePort

但我无法通过公开的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 output:

Kubectl获取所有输出:



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

更多回答

are you on Mac?

你在玩Mac吗?

That's on Linux

这是在Linux上

优秀答案推荐

Kind cluster configuration needs to be like below

种类群集配置需要如下所示


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

This file is then passed to your creation command as kind create cluster --config=config.yaml (according to docs).

然后,该文件以KIND CREATE CLUSTER--CONFIG=config.yaml的形式传递给您的创建命令(根据文档)。



Actually doing what Arghya Sadhu suggested worked. Not sure why the answer got deleted.

事实上,按照阿迦撒督的建议去做是有效的。不知道为什么答案被删除了。



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


I will share my answer as i tried out this today by using multi node kind cluster and another important thing is that needs to take care about kubectl client and server versions. I believe this will help someone.

我将分享我的答案,因为我今天尝试使用多节点类型的集群,另一件重要的事情是需要注意kubectl客户端和服务器版本。我相信这会对某些人有所帮助。


Step 1

步骤1


kind-api-cluster.yml

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

Step 2
Kubernetes service and deployment file. ( If you note here, the kind's control-plane hostPort and kubernetes service's nodePort should be the same )

步骤2:Kubernetes服务和部署文件。(请注意,该类的控制平面HostPort和Kubernetes服务的nodePort应该是相同的)


api.yml

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

Additional Notes:-

其他备注:


kubectl version

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

Kind--版本


kind version 0.10.0


You shouldn't use nodeport with local IP.
Instead you should:

本端IP不能使用nodeport。相反,您应该:



  1. use command : kubectl get nodes -o wide

  2. note down the IP of the node

  3. curl :30000


or



  1. docker exec -it /bin/bash



  2. curl localhost:30000





in the kind config yaml

在KIND配置YAML中


containerPort must be the same with the 'NodePort' in service config.
in your case is 30000

容器端口必须与服务配置中的‘NodePort’相同。在你的情况下是30000


tips: hostPort can be any port ,it does not need to be the same with containerPort

小贴士:主机端口可以是任何端口,它不需要与容器端口相同



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

Now your service will be accessed on host port 30000

现在可以通过主机端口30000访问您的服务



I would try removing the nodePort: 30000 and check that the newly assigned nodePort works with the loopback interface. If you do an update of the service, kube has a problem with satically asigned nodePorts, you have to manige yourself the port collision. Trying to delete the deployment and redeploy could also help.

我会尝试删除nodePort:30000,并检查新分配的nodePort是否与环回接口一起工作。如果您进行服务更新,Kube在静态分配的nodePorts方面有问题,您必须管理自己的端口冲突。尝试删除部署并重新部署也可能有所帮助。



I got it worked by two options:

我通过两种选择让它发挥作用:



  1. Either you keep the same nodePort config while creating the service and the one used to create the kind cluster.

  2. The second is to keep any nodePort in the service manifest just use the port forward command


kubectl port-forward svc/hostname-service [assigned node port to service]:80

Kubectl port-转发服务/主机名-服务[分配给服务的节点端口]:80


Then access it using localhost:[nodePort]

然后使用本地主机访问它:[nodePort]


更多回答

I did not try but would like to know if in this case, only one port 30000 works or multiple services with different ports work?

我没有尝试,但想知道在这种情况下,是只有一个端口30000工作,还是具有不同端口的多个服务工作?

Then set the 30000 port as nodePort in NodePort service. Service should be available at localhost:30000, unfortunately for me it was very unstable (kind thing?), sometimes working but mostly failing.

然后在节点端口服务中将30000端口设置为节点端口。服务应该在本地主机上可用:30000,不幸的是,对我来说,它非常不稳定(好心的事情?),有时可以工作,但大多数都失败了。

In my case, i had to add this mapping in order to access a service of a cluster running within GitLab CI/DinD.

在我的例子中,为了访问在GitLab CI/dind中运行的集群的服务,我必须添加这个映射。

Glad it worked..I was not sure so asked if you are on mac..I have undeleted my answer..

很高兴它起作用了…我不确定,所以问你是否在Mac上。我已经删除了我的答案。

So it's just the containerPort: 30000 setting what was wrong? And after that localhost:30000 worked? Because I am still having problems

所以这只是集装箱端口:30000的设置有什么问题?在那个当地人之后:30000的人工作了吗?因为我还是有一些问题

@MichaelLossagk were you able to figure this out?

@MichaelLossagk你能弄明白这一点吗?

It is working even on a Mac, please delete the Kind Cluster first then re-create. I was able to get it working this way. Make sure the NodePort is mapped to the same container port.

它即使在Mac上也可以工作,请先删除Kind集群,然后重新创建。我能够让它以这种方式工作。确保NodePort映射到相同的容器端口。

hostPort and containerport value of 30000 worked for me.

HostPort和Containerport的值30000对我来说很有效。

u are right however, the kind clusters don't expose external node ips

然而,您是对的,这类集群不会暴露外部节点IPS

@user2083529 In you machine, it is if you use extraPortMappings of kind config. If you use aws ec2, you can even use aws ec2 public IP to access it. kind.sigs.k8s.io/docs/user/configuration/….

@user2083529在你的机器上,如果你使用extraPortMappings的配置,那就是。如果您使用AWS EC2,您甚至可以使用AWS EC2公共IP来访问它。kind.sigs.k8s.io/docs/user/configuration/..

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com