authentik部署全攻略:Docker/Kubernetes/云平台详解

【免费下载链接】authentik The authentication glue you need. 【免费下载链接】authentik 项目地址: https://gitcode.com/GitHub_Trending/au/authentik

概述

authentik是一款开源的身份提供商(Identity Provider,IdP),专为现代单点登录(SSO)设计。它支持SAML、OAuth2/OIDC、LDAP、RADIUS等多种协议,从小型实验室到大型生产集群都能完美适配。本文将全面解析authentik在各种环境下的部署方案。

部署方案对比

部署方式适用场景复杂度扩展性维护成本
Docker Compose开发测试、小型环境⭐⭐⭐⭐⭐⭐
Kubernetes生产环境、大规模部署⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
AWS CloudFormationAWS云环境⭐⭐⭐⭐⭐⭐⭐⭐⭐
云服务商云平台部署⭐⭐⭐⭐⭐⭐⭐

Docker Compose部署

环境准备

首先创建环境配置文件 .env

# 生成随机密钥
AUTHENTIK_SECRET_KEY=$(openssl rand -base64 32)
PG_PASS=$(openssl rand -base64 16)
PG_USER=authentik
PG_DB=authentik
COMPOSE_PORT_HTTP=9000
COMPOSE_PORT_HTTPS=9443

Docker Compose配置

version: '3.8'

services:
  postgresql:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: ${PG_DB}
      POSTGRES_PASSWORD: ${PG_PASS}
      POSTGRES_USER: ${PG_USER}
    volumes:
      - database:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d ${PG_DB} -U ${PG_USER}"]
      interval: 30s
      timeout: 5s
      retries: 5

  redis:
    image: redis:alpine
    command: --save 60 1 --loglevel warning
    volumes:
      - redis:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
      interval: 30s
      timeout: 3s
      retries: 5

  server:
    image: ghcr.io/goauthentik/server:latest
    command: server
    ports:
      - "${COMPOSE_PORT_HTTP}:9000"
      - "${COMPOSE_PORT_HTTPS}:9443"
    environment:
      AUTHENTIK_POSTGRESQL__HOST: postgresql
      AUTHENTIK_POSTGRESQL__NAME: ${PG_DB}
      AUTHENTIK_POSTGRESQL__PASSWORD: ${PG_PASS}
      AUTHENTIK_POSTGRESQL__USER: ${PG_USER}
      AUTHENTIK_REDIS__HOST: redis
      AUTHENTIK_SECRET_KEY: ${AUTHENTIK_SECRET_KEY}
    depends_on:
      postgresql:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./media:/media
      - ./custom-templates:/templates

  worker:
    image: ghcr.io/goauthentik/server:latest
    command: worker
    environment:
      AUTHENTIK_POSTGRESQL__HOST: postgresql
      AUTHENTIK_POSTGRESQL__NAME: ${PG_DB}
      AUTHENTIK_POSTGRESQL__PASSWORD: ${PG_PASS}
      AUTHENTIK_POSTGRESQL__USER: ${PG_USER}
      AUTHENTIK_REDIS__HOST: redis
      AUTHENTIK_SECRET_KEY: ${AUTHENTIK_SECRET_KEY}
    depends_on:
      postgresql:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./media:/media
      - ./certs:/certs
      - ./custom-templates:/templates

volumes:
  database:
  redis:

部署流程

mermaid

Kubernetes部署

Helm Chart安装

# 添加Helm仓库
helm repo add authentik https://charts.goauthentik.io
helm repo update

# 创建命名空间
kubectl create namespace authentik

# 安装authentik
helm install authentik authentik/authentik \
  --namespace authentik \
  --set postgresql.auth.password="your-secure-password" \
  --set secretKey="your-secure-secret-key" \
  --set ingress.enabled=true \
  --set ingress.hosts[0].host=authentik.example.com

Kubernetes资源配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: authentik-server
  namespace: authentik
spec:
  replicas: 3
  selector:
    matchLabels:
      app: authentik-server
  template:
    metadata:
      labels:
        app: authentik-server
    spec:
      containers:
      - name: server
        image: ghcr.io/goauthentik/server:latest
        env:
        - name: AUTHENTIK_POSTGRESQL__HOST
          value: "postgresql"
        - name: AUTHENTIK_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: authentik-secrets
              key: secretKey
        ports:
        - containerPort: 9000
        - containerPort: 9443
        readinessProbe:
          httpGet:
            path: /-/health/
            port: 9000
          initialDelaySeconds: 30
          periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: authentik-service
  namespace: authentik
spec:
  selector:
    app: authentik-server
  ports:
  - name: http
    port: 9000
    targetPort: 9000
  - name: https
    port: 9443
    targetPort: 9443

云平台部署方案

AWS CloudFormation部署

AWSTemplateFormatVersion: '2010-09-09'
Description: authentik Identity Provider Stack

Parameters:
  VPCId:
    Type: AWS::EC2::VPC::Id
    Description: VPC ID
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
    Description: Subnet IDs

Resources:
  AuthentikCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: authentik-cluster

  AuthentikTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: authentik
      NetworkMode: awsvpc
      RequiresCompatibilities: [FARGATE]
      Cpu: 1024
      Memory: 2048
      ContainerDefinitions:
      - Name: authentik-server
        Image: ghcr.io/goauthentik/server:latest
        Essential: true
        PortMappings:
        - ContainerPort: 9000
        - ContainerPort: 9443
        Environment:
        - Name: AUTHENTIK_SECRET_KEY
          Value: !Ref SecretKey

云服务商一键部署

通过云服务商Marketplace可以快速部署:

  1. 访问云服务商控制台
  2. 选择Marketplace → Applications
  3. 搜索"authentik"
  4. 选择套餐规格
  5. 配置网络和安全组
  6. 一键部署

高级配置选项

数据库配置

# 高级PostgreSQL配置
AUTHENTIK_POSTGRESQL__OPTIONS: >
  -c shared_buffers=256MB
  -c max_connections=200
  -c work_mem=4MB

Redis缓存优化

# Redis性能优化
AUTHENTIK_REDIS__CACHE_TIMEOUT: 3600
AUTHENTIK_REDIS__CACHE_TIMEOUT_FLOWS: 86400

TLS/SSL配置

# 自定义证书
AUTHENTIK_CERTIFICATE_KEY: /certs/key.pem
AUTHENTIK_CERTIFICATE_CHAIN: /certs/fullchain.pem

监控与维护

健康检查配置

# Docker健康检查
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:9000/-/health/"]
  interval: 30s
  timeout: 10s
  retries: 3
  start_period: 40s

日志配置

# 结构化日志
AUTHENTIK_LOG_LEVEL: INFO
AUTHENTIK_LOG_HANDLERS: console,json

故障排除指南

常见问题解决

问题现象可能原因解决方案
数据库连接失败密码错误/网络问题检查环境变量和网络连通性
服务启动超时资源不足增加CPU/内存分配
SSL证书错误证书配置问题验证证书路径和权限

性能优化建议

  1. 数据库优化:配置连接池和索引
  2. 缓存策略:合理设置Redis缓存超时
  3. 负载均衡:使用多副本部署提高可用性
  4. 资源分配:根据实际负载调整CPU/内存

安全最佳实践

密钥管理

# 使用Kubernetes Secrets
kubectl create secret generic authentik-secrets \
  --from-literal=secretKey=$(openssl rand -base64 32) \
  --from-literal=postgresPassword=$(openssl rand -base64 16)

网络隔离

# Docker网络配置
networks:
  authentik-net:
    driver: bridge
    internal: true

总结

authentik提供了灵活的部署方案,从简单的Docker Compose到复杂的Kubernetes集群都能完美适配。选择合适的部署方式需要考虑团队的技术栈、规模需求和安全要求。建议从小规模开始,逐步扩展到生产环境,确保每个环节都经过充分测试和验证。

通过本文的详细指南,您应该能够成功在各种环境中部署和管理authentik身份提供商服务。

【免费下载链接】authentik The authentication glue you need. 【免费下载链接】authentik 项目地址: https://gitcode.com/GitHub_Trending/au/authentik

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐