metrics-server用于监测node,pod等的CPU,内存使用情况(hpa弹性伸缩依赖metrics-server插件)

1.13.0、创建metrics-server证书和私钥

k8s-01:~ # cd /opt/k8s/ssl/
k8s-01:/opt/k8s/ssl # source /opt/k8s/bin/k8s-env.sh
k8s-01:/opt/k8s/ssl # cat > metrics-server-csr.json <<EOF
{
"CN": "aggregator",
"hosts": [
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "k8s",
"OU": "bandian"
}
]
}
EOF

1.13.1、生成metrics-server证书和私钥

k8s-01:/opt/k8s/ssl # cfssl gencert -ca=/opt/k8s/ssl/ca.pem \
-ca-key=/opt/k8s/ssl/ca-key.pem \
-config=/opt/k8s/ssl/ca-config.json \
-profile=kubernetes metrics-server-csr.json | cfssljson -bare metrics-server

1.13.2、开启kube-apiserver聚合配置

  • 在kube-apiserver.service文件里面,增加如下内容,用来开启聚合(此操作,后面需要重启kube-apiserver组件,建议在部署kube-apiserver的时候,就开启聚合)
--proxy-client-cert-file=/etc/kubernetes/cert/metrics-server.pem \\
--proxy-client-key-file=/etc/kubernetes/cert/metrics-server-key.pem \\
--requestheader-client-ca-file=/etc/kubernetes/cert/ca.pem \\
--requestheader-allowed-names=aggregator \\
--requestheader-extra-headers-prefix="X-Remote-Extra-" \\
--requestheader-group-headers=X-Remote-Group \\
--requestheader-username-headers=X-Remote-User
  • 为了方便,就重新创建kube-apiserver.service文件了(注意自己kube-apiserver的service文件,别直接复制黏贴我的配置文件)
k8s-01:~ # cd /opt/k8s/conf/
k8s-01:/opt/k8s/conf # cat > kube-apiserver.service.template <<EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target [Service]
WorkingDirectory=${K8S_DIR}/kube-apiserver
ExecStart=/opt/k8s/bin/kube-apiserver \\
--v=2 \\
--advertise-address=##NODE_IP## \\
--secure-port=6443 \\
--bind-address=##NODE_IP## \\
--etcd-servers=${ETCD_ENDPOINTS} \\
--allow-privileged=true \\
--service-cluster-ip-range=${SERVICE_CIDR} \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--enable-bootstrap-token-auth=true \\
--token-auth-file=/etc/kubernetes/cert/token.csv \\
--service-node-port-range=${NODE_PORT_RANGE} \\
--kubelet-client-certificate=/etc/kubernetes/cert/kubernetes.pem \\
--kubelet-client-key=/etc/kubernetes/cert/kubernetes-key.pem \\
--tls-cert-file=/etc/kubernetes/cert/kubernetes.pem \\
--tls-private-key-file=/etc/kubernetes/cert/kubernetes-key.pem \\
--client-ca-file=/etc/kubernetes/cert/ca.pem \\
--service-account-key-file=/etc/kubernetes/cert/ca.pem \\
--etcd-cafile=/etc/kubernetes/cert/ca.pem \\
--etcd-certfile=/etc/kubernetes/cert/kubernetes.pem \\
--etcd-keyfile=/etc/kubernetes/cert/kubernetes-key.pem \\
--audit-log-maxage=15 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-truncate-enabled \\
--audit-log-path=${K8S_DIR}/kube-apiserver/audit.log \\
--proxy-client-cert-file=/etc/kubernetes/cert/metrics-server.pem \\
--proxy-client-key-file=/etc/kubernetes/cert/metrics-server-key.pem \\
--requestheader-client-ca-file=/etc/kubernetes/cert/ca.pem \\
--requestheader-allowed-names=aggregator \\
--requestheader-extra-headers-prefix="X-Remote-Extra-" \\
--requestheader-group-headers=X-Remote-Group \\
--requestheader-username-headers=X-Remote-User Restart=on-failure
RestartSec=10
Type=notify
LimitNOFILE=65536 [Install]
WantedBy=multi-user.target
EOF

1.13.3、分发配置文件和秘钥到其他节点

#!/usr/bin/env bash
source /opt/k8s/bin/k8s-env.sh # 替换模板文件
for (( i=0; i < 3; i++ ))
do
sed -e "s/##NODE_IP##/${MASTER_IPS[i]}/" /opt/k8s/conf/kube-apiserver.service.template > \
/opt/k8s/conf/kube-apiserver-${MASTER_IPS[i]}.service
done for host in ${MASTER_IPS[@]}
do
printf "\e[1;34m${host}\e[0m\n"
scp /opt/k8s/ssl/metrics-server*.pem ${host}:/etc/kubernetes/cert/
scp /opt/k8s/conf/kube-apiserver-${host}.service ${host}:/etc/systemd/system/kube-apiserver.service
done

1.13.4、重启所有的kube-apiserver组件

#!/usr/bin/env bash
source /opt/k8s/bin/k8s-env.sh for host in ${MASTER_IPS[@]}
do
printf "\e[1;34m${host}\e[0m\n"
ssh root@${host} "systemctl daemon-reload && \
systemctl restart kube-apiserver && \
systemctl status kube-apiserver | grep Active"
done

1.13.5、下载yaml文件

k8s-01:~ # wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml

1.13.6、配置yaml文件

  • 由于github上面拉取的yaml当中,有许多内容需要修改,因此,下面将修改好的yaml文件放上来了,可以直接使用
k8s-01:~ # vim components.yaml
---
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:aggregated-metrics-reader
labels:
rbac.authorization.k8s.io/aggregate-to-view: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rules:
- apiGroups: ["metrics.k8s.io"]
resources: ["pods", "nodes"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1beta1.metrics.k8s.io
spec:
service:
name: metrics-server
namespace: kube-system
group: metrics.k8s.io
version: v1beta1
insecureSkipTLSVerify: true
groupPriorityMinimum: 100
versionPriority: 100
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: metrics-server
namespace: kube-system
labels:
k8s-app: metrics-server
spec:
selector:
matchLabels:
k8s-app: metrics-server
template:
metadata:
name: metrics-server
labels:
k8s-app: metrics-server
spec:
serviceAccountName: metrics-server
volumes:
# mount in tmp so we can safely use from-scratch images and/or read-only containers
- name: tmp-dir
emptyDir: {}
containers:
- name: metrics-server
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
imagePullPolicy: IfNotPresent
args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
ports:
- name: main-port
containerPort: 4443
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- name: tmp-dir
mountPath: /tmp
nodeSelector:
kubernetes.io/os: linux
kubernetes.io/arch: "amd64"
---
apiVersion: v1
kind: Service
metadata:
name: metrics-server
namespace: kube-system
labels:
kubernetes.io/name: "Metrics-server"
kubernetes.io/cluster-service: "true"
spec:
selector:
k8s-app: metrics-server
ports:
- port: 443
protocol: TCP
targetPort: main-port
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
- configmaps
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
k8s-01:~ # kubectl apply -f components.yaml

1.13.7、验证metrics-server功能

  • metrics-server启动会比较慢,耐心等待1-3分钟,出现如下效果,则成功
  • 若没有出现,则使用 kubectl logs -n kube-system metrics-server-xxxx 查看日志
k8s-01:~ # kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
192.168.72.55 129m 6% 2232Mi 58%
192.168.72.56 119m 5% 1555Mi 40%
192.168.72.57 114m 5% 1425Mi 37%
192.168.72.58 31m 1% 711Mi 18%
192.168.72.59 28m 1% 733Mi 19%
k8s-01:~ # kubectl top pod -A
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system coredns-689d7d9f49-s2qjn 2m 13Mi
kube-system coredns-689d7d9f49-vc9k4 3m 17Mi
kube-system metrics-server-666566b66d-jfl7v 2m 12Mi

suse 12 二进制部署 Kubernetets 1.19.7 - 第13章 - 部署metrics-server插件的更多相关文章

  1. suse 12 二进制部署 Kubernetets 1.19.7 - 第02章 - 部署etcd集群

    文章目录 1.2.部署etcd集群 1.2.0.下载etcd二进制文件 1.2.1.创建etcd证书和私钥 1.2.2.生成etcd证书和私钥 1.2.3.配置etcd为systemctl管理 1.2 ...

  2. suse 12 二进制部署 Kubernetets 1.19.7 - 第03章 - 部署flannel插件

    文章目录 1.3.部署flannel网络 1.3.0.下载flannel二进制文件 1.3.1.创建flannel证书和私钥 1.3.2.生成flannel证书和私钥 1.3.3.将pod网段写入et ...

  3. suse 12 二进制部署 Kubernetets 1.19.7 - 第04章 - 部署docker服务

    文章目录 1.4.部署docker 1.4.0.下载docker二进制文件 1.4.1.配置docker镜像加速 1.4.2.配置docker为systemctl管理 1.4.3.启动docker服务 ...

  4. suse 12 二进制部署 Kubernetets 1.19.7 - 第05章 - 部署kube-nginx

    文章目录 1.5.部署kube-nginx 1.5.0.下载nginx二进制文件 1.5.1.编译部署nginx 1.5.2.配置nginx.conf 1.5.3.配置nginx为systemctl管 ...

  5. suse 12 二进制部署 Kubernetets 1.19.7 - 第06章 - 部署kube-apiserver组件

    文章目录 1.6.部署kube-apiserver 1.6.0.创建kubernetes证书和私钥 1.6.1.生成kubernetes证书和私钥 1.6.2.创建metrics-server证书和私 ...

  6. suse 12 二进制部署 Kubernetets 1.19.7 - 第07章 - 部署kube-controller-manager组件

    文章目录 1.7.部署kube-controller-manager 1.7.0.创建kube-controller-manager请求证书 1.7.1.生成kube-controller-manag ...

  7. suse 12 二进制部署 Kubernetets 1.19.7 - 第08章 - 部署kube-scheduler组件

    文章目录 1.8.部署kube-scheduler 1.8.0.创建kube-scheduler请求证书 1.8.1.生成kube-scheduler证书和私钥 1.8.2.创建kube-schedu ...

  8. suse 12 二进制部署 Kubernetets 1.19.7 - 第09章 - 部署kubelet组件

    文章目录 1.9.部署kubelet 1.9.0.创建kubelet bootstrap kubeconfig文件 1.9.1.创建kubelet配置文件 1.9.2.配置kubelet为system ...

  9. suse 12 二进制部署 Kubernetets 1.19.7 - 第10章 - 部署kube-proxy组件

    文章目录 1.10.部署kube-proxy 1.10.0.创建kube-proxy证书 1.10.1.生成kube-proxy证书和秘钥 1.10.2.创建kube-proxy的kubeconfig ...

随机推荐

  1. linux光标命令快捷键(常用)

    Ctrl+a 移动到首行 Ctrl+e 移动到尾行 Ctrl+u 将当前光标后面的内容全部删除 (剪辑) ctrl+k 将当前光标前面的内容全部删除 (剪辑) Ctrl+→(左右同理) 移动到下个空格 ...

  2. 在实验中观察指针——C++ 函数参数的压栈顺序

    前言 好久没写东西了,突发奇想,写写函数参数的压栈顺序 先看看这个问题 https://q.cnblogs.com/q/137133/ 然后看我简化的代码,猜输出结果是多少? #include< ...

  3. 读《疯狂Java讲义》笔记总结一

    最近在读<疯狂Java讲义>,现把其中遇到的一些自己以前没掌握的点儿记录下来. 1.字符串相关 字符串不是基本类型,字符串是一个类,也就是一个引用类型. 字符串转int类型String a ...

  4. java日志打印使用指南

    一.简介 日志打印是java代码开发中不可缺少的重要一步. 日志可以排查问题,可以搜集数据 二.常用日志框架 比较常用的日志框架就是logback, 一些老项目会使用log4j,他们用的都是slf4j ...

  5. 问题记录——BigDecimal保留两位小数及格式化成百分比

    1.函数总结 BigDecimal.setScale()方法用于格式化小数点 setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOW ...

  6. 【刷题-PAT】A1119 Pre- and Post-order Traversals (30 分)

    1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...

  7. 基于Centos7.X的CS:GO社区服搭建

    基于Centos7.X的CS:GO私人服务器搭建 由于比完了赛,在学校太过无聊,便想搭建一个CSGO社区服务器,方便舍友同学进来游玩,顺便帮助一些有想法的人,让他们少走一点弯路 一.创建新用户,并下载 ...

  8. 测试开发实战[提测平台]19-Echarts图表在项目的应用

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 在图表统计展示方面,笔者目前使用过的两种开源,分别是 Echats 和 G2Plot 组件,从个人使用上来讲前者应用更广.自定义开发更灵活 ...

  9. 一个简单的javaweb项目模板

    Controller包:表现层(视图)层.用来显示数据和接收用户数据 Service包:业务逻辑层,用来处理页面.先写接口,后写实现类 Dao包:持久层(数据访问层).用来操作数据库 其中Dao包处于 ...

  10. Docker常用命令速查

    docker pull ${CONTAINER NAME} #拉取镜像 docker images #查看本地所有镜像 docker ps #查看所有正在运行的容器,加-q返回id docker ps ...