通过K8S快速部署stable-diffusion-webui,体验AI画图的乐趣

2,407 阅读4分钟

通过K8S快速部署stable-diffusion-webui

Stable Diffusion WebUI是一款开源免费的AI绘图软件,基于CompVis开发的Stable Diffusion模型,支持文字生成图片(text to image,基于文本描述生成图片)、图片生成图片(image to image,基于用于输入的图片和描述生成图片)等功能。还能集成各种丰富的插件,大大降低了用户的使用门槛。目前网上大多数方案都是基于windows或者linux环境部署,本文介绍通过k8s集群快速部署的方案。

基础环境

  • K8S 1.23.4

  • containerd v1.6.8

  • Centos 7.9

  • CNI cilium v1.12.0

  • 显卡 GeForce RTX 3070

  • nvidia-container-toolkit

  • ingress-nginx

关于K8s集群的部署以及nvidia驱动及插件的安装可以参考一下链接,不再过多叙述

Old Dockerfile(废弃)

由于镜像比较大,没有上传公有仓库。请根据dockerfile自行构建

FROM nvidia/cuda:11.7.1-cudnn8-runtime-ubuntu20.04

ENV DEBIAN_FRONTEND noninteractive 
RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
    sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
        apt-get update && apt-get install -y --no-install-recommends \
        libgl1 libglib2.0-0 \
        python3 python3-venv \
        git \
        wget \
        vim \
        inetutils-ping \
        sudo \
        net-tools \
        iproute2 \
        && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*

COPY webui.sh webui.sh
ENV INDEX_URL https://pypi.tuna.tsinghua.edu.cn/simple
RUN mkdir ~/.pip && echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n[install]\ntrusted-host = https://pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf
ENV install_dir=/
RUN ./webui.sh -f can_run_as_root --exit --skip-torch-cuda-test --reinstall-xformers

ENV VIRTUAL_ENV=/stable-diffusion-webui/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

WORKDIR "/stable-diffusion-webui/"
VOLUME /stable-diffusion-webui/models
VOLUME /root/.cache

CMD ["python3", "launch.py", "--listen", "--enable-insecure-extension-access"]

New Dockerfile

FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04

ENV DEBIAN_FRONTEND noninteractive

RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && apt-get update

RUN set -ex && \

apt install -y wget git python3 python3-venv python3-pip libglib2.0-0 ffmpeg libsm6 libxext6 && \

rm -rf /var/lib/apt/lists/*

ENV INDEX_URL https://pypi.tuna.tsinghua.edu.cn/simple

RUN mkdir ~/.pip && echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n[install]\ntrusted-host = https://pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf

RUN python3 -m pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117

RUN python3 -m pip install git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 --prefer-binary

RUN python3 -m pip install git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 --prefer-binary

RUN python3 -m pip install git+https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b --prefer-binary

RUN python3 -m pip install xformers==0.0.16rc425 --prefer-binary

RUN python3 -m pip install pyngrok --prefer-binary

RUN git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

RUN git clone https://github.com/Stability-AI/stablediffusion.git /stable-diffusion-webui/repositories/stable-diffusion-stability-ai

RUN git -C /stable-diffusion-webui/repositories/stable-diffusion-stability-ai checkout cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf

RUN git clone https://github.com/CompVis/taming-transformers.git /stable-diffusion-webui/repositories/taming-transformers

RUN git -C /stable-diffusion-webui/repositories/taming-transformers checkout 24268930bf1dce879235a7fddd0b2355b84d7ea6

RUN git clone https://github.com/crowsonkb/k-diffusion.git /stable-diffusion-webui/repositories/k-diffusion

RUN git -C /stable-diffusion-webui/repositories/k-diffusion checkout 5b3af030dd83e0297272d861c19477735d0317ec

RUN git clone https://github.com/sczhou/CodeFormer.git /stable-diffusion-webui/repositories/CodeFormer

RUN git -C /stable-diffusion-webui/repositories/CodeFormer checkout c5b4593074ba6214284d6acd5f1719b6c5d739af

RUN git clone https://github.com/salesforce/BLIP.git /stable-diffusion-webui/repositories/BLIP

RUN git -C /stable-diffusion-webui/repositories/BLIP checkout 48211a1594f1321b00f14c9f7a5b4813144b2fb9

RUN python3 -m pip install -r /stable-diffusion-webui/repositories/CodeFormer/requirements.txt --prefer-binary

RUN python3 -m pip install -r /stable-diffusion-webui/requirements_versions.txt --prefer-binary

RUN set -ex && cd stable-diffusion-webui \

&& git clone https://gitcode.net/ranting8323/sd-webui-additional-networks.git extensions/sd-webui-additional-networks \

&& git clone https://gitcode.net/ranting8323/sd-webui-cutoff extensions/sd-webui-cutoff \

&& git clone https://ghproxy.com/https://github.com/toshiaki1729/stable-diffusion-webui-dataset-tag-editor.git extensions/stable-diffusion-webui-dataset-tag-editor \

&& git clone https://ghproxy.com/https://github.com/yfszzx/stable-diffusion-webui-images-browser extensions/stable-diffusion-webui-images-browser \

&& git clone https://gitcode.net/ranting8323/stable-diffusion-webui-wd14-tagger.git extensions/stable-diffusion-webui-wd14-tagger \

&& git clone https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans.git extensions/stable-diffusion-webui-localization-zh_Hans \

&& git clone https://gitcode.net/ranting8323/a1111-sd-webui-tagcomplete.git extensions/a1111-sd-webui-tagcomplete \

&& git clone https://github.com/Mikubill/sd-webui-controlnet.git extensions/sd-webui-controlnet

RUN python3 -m pip install -r /stable-diffusion-webui/extensions/sd-webui-controlnet/requirements.txt --prefer-binary

EXPOSE 7860

WORKDIR /stable-diffusion-webui/

CMD ["python3", "launch.py", "--listen", "--xformers", "--medvram", "--enable-insecure-extension-access"]

部署

创建namespace

kubectl create ns sd

部署webui

由于模型文件和扩展组件目录需要持久化并且文件比较大,不适合打包在容器中。所以通过挂载的形式。outputs用于保存生成文件,也挂载到外部。本文使用hostpath,用于可根据自身情况自行修改。

对应主机目录分别为:

  • /stable-diffusion-webui/models

  • /stable-diffusion-webui/extensions

  • /stable-diffusion-webui/outputs

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sd-webui
  namespace: sd
spec:
  selector:
    matchLabels:
      app: sd
  replicas: 1
  template:
    metadata:
      labels:
        app: sd
    spec:
      runtimeClassName: nvidia
      containers:
        - name: sd
          image: stable-diffusion:1.5 ## 自行构建
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              nvidia.com/gpu: 1
          ports:
            - name: http
              containerPort: 7860
          volumeMounts:
          - name: models
            mountPath: /stable-diffusion-webui/models
          - name: extensions
            mountPath: /stable-diffusion-webui/extensions
          - name: outputs
            mountPath: /stable-diffusion-webui/outputs
      volumes:
      - name: models
        hostPath:
          path: /stable-diffusion-webui/models
      - name: extensions
        hostPath:
          path: /stable-diffusion-webui/extensions
      - name: outputs
        hostPath:
          path: /stable-diffusion-webui/outputs

创建service

apiVersion: v1
kind: Service
metadata:
  name: sd-webui
  namespace: sd
spec:
  selector:
    app: sd
  ports:
  - protocol: TCP
    port: 7860
    targetPort: 7860
  type: NodePort

创建ingress(也可以直接通过nodeport访问,可以忽略这一步)

本文ingress controller使用的是ingress-nginx,用户可根据情况自行更换。域名也根据需求替换。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sd-ingress
  namespace: sd
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - sd.example.com
  rules:
  - host: sd.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: sd-webui
            port:
              number: 7860

访问验证

主页

webui.png

插件管理页面

1.png

参考

常用的模型下载网站

hg.png

civi.png

另外提示,Civitai上的图片点击查看详情一般都带有用户使用的模型、prompt、参数等,可以查看自己喜欢的风格下载对应的模型并参考参数生成图片。

ck.png

参考链接