基于容器平台 ACK 快速搭建 Stable Diffusion

本文介绍如何在阿里云容器平台 ACK 上快速搭建一套可对外提供服务的 Stable Diffusion。

CPU 版本

前提条件

  • 已创建 Kubernetes 托管版集群。具体操作,请参见创建 Kubernetes 托管版集群[1]。

无需 GPU,节点需要 8c16g 以上

  • 已通过 kubectl 连接 kubernetes 集群。具体操作,请参见通过 Kubectl 连接 Kubernetes 集群[2]

使用控制台创建

  1. 登录容器服务管理控制台[3],在左侧导航栏选择集群。
  2. 在集群列表页面中,单击目标集群名称或者目标集群右侧操作列下的详情。
  3. 在集群管理页左侧导航栏中,选择工作负载 > 无状态
  4. 无状态页面中,单击使用镜像创建
  5. 应用基本信息配置向导页面中,设置应用的基本信息。

 

zibai-registry.cn-hangzhou.cr.aliyuncs.com/gpt/stable-diffusion:v1.cpu

["python3", "launch.py"]
["--listen", "--skip-torch-cuda-test", "--no-half"]

等待 pod ready

镜像大小为 12.7GB,内网下载约 10min

  1. 在集群管理页左侧导航栏中,选择网络 > 服务

新建服务,选择负载均衡类型。

等待约 1min 后,刷新页面可以看到 External IP 列有具体 IP

在浏览器中访问上一步获取到的 http://xxx.xxx.xxx.xxx:7860,即可看到如下页面。

Prompt:Black and white photo of a beautiful city

Sampling method:DPM++ SDE

使用 kubectl 创建

stable-diffusion.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: stable-diffusion
  name: stable-diffusion
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stable-diffusion
  template:
    metadata:
      labels:
        app: stable-diffusion
    spec:
      containers:
      - args:
        - --listen
        - --skip-torch-cuda-test
        - --no-half
        command:
        - python3
        - launch.py
        image: zibai-registry.cn-hangzhou.cr.aliyuncs.com/gpt/stable-diffusion:v1.cpu
        imagePullPolicy: IfNotPresent
        name: stable-diffusion
        resources:
          requests:
            cpu: "2"
            memory: 2Gi
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: internet
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-instance-charge-type: PayByCLCU
  name: stable-diffusion
  namespace: default
spec:
  externalTrafficPolicy: Local
  ports:
  - port: 7860
    protocol: TCP
    targetPort: 7860
  selector:
    app: stable-diffusion
  type: LoadBalancer
kubectl apply -f stable-diffusion.yaml

等待 pod ready

镜像大小为 12.7GB,内网下载约 10min

# 查看pod状态,等待pod running
kubectl get po |grep stable-diffusion

# 查看CLB IP
kubectl get svc stable-diffusion

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
stable-diffusion   LoadBalancer   192.168.x.x     xx.xx.xx.xxx   7860:32320/TCP   12m

在浏览器中访问上一步获取到的 http://xxx.xxx.xxx.xxx:7860,即可看到如下页面。

Prompt:Black and white photo of a beautiful city

Sampling method:DPM++ SDE

GPU 版本

前提条件

  • 已创建 Kubernetes 异构集群集群。具体操作,请参见创建托管 GPU 集群[4]。

需要 GPU 节点,磁盘剩余容量需大于 40G

  • 已通过 kubectl 连接kubernetes集群。具体操作,请参见通过 Kubectl 连接 Kubernetes 集群。

使用 kubectl 创建

stable-diffusion.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: stable-diffusion
  name: stable-diffusion
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stable-diffusion
  template:
    metadata:
      labels:
        app: stable-diffusion
    spec:
      containers:
      - args:
        - --listen
        command:
        - python3
        - launch.py
        image: zibai-registry.cn-hangzhou.cr.aliyuncs.com/gpt/stable-diffusion:v1.gpu
        imagePullPolicy: IfNotPresent
        name: stable-diffusion
        resources:
          requests:
            cpu: "2"
            memory: 2Gi
          limits:
            nvidia.com/gpu: 1
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: internet
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-instance-charge-type: PayByCLCU
  name: stable-diffusion
  namespace: default
spec:
  externalTrafficPolicy: Local
  ports:
  - port: 7860
    protocol: TCP
    targetPort: 7860
  selector:
    app: stable-diffusion
  type: LoadBalancer
kubectl apply -f stable-diffusion.yaml

等待 pod ready

镜像大小为 15.1GB,内网下载约 15min

# 查看pod状态,等待pod running
kubectl get po |grep stable-diffusion

# 查看CLB IP
kubectl get svc stable-diffusion

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
stable-diffusion   LoadBalancer   192.168.x.x     xx.xx.xx.xxx   7860:32320/TCP   12m

在浏览器中访问上一步获取到的 http://xxx.xxx.xxx.xxx:7860,即可看到如下页面。

Prompt:Black and white photo of a beautiful city

Sampling method:DPM++ SDE

GPU 版本的图片生成速度明显优于 CPU 版本。

注:镜像可拉取时间截止至 2023 年 5 月 17 日
镜像仓库地址:http://zibai-registry.cn-hangzhou.cr.aliyuncs.com/gpt/stable-diffusion

相关链接:

https://github.com/AUTOMATIC1111/stable-diffusion-webui

[1] 创建 Kubernetes 托管版集群

https://help.aliyun.com/document_detail/95108.htm#task-skz-qwk-qfb

[2] 通过 Kubectl 连接 Kubernetes 集群

https://help.aliyun.com/document_detail/86494.htm#task-ubf-lhg-vdb

[3] 容器服务管理控制台

https://account.aliyun.com/login/login.htm?oauth_callback=https%3A%2F%2Fcs.console.aliyun.com%2F

[4] 创建托管 GPU 集群

https://help.aliyun.com/document_detail/171074.html?spm=a2c4g.171073.0.0.7989f95acmbnoT

作者:子白

原文链接

本文为阿里云原创内容,未经允许不得转载。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章