简介
kubectl是一个工具,类似于etcdctl一样,不是必须安装的工具。各节点和管理节点的通讯都是通过api-server进行的。api-server可以部署在任意的节点上,kube-proxy也是一样的,都是独立的组件。
kubectl top 可以很方便地查看node、pod的实时资源使用情况:如CPU、内存。
实现原理
kubectl top 、 k8s dashboard 以及 HPA 等调度组件使用的数据是一样,数据链路如下:
使用 heapster 时:apiserver 会直接将metric请求通过 proxy 的方式转发给集群内的 hepaster 服务。
而使用 metrics-server 时:apiserver是通过/apis/metrics.k8s.io/的地址访问metric
这里可以对比下kubect get pod时的日志:
1.下载yaml 文件
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/auth-delegator.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/metrics-server-service.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/auth-reader.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/metrics-apiservice.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/metrics-server-deployment.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/aggregated-metrics-reader.yaml
wget http://pencil-file.oss-cn-hangzhou.aliyuncs.com/blog/resource-reader.yaml
2. 修改metrics-server-deployment.yaml文件
核心配置:
containers:
- name: metrics-server
image: k8s.gcr.io/metrics-server-amd64:v0.3.2
imagePullPolicy: IfNotPresent
#修改为本地有镜像优先使用
command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
#容器的预设值 脚本
volumeMounts:
- name: tmp-dir
mountPath: /tmp
3.拉取镜像
docker pull k8s.gcr.io/metrics-server-amd64
由于该镜像在国外所以可能使用其他途径 代理上网或者本地导入
代理上网法
mkdir -p /etc/systemd/system/docker.service.d
#创建放代理的文件夹,默认没有
echo '[Service]
Environment="HTTP_PROXY=192.168.0.26:8118" "HTTPS_PROXY=192.168.0.26:8118"' >/etc/systemd/system/docker.service.d/http-proxy.conf
#创建代理的配置件,前提得有能访问到国外镜像服务器的代理服务器
systemctl daemon-reload
systemctl restart docker
#重启docker
以上脚本运行一下 就能pull 到国外镜像了
本地导出导入法
先到拉取完毕的主机上导出
docker save -o metrics-server-amd64:v0.3.2.tar k8s.gcr.io/metrics-server-amd64:v0.3.2
#.tar 为完成的打包文件,后面的是需要导出的镜像名需要带版本名,镜像名用docker images 查看
导入
docker load <metrics-server-amd64\:v0.3.2.tar
4.应用
kubectl apply -f ./
#应用下载的所有yaml文件
kubectl get pod -n kube-system
#查看一下pod 是否正常