rancher部署k8s集群后如何通过kubectl管理集群
有了rancher,我们可以很方便的部署一套k8s集群。但是,这种方式部署的集群,并不能直接通过kubectl命令在终端进行管理,需要配置一下认证。这里就演示一下如何配置认证
首先在master机器上安装kubectl命令
yum安装kubectl
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF yum install -y kubectl
然后在master上执行kubectl get nodes命令,输出如下
[root@HB110-XH-CQ-GMSVR-60-217 ~]# kubectl get nodes The connection to the server localhost:8080 was refused - did you specify the right host or port?
这就是因为没有配置认证的缘故,那怎么配置呢?
首先,在rancher界面找到kubeconfig这段配置
不同版本的rancher操作上可能稍有不同,但是不管怎么,拿到kubeconfig里这段配置。
然后在master上,在家目录下创建.kube文件夹,在该文件夹下创建config文件
# 在家目录下创建文件夹 mkdir .kube cd .kube 创建config文件 vim config 将kubeconfig的内容放进config文件里
再次执行kubectl get nodes命令,发现可以操作了
[root@HB110-XH-CQ-GMSVR-60-217 .kube]# kubectl get nodes NAME STATUS ROLES AGE VERSION hb110-xh-cq-gmsvr-60-217 Ready controlplane,etcd,worker 60m v1.20.10