nginx-ingress.yaml配置如下:

kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: scp-sys-ui-ingress
  namespace: default
  uid: d94afa5d-2d83-45c8-8e01-78d0e7ded4a9
  resourceVersion: '35027'
  generation: 3
  creationTimestamp: '2026-07-09T07:13:25Z'
  annotations:
    kubernetes.io/ingress.class: nginx
  managedFields:
    - manager: dashboard
      operation: Update
      apiVersion: networking.k8s.io/v1
      time: '2026-07-09T07:46:11Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:kubernetes.io/ingress.class: {}
        f:spec:
          f:rules: {}
spec:
  rules:
    - host: test.shpl.asia
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: k8s-nginx
                port:
                  number: 80
status:
  loadBalancer: {}

一、先搞懂核心原理

HTTP 协议默认端口是 80,HTTPS 默认是 443,浏览器访问时不带端口等价于访问这两个端口。

你现在的问题是:Ingress 只是「路由规则」,真正处理流量的 Ingress Controller(nginx)​ 默认暴露的是 NodePort(30000+ 端口),所以访问必须带端口。要实现「域名直接访问」,只需要把 Ingress Controller 的 80/443 端口直接映射到宿主机的 80/443 即可。

二、推荐方案(按优先级排序)

方案1:hostNetwork 模式(裸金属/单节点首选,最常用)

让 Ingress Controller 直接监听宿主机的 80/443 端口,不需要额外的端口转发,性能也最好。

找到nginx-ingress 原生的deployment .yaml 配置,

spec.template.spec下添加如下注释两行:

spec:
  template:
    spec:
      hostNetwork: true          # 添加1处配置 ,直接使用宿主机网络
      dnsPolicy: ClusterFirstWithHostNet # 添加2处配置 , 配合 hostNetwork 的 DNS 策略
      containers:
      - name: ingress-nginx-controller
        # 原有配置不动

以上2处配置添加完成直接映射宿主机,即可通过宿主机的80,443端口,在浏览器输入域名直接访问。

kind: Deployment
apiVersion: apps/v1
metadata:
  name: k8s-nginx
  namespace: default
  uid: bc71ebd7-a0be-4c65-b597-d0fe7ad6084f
  resourceVersion: '40297'
  generation: 9
  creationTimestamp: '2026-07-09T05:18:33Z'
  labels:
    k8s-app: k8s-nginx
  annotations:
    deployment.kubernetes.io/revision: '7'
  
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: k8s-nginx
  template:
    metadata:
      name: k8s-nginx
      creationTimestamp: null
      labels:
        k8s-app: k8s-nginx
      annotations:
        kubectl.kubernetes.io/restartedAt: '2026-07-09T08:19:03Z'
    spec:
      containers:
        - name: k8s-nginx
          image: registry.cn-hangzhou.aliyuncs.com/dockerhub_mirror/nginx
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
          securityContext:
            privileged: false
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirstWithHostNet #直接映射宿主机配置2
      hostNetwork: true   #直接映射宿主机配置2
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
status:
  observedGeneration: 9
  replicas: 1
  updatedReplicas: 1
  readyReplicas: 1
  availableReplicas: 1
  conditions:
    - type: Available
      status: 'True'
      lastUpdateTime: '2026-07-09T08:10:23Z'
      lastTransitionTime: '2026-07-09T08:10:23Z'
      reason: MinimumReplicasAvailable
      message: Deployment has minimum availability.
    - type: Progressing
      status: 'True'
      lastUpdateTime: '2026-07-09T08:25:15Z'
      lastTransitionTime: '2026-07-09T05:18:33Z'
      reason: NewReplicaSetAvailable
      message: ReplicaSet "k8s-nginx-64c5fc5f4b" has successfully progressed.

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐