Kubernetes - Minkube 使用随机端口号公开 Nodeport

问题描述 投票:0回答:2

我是 kubernate 和 minikube 的新手,我尝试使用 NodePort 类型的服务能够从主机浏览器访问 pod,minikube 忽略服务 yaml 文件中指定端口的问题,而是 minikube 公开一个新端口随机数。

这是我的配置

apiVersion: v1
kind: Service
metadata:
  name: test
spec:
  type: NodePort
  selector:
    app: test
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30036 ==> here is port

你可以看到指定的端口是30036,这是我在描述服务时得到的

kubectl describe services test
-----------------------------------

Name:                     test
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=test
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.104.58.44
IPs:                      10.104.58.44
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30036/TCP
Endpoints:                192.168.50.2:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

但是当尝试运行 minikube 服务 --url 时,我得到了一个不同的端口号,我认为它是随机生成的

minikube service test --url
------------------------------
* Starting tunnel for service test.
|-----------|------------|-------------|------------------------|
| NAMESPACE |    NAME    | TARGET PORT |          URL           |
|-----------|------------|-------------|------------------------|
| default   | test       |             | http://127.0.0.1:56758 |
|-----------|------------|-------------|------------------------|
http://127.0.0.1:56758

这是

的输出
kubectl get svc -A
-------------------------
NAMESPACE     NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
default       test         NodePort    10.104.58.44   <none>        80:30036/TCP             4h4m
default       kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP                  3d1h
kube-system   kube-dns     ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   3d1h

那么如何让 minikube 使用指定的端口“30036”而不是生成新的随机端口。

kubernetes kubectl minikube
2个回答
0
投票

你需要改变

spec:
  type: NodePort

spec:
  type: LoadBalancer

不要创建隧道,而是使用端口转发:

kubectl port-forward svc/postgres <your machine port>:<K8t service port>

您的情况是:

kubectl port-forward svc/test 80:80

-1
投票

根据文档

nodePort 类型的 minikube 默认有效端口为

30000-32767
。如果您想要特定的端口号,可以在 nodePort 字段中指定一个值。控制平面将为您分配该端口或报告 API 事务失败。这意味着您需要自己处理可能的端口冲突。您还必须使用有效的端口号,该端口号位于为 NodePort 使用配置的范围内。

请注意,此服务显示为

<NodeIP>:spec.ports[*].nodePort
.spec.clusterIP:spec.ports[*].port
。如果设置了 kube-proxy 的
--nodeport-addresses
标志或 kube-proxy 配置文件中的等效字段,则
<NodeIP>
将被过滤节点 IP。

以这种方式启动 minikube 时:

minikube start --extra-config=apiserver.service-node-port-range=80-30036
,80端口也可以使用:

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

minikube service test --url
现在按预期返回
http://192.168.99.100:80
,并且应用程序可在端口 80 上使用。

查看类似的案例并参考此链接了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.