简体   繁体   English

kubectl从头开始与远程集群连接

[英]kubectl connect with remote cluster from scratch

I've created a local Kubernetes cluster using ansible . 使用ansible创建了一个本地Kubernetes集群 Everything is running but now I try to connect my kubectl with the cluster (in the VM's). 一切都在运行,但是现在我尝试将kubectl与群集(在VM中)连接。

My cluster is running on https://IP:6443 我的集群在https://IP:6443

First I got: 首先我得到:

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

So I tried this solution : 所以我尝试了这个解决方案

kubectl config set-credentials kubeuser/IP --username=kubeuser --password=kubepassword
kubectl config set-cluster IP --insecure-skip-tls-verify=true --server=https://IP:6443
kubectl config set-context default/IP/kubeuser --user=kubeuser/IP --namespace=default --cluster=IP
kubectl config use-context default/IP/kubeuser

and tried again: 然后再试一次:

$ kubectl get pods
Error from server (Forbidden): pods is forbidden: User "system:anonymous" cannot list pods in the namespace "default"

I understand I need to create a clusterrolebinding but I still need an initial "admin-ownership" for that? 我知道我需要创建clusterrolebinding,但是仍然需要初始的“管理员所有权”吗? What step am I missing or doing wrong so I can access my cluster with kubectl and get my pods? 我错过了什么步骤或做错了什么,所以可以使用kubectl访问群集并获取吊舱?

Kubectl is a command line tool for remote management of Kubernetes cluster. Kubectl是用于Kubernetes集群远程管理的命令行工具。

Kubectl is using a config file you must have to connect to the cluster. Kubectl使用的配置文件必须连接到集群。 It is possible that your config file is inconsistent due to a lot of major or minor changes. 由于许多主要或次要更改,您的配置文件可能不一致。 If further analyses of the issues does not show good results, try to rm -f ~/.kube/config and start it from scratch. 如果对问题的进一步分析未显示出良好的结果,请尝试rm -f〜/ .kube / config并从头开始。

As I see, you suspect that the problem is with the self signed certificates. 如我所见,您怀疑问题出在自签名证书上。 It may require updating cluster root Certificate Authority (CA) on clients, then refreshing the local list for valid certificates. 可能需要更新客户端上的群集根证书颁发机构(CA),然后刷新本地列表以获取有效证书。

Go to your local CA directory, check if ca.crt file exists, then copy it to the clients. 转到本地CA目录,检查ca.crt文件是否存在,然后将其复制到客户端。 For clients, perform the following operations: 对于客户端,请执行以下操作:

sudo cp ca.crt /usr/local/share/ca-certificates/kubernetes.crt
sudo update-ca-certificates

I agree with the suggestion provided by Matthew L. Daniel: 我同意Matthew L. Daniel的建议:

cluster: tag accepts either the filename of the CA certificate or an "inline" base64-ed version of the PEM you can see it with: cluster:标签接受CA证书的文件名或PEM的“内联” base64版本的PEM,您可以通过以下方式查看它:

kubectl config set-cluster $foo --certificate-authority=... --embed-certs=true

or you can cheat and use --insecure-skip-tls-verify=true to switch off the CA verification for testing things 或者您可以作弊并使用--insecure-skip-tls-verify=true来关闭CA验证以进行测试

If you do not have a certificate generated during the instalation of Kubernetes, I suggest starting with Kubernetes TLS documentation . 如果在安装Kubernetes时没有生成证书,建议从Kubernetes TLS文档开始 You may have a look at Kubernetes cloud providers like GKE or AKS - they give a possibility to create Kubernetes config file remotely with all certificates included, and then download it to a local ~/.kube directory. 您可能会看到诸如GKE或AKS之类的Kubernetes云提供程序-他们提供了使用包含所有证书的远程创建Kubernetes配置文件的可能性,然后将其下载到本地〜/ .kube目录。 Maybe this will solve some issues with the config if you adopt it to local installation. 如果您将其用于本地安装,则可能会解决一些配置问题。 It works like a charm and saves a lot of time. 它就像一个护身符,可以节省大量时间。 If you're still undecided, please provide here the output of kubectl config view for us to help you with your trouble. 如果您仍然不确定,请在此处提供kubectl config view的输出,以帮助我们解决问题。 (Please remove sensitive data from it before posting here). (请先删除敏感数据,然后再在此处发布)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM