我想在堆栈中管理环境变量,然后服务可以使用它。例如:我定义了一个 evn tracker_ip=192.168.0.101,然后我想在服务创建中使用它。
我应该怎么办
There can be several answers depending on what you are trying to do and how you are deploying your stack.
Using the CLI / Rancher Compose
If you are using the command line, you can just use variable interpolation. Instructions on how to do so can be found in the official documentation:
https://docs.rancher.com/rancher/v1.5/en/cli/variable-interpolation/
Using the Rancher UI / Catalogs
If you want to do it through the Rancher UI, you can do it by creating a template in a catalog and having questions to input your environment variables. More details on how to do so here:
https://docs.rancher.com/rancher/v1.5/en/catalog/private-catalog/
You can define questions in the rancher-compose.yml file like this:
version: '2'
catalog:
name: My Application
version: v0.0.1
questions:
- variable: TRACKER_IP
label: Tracker IP address
required: true
default: 192.168.0.101
type: string
You can then push the answers to the environment section of your docker-compose.yml template for use within your image:
version: '2'
services:
web:
image: myimage
ports:
- 8000
environment:
TRACKER_IP: ${TRACKER_IP}
没有办法完全按照您的要求进行操作,因为这将允许编辑正在运行的容器的变量,并且容器是不可变的。环境变量可以在服务上定义,但不能在堆栈上定义一次并可供所有服务使用。
秘密有点像这样,可以跨服务共享,但不能编辑。
根据 tracker_ip 关联的位置,您还可以创建一个外部服务作为堆栈的一部分。外部服务本质上只是在 Rancher 中创建了一个 DNS 条目。因此,您可以将您的服务链接到 compose 中的 external_tracker 服务并参考tracker
.
version: '2'
services:
myservice:
...
link:
- tracker_service:tracker
...