Kubernetes Ingress 网络拒绝某些路径

Kubernetes Ingress network deny some paths

我有一个简单的 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 annonations 或其他方法拒绝自定义路径。


我使用如下所示的注释处理此问题。

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 

您可以使用 server-snippet annotation. This 似乎正是您想要实现的目标。

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

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:

中的描述为 auth 创建一个 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

第二个 Ingress 需要限制的路径:

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,他的解决方案可能存在一些问题。

复制官方的 Kubernetes 方法并使用始终 returns 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