将json字符串传递给Envoy的k8s部署中的环境变量 [英] Pass json string to environment variable in a k8s deployment for Envoy

查看:273
本文介绍了将json字符串传递给Envoy的k8s部署中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a K8s deployment with one pod running among others a container with Envoy sw. I have defined image in such way that if there is an Environment variable EXTRA_OPTS defined it will be appended to the command line to start Envoy. I want to use that variable to override default configuration as explained in https://www.envoyproxy.io/docs/envoy/latest/operations/cli#cmdoption-config-yaml

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

Environment variable works ok for other command options such as "-l debug" for example. Also, I have tested expected final command line and it works.

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

Dockerfile set Envoy to run in this way:

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 } } }"'
        ...

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

I have succesfully tested running envoy with final command line:

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

我还测试了EXTRA_OPTS中的简单"选项,它的工作原理是:

And I have also tested a "simpler" option in EXTRA_OPTS and it works:

       ...
      - image: envoy-proxy:1.10.0
        imagePullPolicy: IfNotPresent
        name: envoy-proxy
        env:
           - name: EXTRA_OPTS
             value: ' -l debug'
        ...

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

I would expect Envoy running with this new admin port, instead I'm having param errors:

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

似乎引号没有传递到容器中的实际Environment变量...

It looks like quotes are not being passed to the actual Environment variable into the container...

有任何线索吗???

感谢所有人

推荐答案

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

You should set ["/bin/bash", "-c", "envoy -c envoy.yaml"] as an ENTRYPOINT in you dockerfile or use command in kubernetes and then use args to add additional arguments.

您可以在 docker文档中找到更多信息.

You can find more information in docker documentation

让我通过示例进行解释:

Let me explain by example:

$ 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

如果您需要重新创建我的示例,则可以使用此循环获取上述输出:

If you need to recreate my example you can use this loop to get this output like above:

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作为参数传递,请使用:

Conclusion if you need to pass env as arguments use:

    command:
    - /bin/bash
    - -c
    - exec echo "$ENV_ARG"

我希望现在清楚了.

这篇关于将json字符串传递给Envoy的k8s部署中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆