K8S metrics server监控
1、创建aggregator证书
方法一:直接使用二进制源码包安装
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$ wget https: //pkg .cfssl.org /R1 .2 /cfssl_linux-amd64 $ chmod +x cfssl_linux-amd64 $ mv cfssl_linux-amd64 /usr/local/bin/cfssl $ wget https: //pkg .cfssl.org /R1 .2 /cfssljson_linux-amd64 $ chmod +x cfssljson_linux-amd64 $ mv cfssljson_linux-amd64 /usr/local/bin/cfssljson $ wget https: //pkg .cfssl.org /R1 .2 /cfssl-certinfo_linux-amd64 $ chmod +x cfssl-certinfo_linux-amd64 $ mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo $ export PATH= /usr/local/bin :$PATH |
方式二:使用go命令安装
1
2
3
|
$ go get -u github.com /cloudflare/cfssl/cmd/ ... $ ls $GOPATH /bin/cfssl * cfssl cfssl-bundle cfssl-certinfo cfssljson cfssl-newkey cfssl-scan |
2、创建 CA (Certificate Authority)
创建 CA 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
$ mkdir /root/ssl $ cd /root/ssl $ cfssl print-defaults config > config.json $ cfssl print-defaults csr > csr.json # 根据config.json文件的格式创建如下的ca-config.json文件 # 过期时间设置成了 87600h $ cat > aggregator-ca-config.json <<EOF { "signing" : { "default" : { "expiry" : "87600h" }, "profiles" : { "aggregator" : { "usages" : [ "signing" , "key encipherment" , "server auth" , "client auth" ], "expiry" : "87600h" } } } } EOF |
字段说明:
profiles
: 可以定义多个 profiles,分别指定不同的过期时间、使用场景等参数;后续在签名证书时使用某个 profile。signing
:表示该证书可用于签名其它证书;生成的 aggregator-ca.pem 证书中CA=TRUE
。server auth
:表示 Client 可以用该 CA 对 Server 提供的证书进行验证。client auth
:表示 Server 可以用该 CA 对 Client 提供的证书进行验证。
创建 CA 证书签名请求
创建 aggregator-ca-csr.json
文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
{ "CN" : "aggregator" , "key" : { "algo" : "rsa" , "size" : 2048 }, "names" : [ { "C" : "CN" , "ST" : "Shanghai" , "L" : "Shanghai" , "O" : "k8s" , "OU" : "System" } ], "ca" : { "expiry" : "87600h" } } |
字段说明:
- “CN” :
Common Name
,kube-apiserver 从证书中提取该字段作为请求的用户名 (User Name);浏览器使用该字段验证网站是否合法。 - “O” :
Organization
,kube-apiserver 从证书中提取该字段作为请求用户所属的组 (Group);
生成 CA 证书和私钥
1
2
3
|
$ cfssl gencert -initca aggregator-ca-csr.json | cfssljson -bare aggregator-ca $ ls aggregator-ca* aggregator-ca-config.json aggregator-ca.csr aggregator-ca-csr.json aggregator-ca-key.pem |
3、创建 kubernetes 证书
创建 aggregator 证书签名请求文件 aggregator-csr.json
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
{ "CN" : "aggregator" , "hosts" : [ "127.0.0.1" , "192.168.123.250" , "192.168.123.248" , "192.168.123.249" , "10.254.0.1" , "kubernetes" , "kubernetes.default" , "kubernetes.default.svc" , "kubernetes.default.svc.cluster" , "kubernetes.default.svc.cluster.local" ], "key" : { "algo" : "rsa" , "size" : 2048 }, "names" : [ { "C" : "CN" , "ST" : "Shanghai" , "L" : "Shanghai" , "O" : "k8s" , "OU" : "System" } ] } |
- 如果 hosts 字段不为空则需要指定授权使用该证书的 IP 或域名列表,由于该证书后续被 etcd 集群和 kubernetes master 集群使用,所以上面分别指定了
etcd
集群、kubernetes master
集群的主机 IP 和 kubernetes 服务的服务 IP(一般是 kube-apiserver 指定的service-cluster-ip-range
网段的第一个 IP,如 10.254.0.1)。 - 以上物理节点的 IP 也可以更换为主机名。
生成 aggregator 证书和私钥
1
2
3
|
$ cfssl gencert -ca=aggregator-ca.pem -ca-key=aggregator-ca-key.pem -config=aggregator-ca-config.json -profile=aggregator aggregator-csr.json | cfssljson -bare aggregator $ ls aggregator* aggregator.csr aggregator-csr.json aggregator-key.pem aggregator.pem |
4、分发证书
将生成的证书和秘钥文件(后缀名为.pem)拷贝到 Master 节点的 /etc/kubernetes/ssl
目录下备用。
1
|
cp *.pem /etc/kubernetes/ssl |
5、开启聚合层 API
kube-apiserver
增加以下配置:
1
2
3
4
5
6
7
|
--requestheader-client-ca- file = /etc/kubernetes/ssl/aggregator-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/aggregator .pem --proxy-client-key- file = /etc/kubernetes/ssl/aggregator-key .pem |
注意:前面创建的证书的 CN
字段的值必须和参数 --requestheader-allowed-names
指定的值 aggregator
相同。
重启 kube-apiserver:
1
2
|
$ systemctl daemon-reload $ systemctl restart kube-apiserver |
如果 kube-proxy
没有在 Master 上面运行,kube-proxy
还需要添加配置:
1
|
-- enable -aggregator-routing= true |
6、部署metrics server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
git clone https: //github .com /kubernetes-incubator/metrics-server $ cd metrics-server $ cat deploy /1 .8+ /metrics-server-deployment .yaml --- apiVersion: v1 kind: ServiceAccount metadata: name: metrics-server namespace: kube-system --- apiVersion: extensions /v1beta1 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: k8s.gcr.io /metrics-server-amd64 :v0.3.2 command : - /metrics-server - --kubelet-preferred-address-types=InternalIP - --kubelet-insecure-tls imagePullPolicy: IfNotPresent volumeMounts: - name: tmp- dir mountPath: /tmp $ kubectl create -f deploy /1 .8+/ |
注意:这里我修改了metrics-server的启动命令,增加了--kubelet-preferred-address-types=InternalIP和--kubelet-insecure-tls参数,否则metrics server可能会从kubelet拿不到监控数据。具体报错可以通过kubectl log metrics-server-5687578d67-tx8m4 -n kube-system命令查看
7、验证metrics server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@k8s-10-21-17-56 1.8+] # kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes [root@k8s-10-21-17-56 1.8+] # kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-10-21-17-41 136m 13% 2131Mi 93% k8s-10-21-17-42 167m 2% 8904Mi 28% k8s-10-21-17-43 978m 13% 17733Mi 57% k8s-10-21-17-56 707m 17% 16621Mi 51% k8s-10-21-17-57 320m 8% 12478Mi 38% k8s-10-21-17-58 442m 11% 13087Mi 40% k8s-10-21-17-59 242m 8% 13838Mi 45% [root@k8s-10-21-17-56 1.8+] # kubectl top pod NAME CPU(cores) MEMORY(bytes) eager-alpaca-zookeeper-0 6m 780Mi eager-alpaca-zookeeper-1 5m 755Mi eager-alpaca-zookeeper-2 7m 793Mi filled-scorpion-minio-96595c48-bfwrd 1m 10Mi filled-scorpion-redis-master-0 5m 28Mi filled-scorpion-spinnake-halyard-0 1m 1365Mi idolized-wallaby-nfs-client-provisioner-5dbcfc8c9-8kpwk 2m 11Mi jaundiced-possum-gitlab-runner-64dcdccc4c-k5927 4m 7Mi nginx-deployment-586f5f95f7-dvmw7 0m 1Mi nginx-deployment-586f5f95f7-hpw5n 0m 2Mi prometheus-operator-6c8d8456cd-ccfwx 2m 24Mi prometheus-sample-metrics-prom-0 1m 30Mi sample-metrics-app-5f67fcbc57-9ghxt 1m 9Mi sample-metrics-app-5f67fcbc57-t9pzn 1m 9Mi |