創建 Pod 時設置命令及參數 - daniel-qa/Azure-Kubernetes-Service GitHub Wiki

https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/define-command-argument-container/

簡單 Command 指令

spec:
  containers:
  - name: command-demo-container
    image: debian
    stdin: true
    tty: true
    env:
      - name: MESSAGE
        value: "hello world"    
	command: ["/bin/echo"]
        args: ["$(MESSAGE)",">","/opt/hello.txt"]
  • 可能 arg 參數形式
 #  args: ["-c","echo 'server $(PHP_UPSTREAM_CONTAINER):$(PHP_UPSTREAM_PORT); }' > /opt/hello.txt"]
    
 #  args: ["'upstream php-upstream { server $(PHP_UPSTREAM_CONTAINER):$(PHP_UPSTREAM_PORT); }' > /etc/nginx/conf.d/upstream.conf \
 #         && rm /etc/nginx/conf.d/default.conf"]    
    
 #  args: ["-c", "while true; do echo hello; sleep 10;done"]
  

執行 shell

https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

使用环境变量来设置参数

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - command:
    - /bin/sh
    - -c
    - |
      echo "running below scripts"
      i=0; 
      while true; 
      do 
        echo "$i: $(date)"; 
        i=$((i+1)); 
        sleep 1; 
      done
    name: busybox
    image: busybox
apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    env:
    - name: MESSAGE
      value: "hello world"
    command: ["/bin/sh","-c"]
    args: ["/bin/echo \"${MESSAGE}\""]
  • sh 範例
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

解釋:上面command ["/bin/sh", "-c"] 寫著“運行一個shell,並執行以下指令”。 然後將 args 作為命令傳遞給 shell。

在 shell 腳本中,分號分隔命令,&&如果第一個成功,則有條件地運行以下命令。 在上面的例子中,它總是在command one之後運行command two, 並且只有在command three成功command two時才運行。

env:
- name: MESSAGE
  value: "hello world"
command: ["/bin/echo"]
args: ["$(MESSAGE)"]

在 Shell 来执行命令

有时候,你需要在 Shell 脚本中运行命令。 例如,你要执行的命令可能由多个命令组合而成,或者它就是一个 Shell 脚本。 这时,就可以通过如下方式在 Shell 中执行命令:

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

创建一个只包含单个容器的 Pod。添加一个命令与两个参数:

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"]
  restartPolicy: OnFailure
  • 創建 pod
kubectl apply -f https://k8s.io/examples/pods/commands.yaml
  • 取得 pod
kubectl get pods
  • 如果要获取容器启动时执行命令的输出结果,可以通过 Pod 的日志进行查看:
kubectl logs command-demo
  • 日志中显示了 HOSTNAME 与 KUBERNETES_PORT 这两个环境变量的值:
command-demo
tcp://10.3.240.1:443
  • 範例
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        stdin: true
        tty: true