我有一个K8s部署,其中一个 pods 运行着Envoy sw一个容器。我以这样的方式定义了镜像:如果定义了环境变量EXTRA_OPTS,它将被附加到命令行以启动Envoy。
我想使用该变量来覆盖默认配置,如
https://www.envoyproxy.io/docs/envoy/latest/operations/cli#cmdoption-config-yaml

对于其他命令选项,例如“-l debug”,环境变量可以正常工作。
另外,我已经测试了预期的最终命令行,并且可以正常工作。

Dockerfile将Envoy设置为以这种方式运行:

CMD ["/bin/bash", "-c", "envoy -c envoy.yaml $EXTRA_OPTS"]

我要设置的是:
       ...
      - image: envoy-proxy:1.10.0
        imagePullPolicy: IfNotPresent
        name: envoy-proxy
        env:
           - name: EXTRA_OPTS
             value: ' --config-yaml "admin: { address: {  socket_address: { address: 0.0.0.0, port_value: 9902 } } }"'
        ...

我已经使用最终命令行成功测试了运行特使:

envoy -c /etc/envoy/envoy.yaml --config-yaml "admin: { address: {  socket_address: { address: 0.0.0.0, port_value: 9902 } } }"

而且我还测试了EXTRA_OPTS中的“简单”选项,它的工作原理是:
       ...
      - image: envoy-proxy:1.10.0
        imagePullPolicy: IfNotPresent
        name: envoy-proxy
        env:
           - name: EXTRA_OPTS
             value: ' -l debug'
        ...

我希望Envoy使用此新的管理端口运行,而不是出现参数错误:

PARSE ERROR: Argument: {
             Couldn't find match for argument

看来引号没有被传递到容器中的实际环境变量...

有任何线索吗?

谢谢大家

最佳答案

您应该在dockerfile中将["/bin/bash", "-c", "envoy -c envoy.yaml"]设置为ENTRYPOINT,或者在kubernetes中使用command,然后使用args添加其他参数。

您可以在docker documentation中找到更多信息。

让我举例说明:
$ docker build -t fl3sh/test:bash .

$ cat Dockerfile
FROM ubuntu
RUN echo '#!/bin/bash' > args.sh && \
    echo 'echo "$@"' >> args.sh && \
    chmod -x args.sh
CMD ["args","from","docker","cmd"]
ENTRYPOINT ["/bin/bash", "args.sh", "$ENV_ARG"]
cat args.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: args
  name: args
spec:
  containers:
  - args:
    - args
    - from
    - k8s
    image: fl3sh/test:bash
    name: args
    imagePullPolicy: Always
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Output:
pod/args $ENV_ARG args from k8s
cat command-args.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: command-args
  name: command-args
spec:
  containers:
  - command:
    - /bin/bash
    - -c
    args:
    - 'echo args'
    image: fl3sh/test:bash
    imagePullPolicy: Always
    name: args
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Output:
pod/command-args args
cat command-env-args.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: command-env-args
  name: command-env-args
spec:
  containers:
  - env:
    - name: ENV_ARG
      value: "arg from env"
    command:
    - /bin/bash
    - -c
    - exec echo "$ENV_ARG"
    image: fl3sh/test:bash
    imagePullPolicy: Always
    name: args
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Output:
pod/command-env-args arg from env
cat command-no-args.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: command-no-args
  name: command-no-args
spec:
  containers:
  - command:
    - /bin/bash
    - -c
    - 'echo "no args";echo "$@"'
    image: fl3sh/test:bash
    name: args
    imagePullPolicy: Always
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Output:
pod/command-no-args no args

#notice ^ empty line above
cat no-args.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: no-args
  name: no-args
spec:
  containers:
  - image: fl3sh/test:bash
    name: no-args
    imagePullPolicy: Always
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Output:
pod/no-args $ENV_ARG args from docker cmd

如果您需要重新创建我的示例,则可以使用此循环获取上述输出:
for p in `kubectl get po -oname`; do echo cat ${p#*/}.yaml; echo ""; \
cat ${p#*/}.yaml; echo -e "\nOutput:"; printf "$p "; \
kubectl logs $p;echo "";done

结论,如果您需要将env作为参数传递,请使用:
    command:
    - /bin/bash
    - -c
    - exec echo "$ENV_ARG"

我希望现在很清楚。

关于kubernetes - 将json字符串传递给Envoy的k8s部署中的环境变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57389810/

10-11 11:02