亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 Alpine 容器中使用 os.exec 通過 shell 運行 kubectl 命令

無法在 Alpine 容器中使用 os.exec 通過 shell 運行 kubectl 命令

Go
尚方寶劍之說 2022-06-27 15:54:16
我正在嘗試從我的容器中運行一些 kubectl 命令。package mainimport (    "fmt"    "os/exec")    op, err := GetPods("test-containers", "qa")    if err != nil {        fmt.Printf("\nError: %v", err)    }    fmt.Printf(op)func GetPods(name, namespace string) (podName string, err error) {    fmt.Println("Get pod names ....")    cmd := "kubectl get pods -n " + namespace + " -o wide | grep " + name + " | awk '{print $1}' "    cmnd, err := exec.Command("sh", "-c", cmd).Output()    if err != nil {        fmt.Println("Failed to find pod name." + string(cmnd))    }    podName = string(cmnd)    return}我的多級 Dockerfile 基于 Alpine3.6 并安裝了 kubectl 二進制文件。FROM alpine:3.6RUN apk add --update curl wget ca-certificates unzip python py-pip openssl bash && \    apk --update add openssh-client && \    apk --update add --virtual build-dependencies python-dev libffi-dev openssl-dev build-base && \    apk add --no-cache --upgrade bash && \    pip install --upgrade pip cffi && \    pip install awscli && \    wget  https://storage.googleapis.com/kubernetes-release/release/v1.15.1/bin/linux/amd64/kubectl && \    chmod u+x kubectl && mv kubectl /bin/kubectl && \    apk del build-dependencies && \    rm -rf /var/cache/apk/*ENV HOME=/go/app/WORKDIR /go/appCOPY --from=go-container-build /go/app/ .ENTRYPOINT ["bash"]當我將容器作為 pod 運行并嘗試執行命令時;我總是遇到以下錯誤:cmd:  kubectl -n qa get pods -o wide | grep test-containers | awk '{print $1}'Failed to find pod name.exit status 2/bin/sh: illegal option -我也嘗試過使用 os.exec.Command() exec.Command("/bin/sh", "-c", cmd),exec.Command("/bin/bash", "-c", cmd)但我遇到了類似的錯誤。Failed to find pod name.exit status 2/bin/sh: illegal option -我也直接試過cmnd := exec.Command(cmd);但我得到了錯誤:Failed to find pod name.fork/exec kubectl -n qa get pods -o wide | grep test-containers | awk '{print $1}':executable file not found in $PATH我已經在容器中安裝了 bash,當我直接在容器 shell 上運行命令時,我總是得到輸出。
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

/bin/sh: 非法選項 -


是因為-carg 解析器將其解釋為/bin/sh "- "- 具有諷刺意味的是(?)您的golang代碼段是正確的,但是您粘貼的代碼部分和問題的癥結顯示了錯誤的語法


無需涉及busybox或其他任何東西,就可以輕松地重現這一點


package main

import (

    "fmt"

    "os/exec"

)

func main() {

    // c, err := exec.Command("/bin/sh", "-c", "echo -n hello | grep lo").Output()

    c, err := exec.Command("/bin/sh", "-c ", "echo -n hello | grep lo").Output()

    if err != nil {

        ee := err.(*exec.ExitError)

        panic(fmt.Errorf("c is %q and err is %q and stderr is %q", string(c), err, string(ee.Stderr)))

    }

    fmt.Println("OK")

}

結果,panic: c is "" and err is "exit status 2" and stderr is "/bin/sh: - : invalid option\n如果交換注釋行,它會打印OK


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號