K8s从入门到精通系列06-Kubernetes前端UI可视化应用部署
一、Kubernetes前端UI可视化应用部署之Prometheus
在真正部署Prometheus之前,应了解一下Prometheus的各个组件之间的关系及作用:
1)MertricServer:是k8s集群资源使用情况的聚合器,收集数据给K8s集群内使用,如:kubectl,hpa,scheduler;
2)PrometheusOperator:是一个系统检测和警报工具箱,用来存储监控数据;
3)NodeExporter:用于各node的关键度量指标状态数据;
4)kubeStateMetrics:收集k8s集群内资源对象数据,指定告警规则;
5)Prometheus:采用pull方式收集apiserver,scheduler,controller-manager,kubelet组件数据,通过http协议传输;
6)Grafana:是可视化数据统计和监控平台;
1)获取yaml文件修改并执行
注意:这里部署的Kubernetes前端UI可视化,并不是Prometheus官网提供的,而是使用的coreos提供的Prometheus项目。
## 将Kubernetes前端UI可视化项目克隆到指定路径/usr/local/
git clone https://github.com/coreos/kube-prometheus.git
## 编辑相关配置
vim /usr/local/kube-prometheus/manifests/grafana-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 7.5.4
name: grafana
namespace: monitoring
spec:
type: NodePort #添加类型为NodePort
ports:
- name: http
port: 3000
targetPort: http
nodePort: 31010 #自定义映射的端口
selector:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
vim /usr/local/kube-prometheus/manifests/alertmanager-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
alertmanager: main
app.kubernetes.io/component: alert-router
app.kubernetes.io/name: alertmanager
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 0.21.0
name: alertmanager-main
namespace: monitoring
spec:
type: NodePort #添加类型为NodePort
ports:
- name: web
port: 9093
targetPort: web
nodePort: 31020 #自定义映射的端口
selector:
alertmanager: main
app: alertmanager
app.kubernetes.io/component: alert-router
app.kubernetes.io/name: alertmanager
app.kubernetes.io/part-of: kube-prometheus
sessionAffinity: ClientIP
vim /usr/local/kube-prometheus/manifests/prometheus-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.26.0
prometheus: k8s
name: prometheus-k8s
namespace: monitoring
spec:
type: NodePort #添加类型为NodePort
ports:
- name: web
port: 9090
targetPort: web
nodePort: 31030 #自定义映射的端口
selector:
app: prometheus
app.kubernetes.io/component: prometheus
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
prometheus: k8s
sessionAffinity: ClientIP
kubectl apply -f /usr/local/kube-prometheus/manifests/setup/
namespace/monitoring created
customresourcedefinition.apiextensions.k8s.io/alertmanagerconfigs.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/alertmanagers.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/podmonitors.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/probes.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheuses.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheusrules.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/servicemonitors.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/thanosrulers.monitoring.coreos.com created
clusterrole.rbac.authorization.k8s.io/prometheus-operator created
clusterrolebinding.rbac.authorization.k8s.io/prometheus-operator created
deployment.apps/prometheus-operator created
service/prometheus-operator created
serviceaccount/prometheus-operator created
kubectl apply -f /usr/local/kube-prometheus/manifests/
Warning: policy/v1beta1 PodDisruptionBudget is deprecated in v1.21+, unavailable in v1.25+; use policy/v1 PodDisruptionBudget
poddisruptionbudget.policy/alertmanager-main created
prometheusrule.monitoring.coreos.com/alertmanager-main-rules created
secret/alertmanager-main created
service/alertmanager-main created
serviceaccount/alertmanager-main created
servicemonitor.monitoring.coreos.com/alertmanager created
clusterrole.rbac.authorization.k8s.io/blackbox-exporter created
clusterrolebinding.rbac.authorization.k8s.io/blackbox-exporter created
configmap/blackbox-exporter-configuration created
deployment.apps/blackbox-exporter created
service/blackbox-exporter created
serviceaccount/blackbox-exporter created
servicemonitor.monitoring.coreos.com/blackbox-exporter created
secret/grafana-datasources created
configmap/grafana-dashboard-apiserver created
configmap/grafana-dashboard-cluster-total created
configmap/grafana-dashboard-controller-manager created
configmap/grafana-dashboard-k8s-resources-cluster created
configmap/grafana-dashboard-k8s-resources-namespace created
configmap/grafana-dashboard-k8s-resources-node created
configmap/grafana-dashboard-k8s-resources-pod created
configmap/grafana-dashboard-k8s-resources-workload created
configmap/grafana-dashboard-k8s-resources-workloads-namespace created
configmap/grafana-dashboard-kubelet created
configmap/grafana-dashboard-namespace-by-pod created
configmap/grafana-dashboard-namespace-by-workload created
configmap/grafana-dashboard-node-cluster-rsrc-use created
configmap/grafana-dashboard-node-rsrc-use created
configmap/grafana-dashboard-nodes created
configmap/grafana-dashboard-persistentvolumesusage created
configmap/grafana-dashboard-pod-total created
configmap/grafana-dashboard-prometheus-remote-write created
configmap/grafana-dashboard-prometheus created
configmap/grafana-dashboard-proxy created
configmap/grafana-dashboard-scheduler created
configmap/grafana-dashboard-statefulset created
configmap/grafana-dashboard-workload-total created
configmap/grafana-dashboards created
Warning: spec.template.spec.nodeSelector[beta.kubernetes.io/os]: deprecated since v1.14; use "kubernetes.io/os" instead
deployment.apps/grafana created
service/grafana created
serviceaccount/grafana created
servicemonitor.monitoring.coreos.com/grafana created
prometheusrule.monitoring.coreos.com/kube-prometheus-rules created
clusterrole.rbac.authorization.k8s.io/kube-state-metrics created
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created
deployment.apps/kube-state-metrics created
prometheusrule.monitoring.coreos.com/kube-state-metrics-rules created
service/kube-state-metrics created
serviceaccount/kube-state-metrics created
servicemonitor.monitoring.coreos.com/kube-state-metrics created
prometheusrule.monitoring.coreos.com/kubernetes-monitoring-rules created
servicemonitor.monitoring.coreos.com/kube-apiserver created
servicemonitor.monitoring.coreos.com/coredns created
servicemonitor.monitoring.coreos.com/kube-controller-manager created
servicemonitor.monitoring.coreos.com/kube-scheduler created
servicemonitor.monitoring.coreos.com/kubelet created
clusterrole.rbac.authorization.k8s.io/node-exporter created
clusterrolebinding.rbac.authorization.k8s.io/node-exporter created
daemonset.apps/node-exporter created
prometheusrule.monitoring.coreos.com/node-exporter-rules created
service/node-exporter created
serviceaccount/node-exporter created
servicemonitor.monitoring.coreos.com/node-exporter created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
clusterrole.rbac.authorization.k8s.io/prometheus-adapter created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/prometheus-adapter created
clusterrolebinding.rbac.authorization.k8s.io/resource-metrics:system:auth-delegator created
clusterrole.rbac.authorization.k8s.io/resource-metrics-server-resources created
configmap/adapter-config created
deployment.apps/prometheus-adapter created
poddisruptionbudget.policy/prometheus-adapter created
rolebinding.rbac.authorization.k8s.io/resource-metrics-auth-reader created
service/prometheus-adapter created
serviceaccount/prometheus-adapter created
servicemonitor.monitoring.coreos.com/prometheus-adapter created
clusterrole.rbac.authorization.k8s.io/prometheus-k8s created
clusterrolebinding.rbac.authorization.k8s.io/prometheus-k8s created
prometheusrule.monitoring.coreos.com/prometheus-operator-rules created
servicemonitor.monitoring.coreos.com/prometheus-operator created
poddisruptionbudget.policy/prometheus-k8s created
prometheus.monitoring.coreos.com/k8s created
prometheusrule.monitoring.coreos.com/prometheus-k8s-prometheus-rules created
rolebinding.rbac.authorization.k8s.io/prometheus-k8s-config created
rolebinding.rbac.authorization.k8s.io/prometheus-k8s created
rolebinding.rbac.authorization.k8s.io/prometheus-k8s created
rolebinding.rbac.authorization.k8s.io/prometheus-k8s created
role.rbac.authorization.k8s.io/prometheus-k8s-config created
role.rbac.authorization.k8s.io/prometheus-k8s created
role.rbac.authorization.k8s.io/prometheus-k8s created
role.rbac.authorization.k8s.io/prometheus-k8s created
service/prometheus-k8s created
serviceaccount/prometheus-k8s created
servicemonitor.monitoring.coreos.com/prometheus-k8s created
kubectl get pod -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-main-0 2/2 Running 0 3m4s
alertmanager-main-1 2/2 Running 0 3m4s
alertmanager-main-2 2/2 Running 0 3m4s
blackbox-exporter-55c457d5fb-2wqbb 3/3 Running 0 3m4s
grafana-6dd5b5f65-kl2gs 1/1 Running 0 3m2s
kube-state-metrics-76f6cb7996-4bdgr 2/3 Running 0 3m1s
node-exporter-4m4jb 2/2 Running 0 3m
node-exporter-8cwrz 2/2 Running 0 3m
node-exporter-czz9w 2/2 Running 0 3m
prometheus-adapter-59df95d9f5-52v92 1/1 Running 0 2m59s
prometheus-adapter-59df95d9f5-hfbzp 1/1 Running 0 2m59s
prometheus-k8s-0 2/2 Running 1 (68s ago) 2m57s
prometheus-k8s-1 2/2 Running 1 (90s ago) 2m57s
prometheus-operator-7775c66ccf-hhpxj 2/2 Running 0 4m14s
kubectl get svc -n monitoring | grep grafana
grafana NodePort 10.98.186.85 <none> 3000:31010/TCP 2m26s
二、Kubernetes前端UI可视化部署-常用命令
1、查看所有命名空间
root@master:/home/ley# kubectl get namespaces
NAME STATUS AGE
default Active 5d17h
kube-node-lease Active 5d17h
kube-public Active 5d17h
kube-system Active 5d17h
monitoring Active 18h
root@master:/home/ley# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 5d17h v1.22.0
node Ready <none> 5d17h v1.22.0
work Ready <none> 5d16h v1.22.0
2、monitoring命名空间下所有的pod,不加 -n monitoring 得到的是default命名空间下的数据
root@master:/home/ley# kubectl get pods -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-main-0 2/2 Running 2 (47m ago) 18h
alertmanager-main-1 2/2 Running 4 (18m ago) 18h
alertmanager-main-2 2/2 Running 2 (47m ago) 18h
blackbox-exporter-55c457d5fb-2wqbb 3/3 Running 6 (18m ago) 18h
grafana-6dd5b5f65-kl2gs 1/1 Running 1 (47m ago) 18h
kube-state-metrics-76f6cb7996-4bdgr 3/3 Running 5 (18m ago) 18h
node-exporter-4m4jb 2/2 Running 2 (47m ago) 18h
node-exporter-8cwrz 2/2 Running 4 (47m ago) 18h
node-exporter-czz9w 2/2 Running 4 (18m ago) 18h
prometheus-adapter-59df95d9f5-52v92 1/1 Running 1 (47m ago) 18h
prometheus-adapter-59df95d9f5-hfbzp 1/1 Running 2 (18m ago) 18h
prometheus-k8s-0 2/2 Running 3 (47m ago) 18h
prometheus-k8s-1 2/2 Running 5 (18m ago) 18h
prometheus-operator-7775c66ccf-hhpxj 2/2 Running 4 (18m ago) 18h
## 查看Deployment
root@master:/home/ley# kubectl get deployments -n monitoring
NAME READY UP-TO-DATE AVAILABLE AGE
blackbox-exporter 1/1 1 1 18h
grafana 1/1 1 1 18h
kube-state-metrics 1/1 1 1 18h
prometheus-adapter 2/2 2 2 18h
prometheus-operator 1/1 1 1 18h
3、查看Pod的详细信息(如pod所属节点)
root@master:/home/ley# kubectl get pods -o wide -n monitoring
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
alertmanager-main-0 2/2 Running 2 (49m ago) 18h 10.244.1.18 node <none> <none>
alertmanager-main-1 2/2 Running 4 (20m ago) 18h 10.244.2.30 work <none> <none>
alertmanager-main-2 2/2 Running 2 (49m ago) 18h 10.244.1.17 node <none> <none>
blackbox-exporter-55c457d5fb-2wqbb 3/3 Running 6 (20m ago) 18h 10.244.2.28 work <none> <none>
grafana-6dd5b5f65-kl2gs 1/1 Running 1 (49m ago) 18h 10.244.1.20 node <none> <none>
kube-state-metrics-76f6cb7996-4bdgr 3/3 Running 5 (20m ago) 18h 10.244.2.27 work <none> <none>
node-exporter-4m4jb 2/2 Running 2 (49m ago) 18h 10.100.21.25 node <none> <none>
node-exporter-8cwrz 2/2 Running 4 (49m ago) 18h 10.100.21.24 master <none> <none>
node-exporter-czz9w 2/2 Running 4 (20m ago) 18h 10.100.21.26 work <none> <none>
prometheus-adapter-59df95d9f5-52v92 1/1 Running 1 (49m ago) 18h 10.244.1.21 node <none> <none>
prometheus-adapter-59df95d9f5-hfbzp 1/1 Running 2 (20m ago) 18h 10.244.2.29 work <none> <none>
prometheus-k8s-0 2/2 Running 3 (49m ago) 18h 10.244.1.19 node <none> <none>
prometheus-k8s-1 2/2 Running 5 (20m ago) 18h 10.244.2.26 work <none> <none>
prometheus-operator-7775c66ccf-hhpxj 2/2 Running 4 (20m ago) 18h 10.244.2.25 work <none> <none>
4、查看monitoring命名空间下所有service的详细信息(包含端口信息)
root@master:/home/ley# kubectl get svc -n monitoring
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alertmanager-main NodePort 10.100.133.11 <none> 9093:31020/TCP 18h
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 18h
blackbox-exporter ClusterIP 10.102.216.141 <none> 9115/TCP,19115/TCP 18h
grafana NodePort 10.98.186.85 <none> 3000:31010/TCP 18h
kube-state-metrics ClusterIP None <none> 8443/TCP,9443/TCP 18h
node-exporter ClusterIP None <none> 9100/TCP 18h
prometheus-adapter ClusterIP 10.107.240.188 <none> 443/TCP 18h
prometheus-k8s NodePort 10.105.90.142 <none> 9090:31030/TCP 18h
prometheus-operated ClusterIP None <none> 9090/TCP 18h
prometheus-operator ClusterIP None <none> 8443/TCP 18h
5、删除weave命名空间
kubectl delete namespace weave
6、删除k8s集群中的node节点
kubectl delete node <节点名> //如node1、node2
7、查看monitoring命名空间下某pod的详细信息
kubectl describe pod qperf-server-6j6f9 -n monitoring
kubectl describe deployment prometheus-core -n monitoring
8、查看monitoring命名空间下某pod的日志
kubectl logs qperf-server-6j6f9 -n monitoring
注:一个Pod中有多个container,使用上面的命令就会报错,提示Error from server (BadRequest): a container name must be specified for pod kube-state-metrics-64497b8856-j9tv9, choose one of: [kube-state-metrics addon-resizer]
正确的方式为:
kubectl logs kube-state-metrics-64497b8856-j9tv9 -n kube-system -c kube-state-metrics
9、使用yaml文件为ley 命名空间创建一个资源
kubectl create --namespace ley -f scope.yaml
10、通过kubectl edit 修改configmap,过十几秒左右会自动同步到容器挂载目录对应的文件中
编辑的步骤为:
#得到configmap的名字:
kubectl get configmap -n monitoring
#开始编辑
kubectl edit configmap prometheus-core -n monitoring.
注1:在容器中 vi 之后无法通过 :wq! 保存,即只有读权限。
注2:k8s只有主节点可以使用kubectl指令
注3:如何使文件传输工具拖拽进来的yaml文件生效:delete再create
11、对节点增加标签
kubectl label nodes node-kle6kz7u specialLabel=promconsul
node/node-kle6kz7u labeled
Kubernetes前端UI可视化部署整个pod镜像拉取较慢,最好是保持quay.io/访问畅通,或者手动拉取私有仓库镜像