# 安装Kubectl

日常工作中,您可能需要在自己的笔记本电脑上执行 kubectl 命令以管理远程 Linux 服务器上的 Kubernetes 集群。

TIP

从客户端电脑使用 kubectl 远程管理 Kubernetes,这个步骤并不是使用 Kuboard 的必要步骤。可以忽略

# 在客户端电脑安装 kubectl

Kubernetes 官网文档参照 安装 kubectl 安装时,经常会失败,因为国内访问 google 的镜像仓库存在问题。

请参考下述内容,完成 kubectl 的安装

配置K8S的yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1
2
3
4
5
6
7
8
9
10

安装kubectl

yum install -y kubectl
1

验证安装

kubectl version
1

# 获取 kubectl config 文件

如果您参考 安装 Kubernetes 单Master节点安装 Kubernetes 高可用 完成了 Kubernetes 安装,请在 demo-master-a-1 节点上执行如下命令

cat /etc/kubernetes/admin.conf
1

输出内容如下所示:





 















apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRxpJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNU1EY3hPREEzTURFMU1Gb1hEVEk1TURjeE5UQTNNREUxTUZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWxfY05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTUlvCit1TDVvWWFaMUc5UVR2SS9Cd0RNWWNNOTVqVkdNN2E1eWZIQWtBTVptOThWNGdDamNTd0NhOWFEbHJOKzliSUkKK054OXc3L0phdG1mb1ZwYlNoZ3dqc0lBT3QrTXlxQTh1WFVvMXlwSGFBd2NkaXhYbk81ZlVRRTlKN2xDbm9NZApKM0RYNmhNWnVmNmxwRVVBK2JkaVVTYzdkOEFKVDRpSm41bDAyL21pd3hRZUJweWwzQXFGVUZUQTQxUlNLbnBECjBuTzdoaEk3cE02aUVUTVBSa29lZkZrcHFtSEhqd0g5MGZQVzhETGgwdG5ENUlTM0VUUlVjMDdHbms0ZWh1OWMKamNhRXoxK1Roe3JhcWFTRmpTdUVSMGxhU3BYOXQyd3k1S3NxZFVBRkQySGNDK0Y1eUsvTXppL2krS01FdTM0SQpiTHJNUy90L3lta1V5RTQ3T3FrQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZ0QU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFFbXRORFVVZ2FhRWs3Z0lSMnE1MkRxZ21FRkMKZW1xR21rT3U5UERvVmZ0Z0Nva3VBUXEzZzBVdzA3cTJTQnhSZzJDNzQ2Y08wbkdURFZlL2ecXNmeVBFK2RFbApPQWJmQ3R1QUlKTHVQWDB6OVBUdDBKekhobnk1ODk5RE81ODdLMm5VdXd3S01xeEYvdWRHMUNtWUkza2g2cS9RCmN0WDI5RjlhTWhDRXluWU1lVU5EUVQ4cGN0TkOpNm4rYjg0NDlmMU5RWG9kVzM0TVRrL25pd3JuQ3pDUUE5M0wKR3YxelhRL2pwTTZVeVo5dU9ZNWEzK096UjJCTzRtZ2RiL0xoam5rL0hxTGs5RUszZmhJak5Tc2h0RWsrf9NpVApPQ3JTQkRHVk5ueHIzeGRwYzltRWNCeDlQdVBqVE1ER1ZkYlowUlZHbGVVODN3dVpUaTdJdUZaR2Jicz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://apiserver.demo:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernet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lYY0VubGlrM3NTc2FGSUtrdDBNV3gwYVFrdy9jemJhCjJPNGcxRlZraFMreUdWVTUxcW9MQ1FLQmdDOHdwN3BJMjFIOUxTVStuTmUzbkxzUk81U0gra2dPQk9qeFdNRTYKUGErb3pUdEdjZTkza0R0K0NhbzQvaUtLa1hCYmc4SzM2OG5Gd1c2V09SRmVtbjBOSisybWZLQi94UlVrVFlRSgptNFh5OTQzL0ZWak5GYkxxa2wvcEcxbE42T2tmTm5rRCtBaEVvK2tvcWc4cTljSE5TbGhWeCtLMDZDSjJyUlQ3CkROK0JBb0dB63h1R3g5OHc0MENESWlHWmhDWGY0ZTRxTllWdVhTcm9YTDVpeWFKTDZka3BLSE9UUk5VSlBmSVYKZ1FDMy9rN0xYZkFjbTNqTktSS1huQ2cveWxTd2N6RjFGYkZKb1hjNW9ackc5TE8yQXNSdGcxanByN1ZpRlZNYwo0LzB5cEFSTmJqRzA4Y0I5bzBrbVBjUVExUHd4Q05EeXVONVRQYzRKejN3OHR5SnhTN289Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 配置 kubectl 客户端

在客户端电脑完成如下配置,即可远程管理 Kubernetes 集群

执行命令

vi ~/.kube/config
1

将前面获得的 /etc/kubernetes/admin.conf 文件的内容粘贴进该文件并保存

配置 hosts

echo "x.x.x.x    apiserver.demo" >> /etc/hosts
1
  • 将 x.x.x.x 替换成 demo-master-a-1 的实际 IP 地址
  • 将 apiserver.demo 替换成前面获得 /etc/kubernetes/admin.conf 文件中 clusters/cluster/server 中 URL 里 host 对应的部分

验证安装结果

执行命令

kubectl get nodes
kubectl get pods -n kube-system
1
2

# kubectl 参考文档推荐

https://kubernetes.io/docs/reference/kubectl/overview/