Kubernetes 1.5 实践 给Pod中的容器定义命令和参数
2017-02-09 10:34
489 查看
Kubernetes 1.5 实践 给Pod中的容器定义命令和参数
defining a Command and Arguments for a Container给容器定义一个命令和参数。
This page shows how to define commands and arguments when you run a container in a Kubernetes Pod.
这篇文章展示当运行kubernetes pod的时候,如何给容器定义命令和参数。
When you create a Pod, you can define a command and arguments for the containers that run in the Pod. To define a command, include the command field in the configuration file. To define arguments for the command, include the args field in the configuration file. The command and arguments that you define cannot be changed after the Pod is created.
当创建Pod的时候,我们可以给这个Pod中的容器定义命令和参数。 定义命令,需要在配置文件中加入command标签。为这个命令定义参数,需要加入args标签。当Pod创建好了以后,定义好的命令和参数不可以被变更和修改。
The command and arguments that you define in the configuration file override the default command and arguments provided by the container image. If you define args, but do not define a command, the default command is used with your new arguments. For more information, see Commands and Capabilities.
如果在容器的镜像中定义了命令和参数,那么如果在配置文件中再次定义,则镜像中定义的命令和参数会被覆盖。如果只定义了参数,没有定义命令,那么默认的命令会使用新定义的参数。更多的信息,查看Commands和Capabilities.
In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines a command and two arguments:
在这个练习中,会创建一个运行了一个容器的Pod。在配置文件中配置的Pod定义了一个命令和两个参数:
commands.yaml
apiVersion: v1 kind: Pod metadata: name: command-demo labels: purpose: demonstrate-command spec: containers: - name: command-demo-container image: debian command: ["printenv"] args: ["HOSTNAME", "KUBERNETES_PORT"]
Create a Pod based on the YAML configuration file: 通过yaml配置文件创建Pod:
kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/commands.yaml[/code]List the running Pods: 查看运行的Pods:kubectl get podsThe output shows that the container that ran in the command-demo Pod has completed. 输出可以查看到容器已经在command-demo pod中已经完成。 To see the output of the command that ran in the container, view the logs from the Pod: 查看容器的输出结果,输出Pod的日志:kubectl logs command-demoThe output shows the values of the HOSTNAME and KUBERNETES_PORT environment variables: 输出显示出HOSTNAME和KUBERNETES_PORT两个环境变量的值command-demo tcp://10.3.240.1:443
Using environment variables to define arguments
使用环境变量来定义参数
In the preceding example, you defined the arguments directly by providing strings. As an alternative to providing strings directly, you can define arguments by using environment variables:
env:
- name: MESSAGE
value: “hello world”
command: [“/bin/echo”]
args: [“$(MESSAGE)”]
This means you can define an argument for a Pod using any of the techniques available for defining environment variables, including ConfigMaps and Secrets.
NOTE: The environment variable appears in parentheses, “$(VAR)”. This is required for the variable to be expanded in the command or args field.
Running a command in a shell
In some cases, you need your command to run in a shell. For example, your command might consist of several commands piped together, or it might be a shell script. To run your command in a shell, wrap it like this:
command: [“/bin/sh”]
args: [“-c”, “while true; do echo hello; sleep 10;done”]
相关文章推荐
- Kubernetes 1.5 实践 如何给pod中的容器设置环境变量
- linux alias命令参数及用法详解--linux定义命令别名alias
- linux alias命令参数及用法详解--linux定义命令别名alias
- vs2015中c++中multiset容器定义对象时参数列表中显式调用greater无法被识别的解决办法
- 看Kubernetes 1.5之后如何称霸容器编排管理界
- linux的find命令的奇怪参数定义
- kubernetes单个pod运行两个容器yaml文件实践
- kubernetes(K8s)容器设计模式实践案例 多节点选举模式
- Kubernetes容器集群中的日志系统集成实践
- kubernetes学习2--RC/service/pod实践
- linux alias命令参数及用法详解--linux定义命令别名alias
- C++ Primer 学习笔记_27_STL实践与分析(1) --顺序容器的定义、迭代器
- C++ Primer 学习笔记_27_STL实践与分析(1) --顺序容器的定义、迭代器
- Kubernetes(K8s)容器设计模式实践案例 单节点多容器模式
- 跟我一起写Makefile(14)--- 隐含规则(命令变量+命令参数变量+定义模式规则+自动化变量+模式匹配+)
- 如何在MySQL的存储过程中实现把过程参数用在游标定义的SELECT命令里面作为表名引用
- alias 新的命令='原命令 -选项/参数'。举例说明,alias l=‘ls -lsh' 将重新定义 ls 命令,现在只需输入 l 就可以列目录了。
- kubernetes之单pod多容器的疑难杂症
- Docker实践(三):容器内信息获取、命令的执行、容器的导入和导出
- Kubernetes 1.5通过Ceph实现有状态容器