Nodeport 在 30000-32767 之间随机选择。您可以在服务定义中进行设置。
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: MyApp
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 80
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
来自文档:https ://kubernetes.io/docs/concepts/services-networking/service/#nodeport
更新
放置在包kubernetes/pkg/registry/core/service/portallocator
中的类负责为服务分配节点端口。
此测试记录了行为:https ://github.com/kubernetes/kubernetes/blob/master/pkg/registry/core/service/portallocator/operation_test.go
Kubernetes 只占用一个随机端口,如果该端口不是免费的,它会占用下一个端口。
如果您可以阅读该包中的其他类,则可以很好地了解该行为。