K8S怎么设置NodePort端口

发布时间 2023-12-06 09:47:37作者: minseo

K8S创建service使用NodePort模式时端口是随机的,如需要固定可以修改yaml配置文件
一个完整的service的yaml配置文件如下

# cat zentao-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: zentao
  name: zentao-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: zentao
  type: NodePort
  ports:
  - nodePort: 30081
    port: 80
status:
  loadBalancer: {}

在type: NodePort下新增以下配置,注意空格间距

# 指定nodePort的端口为30081,对应的容器端口为80
# 需要指定port参数否则无法应用改yaml配置文件
 ports:
  - nodePort: 30081
    port: 80

启动后查看svc对应的NodePort端口固定为30081
image