在客户端电脑使用 kubectl 远程管理 Kubernetes

日常工作中,可能需要在自己的笔记本电脑上执行 kubectl 命令以管理远程 Linux 服务器上的 Kubernetes 集群。通过调用 kubernetes API 来实现对 Kubernetes 对象的操作.

在客户端电脑安装 kubectl

Linux

# 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

# yum install -y kubectl
# kubectl version

Windows

从下面的链接下载 kubectl 可执行文件
https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/windows/amd64/kubectl.exe

请将其中的 v1.16.2 替换为最新的版本号
通过此链接可获取最新的版本号 https://storage.googleapis.com/kubernetes-release/release/stable.txt

将下载的可执行文件添加到 PATH 环境变量

执行命令查看已安装的 kubectl 版本号
kubectl version

MAC

# 下载最新的可执行文件
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
# 添加可执行权限
chmod +x ./kubectl
# 复制到 PATH 路径
sudo mv ./kubectl /usr/local/bin/kubectl
# 检查已安装版本
kubectl version

获取 kubectl config 文件

在master节点上执行如下命令查看:

# cat /etc/kubernetes/admin.conf   (或者这个路径:~/.kube/config) (或者直接使用命令查看:kubectl config view --raw)
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01EVXlOekExTXpJeE1Gb1hEVE13TURVeU5UQTFNekl4TUZvd0ZURVRNQkVHQ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XpFWG80VmNEdjFUZzhSR3RKcnE4UGloQU1TUTcwT0ZHbUh6KzU1MXh1T1p0OVdVZk9wQ3RSYzJ3az0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://apiserver.master:6443 # 注意这个地址,master节点上的api地址
  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: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJQ2tycEFwb202d0F3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TURBMU1qY3dOVE15TVRCYUZ3MHlNVEExTWpjd05UTXlNVEphTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXR1aWxHd2xZZU9sNzRpTjMKVzl1eXFKK2F4NzVUeWNIUnNRR0ZMUnB3YWRvTktmQmpuQS9DZjFIVkYyVmxjVGVLWHNQTWp3ZzdoT1E2b25vTgpxOXhWK0J0RUYyVFgwOFRxRU5aRERFblcxOFJZU2dqMkxuem5WUUhkbWp4Y3JQbzlIa0N4RWpsWXFzbWxSYUdJCm53Yjd2Q2hLbGlPU1lxaHUwZ0x6bW9yNGNhdWFCbFdLV1FCbzNnQ0pPd0V4Y3JoT3JxYS80Zk1vSThmd1ppYTgKVnpTQ0xPK3N3eUphdmtQRFJpYWE1dmlXektXOFE1ODh2RjBuekNsdEh1SGlSdjczOVowVUpoYlQrVnN0bWZKagpUVVpQaks1S21teDd0SkJlMVIwSW9nejVTaTJTcE5OdmpBbUlrcX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nCkJ1RHIvWmV3Z3VKY3VKL05ZNnNpK1kzMHVJcm56UnFjN21LRmx3RUNnWUIvT3FmUmFOdE1yTm9QMGEwSHB3bm4KTk12dmVxMFNJLzVhWTJzOHBpVjA5V2l1RUFKOEVKTXl1NWYyNjdkazQ5ekkxRjhybFNQK1NPSTk3WnVvRnVzaAppY0FUUzd4a2NoVitMVzh1OGJjd3Zxb0NZK0QzcHVmTHFlK1lGNm1qK0pQSG9JZ0Jucll5L0ZNN3dWMFpvUFlBCkNndnlpUzVBOWl3ZFdWeTJpNHRtcndLQmdRREl1d1NuNURsZEdLNUV2Q0RsTjhLdkt6S2tIeUJhdVdadFBYUlcKcHZqNStCN0JLRWE2QXRjVjhNSnc2N2QzNE9aa0lnaGdpTjlQR1NEcmZGa2toZ3Vpa1A4K2VZNzQvdk9rdmZoZApxeXJ5b21iOXVhVVJMWXI4bnpMU05jUytEejAzU3Z6OXZLT3JyY1Q4SFZCSzduU1BpZW9uVTR1TnlLbU9pNlpXCkJZc2hBUUtCZ0VIZzlrTzJtV2J4OFR2alhnNHljU0MrTXZMYjZTSTJGWDRrSkJxdVFYemJKSUlzdjNkaHk5QnkKbHRHTE8zdWQ0a096UEFGVFQ3UnJhWlZRS0NTVmpiRFRPNzYrQ0E0RGQxZWFYeGFhK0I2UzBCUk0xdGZ0OGhOeQo4cVRKcCtpK1hKWmQ0VDUxcS9LcXpZdTJLVzRJME9XYW50dzc3T0JmTWJMQVJPemRSWmJtCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

配置 kubectl 客户端

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

Linux

# 执行命令
vi ~/.kube/config
将前面获得的 /etc/kubernetes/admin.conf 文件的内容粘贴进该文件并保存

# 配置 hosts
echo "x.x.x.x    apiserver.demo" >> /etc/hosts

将 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

Windows

用记事本(或其他文本编辑器)创建文件 ~/.kube/config,其中 ~ 代表当前的用户目录
将前面获得的 /etc/kubernetes/admin.conf 文件的内容粘贴进该文件并保存

用记事本打开 C:\windows\System32\drivers\etc\hosts 文件(需要管理员权限),在该文件末尾添加一行记录:
x.x.x.x apiserver.demo

将 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

MAC

# 执行命令
vi ~/.kube/config
将前面获得的 /etc/kubernetes/admin.conf 文件的内容粘贴进该文件并保存

# 配置 hosts
echo "x.x.x.x    apiserver.demo" >> /etc/hosts

将 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
posted @ 2020-10-23 17:26  哈喽哈喽111111  阅读(1175)  评论(0编辑  收藏  举报