1. 程式人生 > 實用技巧 >通過ssh遠端執行kubectl命令報錯問題

通過ssh遠端執行kubectl命令報錯問題

在使用Jenkins連結Kubernetes叢集,如果Jenkins安裝機器與Kubernetes Master節點不在同一臺機器上面,需要使用ssh遠端執行部署命令,如下:

ssh [email protected] -p 60022 'kubectl apply -f deploynetcore.yaml'

執行時會出現8080拒絕訪問錯誤,資訊如下:

The connection to the server localhost:8080 was refused - did you specify the right host or port?

是因為使用ssh執行命令和登入後執行Linux所載入的環境變數不同,在使用ssh執行命令時沒有載入/etc/profile的環境變數,對此,可參考這篇文章:

https://www.jianshu.com/p/f24c7445c4db

將/etc/profile目錄下kubernetes對應的環境變數新增到/root/.bashrc檔案內即可,新增後內容如下,此時在執行時可以正常。

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

export KUBECONFIG=/etc/kubernetes/admin.conf