随着Kubernetes集群的建立和运行,并能够使用ssh-keys通过ssh进入主服务器并在其中运行kubectl命令;我想在本地计算机上运行kubectl命令。因此,我尝试按照kubectl config设置配置:
kubectl config set-cluster mykube --server=https://<master-ip>:6443
kubectl config set-context mykube --cluster=mykube --user=mykube-adm
kubectl config set-credentials mykube-adm --client-key=path/to/private/keyfile
激活上下文:
kubectl config use-context mykube
当我运行kubectl命令时:
kubectl get nodes
它返回:
与服务器localhost:8080的连接被拒绝-您指定了正确的主机或端口吗?
kubectl config view
apiVersion: v1
clusters:
- cluster:
server: https://<master-ip>:6443
name: mykubecontexts:
- context:
cluster: mykube
user: mykube-adm
name: mykube
current-context: mykube
kind: Config
preferences: {}
users:
- name: mykube-adm
user:
client-key: path/to/private/keyfile
答案 0 :(得分:1)
很遗憾,以上kubectl
的配置文件不正确。由于手动格式化或其他原因,似乎出现了错误。
此部分(name: mykubecontexts:
)中缺少新行:
clusters:
- cluster:
server: https://<master-ip>:6443
name: mykubecontexts:
- context:
cluster: mykube
user: mykube-adm
name: mykube
正确的是:
clusters:
- cluster:
server: https://<master-ip>:6443
name: mykube
contexts:
- context:
cluster: mykube
user: mykube-adm
name: mykube
这就是群集名称为mykubecontexts:
的原因:
clusters:
- cluster:
server: https://<master-ip>:6443
name: mykubecontexts:
这就是为什么其中没有上下文的原因,因为未定义contexts:
。
kubectl
找不到上下文mykube
,并切换到默认位置,其中server=localhost:8080
是默认位置。
kubectl
标志或${HOME}/.kube/config
环境变量,默认情况下 --kubeconfig
配置位于$KUBECONFIG
文件中。
请更正以下内容:
apiVersion: v1
clusters:
- cluster:
server: https://<master-ip>:6443
name: mykube
contexts:
- context:
cluster: mykube
user: mykube-adm
name: mykube
current-context: mykube
kind: Config
preferences: {}
users:
- name: mykube-adm
user:
client-key: path/to/private/keyfile