修改k8s中的ingress组件的默认对外端口

安装ingress组件时,默认是随机分到 30000到32767之间的端口。如果需要修改为指定的端口,有两种方式:安装前指定端口,安装后修改为指定端口。

【方式一:安装前修改为指定端口】

安装方式一:一键安装 Ingress-Nginx(裸金属服务器,无云厂商 LB,推荐)

# 下载ingress部署文件到本地
curl -o install-ingress.yaml https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/baremetal/deploy.yaml

# 全局替换镜像地址,增加国内加速前缀 m.daocloud.io/ ,不用国外镜像,ingress的镜像有可能会拉取失败。
sed -i 's|registry.k8s.io|m.daocloud.io/registry.k8s.io|g' install-ingress.yaml

 

下载的 install-ingress.yaml文件安装的igress组件,默认对外暴露的端口是随机的,如果是指定ingress服务组件的端口,按照下面方式更改:

如果要修改为固定端口,固定 HTTP 端口:30080、HTTPS端口:30443

步骤 1:找到 ingress-nginx-controller Service 片段

搜索 kind: Service,名称为 ingress-nginx-controller,原始配置默认 type: LoadBalancer,无 nodePort:
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: http
      protocol: TCP
    - name: https
      port: 443
      targetPort: https
      protocol: TCP
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx

步骤 2:修改三处

  1. type: LoadBalancertype: NodePort
  2. http 端口增加 nodePort: 30080
  3. https 端口增加 nodePort: 30443
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: NodePort # 改为NodePort
  ports:
    - name: http
      port: 80
      targetPort: http
      protocol: TCP
      nodePort: 30080 # 固定HTTP外网端口
    - name: https
      port: 443
      targetPort: https
      protocol: TCP
      nodePort: 30443 # 固定HTTPS外网端口
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx

 

步骤 3:执行部署ingress组件

kubectl apply -f install-ingress.yaml

 

image

查看ingress部署状态

# 查看ingress命名空间pod
kubectl get pods -n ingress-nginx
# 查看ingress对外service(自动分配NodePort 80/443端口)
kubectl get svc -n ingress-nginx
# 确认IngressClass存在(新版k8s必须)
kubectl get ingressclasses

 

安装方式二:Helm 安装(便于后续升级,可选,不建议用,依赖海外镜像)

# 添加仓库
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
# 安装,type=NodePort适配你的服务器集群
helm upgrade --install ingress-nginx ingress-nginx \
--namespace ingress-nginx --create-namespace \
--set controller.service.type=NodePort
# HTTP固定32590【可选配置】
--set controller.service.ports.http.nodePort=30080 \
# HTTPS固定32591【可选配置】
--set controller.service.ports.https.nodePort=30443

 

 

【方式二:安装后修改为指定端口】

已安装 ingress,不想重装,在线修改为固定端口

不用重新下载文件,直接编辑现有 service:
kubectl edit svc ingress-nginx-controller -n ingress-nginx

 

找到 两个nodePort:的位置,分别对应http及https的两个端口,修改为指定的端口,保存即可,不需要重启。

ports:
  - appProtocol: http
    name: http
    nodePort: 30080   #改为指定端口,没有就添加
    port: 80
    protocol: TCP
    targetPort: http
  - appProtocol: https
    name: https
    nodePort: 30443    #改为指定端口,没有就添加
    port: 443
    protocol: TCP
    targetPort: https
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  sessionAffinity: None
  type: NodePort     #改为 NodePort
status:
  loadBalancer: {}

 

posted @ 2026-07-16 18:06  民工黑猫  阅读(23)  评论(0)    收藏  举报