添加存储

清理

请参阅《入门指南》文档,了解如何安装和设置

  • microk8s

  • UCS 工具

    • 访问 NGC

    • 设置存储库

简介

在本教程中,我们将展示如何向 UCS 微服务和应用程序添加存储。

这将帮助您熟悉以下基础知识

  • 使用 local-path-provisioner 创建 PersistentVolume (PV) 和 PersistentVolumeClaim (PVC),以及

  • 定义和添加 PV 和 PVC 到微服务。

本地路径 Provisioner

首先,如果尚未完成,请运行以下命令安装本地路径 Provisioner

$ curl https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.23/deploy/local-path-storage.yaml | sed 's/^  name: local-path$/  name: mdx-local-path/g' | microk8s kubectl apply -f -

现在,我们可以在微服务中使用本地存储了。

定义和添加 PV 和 PVC 到微服务

通过运行以下命令创建 PVC

$ curl https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pvc/pvc.yaml | \
      sed 's/storageClassName: local-path$/storageClassName: mdx-local-path/g' | microk8s kubectl apply -f -

让我们更新 HTTP 服务器微服务 http-server 的微服务清单

type: msapplication
specVersion: 2.5.0
name: ucf.svc.http-server
chartName: http-server
description: http server
version: 0.0.2
tags: []
keywords: []
publish: false
ingress-endpoints:
  - name: http
    description: REST API endpoint
    protocol: TCP
    scheme: http
    mandatory: False
    data-flow: in-out
---
spec:
  - name: http-server-deployment
    type: ucf.k8s.app.deployment
    parameters:
      apptype: stateless

  - name: http-server-container
    type: ucf.k8s.container
    parameters:
      image:
        repository: nvcr.io/nvidia/pytorch
        tag: 22.04-py3
      command: [sh, -c]
      args: [
            "cd /localvol && echo $PWD && touch somefile.txt && ls && python -m http.server 8080
            "]
      ports:
        - containerPort: 8080
          name: http
      volumeMounts:
        - name: localvol
          mountPath: /localvol

  - name: svc
    type: ucf.k8s.service
    parameters:
      ports:
      - port: 8080
        protocol: TCP
        name: http

  - name: localvol
    type: ucf.k8s.volume
    parameters:
      persistentVolumeClaim:
        claimName: local-path-pvc

我们的清单文件中有两处新增内容

  • 一个带有参数的 volumeMounts 关键字,用于 UCS 容器组件

  • 另一个类型为 ucf.k8s.volume 的 UCS 组件,它将在逻辑上连接到 PVC

我们不会对 client 进行任何更改。

app.yaml 文件中进行以下更改

specVersion: 2.5.0
version: 0.0.2
doc: README.md
name: server-client-app
description: Server Client Application

dependencies:
- ucf.svc.curl-client:0.0.1
- ucf.svc.http-server:0.0.2

components:
- name: client
  type: ucf.svc.curl-client
- name: http-server
  type: ucf.svc.http-server

connections:
    client/http: http-server/http

http-server 和应用程序的版本已更新。

构建微服务和应用程序并部署它们

请按照下面提到的步骤操作,但请记住更新 app.yaml 中 dependencies 部分下的 http-server 版本

检查和调试微服务和应用程序

让我们验证应用程序是否已成功部署

$ microk8s kubectl get all
NAME                                                      READY   STATUS             RESTARTS      AGE
pod/http-server-http-server-deployment-575bd9c956-4zd2l   1/1     Running            0             3m27s
pod/curl-client-curl-client-deployment-64b485b4f7-zlqn8   1/1     Running            0             3m27s

NAME                                             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/kubernetes                               ClusterIP   10.152.183.1    <none>        443/TCP    82d
service/http-server-http-server-deployment-svc   ClusterIP   10.152.183.70   <none>        8080/TCP   3m27s

NAME                                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/http-server-http-server-deployment   1/1     1            1           3m27s
deployment.apps/curl-client-curl-client-deployment   1/1     1            1           3m27s

NAME                                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/http-server-http-server-deployment-575bd9c956   1         1         1       3m27s
replicaset.apps/curl-client-curl-client-deployment-64b485b4f7   1         1         1       3m27s

我们可以检查 http-server 容器以查看打印当前工作目录 - 在本例中为 /localvol 文件夹。如果我们使用 Triton Inference Server 之类的东西,我们可以在此处添加模型!

$ microk8s kubectl logs --tail -1 -l "app=http-server-http-server-deployment"
/localvol
somefile.txt

我们还可以获取日志以验证文件是否已在挂载的卷中创建

$ microk8s kubectl logs --tail -1 -l "app=curl-client-curl-client-deployment"
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
  100   346  100   346    0     0   112k      0 --:--:-- --:--:-- --:--:--  112k
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Directory listing for /</title>
  </head>
  <body>
  <h1>Directory listing for /</h1>
  <hr>
  <ul>
  <li><a href="somefile.txt">somefile.txt</a></li>
  </ul>
  <hr>
  </body>
  </html>

正如我们所见,somefile.txt 出现在客户端中,这意味着该文件确实已在挂载的文件路径中创建。

停止和清理微服务和应用程序

最后,要停止和清理应用程序,我们可以运行

$ microk8s helm3 uninstall server-client