ITPub博客

首页 > 云计算 > 容器服务 > 基于容器平台 ACK 快速搭建 Stable Diffusion

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

原创 容器服务 作者:阿里巴巴云原生 时间:2023-04-23 17:11:31 0 删除 编辑

*作者:子白*


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


## CPU 版本


### 前提条件


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


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


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


### 使用控制台创建


1.  登录容器服务管理控制台[3],在左侧导航栏选择集群。

1.  在集群列表页面中,单击目标集群名称或者目标集群右侧操作列下的详情。

1.  在集群管理页左侧导航栏中,选择**工作负载 > 无状态**。

1.  在**无状态**页面中,单击**使用镜像创建**。

1.  在**应用基本信息**配置向导页面中,设置应用的基本信息。


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c4acfe6ef70c43c9bed3ee64e104e756~tplv-k3u1fbpfcp-zoom-1.image "image.png")


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/20690d095ac84bbc862b34a9f347300f~tplv-k3u1fbpfcp-zoom-1.image "image.png")


```

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

```


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9158fd5878cf4a088da9684ccd8189ca~tplv-k3u1fbpfcp-zoom-1.image "image.png")


```

["python3", "launch.py"]

["--listen", "--skip-torch-cuda-test", "--no-half"]

```


等待 pod ready


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


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e07f0a588b6440799ba87c752f3616d1~tplv-k3u1fbpfcp-zoom-1.image "image.png")


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


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


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6d7ef1bc39ab45649f5aeba5c342b8ac~tplv-k3u1fbpfcp-zoom-1.image "image.png")


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/167f455a6b7e43eea5c109235861ab83~tplv-k3u1fbpfcp-zoom-1.image "image.png")


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2094c2565b3d46268ba145262f6b170a~tplv-k3u1fbpfcp-zoom-1.image "image.png")  



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


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a5ec4846f6914e6ab50a6ce151165d58~tplv-k3u1fbpfcp-zoom-1.image "image.png")


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


Prompt:Black and white photo of a beautiful city


Sampling method:DPM++ SDE


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/538ba7a8a50c49f6a991561c1de9d5f2~tplv-k3u1fbpfcp-zoom-1.image "image.png")


### 使用 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


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c61c3d92fc2e4f51ad084d9c3017e744~tplv-k3u1fbpfcp-zoom-1.image "image.png")


## 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


![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d66a752de0e648e6b6ab19ce71dd5656~tplv-k3u1fbpfcp-zoom-1.image "image.png")


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


> *注:镜像可拉取时间截止至 2023 年 5 月 17 日*

>

> *镜像仓库地址: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](https://account.aliyun.com/login/login.htm?spm=a2c6h.13046898.0.0.13796ffaWAabwk&oauth_callback=https%253A%252F%252Fcs.console.aliyun.com%252F)*


[4] 创建托管 GPU 集群  


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


点击[此处](https://www.aliyun.com/product/kubernetes)即可查看容器服务 ACK 产品详情


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69953029/viewspace-2948147/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
阿里云云原生公众号(ID:Alicloudnative)发布云原生技术最新资讯、汇集云原生技术最全内容,定期举办云原生活动、直播,阿里产品及用户最佳实践发布。与你并肩探索云原生技术点滴,分享你需要的云原生内容。

注册时间:2019-11-07

  • 博文量
    1497
  • 访问量
    710909