Kubernetes 1.5 实践 如何给pod中的容器设置环境变量

26 篇文章 0 订阅
17 篇文章 0 订阅

Kubernetes 1.5 给POD设置变量

When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env field in the configuration file.
在创建pod时,你可以为运行在pod中的容器设置环境变量。当我们需要设置变量时,我们只需要在配置文件中(yaml)中加入env选项。

In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines an environment variable with name DEMO_GREETING and value “Hello from the environment”. Here is the configuration file for the Pod:
在这个练习中,你将创建一个运行了一个容器的pod。在配置文件中为pod定义的的一个环境变量名为 DEMO_GREETING,值为 “Hello from the environment”。这个pod的配置文件envars.yaml如下:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"

Create a Pod based on the YAML configuration file:
通过YAML配置文件创建Pod:

kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/envars.yaml

List the running Pods:
查看正在运行的Pods

kubectl get pods -l purpose=demonstrate-envars

The output is similar to this:
输出内容如下:

     NAME            READY     STATUS    RESTARTS   AGE
     envar-demo      1/1       Running   0          9s

Get a shell to the container running in your Pod:
连接到Pod中的容器:

kubectl exec -it envar-demo -- /bin/bash

In your shell, run the printenv command to list the environment variables.
在这个shell中,执行命令printenv命令查看环境变量。

root@envar-demo:/# printenv

The output is similar to this:
输出内容如下:

     NODE_VERSION=4.4.2
     EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237
     HOSTNAME=envar-demo
     ...
     DEMO_GREETING=Hello from the environment

To exit the shell, enter exit.
执行命令exit退出。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值