Skip to main content

Stable Diffusion

Stable Diffusion is a popular machine learning model used for generating high-quality images from textual descriptions. Running this type of workload on a K8s serverless platform allows you to leverage the scalability of serverless for computationally intensive tasks such as AI model inference.

Step 1: Deploying the Container

To deploy Stable Diffusion on K8s, you need to create a deployment manifest. This manifest will specify the use of your Docker container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: stable-diffusion
  labels:
    app: stable-diffusion
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stable-diffusion
  template:
    metadata:
      labels:
        app: stable-diffusion
    spec:
      containers:
      - name: stable-diffusion
        image: docker.io/universonic/stable-diffusion-webui:nightly-20240515030400-full
        resources:
          limits:
            nvidia.com/gpu: 1 # Requesting 1 NVIDIA GPU
      nodeSelector:
        gpu.nvidia.com/class: L40S # New selector for specific GPU class

This deployment specifies 1 replicas of the Stable Diffusion container. The nodeSelector ensures that the pods are deployed on GPU-enabled nodes.

Configuring with Load Balancing Service

A LoadBalancer service in automatically creates an external load balancer and assigns it a fixed, external IP address. Traffic from the external load balancer is directed to backend pods. Below is an example of how to define a LoadBalancer service in a Kubernetes YAML manifest for Stable Diffusion:

apiVersion: v1
kind: Service
metadata:
  name: stable-diffusion-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: stable-diffusion

Running Stable Diffusion in a serverless kubernetes environment showcases the Ori's ability to handle dynamic, resource-intensive tasks efficiently. By leveraging serverless, you can ensure that the infrastructure scales with the demand, optimizing both cost and performance. This setup is particularly advantageous for AI-driven applications, where computational needs can vary significantly.

Step 2: Check Status

1. View your Running Pods

kubectl get pods

Outputs:

NAME                                READY   STATUS    RESTARTS   AGE
stable-diffusion-6d8946dd55-ct42r   1/1     Running   0          4h

2. View your Pods Details

kubectl get pods -o wide

Outputs:

NAME                                READY   STATUS    RESTARTS   AGE    IP          NODE                                NOMINATED NODE   READINESS GATES
stable-diffusion-6d8946dd55-ct42r   1/1     Running   0          4h   10.2.4.51   dev-vcluster-gpu-l40s-node-267321   <none>           <none>

3. Access Stable Diffusion application

In order to access you SD application you can do so in two ways.

1. Local access

kubectl port-forward

You can now access the application at http://localhost:8888.

2. Public access

If you have setup an external Load Balancer, you can access the application from the public internet.

In order to get the public IP address, you can run the following command:

kubetl get services

You can now access the application at http://IP-ADDRESS:8888