将文件传递给微服务

日常维护

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

  • microk8s

  • UCS Tools

    • 访问 NGC

    • 设置仓库

简介

在前两个教程 应用程序的部分重新部署在配置更改时重启 Pod 中,我们演示了如何添加配置文件并通过微服务参数公开字段。这样做的一个问题是,如果配置文件包含大量参数,则通过微服务参数公开它们可能会很麻烦。

相反,UCS Tools 允许

  • 微服务指定应用程序应提供完整的文件/目录以及在微服务容器中挂载它们的位置

  • 应用程序根据这些要求指定来自本地文件系统的文件/目录,并自动将文件挂载到微服务容器中的预期位置

在本教程中,我们将演示如何通过 manifest 文件、应用程序 YAML 和 UCS Studio 实现上述目标。

本教程是教程 在配置更改时重启 Pod 的延续。

更新微服务和应用程序

更新 HTTP 服务器微服务 http-server 的 manifest 文件,如下所示

type: msapplication
specVersion: 2.5.0
name: ucf.svc.http-server
chartName: http-server
description: http server
version: 0.0.6
tags: []
keywords: []
publish: false
ingress-endpoints:
  - name: http
    description: REST API endpoint
    protocol: TCP
    scheme: http
    mandatory: False
    data-flow: in-out

params:
  workingDir: "/localvol"
  #> type: string
  #> description: Working directory for listing contents
  #> flags: mandatory
  fileToCreate: somefile.txt
  #> type: string
  #> description: Name of the file to create in the working directory
  #> flags: mandatory

# Files and directories will be mounted with prefix /opt/ext-files/
externalFiles:
- name: app-config.json # File will be available in containers at /opt/ext-files/app-config.json
  description: App Configuration file
  mandatory: True
  isDirectory: False

---
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: [
            "WORKING_DIR=$(cat /opt/ext-files/app-config.json | jq -r '.workingDir') && cd $WORKING_DIR && echo $PWD && touch $(FILE_TO_CREATE) && ls && python -m http.server 8080
            "]
      env:
      - name: POD_NAME
        valueFrom:
          fieldRef:
            fieldPath: metadata.name
      - name: FILE_TO_CREATE
        value: $params.fileToCreate
      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

  - name: cm-dependencies
    type: ucf.appspec.restartPodOnConfigChanges
    parameters:
      # Add dependency on all configmaps detected in the microservice
      addAll: true

      # Add dependency on default configmaps for scripts, configs and workload configs
      # addDefault: true

      # Add dependency on individual configmaps using the configmap names
      # configMaps:
      # - http-server-configs-cm

manifest 文件有两个更改

  • 微服务的版本已更新

  • 已添加外部文件要求 (externalFiles)

  • 容器参数已更新为从 /opt/ext-files/app-config.json 文件中读取 workingDir

更新应用程序中的参数

app.yaml 旁边的路径 /home/<>/my-config.json 中创建一个文件,内容如下

{
        "workingDir": "/etc"
}

app.yaml 文件更新为以下内容

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

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

components:
- name: client
  type: ucf.svc.curl-client
- name: http-server
  type: ucf.svc.http-server
  parameters:
    workingDir: /var
    fileToCreate: someotherfile.txt
  files:
    app-config.json: /home/<username>/my-config.json

connections:
    client/http: http-server/http

http-server 的文件要求 app-config.json 已设置为新创建的文件。

通过在 UCS Studio 中选择 http-server 微服务并从属性窗口设置文件路径,也可以实现相同的目的。UCS Studio 提供了一个文件浏览器来选择文件。

UCS Studio - Passing files

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

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

检查微服务和应用程序

$ microk8s kubectl get all
NAME                                                      READY   STATUS    RESTARTS   AGE
pod/curl-client-curl-client-deployment-569849964-nz598    1/1     Running   0          52m
pod/http-server-http-server-deployment-769c4d5584-xgjg5   1/1     Running   0          41m

NAME                                             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/kubernetes                               ClusterIP   10.152.183.1     <none>        443/TCP    86d
service/http-server-http-server-deployment-svc   ClusterIP   10.152.183.108   <none>        8080/TCP   52m

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

NAME                                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/curl-client-curl-client-deployment-569849964    1         1         1       52m
replicaset.apps/http-server-http-server-deployment-769c4d5584   1         1         1       52m

检查 curl-client pod 和 http-server pod 的日志,确认 my-config.json 中提到的路径 /etc 已生效

$ 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
<!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=".pwd.lock">.pwd.lock</a></li>
<li><a href="adduser.conf">adduser.conf</a></li>
<li><a href="alternatives/">alternatives/</a></li>
<li><a href="apt/">apt/</a></li>
100  4386  100  4386    0     0  1070k      0 --:--:-- --:--:-- --:--:-- 1070k
<li><a href="bash.bashrc">bash.bashrc</a></li>
<li><a href="bash_completion.d/">bash_completion.d/</a></li>
<li><a href="bindresvport.blacklist">bindresvport.blacklist</a></li>
<li><a href="ca-certificates/">ca-certificates/</a></li>
<li><a href="ca-certificates.conf">ca-certificates.conf</a></li>
...
<li><a href="someotherfile.txt">someotherfile.txt</a></li>
...
</ul>
<hr>
</body>
</html>
$ microk8s kubectl logs --tail -1 -l "app=http-server-http-server-deployment,microservice_version=0.0.6"
/etc
X11
adduser.conf
alternatives
apt
bash.bashrc
bash_completion.d
bindresvport.blacklist
ca-certificates
ca-certificates.conf
cron.d
cron.daily
debconf.conf
...

这确认了主机文件系统上的文件已挂载到微服务容器内部。

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

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

$ microk8s helm3 uninstall server-client

一些注意事项

  • 在输出应用程序 Helm Chart 上运行任何 Helm 命令时,将看到类似于以下的参考日志。这是因为符号链接用于将文件添加到 Helm Chart。这些可以安全地忽略

    walk.go:74: found symbolic link in path: /home/<username>/ucf_tutorial/apps/server-client-app/server-client-app-0.0.6/charts/http-server/files/app-config.json resolves to /home/<username>/my-config.json
    
  • 此方法在内部将文件添加到 configmap,该 configmap 将挂载到微服务 pod 中

  • 传递给微服务的文件和目录的总累积大小必须小于每个微服务 1MiB。此限制来自 Kubernetes。

  • 如果您想传递大于 1MiB 的文件/目录,则必须通过创建卷、填充卷并设置参数以将卷挂载到 pod 上来完成。执行此操作时,请将文件要求设置为应用程序中的特殊关键字 __FROM_VOLUME__,以便 appbuilder 不会运行任何检查。

    - name: http-server
      type: ucf.svc.http-server
      files:
        app-config.json: __FROM_VOLUME__