3.3 Weaviate Kubernetes部署方案 — 向量数据库选型宝典 关键词短语 本节导读:本节详细讲解Weaviate的Kubernetes云原生部署方案,包含单集群、多集群和高可用配置,帮助企业级用户快速构建可扩展的向量搜索平台。 学习目标 掌握Weaviate Kubernetes单集群部署方法 理解Weaviate多节点架构和模块化设计 学会使用Helm Chart管理Weaviate服务 掌握数据持久化和灾备策略 了解监控和运维最佳实践 核心概念 Weaviate架构概述 Weaviate采用微服务架构设计,具有以下核心特征: 模块化设计:核心服务与模块分离,支持动态加载 GraphQL接口:提供灵活的查询语言 向量化原生:内置向量计算能力
本节导读:本节详细讲解Weaviate的Kubernetes云原生部署方案,包含单集群、多集群和高可用配置,帮助企业级用户快速构建可扩展的向量搜索平台。
Weaviate采用微服务架构设计,具有以下核心特征:
Weaviate支持多种部署模式:
基础环境:
网络配置:
软件依赖:
# 安装Helm curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash # 添加Weaviate Helm仓库 helm repo add weaviate https://weaviate.github.io/weaviate-helm helm repo update # 验证安装 helm version kubectl version
# 创建命名空间 kubectl create namespace weaviate-system # 配置资源限制 kubectl apply -f - << EOF apiVersion: v1 kind: ResourceQuota metadata: name: weaviate-quota namespace: weaviate-system spec: hard: requests.cpu: "16" requests.memory: 32Gi limits.cpu: "32" limits.memory: 64Gi pods: 10 EOF
创建values-single.yaml:
# 单集群Weaviate配置 replicaCount: 1 # 服务配置 service: type: ClusterIP port: 8080 # 持久化存储 persistence: enabled: true size: 50Gi accessMode: ReadWriteOnce storageClass: "fast-ssd" # 资源限制 resources: requests: cpu: "4" memory: "8Gi" limits: cpu: "8" memory: "16Gi" # 环境变量 environment: AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: "true" DEFAULT_DATATYPE_MODULE: "text2vec-openai" ENABLE_MODULES: "text2vec-openai,text2vec-huggingface" QUERY_DEFAULT_LIMIT: "25" CLUSTER_HOSTNAME: "weaviate-single" BACKUP_ENABLED: "true" RESTORE_ENABLED: "true" BACKUP_SCHEDULE: "0 2 * * *" BACKUP_RETENTION: "7" # 监控配置 monitoring: enabled: true port: 9000 metricsPath: /metrics # 健康检查 healthCheck: enabled: true livenessProbe: httpGet: path: /v1/.well-known/alive port: 8080 initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 readinessProbe: httpGet: path: /v1/.well-known/ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5 timeoutSeconds: 3
# 安装单集群Weaviate helm install weaviate-single weaviate/weaviate \ --namespace weaviate-system \ -f values-single.yaml # 查看部署状态 kubectl get pods -n weaviate-system kubectl logs -f weaviate-single -n weaviate-system # 等待就绪 kubectl wait --for=condition=ready pod/weaviate-single-0 -n weaviate-system
# 检查服务状态 kubectl get svc -n weaviate-system kubectl describe svc weaviate-single -n weaviate-system # 创建测试数据 curl -X POST http://localhost:8080/v1/objects \ -H "Content-Type: application/json" \ -d '{ "class": "Article", "properties": { "title": "测试文章", "content": "这是Weaviate测试内容", "vector": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] } }' # 执行GraphQL查询 curl -X POST http://localhost:8080/v1/graphql \ -H "Content-Type: application/json" \ -d '{ "query": "{Get {Article {title content}}}" }'
创建values-cluster.yaml:
# 集群Weaviate配置 replicaCount: 3 # 服务配置 service: type: ClusterIP port: 8080 # 为每个节点创建服务 headless: enabled: true name: "weaviate-cluster" # 持久化存储 persistence: enabled: true size: 100Gi accessMode: ReadWriteOnce storageClass: "fast-ssd" # 使用PVClaimTemplate支持动态卷分配 claimTemplate: spec: storageClassName: "fast-ssd" accessModes: ["ReadWriteOnce"] resources: requests: storage: 100Gi # 资源限制 resources: requests: cpu: "8" memory: "16Gi" limits: cpu: "16" memory: "32Gi" # 集群配置 cluster: enabled: true replicas: 3 discovery: type: kubernetes kubernetes: namespace: weaviate-system podLabels: app: weaviate-cluster podSelector: matchLabels: app: weaviate-cluster coordination: enabled: true interval: "30s" timeout: "10s" # 模块配置 modules: enabled: - text2vec-openai - text2vec-huggingface - qna-openai - genai-openai config: text2vec-openai: model: "text-embedding-ada-002" apiBase: "https://api.openai.com" apiKey: "${OPENAI_API_KEY}" text2vec-huggingface: model: "sentence-transformers/all-MiniLM-L6-v2" apiKey: "${HUGGINGFACE_API_KEY}" # 监控配置 monitoring: enabled: true port: 9000 metricsPath: /metrics serviceMonitor: enabled: true namespace: monitoring interval: 30s # 网络配置 networkPolicy: enabled: true ingress: - from: - namespaceSelector: matchLabels: name: default - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080 egress: - to: [] ports: - protocol: TCP port: 443 - protocol: TCP port: 80 # 安全配置 securityContext: runAsUser: 1000 runAsGroup: 1000 fsGroup: 1000 # Pod安全策略 podSecurityPolicy: enabled: false annotations: seccomp.security.alpha.kubernetes.io/allowedProfileNames: '*'
创建cluster-resources.yaml:
apiVersion: v1 kind: Service metadata: name: weaviate-cluster namespace: weaviate-system labels: app: weaviate-cluster spec: ports: - name: graphql port: 8080 targetPort: 8080 protocol: TCP - name: grpc port: 8081 targetPort: 8081 protocol: TCP clusterIP: None selector: app: weaviate-cluster --- apiVersion: v1 kind: ConfigMap metadata: name: weaviate-config namespace: weaviate-system data: weaviate.yaml: | persistence: enabled: true path: "/var/lib/weaviate" backup: enabled: true schedule: "0 2 * * *" retention: 7 storage: provider: local path: "/var/backups" modules: text2vec-openai: model: "text-embedding-ada-002" apiBase: "https://api.openai.com" apiKey: "${OPENAI_API_KEY}" text2vec-huggingface: model: "sentence-transformers/all-MiniLM-L6-v2" apiKey: "${HUGGINGFACE_API_KEY}" query: limit: 25 cluster: enabled: true host: "weaviate-cluster.weaviate-system.svc.cluster.local" port: 8081 replicationFactor: 1 --- apiVersion: v1 kind: Secret metadata: name: weaviate-secrets namespace: weaviate-system type: Opaque data: OPENAI_API_KEY: $(echo -n "your-openai-api-key" | base64) HUGGINGFACE_API_KEY: $(echo -n "your-huggingface-api-key" | base64)
# 应用集群资源 kubectl apply -f cluster-resources.yaml # 安装集群Weaviate helm install weaviate-cluster weaviate/weaviate \ --namespace weaviate-system \ -f values-cluster.yaml # 查看集群状态 kubectl get pods -n weaviate-system kubectl logs -f weaviate-cluster-0 -n weaviate-system # 等待所有节点就绪 kubectl wait --for=condition=ready pods -l app=weaviate-cluster -n weaviate-system
创建values-production.yaml:
# 生产环境优化配置 replicaCount: 5 # 服务配置 service: type: LoadBalancer port: 8080 annotations: service.beta.kubernetes.io/aws-load-balancer-type: nlb service.beta.kubernetes.io/aws-load-balancer-internal: "true" # 持久化存储 persistence: enabled: true size: 200Gi accessMode: ReadWriteOnce storageClass: "gp3" mountPath: "/var/lib/weaviate" reclaimPolicy: Retain # 资源限制 resources: requests: cpu: "16" memory: "32Gi" limits: cpu: "32" memory: "64Gi" # 节点选择 nodeSelector: node-role.kubernetes.io/worker: "true" hardware: high-performance # 容忍度 tolerations: - key: "node-role.kubernetes.io/master" operator: "Exists" effect: "NoSchedule" - key: "dedicated" operator: "Equal" value: "gpu" effect: "NoSchedule" # 自动扩缩容 horizontalPodAutoscaler: enabled: true minReplicas: 3 maxReplicas: 10 targetCPUUtilizationPercentage: 70 targetMemoryUtilizationPercentage: 80 scaleDown: stabilizationWindowSeconds: 300 policies: - type: Percent value: 10 periodSeconds: 60 # JVM参数 jvmOptions: -Xms16g -Xmx16g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+ParallelRefProcEnabled -XX:+AlwaysPreTouch -XX:+UseLargePages -XX:+UseTransparentHugePages # 数据库配置 database: backup: enabled: true schedule: "0 2 * * *" retention: 14 storage: provider: s3 endpoint: "s3.amazonaws.com" bucket: "weaviate-backup-prod" accessKey: "${AWS_ACCESS_KEY}" secretKey: "${AWS_SECRET_KEY}" # 监控配置 monitoring: enabled: true port: 9000 metricsPath: /metrics serviceMonitor: enabled: true namespace: monitoring interval: 15s scrapeTimeout: 10s additionalLabels: release: prometheus # 日志配置 logging: level: INFO format: json output: /var/log/weaviate/weaviate.log maxFiles: 5 maxSize: 100M
创建.env文件:
# 生产环境配置 WEAVIATE_VERSION=v1.20.0 NAMESPACE=weaviate-system CLUSTER_NAME=weaviate-prod # 资源配置 CPU_REQUESTS=16 CPU_LIMITS=32 MEMORY_REQUESTS=32Gi MEMORY_LIMITS=64Gi STORAGE_SIZE=200Gi # 模块配置 OPENAI_API_KEY=your-openai-api-key HUGGINGFACE_API_KEY=your-huggingface-api-key GOOGLE_API_KEY=your-google-api-key # 存储配置 AWS_ACCESS_KEY=your-aws-access-key AWS_SECRET_KEY=your-aws-secret-key AWS_REGION=us-east-1 AWS_BACKUP_BUCKET=weaviate-backup-prod # 监控配置 METRICS_ENABLED=true METRICS_PORT=9000 LOG_LEVEL=INFO LOG_FORMAT=json
# 安装生产环境Weaviate helm install weaviate-prod weaviate/weaviate \ --namespace ${NAMESPACE} \ -f values-production.yaml \ --set image.tag=${WEAVIATE_VERSION} \ --set resources.requests.cpu=${CPU_REQUESTS} \ --set resources.requests.memory=${MEMORY_REQUESTS} \ --set resources.limits.cpu=${CPU_LIMITS} \ --set resources.limits.memory=${MEMORY_LIMITS} \ --set persistence.size=${STORAGE_SIZE} # 验证安装 helm status weaviate-prod kubectl get pods -n ${NAMESPACE} -l app=weaviate-prod kubectl logs -f weaviate-prod-0 -n ${NAMESPACE}
weaviate-deployment/ ├── values-production.yaml ├── values-cluster.yaml ├── values-single.yaml ├── cluster-resources.yaml ├── .env ├── manifests/ │ ├── namespace.yaml │ ├── service.yaml │ ├── configmap.yaml │ ├── secret.yaml │ └── monitoring.yaml ├── scripts/ │ ├── deploy.sh │ ├── backup.sh │ ├── restore.sh │ ├── health-check.sh │ └── monitor.sh └── monitoring/ ├── prometheus.yml ├── grafana/ │ └── dashboards/ │ ├── weaviate-overview.json │ └── weaviate-performance.json ├── alertmanager.yml └── grafana-datasources.yml
scripts/deploy.sh)#!/bin/bash set -e # 加载环境变量 source .env # 创建命名空间 kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f - # 部署基础资源 kubectl apply -f manifests/ # 安装Weaviate helm install weaviate-prod weaviate/weaviate \ --namespace ${NAMESPACE} \ -f values-production.yaml \ --set image.tag=${WEAVIATE_VERSION} \ --set resources.requests.cpu=${CPU_REQUESTS} \ --set resources.requests.memory=${MEMORY_REQUESTS} \ --set resources.limits.cpu=${CPU_LIMITS} \ --set resources.limits.memory=${MEMORY_LIMITS} \ --set persistence.size=${STORAGE_SIZE} # 等待部署完成 echo "等待Weaviate服务启动..." kubectl wait --for=condition=ready pods -l app=weaviate-prod -n ${NAMESPACE} --timeout=300s # 验证部署 echo "验证部署状态..." kubectl get pods -n ${NAMESPACE} kubectl get svc -n ${NAMESPACE} # 获取服务地址 SERVICE_IP=$(kubectl get svc weaviate-prod -n ${NAMESPACE} -o jsonpath='{.status.loadBalancer.ingress[0].ip}') echo "Weaviate GraphQL API: http://${SERVICE_IP}:8080" echo "部署完成!"
scripts/backup.sh)#!/bin/bash # Weaviate备份脚本 BACKUP_DIR="/var/backups" TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="weaviate-backup-${TIMESTAMP}" echo "开始Weaviate备份: $(date)" # 创建备份目录 mkdir -p "${BACKUP_DIR}" # 执行备份 kubectl exec weaviate-prod-0 -n weaviate-system \ -- weaviate backup \ --location "${BACKUP_FILE}" \ --format json # 压缩备份 kubectl exec weaviate-prod-0 -n weaviate-system \ -- tar -czf "${BACKUP_FILE}.tar.gz" "${BACKUP_FILE}" # 下载备份到本地 kubectl cp weaviate-prod-0:${BACKUP_FILE}.tar.gz "${BACKUP_DIR}/" \ -n weaviate-system # 清理容器内临时文件 kubectl exec weaviate-prod-0 -n weaviate-system \ -- rm -rf "${BACKUP_FILE}" "${BACKUP_FILE}.tar.gz" # 清理旧备份(保留14天) find "${BACKUP_DIR}" -name "weaviate-backup-*.tar.gz" -mtime +14 -delete echo "备份完成: ${BACKUP_DIR}/${BACKUP_FILE}.tar.gz"
scripts/monitor.sh)#!/bin/bash # Weaviate监控脚本 METRICS_URL="http://localhost:9000/metrics" LOG_FILE="/var/log/weaviate-monitor.log" NAMESPACE="weaviate-system" check_health() { local pod_status=$(kubectl get pods -n ${NAMESPACE} -l app=weaviate-prod -o jsonpath='{.items[*].status.phase}') if [[ $pod_status == *"Running"* ]]; then echo "$(date): Pod健康" >> "${LOG_FILE}" return 0 else echo "$(date): Pod异常" >> "${LOG_FILE}" return 1 fi } check_performance() { local qps=$(curl -s "${METRICS_URL}" | grep 'weaviate_qps' | tail -1 | awk '{print $2}') local latency=$(curl -s "${METRICS_URL}" | grep 'weaviate_p95_latency_ms' | tail -1 | awk '{print $2}') local memory_usage=$(curl -s "${METRICS_URL}" | grep 'weaviate_memory_usage_bytes' | tail -1 | awk '{print $2}') echo "$(date): QPS: ${qps}, 延迟: ${latency}ms, 内存: ${memory_usage}B" >> "${LOG_FILE}" # 性能阈值检查 if [ -n "${qps}" ] && [ $(echo "${qps} < 1000" | bc -l) -eq 1 ]; then echo "$(date): 警告: QPS过低 (${qps})" >> "${LOG_FILE}" fi if [ -n "${latency}" ] && [ $(echo "${latency} > 100" | bc -l) -eq 1 ]; then echo "$(date): 警告: 延迟过高 (${latency}ms)" >> "${LOG_FILE}" fi if [ -n "${memory_usage}" ] && [ $(echo "${memory_usage} > 25 * 1024 * 1024 * 1024" | bc -l) -eq 1 ]; then echo "$(date): 警告: 内存使用过高 (${memory_usage}B)" >> "${LOG_FILE}" fi } check_storage() { local storage_usage=$(curl -s "${METRICS_URL}" | grep 'weaviate_storage_usage_bytes' | tail -1 | awk '{print $2}') local storage_limit=$(curl -s "${METRICS_URL}" | grep 'weaviate_storage_limit_bytes' | tail -1 | awk '{print $2}') echo "$(date): 存储使用: ${storage_usage}B / ${storage_limit}B" >> "${LOG_FILE}" if [ -n "${storage_usage}" ] && [ $(echo "${storage_usage} > 0.8 * ${storage_limit}" | bc -l) -eq 1 ]; then echo "$(date): 警告: 存储使用率过高 (${storage_usage}B / ${storage_limit}B)" >> "${LOG_FILE}" fi } # 主循环 while true; do check_health check_performance check_storage sleep 30 done
monitoring/prometheus.yml)global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'weaviate' kubernetes_sd_configs: - role: pod relabel_configs: - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] action: keep regex: true - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port] action: replace target_label: __metrics_path__ regex: (.+) metrics_path: /metrics scheme: http scrape_interval: 15s scrape_timeout: 10s - job_name: 'weaviate-cluster' static_configs: - targets: ['weaviate-cluster.weaviate-system.svc.cluster.local:8080'] scrape_interval: 15s metrics_path: /metrics scheme: http
创建Grafana仪表板配置,包含以下面板:
A:首先检查日志和状态:
# 查看Pod状态 kubectl get pods -n weaviate-system # 查看Pod详细状态 kubectl describe pod weaviate-prod-0 -n weaviate-system # 查看事件 kubectl get events -n weaviate-system --sort-by='.metadata.creationTimestamp' # 查看Weaviate日志 kubectl logs weaviate-prod-0 -n weaviate-system
常见原因:
A:数据一致性配置:
# 在values-cluster.yaml中配置 cluster: enabled: true replicationFactor: 1 # 副本因子 consistencyLevel: "ONE" # 一致性级别 quorum: 1 # 仲裁配置
一致性级别选择:
A:性能优化建议:
jvmOptions: -Xms16g -Xmx16g -XX:+UseG1GC -XX:MaxGCPauseMillis=200
query: concurrentRequestLimit: 100 timeout: 60s
vectorIndex: maxConnections: 64 efSearch: 32 efConstruction: 64
modules: text2vec-huggingface: model: "sentence-transformers/all-MiniLM-L6-v2" batchSize: 32 vectorDimension: 384
A:备份恢复方案:
备份策略:
# 每日备份脚本 kubectl exec weaviate-prod-0 -n weaviate-system \ -- weaviate backup \ --location "/var/backups/$(date +%Y%m%d)" \ --format json \ --compress
恢复步骤:
# 1. 停止服务 helm uninstall weaviate-prod -n weaviate-system # 2. 恢复数据 kubectl apply -f manifests/ # 3. 创建新的Weaviate实例 helm install weaviate-prod weaviate/weaviate \ --namespace weaviate-system \ -f values-production.yaml # 4. 执行恢复 kubectl cp weaviate-backup-20240101.tar.gz weaviate-prod-0:/backup -n weaviate-system kubectl exec weaviate-prod-0 -n weaviate-system \ -- weaviate restore \ --location /backup/weaviate-backup-20240101
A:网络配置和问题排查:
网络策略:
# 网络策略配置 networkPolicy: enabled: true ingress: - from: - namespaceSelector: matchLabels: name: default - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080 egress: - to: [] ports: - protocol: TCP port: 443 # OpenAI API - protocol: TCP port: 80 # HTTP
网络问题排查:
# 检查Pod网络状态 kubectl exec weaviate-prod-0 -n weaviate-system -- curl -I http://localhost:8080 # 检查网络策略 kubectl get networkpolicy -n weaviate-system # 检查服务端点 kubectl get endpoints -n weaviate-system # 检查DNS解析 kubectl exec weaviate-prod-0 -n weaviate-system -- nslookup weaviate-prod.weaviate-system.svc.cluster.local
A:监控和调优方案:
关键监控指标:
调优建议:
本节详细讲解了Weaviate的Kubernetes容器化部署方案,从单机部署到高可用集群配置,覆盖了环境准备、安装配置、性能优化和运维管理等各个环节。通过使用Helm Chart和Kubernetes原生的资源管理功能,可以快速构建可扩展、高可用的向量搜索服务平台。在实际应用中,需要根据业务需求和资源情况选择合适的部署方案和配置参数。
关键词:向量数据库选型宝典, Weaviate, Kubernetes, 容器化部署, 高可用集群, 云原生
难度:进阶
预计阅读:45分钟