我有一个简单的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
答案 0 :(得分:2)
您可以使用server-snippet注释。 This似乎正是您想要实现的目标。
答案 1 :(得分:1)
我遇到了同样的问题,并在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
然后,按照doc中的说明为身份验证创建secret
:
创建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
具有auth的第二个入口,用于您需要限制的路径:
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,他的解决方案可能存在一些问题。
答案 2 :(得分:0)
复制 Kubernetes 的官方方法并使用始终返回 404 的 defaultbackend
容器。
apiVersion: apps/v1
kind: Deployment
metadata:
name: defaultbackend
spec:
selector:
matchLabels:
app: defaultbackend
template:
metadata:
labels:
app: defaultbackend
spec:
containers:
- name: defaultbackend
image: k8s.gcr.io/defaultbackend-amd64:1.5
resources:
requests:
memory: 10M
cpu: 5m
limits:
memory: 10M
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: defaultbackend
spec:
selector:
app: defaultbackend
ports:
- port: 80
targetPort: 8080
然后在您的入口中,添加一个 paths
条目:
paths:
- path: /
backend:
serviceName: my-real-service
servicePort: 3000
- path: /admin
backend:
serviceName: defaultbackend
servicePort: 80