Stable Diffusion generates images from text prompts using AI. This tutorial shows you how to deploy the Stable Diffusion WebUI in a Container Service for Kubernetes (ACK) Serverless cluster and access it through an external endpoint.
You can deploy the application using either of these methods:
ACK console -- Deploy through the graphical interface
kubectl -- Deploy using a YAML manifest from the command line
How Stable Diffusion works
Stable Diffusion WebUI is an open-source project on GitHub that generates images from text prompts. It has two core components:
Text comprehension -- Encodes prompts using the Contrastive Language-Image Pre-training (CLIP) model
Image generation -- Creates images using a diffusion model
Alibaba Cloud does not guarantee the legitimacy, security, or accuracy of the third-party model Stable Diffusion. Alibaba Cloud is not liable for any damages caused by the use of Stable Diffusion.
Abide by the user agreements, usage specifications, and relevant laws and regulations of Stable Diffusion. You are responsible for all consequences that result from your use of Stable Diffusion.
Prerequisites
Before you begin, make sure you have:
An ACK Serverless cluster in one of the following regions: China (Beijing), China (Hangzhou), China (Shanghai), or China (Shenzhen). See Create an ACK Serverless cluster
(For kubectl deployment) kubectl connected to the cluster. See Connect to a Kubernetes cluster using kubectl
Step 1: Deploy the Stable Diffusion application
Choose one of the following deployment methods.
Option A: Deploy from the ACK console
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, find your cluster and click its name. In the left navigation pane, choose Workloads > Deployments.
On the Deployments page, click Create from Image.
On the Basic Information step, set Name to
stable-diffusionand Replicas to1. Click Next.On the Container step, configure the following parameters. Keep the default values for all other parameters. Click Next.
For the internal network image, replace
<Region ID>with your cluster's region ID. Supported values:cn-beijing,cn-shanghai,cn-hangzhou,cn-shenzhen. The internal network endpoint pulls the image faster and avoids internet bandwidth charges.Section Parameter Value General Image Name yunqi-registry.cn-shanghai.cr.aliyuncs.com/lab/stable-diffusion:v1.2.0(internet) orregistry-vpc.<Region ID>.aliyuncs.com/yunqi-lab/stable-diffusion:v1.2.0(internal network)Required Resources CPU 8 vCPU Required Resources Memory 16 GB Health Check Readiness Select Readiness, select TCP, and set the port to 7860Lifecycle Start Command: ["python3", "launch.py"]Arguments:["--listen", "--skip-torch-cuda-test", "--no-half"]On the Advanced page, click Create next to Services to expose the application. Configure the Service with the following settings:
Parameter Value Name stable-diffusion-svcService Type SLB -- Classic Load Balancer (CLB), select Create Resource Port Mapping Name: example-port, Service Port:7860, Container Port:7860, Protocol: TCPReturn to the Advanced page and click Create.
Go back to the Deployments page and click the application name. On the Pods tab, wait until the pod status changes to Running. Then click the Access Method tab and copy the IP address from the External Endpoint field.
The application typically takes several minutes to start. The Stable Diffusion model must load into memory before the readiness probe on port 7860 passes and the pod status changes to Running.
Option B: Deploy with kubectl
Connect to the ACK Serverless cluster using kubectl.
Create a file named
stable-diffusion.yamlwith the following content: The YAML uses a public image endpoint by default. To pull the image over an internal network, replace theimagevalue with the endpoint for your cluster region:The Deployment requests 8 vCPU, 16 GB memory, and 50 GiB ephemeral storage. These resources are needed to load and run the Stable Diffusion model for inference. Insufficient resources may cause out-of-memory errors or startup failures.
Region Internal image endpoint China (Beijing) registry-vpc.cn-beijing.aliyuncs.com/yunqi-lab/stable-diffusion:v1.2.0China (Shanghai) registry-vpc.cn-shanghai.aliyuncs.com/yunqi-lab/stable-diffusion:v1.2.0China (Hangzhou) registry-vpc.cn-hangzhou.aliyuncs.com/yunqi-lab/stable-diffusion:v1.2.0China (Shenzhen) registry-vpc.cn-shenzhen.aliyuncs.com/yunqi-lab/stable-diffusion:v1.2.0apiVersion: 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: yunqi-registry.cn-shanghai.cr.aliyuncs.com/lab/stable-diffusion:v1.2.0 imagePullPolicy: IfNotPresent name: stable-diffusion ports: - containerPort: 7860 protocol: TCP readinessProbe: tcpSocket: port: 7860 resources: limits: cpu: "8" memory: 16Gi ephemeral-storage: 50Gi --- 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-svc namespace: default spec: externalTrafficPolicy: Local ports: - port: 7860 protocol: TCP targetPort: 7860 selector: app: stable-diffusion type: LoadBalancerDeploy the application: Expected output:
kubectl apply -f stable-diffusion.yamldeployment.apps/stable-diffusion created service/stable-diffusion-svc createdVerify that the Deployment is ready: Expected output:
The pod may take several minutes to reach the
1/1READY state. The application needs time to load the model into memory.kubectl get deployment stable-diffusionNAME READY UP-TO-DATE AVAILABLE AGE stable-diffusion 1/1 1 1 103mRetrieve the external IP address of the Service: Expected output: Copy the
EXTERNAL-IPvalue. Use this IP address to access the application in the next step.kubectl get service stable-diffusion-svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE stable-diffusion-svc LoadBalancer 172.x.x.x 47.x.x.x 7860:xxxxx/TCP 103m
Step 2: Access the application
Open a browser and go to
http://<EXTERNAL-IP>:7860, replacing<EXTERNAL-IP>with the IP address from Step 1.Enter a prompt in the text field. For example, enter "Black and white photo of a beautiful city" and click Generate to create an image.
Step 3: Clean up resources
After you complete the tutorial, delete the resources to avoid unnecessary charges.
Delete the application and Service
From the ACK console
On the Clusters page of the ACK console, click the name of your cluster.
In the left navigation pane, choose Workloads > Deployments. Select the Stable Diffusion application, click Batch Delete, and confirm the deletion.
Using kubectl
Run the following command to delete the Deployment and Service:
kubectl delete -f stable-diffusion.yamlExpected output:
deployment.apps "stable-diffusion" deleted
service "stable-diffusion-svc" deletedDelete the cluster
ACK Serverless clusters are in public preview and offer a free trial. However, other Alibaba Cloud services used by the cluster are billed separately based on their respective billing rules.
To delete the cluster: Log on to the ACK console. On the Clusters page, choose More > Delete in the Actions column. In the Delete Cluster dialog box, select Delete ALB Instances Created by the Cluster, Delete Alibaba Cloud DNS PrivateZone instances Created by the Cluster, and I understand the above information and want to delete the specified cluster. Click OK. For details, see Delete an ACK Serverless cluster.
To keep the cluster: Recharge your Alibaba Cloud account at least 1 hour before the trial period ends. Maintain an account balance of at least CNY 100. For billing details, see Cloud service fee.
Troubleshooting
Pod stuck in Pending state
The pod may remain in Pending if the cluster cannot allocate the requested resources (8 vCPU, 16 GB memory).
Solution: Verify that your ACK Serverless cluster is in a supported region (China Beijing, Hangzhou, Shanghai, or Shenzhen) and that your account has sufficient quota.
Image pull errors
If the pod fails with an ImagePullBackOff or ErrImagePull status, the container image cannot be downloaded.
Solution: If you use the internal network endpoint, confirm the region ID in the image URL matches your cluster region. If you use the public endpoint, verify that the cluster has internet access.
Pod is Running but the application is not accessible
The pod status shows Running but you cannot reach the WebUI in the browser.
Solution:
Run
kubectl get service stable-diffusion-svcand confirm theEXTERNAL-IPfield has an IP address (not<pending>).Verify the browser URL uses port 7860:
http://<EXTERNAL-IP>:7860.Verify that no network access control lists (ACLs) or security group rules block inbound TCP traffic on port 7860.
Out-of-memory errors during startup
The pod restarts with OOMKilled status.
Solution: Verify the resource limits in the YAML manifest match the required values: cpu: "8", memory: 16Gi, ephemeral-storage: 50Gi. Reducing the values below these limits may cause the application to fail.
Contact us
For questions about Artificial Intelligence Generated Content (AIGC) services on ACK, join DingTalk group 31850017754.