kubectl edit deploy nginx-ingress-controller -n ingress-nginx


 template:
    metadata:
      annotations:
        prometheus.io/port: "10254"
        prometheus.io/scrape: "true"
      creationTimestamp: null
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    spec:
      containers:
      - args:
        - /nginx-ingress-controller
        - --configmap=$(POD_NAMESPACE)/nginx-configuration
        - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
        - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx
        - --annotations-prefix=nginx.ingress.kubernetes.io
        - --http-port=80		#添加该参数可指定端口

..................................................................
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostNetwork: true			#添加该参数开放主机端口
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: nginx-ingress-serviceaccount


ingress创建模板(以prometheus为例)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: 
  name: prom-ingress
  namespace: monitoring
spec: 
  rules: 
  - host: alert.test.com		#此处即为外网访问的域名
    http: 
      paths: 
      - backend: 
          service: 
            name: altermanager-main
            port: 
              number: 9093
        path: /
        pathType: Prefix
  - host: grafana.test.com
    http: 
      paths: 
      - backend: 
          service: 
            name: grafana
            port: 
              number: 3000
        path: /
        pathType: Prefix
  - host: prom.test.com
    http: 
      paths: 
      - backend: 
          service: 
            name: prometheus-k8s
            port: 
              number: 9090
        path: /
        pathType: Prefix
status: 
  loadBalancer: {}

注意:需要定义hosts文件解析,将域名只想ingress-controller节点的ip
如果开放的非80端口,访问时还需要域名+端口

正常时候对应主机开放的端口

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐