🎨
Docker
  • Docker入门
  • Docker overview
    • 隔离与限制
    • copy-on-write(CoW: 写时复制)
    • docker中的联合文件系统
    • Docker存储
      • tmpfs mounts
      • volume
      • bind mounts
    • Docker网络
      • host模式
      • bridge模式
        • default bridge模式
        • User-defined bridge模式
      • none模式
      • container模式
  • 容器监控方式
    • prometheus + grafana+cAdvisor监控容器
  • Dockerfile指令
    • Usage
    • BuildKit
    • Format
    • Parser directives
      • syntax
      • escape
    • Environment replacement
    • FROM
      • 了解ARG和FROM之间的交互方式
    • .dockerignore file
    • RUN
    • CMD
    • COPY
    • ADD
    • ENV
    • WORKDIR
    • ARG
      • Impact on build caching(对构建缓存的影响)
      • Default(默认值)
      • Scope(作用域)
      • Using ARG Variables(使用ARG变量)
      • Predefined ARGS(预先定义好的ARG变量)
      • Automatic platform ARGs in the global scope(全局作用域中自动化平台的ARGS)
    • STOPSIGNAL
    • HEALTHCHECK
    • SHELL
    • ONBUILD
    • Dockerfile示例
    • LABEL
    • MAINTAINER (deprecated)
    • EXPOSE
    • ENTRYPOINT
      • Shell形式的ENTRYPOINT示例
      • 理解CMD和ENTRYPOINT是如何交互的
    • VOLUME
    • USER
  • docker-compose.yaml文件中常用指令
    • compose文件结构和示例
    • 服务配置参考
      • build
        • dockerfile
        • context
        • shm_size
        • network
        • labels
        • cache_from
        • args
        • target
      • cap_add, cap_drop
      • cgroup_parent
      • command
      • configs
        • 短语法形式
        • 长语法形式
      • container_name
      • credential_spec
        • EXAMPLE GMSA CONFIGURATION
      • depends_on
      • deploy
        • endpoint_mode
        • labels
        • mode
        • placement
        • max_replicas_per_node
        • replicas
        • resource
        • restart_policy
        • rollback_config
        • Update_config
        • Not supported for docker stack deploy
      • devices
      • dns
      • dns_search
      • entrypoint
      • env_file
      • environment
      • expose
      • external_links
      • extra_hosts
      • healthcheck
      • image
      • init
      • isolation
      • labels
      • links
      • logging
      • network_mode
      • networks
        • alias
        • IPV4_ADDRESS, IPV6_ADDRESS
      • pid
      • ports
        • 长语法
        • 短语法
      • profiles
      • restart
      • secrets
        • 长语法
        • 短语法
      • security_opt
      • stop_grace_period
      • stop_signal
      • sysctls
      • tmpfs
      • ulimits
      • userns_mode
      • volumes
        • 长语法
        • 短语法
        • VOLUMES FOR SERVICES, SWARMS, AND STACK FILES
      • domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, work
      • Specifying durations
      • Specifying byte values
      • Volume configuration reference
        • driver
        • driver_opts
        • external
        • labels
        • name
      • Network configuration reference
        • name
        • external
        • labels
        • internal
        • ipam
        • enable_ipv6
        • driver
          • overlay
          • bridge
          • host或者none
        • driver_opts
        • attachable
      • configs configuration reference
      • secrets configuration reference
      • Variable substitution
      • Extension fields
  • docker-compose示例
    • WordPress
    • PostgreSQL
    • Django和PostgreSQL
    • Rails和PostgreSQL
  • Dockerfile最佳实践
Powered by GitBook
On this page

Was this helpful?

  1. 容器监控方式

prometheus + grafana+cAdvisor监控容器

监控容器的运行状态我们需要使用下面三个组件:

cAdvisor (Container Advisor) : 用于收集正在运行的容器资源使用和性能信息。 https://github.com/google/cadvisor

Prometheus(普罗米修斯): 容器监控系统。 https://prometheus.io https://github.com/prometheus

Grafana: 是一个开源的度量分析和可视化系统。 https://grafana.com/grafana

如果要通过上面三个组件对容器进行监控,我们需要先执行下面三个步骤:

第一步:安装cAdvisor容器镜像

docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --volume=/dev/disk/:/dev/disk:ro \ --publish=8080:8080 \ --detach=true \ --name=cadvisor \ google/cadvisor:latest

第二步:安装grafana容器镜像

docker run -d --name=grafana -p 3000:3000 grafana/grafana

第三步:安装promethous容器镜像

docker run -d --name=prometheus -p 9090:9090 prom/prometheus

第四步: 查看三个容器是否运行正常。

liuyang@liuyangdeiMac ~ % docker ps -f name=prometheus -f name=grafana -f name=cadvisor --format="table {{.Names}}\t{{.Status}}\t{{.Ports}}"
NAMES        STATUS                 PORTS
prometheus   Up About an hour       0.0.0.0:9090->9090/tcp
grafana      Up 3 hours             0.0.0.0:3000->3000/tcp
cadvisor     Up 4 hours (healthy)   0.0.0.0:8080->8080/tcp

第五步: 通过浏览器访问http://localhost:8080, http://localhost:9090, http://localhost:3000分别可以看到图片中显示的内容:

上面的页面表示cAdvisor运行正常,并正确采集到了数据。cAdvisor在采集数据时,除了采集docker容器的数据,也会采集运行docker的主机的数据。这里我们点击/docker来进入到docker的数据页面:

上面罗列出来的就是我们运行在我们主机上的容器。

上图为prometheus的图形界面。

上图为grafana的图形监控界面。

如果能正常访问上面三个页面,表明我们的容器运行正常。接下来进行第六步。

第六步:通过下面的docker指令进入prometheus的容器内部,修改它的配置文件:

docker exec -it  bf99 /bin/sh

进入容器以后,修改/etc/prometheus/prometheus.yml配置文件。

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'docker'
    static_configs:
    - targets: ['169.254.239.63:8080']

在文件中,我们在指定位置添加一个新的任务,如上所示。添加完毕以后退出容器,执行下面的命令来重启容器:

docker restart bf99

重启以后,通过之前相同的检查手段来确保容器重启成功,防止配置文件语法错误等导致容器异常退出。

Note:在修改prometheus配置文件时,主机targets中的ip地址必须填主机的ip地址,而不是localhost,否则会导致prometheus无法从cAdvisor获取到数据,因为使用localhost或者127.0.0.1都是去访问普罗米修斯容器的本身;我们要使用宿主机ip+端口。

当上图中的targets状态都为up时,表明prometheus能够正常与目标服务器进行通信并在指定的ip地址和端口上获取它的数据。

第七步:确保数据能够成功抓取以后,我们就可以开始使用grafana来图形化的显示抓取到的数据。

根据图中所示对grafana进行配置,将prometheus作为它的数据源。

在上图中相应的位置填好数据并保存。

配置好以后创建一个Dashboard。

在图中Metrics空格中填入相应的promql语句来获取想要现实的数据。上图显示了cpu的相关数据。除了自己创建Dashboard,我们也可以从grafana官网来导入别人已经配置好的Dashboard。具体参考grafana官方网站。

通过上面的步骤我们完整的演示了如何通过grafana+prometheus+cAdvisor来监控容器的运行指标。

在这里我们通过启动三个容器来配置这些服务。为了减少麻烦,我们也可以将三个容器的配置信息直接写入docker-compose.yaml文件中,通过Compose来对容器进行管理。

Previous容器监控方式NextDockerfile指令

Last updated 4 years ago

Was this helpful?