$ 变量在命令 k8s yaml 中被忽略

问题描述 投票:0回答:1

我定义了 k8s pod yaml 文件,我想在 initcontainer 中运行 bash 脚本。当我运行它并检查容器 yaml 中的 k8s 仪表板时,$ip_list 变量被删除。

  initContainers:
    - name: init-redis
      image: busybox:1.36.1-glibc
      command:
        - sh
        - '-c'
        - >-
          until [ $(nslookup headless-svc.default.svc.cluster.local | grep
          'Address: ' | awk '{print $2}' | wc -w) = 3 ]; do echo
          Waiting-all-instances...; sleep 1; done; for ip_list in $(nslookup
          headless-svc.default.svc.cluster.local | awk '/^Address: / { print $2 }'); do until nc -vz $ip_list
          80; do echo waiting-SENTINEL...; sleep 1; done; done;

在 k8s 仪表板中

  initContainers:
    - name: init-redis
      image: busybox:1.36.1-glibc
      command:
        - sh
        - '-c'
        - >-
          until [ $(nslookup headless-svc.default.svc.cluster.local | grep
          'Address: ' | awk '{print $2}' | wc -w) = 3 ]; do echo
          Waiting-all-instances...; sleep 1; done; for ip_list in $(nslookup
          headless-svc.default.svc.cluster.local | awk '/^Address: / { print $2 }'); do until nc -vz
          80; do echo waiting-SENTINEL...; sleep 1; done; done;
kubernetes sh
1个回答
0
投票

我想知道您的 YAML 处理器是否正在执行某种值插值。也许尝试逃避有问题的美元符号?

无论如何,在 IP 地址周围添加引号至少应该可以揭示出什么问题。以下重构还尝试消除一些 shell 脚本代码气味。

  initContainers:
    - name: init-redis
      image: busybox:1.36.1-glibc
      command:
        - sh
        - '-c'
        - >-
          until nslookup headless-svc.default.svc.cluster.local |
               awk '/Address:/ { ++n }
                 END { if(n!=3) print "Waiting-all-instances";
                   exit (n!=3) }';
          do
            sleep 1;
          done;
          nslookup headless-svc.default.svc.cluster.local |
          awk '/^Address: / { print $2 }' |
          while read -r ip; do
            until nc -vz "$ip" 80; do
              echo waiting-SENTINEL...;
              sleep 1;
          done

(我猜[大部分]分号可能是不必要的,但我将它们留在了以防万一您的 YAML 处理器将所有内容有效地包装回单行中。)

多次运行相同的

nslookup
也有点反模式;也许甚至可以尝试

  initContainers:
    - name: init-redis
      image: busybox:1.36.1-glibc
      command:
        - sh
        - '-c'
        - >-
          while true; do
              ips=$(nslookup headless-svc.default.svc.cluster.local |
                   awk '/^Address:/ { print $2 }
                     END { if(n!=3) print "Waiting-all-instances";
                       exit (n!=3) }') && break;
              sleep 1;
          done;
          for ip in $ips; do
              until nc -vz "$ip" 80; do
                  echo waiting-SENTINEL...;
                  sleep 1;
              done;
          done
© www.soinside.com 2019 - 2024. All rights reserved.