一、Kubectl CLI 简介

Kubectl 是Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。

官方网站:https://kubernetes.io/docs/tasks/tools/install-kubectl

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

二、Kubectl CLI 安装

1、使用脚本安装 kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && chmod +x kubectl && cp kubectl /usr/bin/
# =====================================国内备用 ===========================================
curl -fL "https://kinshy-generic.pkg.coding.net/south/study/kubectl?version=latest" -o kubectl && chmod +x kubectl && cp kubectl /usr/bin/


# 下载特定版本 kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/bin/kubectl

sudo wget https://storage.googleapis.com/kubernetes-release/release/v1.15.2/bin/linux/amd64/kubectl
# 制作kubectl二进制可执行文件
sudo chmod +x ./kubectl
# 将二进制文件移动到PATH中
sudo mv ./kubectl /usr/local/bin/kubectl
# 测试以确保您安装的版本是最新的
kubectl version
2、使用本机包管理器安装 kubectl(推荐)

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

# 配置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
# 安装最新版本 kubectl
yum install -y kubectl
# 测试以确保您安装的版本是最新的
kubectl version

三、Kubectl CLI 配置

为了让 kubectl 能够查找和访问Kubernetes集群,它需要一个kubeconfig文件,该文件是在使用kube-up.sh或成功部署Minikube集群创建集群时自动创建的。默认情况下,kubectl配置位于~/.kube/config。

1、获取 kubectl config 文件

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

cat ~/.kube/config

输出内容如下所示:

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: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQxxRxZ0F3SUJBZ0lJY0JpMFhjUWJNWm93RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBM01UZ3dOekF4TlRCYUZ3MHlNREEzTVRjd056QXhOVEZhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQW9CYzZvMHRCekIyRUlldW4KcmhONTVoaU1EaEplcUpxMWxlT3VUSjRic0dqVnlrQVJQVCtGZDlTR3llRjN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0I5bzBrbVBjUVExUHd4Q05EeXVONVRQYzRKejN3OHR5SnhTN289Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

如果是通过Rancher 安装的K8s集群,可在界面看到该配置:

2、配置客户端 kubectl config 文件

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

执行命令

vi ~/.kube/config

将 kubectl config 文件的内容粘贴进该文件并保存

配置 hosts

echo "x.x.x.x  apiserver.k8s" >> /etc/hosts
  • 将 x.x.x.x 替换成 k8s-master 的实际 IP 地址
  • 将 apiserver.k8s 替换成 kubectl config 文件中 clusters/cluster/server 中 URL 里 host 对应的部分

验证安装结果

执行命令

kubectl get nodes
kubectl get pods -n kube-system

参考资料

http://docs.kubernetes.org.cn/61.html
https://kuboard.cn/install/install-kubectl.html
https://kubernetes.io/docs/reference/kubectl/overview/
https://kubernetes.io/docs/tasks/tools/install-kubectl/

作者:Jeebiz  创建时间:2019-08-14 21:27
 更新时间:2024-08-02 14:28