阻止 NGINX-ingress 响应 public IP

Prevent NGINX-ingress from responding to public IP

阻止 NGINX 响应 Public IP,或让它重定向到其他地方的过程是什么 - 就像另一个 URL。

我有点难过,因为我似乎在任何地方都找不到关于这种情况的文档。我们也在使用证书管理器。

基本上 PEN 测试失败了,因为 public IP 正在使用 NGINX/k8s 自签名证书进行响应。我们不想要也不需要那样!

有可能解决这个问题; HTTP 容易,HTTPS 困难。主要问题是为 IP 地址颁发证书,但没有任何颁发者这样做(例如 letsencrypt 不会),因此您必须找到一个或尝试使用您现在使用的任何一个。

要处理未知主机(如 IP 地址),您可以在规则中创建一个没有 host 字段的入口 object。这将使创建的入口作为 'default' 或 'fallback' 规则工作,因此当 Host header 没有更好的匹配时将使用它(任何入口 with host in rules).

要创建入口 object,您需要一个服务,以下是创建没有端点的虚拟服务的方法:

apiVersion: v1
kind: Service
metadata:
  name: dummy-service
spec:
  clusterIP: None

接下来,它的入口:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: default-ingress
  annotations:
    kubernetes.io/ingress_class: nginx
    nginx.ingress.kubernetes.io/configuration-snippet: |
      # Nginx will place this in server block

      # You can redirect all requests somewhere:
      return 301 https://example.com/;
      # or just:
      #return 403;
spec:
  rules:
    # This rule has no 'host' field and because of that
    # NGINX won't include 'server_name' directive in
    # vhost configuration. What this means is that this
    # ingress rule will be used only if the request
    # comes with 'Host' header for which there is no
    # specific rule (IP-address for example).
    - http:
        paths:
          - backend:
              servicePort: 80
              serviceName: dummy-service

此时您已获得适用于 HTTP 和 HTTPS 的重定向(或 403),尽管后者使用的是虚拟证书。如果您设法为您的 IP 地址颁发证书并将其保存为秘密,接下来就是让 NGINX 使用它而不是其默认的虚拟证书。为此,您需要通过添加 --default-ssl-certificate 参数来修改入口控制器部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ingress-nginx-controller
spec:
  template:
    spec:
      containers:
        - name: controller
          args:
            - /nginx-ingress-controller
            # use 'namespace/secret_name' as the value for the argument
            - --default-ssl-certificate=default/ip-cert-secret

现在 NGINX 将使用有效证书响应 IP 地址。

奖励:如果您有 cert-manager IssuerClusterIssuer 可以为 IP 地址颁发证书(例如 self-signed一),您可以使用以下清单申请证书:

#apiVersion: cert-manager.io/v1
apiVersion: cert-manager.io/v1beta1
kind: Certificate
metadata:
  name: ip-cert
spec:
  secretName: ip-cert-secret
  duration: 2160h # 90d
  renewBefore: 360h # 15d
  isCA: false
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  usages:
    - server auth
    - client auth
  commonName: Dummy
  ipAddresses:
  - 10.1.1.13 # fill the list
  issuerRef:
    name: # insert issuer name
    kind: # Issuer or ClusterIssuer 
    group: cert-manager.io