查看 /etc/resolv.conf 是否包含 kubelet 配置的 --cluster-dns 和 --cluster-domain,是否能够将服务 my-nginx 解析到上面显示的 Cluster IP 10.254.191.237
cat > pod-nginx.yaml <
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF
[k8s@kube-server ~]$ kubectl exec -it nginx -c nginx /bin/bash
root@nginx:/# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
14: eth0@if15: mtu 1450 qdisc noqueue state UP
link/ether 02:42:ac:1e:30:04 brd ff:ff:ff:ff:ff:ff
inet 172.30.48.4/24 brd 172.30.48.255 scope global eth0
valid_lft forever preferred_lft forever
root@nginx:/# ping kubernetes
PING kubernetes.default.svc.cluster.local (10.254.0.1): 48 data bytes
56 bytes from 10.254.0.1: icmp_seq=0 ttl=64 time=0.050 ms
56 bytes from 10.254.0.1: icmp_seq=1 ttl=64 time=0.076 ms
56 bytes from 10.254.0.1: icmp_seq=2 ttl=64 time=0.143 ms
56 bytes from 10.254.0.1: icmp_seq=3 ttl=64 time=0.079 ms
^C--- kubernetes.default.svc.cluster.local ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.050/0.087/0.143/0.034 ms
root@nginx:/# ping my-nginx
PING my-nginx.default.svc.cluster.local (10.254.191.237): 48 data bytes
56 bytes from 10.254.191.237: icmp_seq=0 ttl=64 time=0.094 ms
56 bytes from 10.254.191.237: icmp_seq=1 ttl=64 time=0.113 ms
^C--- my-nginx.default.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.094/0.104/0.113/0.000 ms
root@nginx:/# ping coredns
ping: unknown host
root@nginx:/# ping coredns.kube-system.svc.cluster.local
PING coredns.kube-system.svc.cluster.local (10.254.0.2): 48 data bytes
56 bytes from 10.254.0.2: icmp_seq=0 ttl=64 time=0.042 ms
56 bytes from 10.254.0.2: icmp_seq=1 ttl=64 time=0.095 ms
^C--- coredns.kube-system.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.042/0.069/0.095/0.027 ms
root@nginx:/#
root@nginx:/# cat /etc/resolv.conf
nameserver 10.254.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5
root@nginx:/#
9.2 部署 dashboard 插件
修改配置文件
将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 kubernetes-src.tar.gz 文件。
dashboard 对应的目录是:cluster/addons/dashboard。
$ pwd
/opt/k8s/kubernetes/cluster/addons/dashboard
$ cp dashboard-controller.yaml{,.orig}
$ diff dashboard-controller.yaml{,.orig}
33c33
< image: siriuszg/kubernetes-dashboard-amd64:v1.8.3
---
> image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
$ cp dashboard-service.yaml{,.orig}
$ diff dashboard-service.yaml.orig dashboard-service.yaml
10a11
> type: NodePort
指定端口类型为 NodePort,这样外界可以通过地址 nodeIP:nodePort 访问 dashboard。
更换了一个容器镜像的下载地址。
执行所有定义文件
[k8s@kube-server dashboard]$ ls *.yaml
dashboard-configmap.yaml dashboard-controller.yaml dashboard-rbac.yaml dashboard-secret.yaml dashboard-service.yaml
[k8s@kube-server dashboard]$ kubectl create -f .
configmap "kubernetes-dashboard-settings" created
serviceaccount "kubernetes-dashboard" created
deployment.apps "kubernetes-dashboard" created
role.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
rolebinding.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
secret "kubernetes-dashboard-certs" created
secret "kubernetes-dashboard-key-holder" created
service "kubernetes-dashboard" created
[k8s@kube-server dashboard]$
查看分配的 NodePort
[k8s@kube-server dashboard]$ kubectl get deployment kubernetes-dashboard -n kube-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-dashboard 1 1 1 1 50s
[k8s@kube-server dashboard]$ kubectl --namespace kube-system get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
coredns-77c989547b-bq6ff 1/1 Running 558 1d 172.30.49.3 kube-node1
coredns-77c989547b-m8qhw 1/1 Running 556 1d 172.30.48.3 kube-node3
kubernetes-dashboard-65f7b4f486-j659c 1/1 Running 0 7m 172.30.7.2 kube-node2
[k8s@kube-server dashboard]$ kubectl get services kubernetes-dashboard -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.254.56.169 443:8645/TCP 8m
NodePort 8645 映射到 dashboard pod 443 端口
dashboard 的 --authentication-mode 支持 token、basic,默认为 token。如果使用 basic,则 kube-apiserver 必须配置 '--authorization-mode=ABAC' 和 '--basic-auth-file' 参数。
查看 dashboard 支持的命令行参数
kubectl exec --namespace kube-system -it kubernetes-dashboard-65f7b4f486-j659c -- /dashboard --help
访问 dashboard
为了集群安全,从 1.7 开始,dashboard 只允许通过 https 访问,如果使用 kube proxy 则必须监听 localhost 或 127.0.0.1,对于 NodePort 没有这个限制,但是仅建议在开发环境中使用。
对于不满足这些条件的登录访问,在登录成功后浏览器不跳转,始终停在登录界面。
1. kubernetes-dashboard 服务暴露了 NodePort,可以使用 http://NodeIP:NodePort 地址访问 dashboard;
2. 通过 kube-apiserver 访问 dashboard;
3. 通过 kubectl proxy 访问 dashboard:
如果使用了 VirtualBox,需要启用 VirtualBox 的 ForworadPort 功能将虚机监听的端口和 Host 的本地端口绑定。
通过 kubectl proxy 访问 dashboard
启动代理:
[k8s@kube-node2 ~]$ kubectl proxy --address='localhost' --port=8086 --accept-hosts='^*$'
Starting to serve on 127.0.0.1:8086
- --address 必须为 localhost 或 127.0.0.1;
- 需要指定 --accept-hosts 选项,否则浏览器访问 dashboard 页面时提示 “Unauthorized”;
浏览器访问 URL:http://127.0.0.1:8086/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
注:上面的方式最终没有调试通,不知是端口转发哪里没设置正确。
通过 kube-apiserver 访问 dashboard
获取集群服务地址列表:
[root@kube-server ~]# kubectl cluster-info
Kubernetes master is running at https://172.16.10.100:6443
CoreDNS is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy
kubernetes-dashboard is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
必须通过 kube-apiserver 的安全端口(https)访问 dashbaord,访问时浏览器需要使用自定义证书,否则会被 kube-apiserver 拒绝访问。
创建登录 Dashboard 的 token 和 kubeconfig 配置文件
上面提到,Dashboard 默认只支持 token 认证,所以如果使用 KubeConfig 文件,需要在该文件中指定 token,不支持使用 client 证书认证。
创建登录 token
[k8s@kube-server ~]$ kubectl create sa dashboard-admin -n kube-system
serviceaccount "dashboard-admin" created
[k8s@kube-server ~]$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
clusterrolebinding.rbac.authorization.k8s.io "dashboard-admin" created
[k8s@kube-server ~]$ ADMIN_SECRET=$(kubectl get secrets -n kube-system | grep dashboard-admin | awk '{print $1}')
[k8s@kube-server ~]$ DASHBOARD_LOGIN_TOKEN=$(kubectl describe secret -n kube-system ${ADMIN_SECRET} | grep -E '^token' | awk '{print $2}')
[k8s@kube-server ~]$ echo ${DASHBOARD_LOGIN_TOKEN}
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tbWQycWIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMDAxMjA1ZTUtN2I3YS0xMWU4LTkzNjAtMDgwMDI3Mzk1MzYwIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.jgQo6TtcGugKQOlcXbe9-dqoP1_YkKshbeeqMudZOFVigDgSKPAUYNH4LbIqOBoAMnsZxKJPFFd36wR5JRzqUy5hI6cSRhBZr7_XiAZYeAdt0ZmbTq_ZM-Y0HDnokhxonwmV08TkVffj85uLnHUY5IZFYKmiiEUuSecek8LWVqvUAgBj1TIeKyGr5FGYxk2KCzlkHU90yFlhSjN4VqE-YkG7TJuV-2ge2sBWhmnqodrWhOHMD7_CQP-WzZxjPZY-WbznYNrBbuVOkJVjOyaf6EB0lzx1bpMSSeVhkWA3a_BdxOEWEx-OuvQgIxqqn0cY27om5xKItR-B4DiyrKyu6w
[k8s@kube-server ~]$
使用输出的 token 登录 Dashboard。
创建使用 token 的 KubeConfig 文件
source /opt/k8s/bin/environment.sh
# 设置集群参数
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/cert/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=dashboard.kubeconfig
# 设置客户端认证参数,使用上面创建的 Token
kubectl config set-credentials dashboard_user \
--token=${DASHBOARD_LOGIN_TOKEN} \
--kubeconfig=dashboard.kubeconfig
# 设置上下文参数
kubectl config set-context default \
--cluster=kubernetes \
--user=dashboard_user \
--kubeconfig=dashboard.kubeconfig
# 设置默认上下文
kubectl config use-context default --kubeconfig=dashboard.kubeconfig
用生成的 dashboard.kubeconfig 登录 Dashboard。
由于缺少 Heapster 插件,当前 dashboard 不能展示 Pod、Nodes 的 CPU、内存等统计数据和图表。
9.3 部署 heapster 插件
Heapster是一个收集者,将每个Node上的cAdvisor的数据进行汇总,然后导到第三方工具(如InfluxDB)。
Heapster 是通过调用 kubelet 的 http API 来获取 cAdvisor 的 metrics 数据的。
由于 kublet 只在 10250 端口接收 https 请求,故需要修改 heapster 的 deployment 配置。同时,需要赋予 kube-system:heapster ServiceAccount 调用 kubelet API 的权限。
下载 heapster 文件
到 heapster release 页面 下载最新版本的 heapster
wget https://github.com/kubernetes/heapster/archive/v1.5.3.tar.gz
tar -xzvf v1.5.3.tar.gz
mv v1.5.3.tar.gz heapster-1.5.3.tar.gz
官方文件目录: heapster-1.5.3/deploy/kube-config/influxdb
修改配置
[k8s@kube-server ~]$ cd heapster-1.5.3/deploy/kube-config/influxdb
[k8s@kube-server influxdb]$ ls
grafana.yaml heapster.yaml influxdb.yaml
[k8s@kube-server influxdb]$ cp grafana.yaml{,.orig}
[k8s@kube-server influxdb]$ vi grafana.yaml
[k8s@kube-server influxdb]$ diff grafana.yaml.orig grafana.yaml
16c16
< image: gcr.io/google_containers/heapster-grafana-amd64:v4.4.3
---
> image:
wanghkkk/heapster-grafana-amd64-v4.4.3:v4.4.3
67c67
< # type: NodePort
---
> type: NodePort
[k8s@kube-server influxdb]$
更换国内可访问的镜像,并开启 NodePort
[k8s@kube-server influxdb]$ cp heapster.yaml{,.orig}
[k8s@kube-server influxdb]$ vi heapster.yaml
[k8s@kube-server influxdb]$ diff heapster.yaml.orig heapster.yaml
23c23
< image: gcr.io/google_containers/heapster-amd64:v1.5.3
---
> image: fishchen/heapster-amd64:v1.5.3
27c27
< - --source=kubernetes: https://kubernetes.default
---
> - --source=kubernetes: https://kubernetes.default?kubeletHttps=true&kubeletPort=10250
[k8s@kube-server influxdb]$
由于 kubelet 只在 10250 监听 https 请求,故添加相关参数。
[k8s@kube-server influxdb]$ cp influxdb.yaml{,.orig}
[k8s@kube-server influxdb]$ vi influxdb.yaml
[k8s@kube-server influxdb]$ diff influxdb.yaml.orig influxdb.yaml
16c16
< image: gcr.io/google_containers/heapster-influxdb-amd64:v1.3.3
---
> image: fishchen/heapster-influxdb-amd64:v1.3.3
[k8s@kube-server influxdb]$
执行所有定义文件
[k8s@kube-server influxdb]$ pwd
/home/k8s/heapster-1.5.3/deploy/kube-config/influxdb
[k8s@kube-server influxdb]$ ls *.yaml
grafana.yaml heapster.yaml influxdb.yaml
[k8s@kube-server influxdb]$ kubectl create -f .
deployment.extensions "monitoring-grafana" created
service "monitoring-grafana" created
serviceaccount "heapster" created
deployment.extensions "heapster" created
service "heapster" created
deployment.extensions "monitoring-influxdb" created
service "monitoring-influxdb" created
[k8s@kube-server influxdb]$
$ cd ../rbac/
$ pwd
/opt/k8s/heapster-1.5.2/deploy/kube-config/rbac
$ ls
heapster-rbac.yaml
[k8s@kube-server rbac]$ cp heapster-rbac.yaml{,.orig}
[k8s@kube-server rbac]$ vi heapster-rbac.yaml
[k8s@kube-server rbac]$ diff heapster-rbac.yaml.orig heapster-rbac.yaml
4c4
< name: heapster
---
> name: heapster-kubelet-api
8c8
< name: system:heapster
---
> name: system:kubelet-api-admin
[k8s@kube-server rbac]$ kubectl create -f heapster-rbac.yaml
clusterrolebinding.rbac.authorization.k8s.io "heapster-kubelet-api" created
[k8s@kube-server rbac]$
将 serviceAccount kube-system:heapster 与 ClusterRole system:kubelet-api-admin 绑定,授予它调用 kubelet API 的权限。
检查执行结果
[k8s@kube-server rbac]$ kubectl get pods -n kube-system | grep -E 'heapster|monitoring'
heapster-7648ffc7c9-qfvtd 1/1 Running 0 18m
monitoring-grafana-5986995c7b-dlqn4 0/1 ImagePullBackOff 0 18m
monitoring-influxdb-f75847d48-pd97v 1/1 Running 0 18m
检查 kubernets dashboard 界面,可以正确显示各 Nodes、Pods 的 CPU、内存、负载等统计数据和图表了。
访问 grafana
1.通过 kube-apiserver 访问:
获取 monitoring-grafana 服务 URL:
[k8s@kube-server influxdb]$ kubectl cluster-info
Kubernetes master is running at https://172.16.10.100:6443
CoreDNS is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy
Heapster is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/heapster/proxy
kubernetes-dashboard is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
monitoring-grafana is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
monitoring-influxdb is running at https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy
使用浏览器访问:https://172.16.10.100:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy即可。
注:本以为需要在virtualbox上做个端口转发才行,结果发现在virtualbox上给kube-server,kube-node1,2,3的第2块网卡所设置的Host-Only网络,实际上支持从PC主机上直接访问到这几个虚机的Host-Only网卡地址与服务端口!
2. 通过 kubectl proxy 访问:
创建代理
kubectl proxy --address='172.16.10.100' --port=8086 --accept-hosts='^*$'
Starting to serve on 172.16.10.100:8086
浏览器访问 URL:http://172.16.10.100:8086/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy/?orgId=1
3. 通过 NodePort 访问:
[k8s@kube-server influxdb]$ kubectl get svc -n kube-system|grep -E 'monitoring|heapster'
heapster ClusterIP 10.254.14.104 80/TCP 58m
monitoring-grafana NodePort 10.254.36.0 80:8995/TCP 58m
monitoring-influxdb ClusterIP 10.254.206.219 8086/TCP 58m
grafana 监听 NodePort 8995;
浏览器访问 URL:http://172.16.10.101:8995/?orgId=1
9.4 部署 metrics-server 插件
metrics-server为通过api的方式提供nodes或pods的资源使用指标提供支持。目前主要是在HPA自动伸缩和Scheduler自动调度中得到应用。
创建 metrics-server 使用的证书
创建 metrics-server 证书签名请求:
cat > metrics-server-csr.json <
{
"CN": "aggregator",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "k8s",
"OU": "testcorp"
}
]
}
EOF
注意: CN 名称为 aggregator,需要与 kube-apiserver 的 --requestheader-allowed-names 参数配置一致。
生成 metrics-server 证书和私钥:
cfssl gencert -ca=/etc/kubernetes/cert/ca.pem \
-ca-key=/etc/kubernetes/cert/ca-key.pem \
-config=/etc/kubernetes/cert/ca-config.json \
-profile=kubernetes metrics-server-csr.json | cfssljson -bare metrics-server
将生成的证书和私钥文件拷贝到 kube-apiserver 节点:
cp metrics-server*.pem /etc/kubernetes/cert/
修改 kubernetes 控制平面组件的配置以支持 metrics-server
kube-apiserver添加如下配置参数:
--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
--proxy-client-cert-file=/etc/kubernetes/cert/metrics-server.pem
--proxy-client-key-file=/etc/kubernetes/cert/metrics-server-key.pem
--runtime-config=api/all=true
--enable-aggregator-routing=true
- --requestheader-XXX、--proxy-client-XXX 是 kube-apiserver 的 aggregator layer 相关的配置参数,metrics-server & HPA 需要使用;
- --requestheader-client-ca-file:用于签名 --proxy-client-cert-file 和 --proxy-client-key-file 指定的证书;在启用了 metric aggregator 时使用;
- 如果 --requestheader-allowed-names 不为空,则--proxy-client-cert-file 证书的 CN 必须位于 allowed-names 中,默认为 aggregator;
如果 kube-apiserver 机器没有运行 kube-proxy,则还需要添加 --enable-aggregator-routing=true 参数。
注意:requestheader-client-ca-file 指定的 CA 证书,必须具有 client auth and server auth。
kube-controller-manager添加如下配置参数:
--horizontal-pod-autoscaler-use-rest-clients=true
用于配置 HPA 控制器使用 REST 客户端获取 metrics 数据。
修改过启动参数后,需要重启服务以生效。
systemctl daemon-reload
systemctl restart kube-apiserver && systemctl status kube-apiserver
systemctl daemon-reload
systemctl restart kube-controller-manager && systemctl status kube-controller-manager
修改插件配置文件配置文件
metrics-server 插件位于 kubernetes 的 cluster/addons/metrics-server/ 目录下。
修改 metrics-server-deployment 文件:
[k8s@kube-server metrics-server]$ cp metrics-server-deployment.yaml{,.orig}
[k8s@kube-server metrics-server]$ vi metrics-server-deployment.yaml
[k8s@kube-server metrics-server]$ diff metrics-server-deployment.yaml.orig metrics-server-deployment.yaml
51c51
< image: k8s.gcr.io/metrics-server-amd64:v0.2.1
---
> image: mirrorgooglecontainers/metrics-server-amd64:v0.2.1
54c54
< - --source=kubernetes.summary_api:''
---
> - --source=kubernetes.summary_api: https://kubernetes.default?kubeletHttps=true&kubeletPort=10250
60c60
< image: k8s.gcr.io/addon-resizer:1.8.1
---
> image: siriuszg/addon-resizer:1.8.1
[k8s@kube-server metrics-server]$
metrics-server 的参数格式与 heapster 类似。由于 kubelet 只在 10250 监听 https 请求,故添加相关参数。
授予 kube-system:metrics-server ServiceAccount 访问 kubelet API 的权限:
[k8s@kube-server metrics-server]$ cat auth-kubelet.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metrics-server:system:kubelet-api-admin
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:kubelet-api-admin
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
[k8s@kube-server metrics-server]$
新建一个 ClusterRoleBindings 定义文件,授予相关权限。
创建 metrics-server
[k8s@kube-server metrics-server]$ pwd
/home/k8s/kubernetes/cluster/addons/metrics-server
[k8s@kube-server metrics-server]$ ls -l *.yaml
-rw-rw-r--. 1 k8s k8s 398 Jun 4 23:17 auth-delegator.yaml
-rw-rw-r--. 1 k8s k8s 404 Jun 29 13:40 auth-kubelet.yaml
-rw-rw-r--. 1 k8s k8s 419 Jun 4 23:17 auth-reader.yaml
-rw-rw-r--. 1 k8s k8s 393 Jun 4 23:17 metrics-apiservice.yaml
-rw-rw-r--. 1 k8s k8s 2650 Jun 29 13:21 metrics-server-deployment.yaml
-rw-rw-r--. 1 k8s k8s 336 Jun 4 23:17 metrics-server-service.yaml
-rw-rw-r--. 1 k8s k8s 801 Jun 4 23:17 resource-reader.yaml
[k8s@kube-server metrics-server]$ kubectl create -f .
clusterrolebinding.rbac.authorization.k8s.io "metrics-server:system:auth-delegator" created
clusterrolebinding.rbac.authorization.k8s.io "metrics-server:system:kubelet-api-admin" created
rolebinding.rbac.authorization.k8s.io "metrics-server-auth-reader" created
apiservice.apiregistration.k8s.io "v1beta1.metrics.k8s.io" created
serviceaccount "metrics-server" created
configmap "metrics-server-config" created
deployment.extensions "metrics-server-v0.2.1" created
service "metrics-server" created
clusterrole.rbac.authorization.k8s.io "system:metrics-server" created
clusterrolebinding.rbac.authorization.k8s.io "system:metrics-server" created
[k8s@kube-server metrics-server]$
查看运行情况
[k8s@kube-server metrics-server]$ kubectl get pods -n kube-system |grep metrics-server
metrics-server-v0.2.1-86946dfbfb-4fxvz 2/2 Running 0 5m
[k8s@kube-server metrics-server]$ kubectl get svc -n kube-system|grep metrics-server
metrics-server ClusterIP 10.254.71.71 443/TCP 6m
查看 metrics-server 输出的 metrics
metrics-server 输出的 APIs:https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/resource-metrics-api.md
1.通过 kube-apiserver 或 kubectl proxy 访问:
https://172.16.10.100:6443/apis/metrics.k8s.io/v1beta1/nodes
https://172.16.10.100:6443/apis/metrics.k8s.io/v1beta1/nodes/
https://172.16.10.100:6443/apis/metrics.k8s.io/v1beta1/pods
https://172.16.10.100:6443/apis/metrics.k8s.io/v1beta1/namespace//pods/
2.直接使用 kubectl 命令访问:
kubectl get --raw apis/metrics.k8s.io/v1beta1/nodes
kubectl get --raw apis/metrics.k8s.io/v1beta1/pods
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes/
kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespace//pods/
kubectl get --raw "/apis/metrics.k8s.io/v1beta1" | jq .
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .
注:/apis/metrics.k8s.io/v1beta1/nodes 和 /apis/metrics.k8s.io/v1beta1/pods 返回的 usage 包含 CPU 和 Memory 。
注:以上查看metrics的操作执行时报错,暂未找到答案。错误信息主要是:
[k8s@kube-server metrics-server]$ kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .
Error from server (Forbidden): nodes.metrics.k8s.io is forbidden: User "aggregator" cannot list nodes.metrics.k8s.io at the cluster scope.
9.5 EFK插件
EFK 对应的目录:kubernetes/cluster/addons/fluentd-elasticsearch
[k8s@kube-server addons]$ pwd
/home/k8s/kubernetes/cluster/addons
[k8s@kube-server addons]$ cd fluentd-elasticsearch/
[k8s@kube-server fluentd-elasticsearch]$ ls
es-image es-statefulset.yaml fluentd-es-ds.yaml kibana-deployment.yaml OWNERS README.md
es-service.yaml fluentd-es-configmap.yaml fluentd-es-image kibana-service.yaml podsecuritypolicies
[k8s@kube-server fluentd-elasticsearch]$
修改定义文件
$ cp es-statefulset.yaml{,.orig}
$ diff es-statefulset.yaml{,.orig}
76c76
< - image:
netonline/elasticsearch:v5.6.4
---
> - image: k8s.gcr.io/elasticsearch:v5.6.4
$ cp fluentd-es-ds.yaml{,.orig}
$ diff fluentd-es-ds.yaml{,.orig}
79c79
< image: netonline/fluentd-elasticsearch:v2.0.4
---
> image: k8s.gcr.io/fluentd-elasticsearch:v2.0.4
给 Node 设置标签
DaemonSet fluentd-es 只会调度到设置了标签 beta.kubernetes.io/fluentd-ds-ready=true 的 Node,需要在期望运行 fluentd 的 Node 上设置该标签;
[k8s@kube-server fluentd-elasticsearch]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
kube-node1 Ready 3d v1.10.4
kube-node2 Ready 3d v1.10.4
kube-node3 Ready 3d v1.10.4
[k8s@kube-server fluentd-elasticsearch]$ kubectl label nodes kube-node3 beta.kubernetes.io/fluentd-ds-ready=true
node "kube-node3" labeled
[k8s@kube-server fluentd-elasticsearch]$
执行定义文件
[k8s@kube-server fluentd-elasticsearch]$ kubectl create -f .
service "elasticsearch-logging" created
serviceaccount "elasticsearch-logging" created
clusterrole.rbac.authorization.k8s.io "elasticsearch-logging" created
clusterrolebinding.rbac.authorization.k8s.io "elasticsearch-logging" created
statefulset.apps "elasticsearch-logging" created
configmap "fluentd-es-config-v0.1.4" created
serviceaccount "fluentd-es" created
clusterrole.rbac.authorization.k8s.io "fluentd-es" created
clusterrolebinding.rbac.authorization.k8s.io "fluentd-es" created
daemonset.apps "fluentd-es-v2.0.4" created
deployment.apps "kibana-logging" created
service "kibana-logging" created
[k8s@kube-server fluentd-elasticsearch]$
检查执行结果
kubectl get pods -n kube-system -o wide|grep -E 'elasticsearch|fluentd|kibana'
kubectl get service -n kube-system|grep -E 'elasticsearch|kibana'
kibana Pod 第一次启动时会用**较长时间(0-20分钟)**来优化和 Cache 状态页面,可以 tailf 该 Pod 的日志观察进度:
kubectl logs kibana-logging-7445dc9757-jbzvd -n kube-system -f
注意:只有当的 Kibana pod 启动完成后,才能查看 kibana dashboard,否则会提示 refuse。
访问 kibana
通过 kube-apiserver 访问:
kubectl cluster-info|grep -E 'Elasticsearch|Kibana'
通过 kubectl proxy 访问:
创建代理
$ kubectl proxy --address='172.16.10.100' --port=8086 --accept-hosts='^*$'
浏览器访问 URL:http://172.16.10.100:8086/api/v1/namespaces/kube-system/services/kibana-logging/proxy
在 Settings -> Indices 页面创建一个 index(相当于 mysql 中的一个 database),选中 Index contains time-based events,使用默认的 logstash-* pattern,点击 Create ;
创建 Index 后,稍等几分钟就可以在 Discover 菜单下看到 ElasticSearch logging 中汇聚的日志;
注:因为我的模拟测试环境中在一台极为普通的PC机上搭建的,在运行了上面这套EFK环境后,磁盘IO基本就跑不过来了,导致各种服务没响应,最终还是手动又删除了EFK。
问题记录:
1、网卡hairpin_mode设置
在准备配置环境的过程中,就要求设置docker网卡的hairpin_mode,不太理解在未安装docker时为什么要求设置这个,且确实无法设置,因为此时连docker也还没有安装。
注:hairpin_mode模式下,虚机或容器间的流量强制要求必须经过物理交换机才能通信。
2、设置系统参数net.bridge.bridge-nf-call-iptables=1(打开iptables管理网桥的功能)
在各节点上执行以下命令:
modprobe br_netfilter
cat > /etc/sysctl.d/kubernetes.conf <
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
EOF
sysctl -p /etc/sysctl.d/kubernetes.conf
原文中把modprobe br_netfilter放在最后执行的,实际情况是应该首先执行这条命令。
3、
授予 kubernetes 证书访问 kubelet API 的权限的命令的执行顺序错误
应该在成功启动了kube-apiserver服务后再执行该命令。
4、在部署kube-apiserver服务中,制作密钥的证书请求中使用了无法解析的域名kubernetes.default.svc.cluster.local.
该问题已经确认为是go v1.9中的域名语法校验解析bug。在6.29号的最新版本的部署材料中已经发现和纠正了该问题。但此故障引发的coreDNS部署失败并报以下错误,已经折腾了我2天时间寻求答案!
E0628 08:10:41.256264 1 reflector.go:205] github.com/coredns/coredns/plugin/kubernetes/controller.go:319: Failed to list *v1.Namespace: Get https://10.254.0.1:443/api/v1/namespaces?limit=500&resourceVersion=0: tls: failed to parse certificate from server: x509: cannot parse dnsName "kubernetes.default.svc.cluster.local."
关于该bug的修复说明:
https://github.com/opsnull/follow-me-install-kubernetes-cluster/commit/719e5f01e9dcbf96e1a19159ae68a18c7fa9171b
5、关于怎么使用admin密钥访问api接口
下面是正确的方式:
curl -sSL --cacert /etc/kubernetes/cert/ca.pem --cert /home/k8s/admin.pem --key /home/k8s/admin-key.pem https://172.16.10.100:6443/api/v1/endpoints