k8s安装之heapster与metric

heapster
此模块,在1.8版本以后由metricserver替代

wget https://github.com/kubernetes/heapster/archive/v1.5.4.tar.gz
tar -zxf v1.5.4.tar.gz
cd heapster-1.5.4/
kubectl create -f deploy/kube-config/rbac/heapster-rbac.yaml ##授权
kubectl create -f deploy/kube-config/standalone/heapster-controller.yaml

#####说明问题点

kubectl get pod --namespace=kube-system
kubectl logs -f pods/heapster-75c97b8594-qgs5v -n kube-system ##查看日志信息
1 reflector.go:190] k8s.io/heapster/metrics/util/util.go:30: Failed to list *v1.Node: nodes is forbidden: User "system:serviceaccount:kube-system:heapster" cannot list nodes at the cluster scope
kubectl create -f deploy/kube-config/rbac/heapster-rbac.yaml
kubectl delete -f deploy/kube-config/standalone/heapster-controller.yaml
metrics-server
metrics-server 替换heapster

生成证书:
cat > metrics-server-csr.json <<EOF
{
  "CN": "aggregator",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "4Paradigm"
    }
  ]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes metrics-server-csr.json | cfssljson -bare metrics-server

kube-apiserver增加配置,打开相关配置

        --requestheader-allowed-names=aggregator \
        --requestheader-client-ca-file=/etc/kubernetes/ssl/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 \
        --proxy-client-cert-file=/etc/kubernetes/ssl/metrics-server.pem \
        --proxy-client-key-file=/etc/kubernetes/ssl/metrics-server-key.pem \
        --enable-aggregator-routing=true \

–advertise-address:apiserver 对外通告的 IP(kubernetes 服务后端节点 IP);
–default--toleration-seconds:设置节点异常相关的阈值;
–max-
-requests-inflight:请求相关的最大阈值;
–etcd-:访问 etcd 的证书和 etcd 服务器地址;
–experimental-encryption-provider-config:指定用于加密 etcd 中 secret 的配置;
–bind-address: https 监听的 IP,不能为 127.0.0.1,否则外界不能访问它的安全端口 6443;
–secret-port:https 监听端口;
–insecure-port=0:关闭监听 http 非安全端口(8080);
–tls-
-file:指定 apiserver 使用的证书、私钥和 CA 文件;
–audit-:配置审计策略和审计日志文件相关的参数;
–client-ca-file:验证 client (kue-controller-manager、kube-scheduler、kubelet、kube-proxy 等)请求所带的证书;
–enable-bootstrap-token-auth:启用 kubelet bootstrap 的 token 认证;
–requestheader-
:kube-apiserver 的 aggregator layer 相关的配置参数,proxy-client & HPA 需要使用;
–requestheader-client-ca-file:用于签名 –proxy-client-cert-file 和 –proxy-client-key-file 指定的证书;在启用了 metric aggregator 时使用;
–requestheader-allowed-names:不能为空,值为逗号分割的 –proxy-client-cert-file 证书的 CN 名称,这里设置为 “aggregator”;
–service-account-key-file:签名 ServiceAccount Token 的公钥文件,kube-controller-manager 的 –service-account-private-key-file 指定私钥文件,两者配对使用;
–runtime-config=api/all=true: 启用所有版本的 APIs,如 autoscaling/v2alpha1;
–authorization-mode=Node,RBAC、–anonymous-auth=false: 开启 Node 和 RBAC 授权模式,拒绝未授权的请求;
–enable-admission-plugins:启用一些默认关闭的 plugins;
–allow-privileged:运行执行 privileged 权限的容器;
–apiserver-count=3:指定 apiserver 实例的数量;
–event-ttl:指定 events 的保存时间;
–kubelet-:如果指定,则使用 https 访问 kubelet APIs;需要为证书对应的用户(上面 kubernetes.pem 证书的用户为 kubernetes) 用户定义 RBAC 规则,否则访问 kubelet API 时提示未授权;
–proxy-client-*:apiserver 访问 metrics-server 使用的证书;
–service-cluster-ip-range: 指定 Service Cluster IP 地址段;
–service-node-port-range: 指定 NodePort 的端口范围;
kube-apiserver 的 –requestheader-allowed-names 参数需要与metric证书CN字段一致,否则后续访问 metrics 时会提示权限不足。
如果 kube-apiserver 机器没有运行 kube-proxy,则还需要添加 –enable-aggregator-routing=true 参数;
关于 –requestheader-XXX 相关参数,参考:

https://github.com/kubernetes-incubator/apiserver-builder/blob/master/docs/concepts
https://docs.bitnami.com/kubernetes/how-to/configure-autoscaling-custom-metrics/
注意:

requestheader-client-ca-file 指定的 CA 证书,必须具有 client auth and server auth;  
如果 --requestheader-allowed-names 为空,或者 --proxy-client-cert-file 证书的 CN 名称不在 allowed-names 中,则后续查看 node 或 pods 的 metrics 失败,提示:  
###### Error from server (Forbidden): nodes.metrics.k8s.io is forbidden..

kube-controller-manager增加如下参数
--horizontal-pod-autoscaler-use-rest-clients=true

部署:
git clone https://github.com/kubernetes-incubator/metrics-server
cd metrics-server/deploy/1.8+/
修改metrics-server-deployment.yaml
        imagePullPolicy: Always
        command:
        - /metrics-server       
        - --metric-resolution=30s
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP

# cat resource-reader.yaml
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: system:metrics-server
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - nodes
  - nodes/stats
  - namespaces
  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: cluster-admin
subjects:
- kind: ServiceAccount
  name: metrics-server
  namespace: kube-system

kubectl apply -f .

参考链接:
https://blog.51cto.com/blief/2416018
https://www.orchome.com/1203
https://www.jianshu.com/p/a89e1ee3d45e
https://blog.51cto.com/ylw6006/2114338
https://www.jianshu.com/p/d017fc44959e
    原文作者:zmoke
    原文地址: https://blog.51cto.com/phospherus/2445753
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞