02、Prometheus监控:手动部署Prometheus

Prometheus监控k8s(2)-手动部署Prometheus

https://www.cnblogs.com/panwenbin-logs/p/10123833.html

https://github.com/yunlzheng/prometheus-book/blob/master/kubernetes/deploy-prometheus-in-kubernetes.md

https://www.cnblogs.com/xzkzzz/p/10208115.html

1 部署环境

根据之前的环境部署

lK8S中已有内部DNS服务coredns(参考前面的文档)

l已有可使用的动态PV(参考前面的文档)

2 准备文件和环境

2.1 下载文件

项目地址https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/prometheus

git clone https://github.com/kubernetes/kubernetes.git 

#我嫌麻烦就直接克隆kubernetes整个项目了,当然下载zip包其实只有几十M,更快

或者

# 从github官网下载promethues的yaml部署文件

curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/prometheus-rbac.yaml
curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/prometheus-configmap.yaml
curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/prometheus-service.yaml
curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/prometheus-statefulset.yaml

[root@k8s-master prometheus]# ls
kubernetes-master
[root@k8s-master prometheus]# cd kubernetes-master/cluster/addons/

[root@k8s-master addons]# mkdir ~/prometheus/
[root@k8s-master addons]# mv prometheus/* /root/prometheus/
[root@k8s-master addons]# cd /root/prometheus/
[root@k8s-master prometheus]# ls
alertmanager-configmap.yaml   kube-state-metrics-deployment.yaml  node-exporter-service.yaml  prometheus-service.yaml
alertmanager-deployment.yaml  kube-state-metrics-rbac.yaml        OWNERS                      prometheus-statefulset.yaml
alertmanager-pvc.yaml         kube-state-metrics-service.yaml     prometheus-configmap.yaml   README.md
alertmanager-service.yaml     node-exporter-ds.yml                prometheus-rbac.yaml
[root@k8s-master prometheus]#

2.2 命名空间的问题

默认prometheus使用的是kube-system名称空间, 网上有很多文章说自定义一下方便管理。

但我自定义之后出现

Warning FailedCreate 2m40s (x16 over 5m24s) statefulset-controller create Pod prometheus-0 in StatefulSet prometheus failed error: pods "prometheus-0" is forbidden: pods with system-cluster-critical priorityClass is not permitted in k8s-monitor namespace

prometheus-statefulset.yaml 起不来,还是用系统的kube-system,这一块还是别动了吧,可能和版本有关,这个坑还是别踩了吧

2.3 建立分类目录方便管理

mkdir node-exporter kube-state-metrics alertmanager prometheus 
#将每个组件单独放入一个目录中,方便部署及管理
mv node-exporter-* node-exporter
mv alertmanager-* alertmanager
mv kube-state-metrics-* kube-state-metrics
mv prometheus-* prometheus

*

3 安装prometheus-server

3.1 PV和PVC

如果没有动态PVC,手动创建

apiVersion: v1
kind: PersistentVolume
metadata:
  name: prometheus-data
spec:
  capacity:
    storage: 25Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: prometheus-data   #storageClassName与prometheus-statefulset.yaml中volumeClaimTemplates下定义的需要保持一致
  nfs:
    path: /data/volumes/v2
    server: 10.6.76.25

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-data
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: prometheus-data

[root@k8s-master prometheus]# kubectl apply -f pv-pvc.yaml
[root@k8s-master prometheus]# kubectl get pv,pvc|grep prometheus-data

persistentvolume/prometheus-data                                               25Gi       RWO            Recycle          Bound    kube-system/prometheus-data                prometheus-data                    18h

修改statefulset.yaml
# 删掉最下面的10行
  volumeClaimTemplates:
  - metadata:
      name: prometheus-data
    spec:
      storageClassName: standard
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: "16Gi"

# 新增下面3行,注意缩进
        - name: prometheus-data
          persistentVolumeClaim:  
            claimName: prometheus-data

有nfs那种自动创建的

  volumeClaimTemplates:
  - metadata:
      name: prometheus-data
    spec:
      storageClassName: managed-nfs-storage
      #storageClassName: standard
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: "16Gi"

3.2 设置外网访问

默认prometheus的service端口类型为ClusterIP,为了可以集群外访问,修改为NodePort

#[root@k8s-master prometheus]# cat prometheus-service.yaml
kind: Service
apiVersion: v1
metadata:
  name: prometheus
  namespace: kube-system
  labels:
    kubernetes.io/name: "Prometheus"
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  type: NodePort
  ports:
    - name: http
      port: 9090
      protocol: TCP
      targetPort: 9090
  selector:
k8s-app: Prometheus

3.3 部署

[root@k8s-master prometheus]# kubectl apply -f  Prometheus
configmap/prometheus-config created
serviceaccount/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
service/prometheus created
statefulset.apps/prometheus created
[root@k8s-master prometheus]#
[root@k8s-master prometheus]# kubectl get  all -n kube-system | grep prometheus

pod/prometheus-0                            2/2     Running   0          63m
service/prometheus             ClusterIP   10.111.156.156   <none>        9090:31500/TCP                 63m


statefulset.apps/prometheus   1/1     63m
[root@k8s-master prometheus]#

4 访问prometheus(node节点IP:端口)

*

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: