首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >kubectl配置单、多集群访问

kubectl配置单、多集群访问

原创
作者头像
马凌鑫
修改2018-08-10 15:40:34
2.8K0
修改2018-08-10 15:40:34
举报
文章被收录于专栏:云知识学习云知识学习

您可以通过 Kubernetes 命令行工具 kubectl 从本地客户端机器连接到 TKE 集群。

这里是官方文档https://cloud.tencent.com/document/product/457/8438

写这篇的意义主要记录一些小问题,以及单个客户端如何连接多个TKE集群

一. 下载 kubectl 工具

Linux 系统

这个一看就知道是国内下载不了系列,请发挥您的智慧下载吧,这里我是添加了国内源直接yum -y install kubectl-1.10.5 如不加版本默认下载最新版本

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.8.13/bin/linux/amd64/kubectl

注:v1.8.13可根据需求替换成业务所需的kubectl版本。 关于版本的事情不得不多说一句,尽量跟着自己集群的版本走,我在实验的过程中下了最新的kubectl1.11的,有些指令不识别了。

这里一路跟着教程走就行了

二. 获取集群账号密码以及证书信息

  1. 登录 容器服务控制台 > 集群,单击需要连接的集群 ID/名称,查看集群详情。
  2. 在集群信息页,单击【显示凭证】,查看用户名、密码和证书信息。
  3. 复制或下载证书文件到本地。这里我是直接复制粘贴到linux环境去加粗的,文件的命名看个人喜好,这个文件名在后续的操作中需要使用到,所以敲个好记的名字或好区分的名字吧。
  4. 开启访问入口这里分公网入口已经内网入口,在控制台页面体现为“外网访问地址”、“内网访问地址”,这里开启外网访问地址就行了,内网访问地址会超时

三. 通过证书信息使用 kubectl 操作集群

方法一:单次 kubectl 操作请求,附带证书信息

该方法适用于单次操作集群,不将容器集群的证书信息保存到机器上。

请求方法,kubectl 命令格式:

kubectl get node -s "域名信息/外网访问地址" --username=用户名 --password=密码 --certificate-authority=证书路径

示例:

kubectl get node -s "https://cls-66668888.ccs.tencent-cloud.com" --username=admin --password=6666o9oIB2gHD88882quIfLMy6666 --certificate-authority=/etc/kubernetes/cluster-ca.crt

如果用的是内网访问地址,将会出现Unable to connect to the server: i/o timeout,这里如果有读者了解的可以在评论中指出

[root@VM_0_7_centos ~]# kubectl get node -s "cls-h5dfwr4g.ccs.tencent-cloud.com" --username=xxxx  --password=xxxx --certificate-authority=/etc/kubernetes/cluster-ca.crt
Unable to connect to the server: dial tcp 10.0.0.12:80: i/o timeout
[root@VM_0_7_centos ~]# cat /etc/hosts
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.redhat.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
#
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 VM_0_7_centos VM_0_7_centos
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

# The following lines are desirable for IPv6 capable hosts
::1 VM_0_7_centos VM_0_7_centos
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

10.0.0.12 cls-h5dfwr4g.ccs.tencent-cloud.com

到此就可以正确的获取到集群的信息了

[root@VM_0_7_centos ~]# kubectl get node -s "https://cls-h5dfwr4g.ccs.tencent-cloud.com" --username=admin --password=XyUi2kzTM7yhPiynfrDnuhBdX0Uzinvi --certificate-authority=/etc/kubernetes/cluster-ca.crt
NAME       STATUS    ROLES     AGE       VERSION
10.0.0.4   Ready     <none>    5h        v1.10.5-qcloud
10.0.0.5   Ready     <none>    4h        v1.10.5-qcloud

至于操作多个集群,就是把域名信息换了,账户密码换了还有证书换了。

如果想修改 kubectl 配置文件,且多集群的请继续往下看

该方法适用于长期通过 kubectl 操作集群, 一次配置,只要文件不修改就长期有效。

背景:假设我有两个集群,一个是生产集群,一个是测试集群,我想通过kubectl操作多个集群请往下配置:

首先配置生产集群的链接

kubectl config set-credentials produce-admin --username=admin --password=6666o9oIB2gHD88882quIfLMy6666
kubectl config set-cluster produce-cluster --server=https://cls-66668888.ccs.tencent-cloud.com --certificate-authority=/etc/kubernetes/cluster-ca.crt
kubectl config set-context produce-system --cluster=produce-cluster --user=produce-admin

配置的格式以及含义

kubectl config set-credentials NAME [--username=basic_user] [--password=basic_password]
kubectl config set-cluster NAME [--server=server] [--certificate-authority=path/to/certficate/authority] 
kubectl config set-context NAME [--cluster=cluster_nickname] [--user=user_nickname] [--namespace=namespace]
set-credentials 在kubeconfig配置文件中设置一个用户项。
set-cluster 在kubeconfig配置文件中设置一个集群项。
set-context 在kubeconfig配置文件中设置一个环境项。

配置完,切换环境项

kubectl config use-context produce-system

这样直接输入kubectl get node即可查看produce-system集群的信息。

那么第二个集群只需照葫芦画瓢创建第二个环境再用use-context切换即可

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. 下载 kubectl 工具
    • Linux 系统
    • 二. 获取集群账号密码以及证书信息
    • 三. 通过证书信息使用 kubectl 操作集群
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档