gpt4 book ai didi

nginx - Kubernetes Ingress 网络拒绝某些路径

转载 作者:行者123 更新时间:2023-12-04 18:19:42 35 4
gpt4 key购买 nike

我有一个简单的 kubernetes 入口网络。

我需要拒绝访问某些关键路径,例如/admin 等。

我的入口网络文件如下所示。

 apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-test
spec:
rules:
- host: host.host.com
http:
paths:
- path: /service-mapping
backend:
serviceName: /service-mapping
servicePort: 9042

我如何使用 kubernetes 入口网络、nginx 注释或其他方法拒绝自定义路径。

我使用如下所示的注释处理这个问题。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-configuration-snippet
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |

server_tokens off;
location DANGER-PATH {
deny all;
return 403;
}

spec:
rules:
- host: api.myhost.com
http:
paths:
- backend:
serviceName: bookapi-2
servicePort: 8080
path: PATH

最佳答案

我遇到了同样的问题,并在 github 上找到了解决方案.
为了实现你的目标,你需要先默认创建两个 Ingress,没有任何限制:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-test
spec:
rules:
- host: host.host.com
http:
paths:
- path: /service-mapping
backend:
serviceName: /service-mapping
servicePort: 9042

然后,创建一个 secret用于身份验证,如 doc 中所述:

创建 htpasswd
$ htpasswd -c auth foo
New password: <bar>
New password:
Re-type new password:
Adding password for user foo

创建 secret :
$ kubectl create secret generic basic-auth --from-file=auth
secret "basic-auth" created

需要限制路径的带有身份验证的第二个入口:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-with-auth
annotations:
# type of authentication
nginx.ingress.kubernetes.io/auth-type: basic
# name of the secret that contains the user/password definitions
nginx.ingress.kubernetes.io/auth-secret: basic-auth
# message to display with an appropiate context why the authentication is required
nginx.ingress.kubernetes.io/auth-realm: "Authentication Required - foo"
spec:
rules:
- host: host.host.com
http:
paths:
- path: /admin
backend:
serviceName: service_name
servicePort: 80

根据 sedooe answer ,他的解决方案可能有一些问题。

关于nginx - Kubernetes Ingress 网络拒绝某些路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874503/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com