Stable Diffusion是一款基于深度学习的AI图像生成模型,可以根据文本描述生成逼真的图像。由于模型的庞大体积,在本地运行和部署存在一定困难。幸运的是,AWS提供了一个解决方案,可以在AWS EKS(Elastic Kubernetes Service)上快速部署Stable Diffusion。

![参考](https://aws-samples.github.io/stable-diffusion-on-eks/zh/)

准备工作

  1. 安装AWS CLI并配置您的AWS凭证
  2. 安装Git和Node.js/npm
yuminstall -y git npm
  • 1.

注意:npm 9.8.1和Node.js 18.18.2

部署Stable Diffusion

AWS提供了一个开源项目stable-diffusion-on-eks,可以简化部署流程。我们首先克隆该项目:

git clone --recursive https://github.com/aws-samples/stable-diffusion-on-eks
cd stable-diffusion-on-eks
# git pull ##更新代码
  • 1.
  • 2.
  • 3.

接下来,进入deploy目录并运行deploy.sh脚本。该脚本将自动完成以下工作:

  • 安装必要的运行时和工具
  • 创建S3存储桶,从HuggingFace下载Stable Diffusion 1.5基础模型并存储在桶中
  • 使用提供的示例镜像创建包含Stable Diffusion Web UI的EBS快照
  • 创建并部署包含Stable Diffusion运行时的解决方案

您可以使用多个参数来自定义部署:

./deploy.sh-n dev-sdoneks -R us-east-1 -b dev-bucket-sdoneks -r sdruntime-01
  • 1.
  • -n: 自定义解决方案名称
  • -R: 指定部署区域
  • -b: 指定现有S3桶名称,用于存储模型(提前创建)
  • -r: 指定运行时名称

其他可选参数包括:

  • -d: 只生成配置文件,不执行部署
  • -s: 指定现有EBS快照ID
  • -t: 指定运行时类型(sdwebui或comfyui)

如果需要部署多个运行时或自定义配置,可以先使用--dry-run生成配置文件,再手动修改。

部署完成后,您可以使用以下命令查看部署状态:

kubectl getnode
kubectl get deploy
kubectl get pod
  • 1.
  • 2.
  • 3.

添加新模型

如果需要添加新的Stable Diffusion模型,可以按照以下步骤操作:

使用--dry-run生成配置文件:

./deploy.sh-n dev-govee-sdoneks -R us-east-1 -b dev-goveebucket-sdoneks -r sdruntime-01 --dry-run #首次
  • 1.

下载新增一个运行时为sdruntime-02的部分:

stackName: dev-sdoneks
modelBucketArn: arn:aws:s3:::dev-bucket-sdoneks
APIGW:
  stageName: dev
  throttle:
    rateLimit: 30
    burstLimit: 50
modelsRuntime:
- name: sdruntime-01
  namespace: default
  modelFilename: v1-5-pruned-emaonly.safetensors
  dynamicModel: false
  type: sdwebui
  extraValues:
    runtime:
      inferenceApi:
        image:
          repository: <account_id>.dkr.ecr.<region>.amazonaws.com/sd-on-eks/sdwebui # Stable Diffusion 运行时镜像的地址.
          tag: latest # 镜像的Tag
      queueAgent:
        image:
          repository: <account_id>.dkr.ecr.<region>.amazonaws.com/sd-on-eks/queue-agent # Queue agent镜像的地址.
          tag: latest # 镜像的Tag
    karpenter:
      nodeTemplate:
        amiFamily: Bottlerocket
        dataVolume:
          volumeSize: 80Gi
          volumeType: gp3
          deleteOnTermination: true
          iops: 4000
          throughput: 1000
          snapshotID: snap-0fc2970a9218047e5
      provisioner:
        instanceType:
        - g4dn.xlarge
        - g4dn.2xlarge
        capacityType:
          onDemand: true
          spot: true
      scaling:
        queueLength: 5 # Target value for queue length. One new instance will be launched when queue launch reaches the value.
        minReplicaCount: 1 # Minimum number of replicas of SD Runtime.
        maxReplicaCount: 20 # Maxinum number of replicas of SD Runtime.
        cooldownPeriod: 60 # The period (in seconds) to wait after the last message before scaling down.
- name: sdruntime-02
  namespace: default
  modelFilename: v1-5-pruned-emaonly-gif.safetensors
  dynamicModel: false
  type: sdwebui
  extraValues:
    runtime:
      inferenceApi:
        image:
          repository: <account_id>.dkr.ecr.<region>.amazonaws.com/sd-on-eks/sdwebui # Stable Diffusion 运行时镜像的地址.
          tag: latest # 镜像的Tag
      queueAgent:
        image:
          repository: <account_id>.dkr.ecr.<region>.amazonaws.com/sd-on-eks/queue-agent # Queue agent镜像的地址.
          tag: latest # 镜像的Tag
    karpenter:
      nodeTemplate:
        amiFamily: Bottlerocket
        dataVolume:
          volumeSize: 80Gi
          volumeType: gp3
          deleteOnTermination: true
          iops: 4000
          throughput: 1000
          snapshotID: snap-0fc2970a9218047e5
      provisioner:
        instanceType:
        - g5.xlarge
        - g5.2xlarge
        capacityType:
          onDemand: true
          spot: true
      scaling:
        queueLength: 2 # Target value for queue length. One new instance will be launched when queue launch reaches the value.
        minReplicaCount: 1 # Minimum number of replicas of SD Runtime.
        maxReplicaCount: 10 # Maxinum number of replicas of SD Runtime.
        cooldownPeriod: 60 # The period (in seconds) to wait after the last message before scaling down.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.

退出deploy目录,使用CDK部署更新:

cd ..
#export AWS_DEFAULT_REGION=<想要的region> ##跨区更新要先设置
cdk deploy --no-rollback --require-approval never
或
cdk deploy --require-approval never
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

验证

STACK_NAME=dev-govee-sdoneksStack RUNTIME_TYPE=sdwebui ./run.sh
  • 1.

维护命令

以下是一些常用的维护命令:

排查链路:keda scaledobject -> hpa -> deployment -> replicaset -> pod

测试:单台机器的QPS及扩容时的错误数量和产生错误数的规律

aws eks update-kubeconfig--name dev-govee-sdoneksStack --region us-east-1 # 更新kubeconfig
kubectl get node # 查看节点信息
kubectl get deploy # 查看部署信息
kubectl get karpenter # 查看Karpenter信息(Karpenter是AWS提供的自动节点扩展工具)
kubectl get AWSNodeTemplate # 查看AWS节点模板信息
kubectl get ScaledObject # 查看Scaled Object信息(用于自动扩展资源)
kubectl get pod # 查看Pod信息
kubectl get cm # 查看配置映射信息
kubectl logs <pod name> -c queue-agent # 查看特定 Pod 中名为 queue-agent 的容器的日志
kubectl scale deployment <deployment name> --replicas=0 # 将名为 deployment name 的部署的副本数缩减为 0
kubectl delete pod --all -n keda #重启keda
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

通过在AWS EKS上部署Stable Diffusion,您可以轻松获得强大的AI图像生成能力,而无需在本地运行庞大的模型。AWS的解决方案使部署过程变得简单高效。