简体   繁体   English

如何通过类似于 Kubernetes 的 CloudFoundry 清单挂载文件?

[英]How to mount a file via CloudFoundry manifest similar to Kubernetes?

With Kubernetes, I used to mount a file containing feature-flags as key/value pairs.使用 Kubernetes,我曾经将包含功能标志的文件挂载为键/值对。 Our UI would then simply get the file and read the values.然后我们的 UI 将简单地获取文件并读取值。

Like this: What's the best way to share/mount one file into a pod?像这样: 将一个文件共享/挂载到 pod 中的最佳方式是什么?

Now I want to do the same with the manifest file for CloudFoundry.现在我想对 CloudFoundry 的清单文件做同样的事情。 How can I mount a file so that it will be available in /dist folder at deployment time?如何挂载文件以便在部署时可以在/dist文件夹中使用它?

The typical approach to mounting files into a CloudFoundry application is called Volume Services .将文件挂载到 CloudFoundry 应用程序的典型方法称为Volume Services This takes a remote file system like NFS or SMB and mounts it into your application container.这需要一个远程文件系统,如 NFS 或 SMB,并将其安装到您的应用程序容器中。

I don't think that's what you want here.我不认为这就是你想要的。 It would probably be overkill to mount in a single file.挂载在单个文件中可能会有些过分。 You totally could go this route though.不过你完全可以走这条路。

That said, CloudFoundry does not have a built-in concept that's similar to Kubernetes, where you can take your configuration and mount it as a file.也就是说,CloudFoundry 没有类似于 Kubernetes 的内置概念,您可以在其中获取配置并将其作为文件挂载。 With CloudFoundry, you do have a few similar options.使用 CloudFoundry,您确实有一些类似的选择。 They are not exactly the same though so you'll have to make the determination if one will work for your needs.但它们并不完全相同,因此您必须确定一个是否适合您的需求。

  1. You can pass config through environment variables (or through user-provided service bindings, but that comes through an environment variable VCAP_SERVICES as well).您可以通过环境变量(或通过用户提供的服务绑定,但这也来自环境变量VCAP_SERVICES )传递配置。 This won't be a file, but perhaps you can have your UI read that instead (You didn't mention how the UI gets that file, so I can't comment further. If you elaborate on that point like if it's HTTP or reading from disk, I could perhaps expand on this option).这不会是一个文件,但也许你可以让你的 UI 读取它(你没有提到 UI 如何获取该文件,所以我不能进一步评论。如果你详细说明这一点,比如它是 HTTP 还是从磁盘读取,我也许可以扩展这个选项)。

    If it absolutely needs to be a file, your application could read the environment variable contents and write it to disk when it starts.如果它绝对需要是一个文件,您的应用程序可以在启动时读取环境变量内容并将其写入磁盘。 If your application isn't able to do that like if you're using Nginx, you could include a .profile script at the root of your application that reads it and generates the file.如果您的应用程序无法像使用 Nginx 那样执行此操作,您可以在应用程序的根目录中包含一个.profile脚本,用于读取它并生成文件。 For example: echo "$CFG_VAR" > /dist/file or whatever you need to do to generate that file.例如: echo "$CFG_VAR" > /dist/file或任何你需要做的来生成那个文件。

    A couple of more notes when using environment variables.使用环境变量时的一些注意事项。 There are limits to how much information can go in them (sorry I don't know the exact value off the top of my head, but I think it's around 128K).可以输入多少信息是有限制的(抱歉,我不知道我脑子里的确切值,但我认为它大约是 128K)。 It is also not great for binary configuration, in which case, you'd need to base64 encode your data first.它也不适用于二进制配置,在这种情况下,您需要先对数据进行 base64 编码。

  2. You can pull the config file from a config server and cache it locally.您可以从配置服务器中提取配置文件并将其缓存在本地。 This can be pretty simple.这可以很简单。 The first thing your app does when it starts is to reach out and download the file, place it on the disk and the file will persist there for the duration of your application's lifetime.您的应用程序在启动时所做的第一件事是获取并下载文件,将其放置在磁盘上,该文件将在您的应用程序生命周期内一直保留在那里。

    If you don't have a server-side application like if you're running Nginx, you can include a .profile script (can be any executable script) at the root of your application which can use curl or another tool to download and set up that configuration.如果您没有像运行 Nginx 那样的服务器端应用程序,您可以在应用程序的根目录中包含一个.profile脚本(可以是任何可执行脚本),它可以使用curl或其他工具来下载和设置上那个配置。

    You can replace "config server" with an HTTP server, Git repository, Vault server, CredHub, database, or really any place you can durably store your data.您可以将“配置服务器”替换为 HTTP 服务器、Git 存储库、Vault 服务器、CredHub、数据库或任何可以持久存储数据的地方。

  3. Not recommended, but you can also push your configuration file with the application.不推荐,但您也可以使用应用程序推送您的配置文件。 This would be as simple as including it in the directory or archive that you push.这就像将其包含在您推送的目录或存档中一样简单。 This has the obvious downside of coupling your configuration to the application bits that you push.这具有将您的配置耦合到您推送的应用程序位的明显缺点。 Depending on where you work, the policies you have to follow, and the tools you use this may or may not matter.根据您的工作地点、您必须遵循的政策以及您使用的工具,这些政策可能重要,也可能无关紧要。

There might be other variations you could use as well.您可能还可以使用其他变体。 Loading the file in your application when it starts or through a .profile script is very flexible.在应用程序启动时或通过.profile脚本在应用程序中加载文件非常灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM