1.安装Authentik

文档参考:https://docs.goauthentik.io/install-config/install/kubernetes/。

1.1 helm安装配置准备

先提前准备一个Authentik的values.yaml配置文件:

authentik:
  secret_key: "PleaseGenerateA50CharKey"
  # This sends anonymous usage-data, stack traces on errors and
  # performance data to authentik.error-reporting.a7k.io, and is fully opt-in
  error_reporting:
    enabled: true
  postgresql:
    password: "ThisIsNotASecurePassword"

postgresql:
  enabled: true
  auth:
    password: "ThisIsNotASecurePassword"

其中:secret_key需要可以使用openssl rand 60 | base64 -w 0命令进行生成,粘贴进去即可。注意这个secret_key一定需要记下来,不然如果Authentik挂掉了,后续没有办法进行恢复。

1.2 helm安装Authentik

使用如下的流程安装Authentik:

helm repo add authentik https://charts.goauthentik.io
helm repo update

helm install authentik authentik/authentik -n authentik -f values.yaml

1.3 Authentik路由网关Ingress配置

这里使用的是Traefik网关路由配置,使用如下的配置进行IngressRoute的配置:

apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: authentik-http
  namespace: authentik
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    match: Host(`ak.example.com`)
    services:
    - name: authentik-server
      port: 80

如果是使用的是Nginx的Ingress,也可以进行类似的配置。配置完成之后,可以通过ak.example.com地址进行访问。

2.配置外部OpenResty代理访问K8S集群

在外部OR将流量打到K8S(外部OR使用https访问,内部K8S使用Traefic作为Ingress入口),发现会出现报错:

The request failed and the interceptors did not return an alternative response

问题:多层代理,导致HTTP请求头信息透传丢失。
解决方案:在Traefik的Helm配置文件当中新增一个中间件Middleware,名称为authentik-headers,注意指定namespace为authentik所在的Namespace。

extraObjects:
  - apiVersion: traefik.io/v1alpha1
    kind: Middleware
    metadata:
      name: authentik-headers
      namespace: authentik
    spec:
      headers:
        browserXssFilter: true
        contentTypeNosniff: true
        customRequestHeaders:
          X-Forwarded-Proto: "https"

其中最关键的配置,是将这个X-Forwarded-Proto设置为https

        customRequestHeaders:
          X-Forwarded-Proto: "https"

接着,在IngressRoute当中,新增这个中间件authentik-headers的配置。

apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: authentik-http
  namespace: authentik
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    match: Host(`ak.example.com`)
    middlewares:
    - name: authentik-headers
    services:
    - name: authentik-server
      port: 80