k8s部署node-exporter

实验k8s集群环境如下:

主机名主机ip类型
k8s-master192.168.56.104master
k8s-node1192.168.56.105worker
k8s-node2192.168.56.106worker

node-exporter介绍:

node-exporter 可以采集机器(物理机、虚拟机、云主机等)的监控指标数据,能够采集到的指标包
括 CPU, 内存,磁盘,网络,文件数等信息。

1 创建空间monitor-sa

kubectl create ns monitor-sa

2 创建prometheus相关文件夹

mkdir -p /home/kevin/k8s/prometheus_grafana && cd /home/kevin/k8s/prometheus_grafana

3 下载node-exporter镜像包

镜像包下载到k8s每个节点prometheus_grafana路径下

链接:https://pan.baidu.com/s/115CF-0IV0cg72upv2WR36Q
提取码:79xj

4 加载node-exporter镜像包

加载k8s每个节点的node-exporter镜像包

docker load -i node-exporter.tar.gz

5 下载部署文件node-export.yaml

下载部署文件node-export.yaml到k8s控制节点k8s-master上

链接:https://pan.baidu.com/s/1QGMvqArDM8vElw3C3vLrTA
提取码:tgsp

6 查看node-exporter.yaml

[root@k8s-master prometheus_grafana]# cat node-export.yaml
apiVersion: apps/v1
kind: DaemonSet # 可以保证 k8s 集群的每个节点都运行完全一样的 pod
metadata:
  name: node-exporter
  namespace: monitor-sa
  labels:
    name: node-exporter
spec:
  selector:
    matchLabels:
     name: node-exporter
  template:
    metadata:
      labels:
        name: node-exporter
    spec:
      hostPID: true
      hostIPC: true
      hostNetwork: true
      # hostNetwork、 hostIPC、 hostPID 都为 True 时:
      # 表示这个 Pod 里的所有容器,会直接使用宿主机的网络,
      # 直接与宿主机进行 IPC(进程间通信) 通信, 
      # 可以看到宿主机里正在运行的所有进程。
      # 加入了 hostNetwork:true 会直接将我们的宿主机的 9100 端口映射出来,
      # 从而不需要创建 service 在我们的宿主机上就会有一个 9100 的端口
      containers:
      - name: node-exporter
        image: prom/node-exporter:v0.16.0
        ports:
        - containerPort: 9100
        resources:
          requests:
            cpu: 0.15 # 这个容器运行至少需要 0.15 核 cpu
        securityContext:
          privileged: true # 开启特权模式
        args:
        - --path.procfs # 配置挂载宿主机(node节点)的路径
        - /host/proc
        - --path.sysfs # 配置挂载宿主机(node节点)的路径
        - /host/sys
        - --collector.filesystem.ignored-mount-points
        - '"^/(sys|proc|dev|host|etc)($|/)"'
        # 通过正则表达式忽略某些文件系统挂载点的信息收集
        volumeMounts:
        - name: dev
          mountPath: /host/dev
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: rootfs
          mountPath: /rootfs
        # 将主机/dev、 /proc、 /sys 这些目录挂在到容器中,
        # 这是因为我们采集的很多节点数据都是通过这些文件来获取系统信息的
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"
      volumes:
        - name: proc
          hostPath:
            path: /proc
        - name: dev
          hostPath:
            path: /dev
        - name: sys
          hostPath:
            path: /sys
        - name: rootfs
          hostPath:
            path: /

7 更新 node-exporter.yaml 文件

[root@k8s-master]# kubectl apply -f node-export.yaml

8 查看 node-exporter 是否部署成功

显示如下,看到 pod 的状态都是 Running,说明部署成功

[root@k8s-master]# kubectl get pods -n monitor-sa
NAME                                  READY   STATUS    RESTARTS   AGE
node-exporter-84tk7                   1/1     Running   4          2d
node-exporter-ctmks                   1/1     Running   9          2d
node-exporter-jzg8t                   1/1     Running   2          2d

9 查看监控指标

查看k8s控制节点192.168.56.104的指标

[root@k8s-master prometheus_grafana]# curl http://192.168.56.104:9100/metrics
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.9.6"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 1.333648e+06
              ......
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、prometheus简介 Prometheus是一个开源的系统监控和告警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF维护管理的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,prometheus支持多种exporter采集数据,还支持通过pushgateway进行数据上报,Prometheus再性能上可支撑上万台规模的集群。 二、prometheus架构图 三、prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据。 2.Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。 3.Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端 4.Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出报警,常见的接收方式有:电子邮件,微信,钉钉, slack等。 5.Grafana:监控仪表盘 6.pushgateway: 各个目标主机可上报数据到pushgatewy,然后prometheus server统一从pushgateway拉取数据。 四、课程亮点 五、效果图展示 六、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等 七、课程大纲
1. 创建命名空间 首先我们需要创建一个命名空间,用于部署prometheus和其他相关组件。 ``` kubectl create namespace monitoring ``` 2. 创建configmap 接下来我们需要创建一个configmap,用于存储prometheus的配置文件。在这个例子中,我们将使用以下配置文件: ``` global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: prometheus static_configs: - targets: ['localhost:9090'] - job_name: node-exporter static_configs: - targets: ['localhost:9100'] ``` 我们将这个配置文件保存为prometheus.yaml,并使用以下命令创建configmap。 ``` kubectl create configmap prometheus-config --namespace=monitoring --from-file=prometheus.yaml ``` 3. 创建RBAC 我们需要为prometheus创建一些RBAC规则,以便它可以在Kubernetes集群中获取监控数据。 首先,我们需要创建一个ServiceAccount: ``` kubectl create serviceaccount prometheus --namespace=monitoring ``` 接下来,我们需要创建一个ClusterRole和一个ClusterRoleBinding: ``` kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-cluster-role.yaml kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-cluster-role-binding.yaml ``` 最后,我们需要创建一个Role和一个RoleBinding: ``` kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-role.yaml kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-role-binding.yaml ``` 4. 部署prometheus-operator 现在我们可以开始部署prometheus-operator了。prometheus-operator是一个自动化部署prometheus的工具。 kubectl apply -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/bundle.yaml 5. 部署prometheus 接下来我们需要部署prometheus本身。我们将使用prometheus-operator来完成自动化部署。 首先,我们需要创建一个Prometheus对象,它将自动创建一个Deployment和Service。 ``` apiVersion: monitoring.coreos.com/v1 kind: Prometheus metadata: name: prometheus labels: prometheus: prometheus spec: replicas: 1 serviceAccountName: prometheus serviceMonitorSelector: matchLabels: app: prometheus ruleSelector: matchLabels: prometheus: prometheus alerting: alertmanagers: - namespace: monitoring name: alertmanager port: web ``` 将上面的配置保存为prometheus.yaml,并使用以下命令创建Prometheus对象。 ``` kubectl apply -f prometheus.yaml ``` 6. 部署alertmanager 现在我们需要部署alertmanager,它将用于发送警报通知。 首先,我们需要创建一个configmap,用于存储alertmanager的配置文件。在这个例子中,我们将使用以下配置文件: ``` global: resolve_timeout: 5m route: receiver: 'default-receiver' group_by: - alertname receivers: - name: 'default-receiver' email_configs: - to: 'your-email@example.com' from: 'alertmanager@example.com' smarthost: smtp.gmail.com:587 auth_username: 'your-email@example.com' auth_identity: 'your-email@example.com' auth_password: 'your-password' ``` 我们将这个配置文件保存为alertmanager.yaml,并使用以下命令创建configmap。 ``` kubectl create configmap alertmanager-config --namespace=monitoring --from-file=alertmanager.yaml ``` 接下来,我们需要创建一个alertmanager对象,它将自动创建一个Deployment和Service。 ``` apiVersion: monitoring.coreos.com/v1 kind: Alertmanager metadata: name: alertmanager labels: alertmanager: alertmanager spec: replicas: 1 serviceAccountName: prometheus configSecret: name: alertmanager-config ``` 将上面的配置保存为alertmanager.yaml,并使用以下命令创建alertmanager对象。 ``` kubectl apply -f alertmanager.yaml ``` 7. 部署node-exporter 最后,我们需要部署node-exporter,它将用于收集节点的监控数据。 首先,我们需要创建一个DaemonSet,它将自动在每个节点上部署一个node-exporter。 ``` apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter labels: app: node-exporter spec: selector: matchLabels: app: node-exporter template: metadata: labels: app: node-exporter spec: containers: - name: node-exporter image: prom/node-exporter:v1.0.1 ports: - containerPort: 9100 name: http resources: limits: cpu: 200m memory: 200Mi requests: cpu: 100m memory: 100Mi volumeMounts: - name: proc mountPath: /host/proc readOnly: true - name: sys mountPath: /host/sys readOnly: true - name: docker mountPath: /host/var/lib/docker readOnly: true hostNetwork: true hostPID: true volumes: - name: proc hostPath: path: /proc - name: sys hostPath: path: /sys - name: docker hostPath: path: /var/lib/docker ``` 将上面的配置保存为node-exporter.yaml,并使用以下命令创建DaemonSet。 ``` kubectl apply -f node-exporter.yaml ``` 现在,您已经成功部署prometheus、alertmanagernode-exporter。您可以使用以下命令查看它们的状态: ``` kubectl get all -n monitoring ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值