防止NGINX响应公共IP,或者让它重定向到其他地方--比如另一个URL --的过程是什么?
我有点困惑,因为我似乎在任何地方都找不到适合这种情况的文档。我们也用证书经理。
实际上,笔试失败了,因为公共IP使用的是NGINX/ k8s自签名证书。我们不想也不需要!
发布于 2021-04-14 18:48:15
解决这个问题是可能的,HTTP很容易,HTTPS很难。主要的问题是要为IP地址颁发证书,而不是任何颁发者都这样做(例如,让加密),所以您必须找到一个或尝试您现在使用的任何一个证书。
要处理未知主机(如IP地址),可以在规则中不使用host字段创建ingress对象。这将使创建的入口作为“默认”或“回退”规则工作,因此当Host头没有更好的匹配时(规则中有host的任何入口)将使用它。
要创建一个ingress对象,您需要一个服务,下面是如何在没有端点的情况下创建一个虚拟服务:
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地址。
:如果您的证书管理器Issuer或ClusterIssuer可以为IP地址颁发证书(类似于自签名的证书),则可以请求包含以下清单的证书:
#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.iohttps://stackoverflow.com/questions/67093249
复制相似问题