• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

cAdvisor介绍

Docker 蜷缩的蜗牛 8个月前 (01-23) 12次浏览 已收录

1. cAdvisor 简介

​ cAdvisor 对 Node 机器上的资源及容器进行实时监控和性能数据采集,包括 CPU 使用情况、内存使用情况、网络吞吐量及文件系统使用情况,cAdvisor 集成在 Kubelet 中,当 kubelet 启动时会自动启动 cAdvisor,即一个 cAdvisor 仅对一台 Node 机器进行监控。kubelet 的启动参数–cadvisor-port 可以定义 cAdvisor 对外提供服务的端口,默认为 4194。可以通过浏览器访问。项目主页:http://github.com/google/cadvisor

2. cAdvisor 结构图

这里写图片描述

3. Metrics

分类 字段 描述
cpu cpu_usage_total
cpu_usage_system
cpu_usage_user
cpu_usage_per_cpu
load_average Smoothed average of number of runnable threads x 1000
memory memory_usage Memory Usage
memory_working_set Working set size
network rx_bytes Cumulative count of bytes received
rx_errors Cumulative count of receive errors encountered
tx_bytes Cumulative count of bytes transmitted
tx_errors Cumulative count of transmit errors encountered
filesystem fs_device Filesystem device
fs_limit Filesystem limit
fs_usage Filesystem usage

4. cAdvisor 源码

4.1. cAdvisor 入口函数

cadvisor.go

<code class="language-go hljs "><span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">defer</span> glog.Flush()
    flag.Parse()
    <span class="hljs-keyword">if</span> *versionFlag {
        fmt.Printf(<span class="hljs-string">"cAdvisor version %s (%s)\n"</span>, version.Info[<span class="hljs-string">"version"</span>], version.Info[<span class="hljs-string">"revision"</span>])
        os.Exit<span class="hljs-number">(0</span>)
    }
    setMaxProcs()
    memoryStorage, err := NewMemoryStorage()
    <span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
        glog.Fatalf(<span class="hljs-string">"Failed to initialize storage driver: %s"</span>, err)
    }
    sysFs, err := sysfs.NewRealSysFs()
    <span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
        glog.Fatalf(<span class="hljs-string">"Failed to create a system interface: %s"</span>, err)
    }
    collectorHttpClient := createCollectorHttpClient(*collectorCert, *collectorKey)
    containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient)
    <span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
        glog.Fatalf(<span class="hljs-string">"Failed to create a Container Manager: %s"</span>, err)
    }
    mux := http.NewServeMux()
    <span class="hljs-keyword">if</span> *enableProfiling {
        mux.HandleFunc(<span class="hljs-string">"/debug/pprof/"</span>, pprof.Index)
        mux.HandleFunc(<span class="hljs-string">"/debug/pprof/cmdline"</span>, pprof.Cmdline)
        mux.HandleFunc(<span class="hljs-string">"/debug/pprof/profile"</span>, pprof.Profile)
        mux.HandleFunc(<span class="hljs-string">"/debug/pprof/symbol"</span>, pprof.Symbol)
    }
    <span class="hljs-comment">// Register all HTTP handlers.</span>
    err = cadvisorhttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm)
    <span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
        glog.Fatalf(<span class="hljs-string">"Failed to register HTTP handlers: %v"</span>, err)
    }
    cadvisorhttp.RegisterPrometheusHandler(mux, containerManager, *prometheusEndpoint, <span class="hljs-constant">nil</span>)
    <span class="hljs-comment">// Start the manager.</span>
    <span class="hljs-keyword">if</span> err := containerManager.Start(); err != <span class="hljs-constant">nil</span> {
        glog.Fatalf(<span class="hljs-string">"Failed to start container manager: %v"</span>, err)
    }
    <span class="hljs-comment">// Install signal handler.</span>
    installSignalHandler(containerManager)
    glog.Infof(<span class="hljs-string">"Starting cAdvisor version: %s-%s on port %d"</span>, version.Info[<span class="hljs-string">"version"</span>], version.Info[<span class="hljs-string">"revision"</span>], *argPort)
    addr := fmt.Sprintf(<span class="hljs-string">"%s:%d"</span>, *argIp, *argPort)
    glog.Fatal(http.ListenAndServe(addr, mux))
}</code>

核心代码:

<code class="language-go hljs ">memoryStorage, err := NewMemoryStorage()
sysFs, err := sysfs.NewRealSysFs()
#创建 containerManager
containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient)
#启动 containerManager
err := containerManager.Start()</code>

4.2. cAdvisor Client 的使用

<code class="language-go hljs "><span class="hljs-keyword">import</span> <span class="hljs-string">"github.com/google/cadvisor/client"</span>
<span class="hljs-keyword">func</span> main(){
    client, err := client.NewClient(<span class="hljs-string">"http://192.168.19.30:4194/"</span>)   <span class="hljs-comment">//http://<host-ip>:<port>/</span>
}</code>

4.2.1 client 定义

cadvisor/client/client.go

<code class="language-go hljs "><span class="hljs-comment">// Client represents the base URL for a cAdvisor client.</span>
<span class="hljs-keyword">type</span> Client <span class="hljs-keyword">struct</span> {
    baseUrl <span class="hljs-typename">string</span>
}
<span class="hljs-comment">// NewClient returns a new v1.3 client with the specified base URL.</span>
<span class="hljs-keyword">func</span> NewClient(url <span class="hljs-typename">string</span>) (*Client, error) {
    <span class="hljs-keyword">if</span> !strings.HasSuffix(url, <span class="hljs-string">"/"</span>) {
        url += <span class="hljs-string">"/"</span>
    }
    <span class="hljs-keyword">return</span> &Client{
        baseUrl: fmt.Sprintf(<span class="hljs-string">"%sapi/v1.3/"</span>, url),
    }, <span class="hljs-constant">nil</span>
}</code>

4.2.2. client 方法

1)MachineInfo

<code class="language-go hljs "><span class="hljs-comment">// MachineInfo returns the JSON machine information for this client.</span>
<span class="hljs-comment">// A non-nil error result indicates a problem with obtaining</span>
<span class="hljs-comment">// the JSON machine information data.</span>
<span class="hljs-keyword">func</span> (self *Client) MachineInfo() (minfo *v1.MachineInfo, err error) {
       u := self.machineInfoUrl()
       ret := <span class="hljs-built_in">new</span>(v1.MachineInfo)
       <span class="hljs-keyword">if</span> err = self.httpGetJsonData(ret, <span class="hljs-constant">nil</span>, u, <span class="hljs-string">"machine info"</span>); err != <span class="hljs-constant">nil</span> {
              <span class="hljs-keyword">return</span>
       }
       minfo = ret
       <span class="hljs-keyword">return</span>
}</code>

2)ContainerInfo

<code class="language-go hljs "><span class="hljs-comment">// ContainerInfo returns the JSON container information for the specified</span>
<span class="hljs-comment">// container and request.</span>
<span class="hljs-keyword">func</span> (self *Client) ContainerInfo(name <span class="hljs-typename">string</span>, query *v1.ContainerInfoRequest) (cinfo *v1.ContainerInfo, err error) {
       u := self.containerInfoUrl(name)
       ret := <span class="hljs-built_in">new</span>(v1.ContainerInfo)
       <span class="hljs-keyword">if</span> err = self.httpGetJsonData(ret, query, u, fmt.Sprintf(<span class="hljs-string">"container info for %q"</span>, name)); err != <span class="hljs-constant">nil</span> {
              <span class="hljs-keyword">return</span>
       }
       cinfo = ret
       <span class="hljs-keyword">return</span>
}</code>

3)DockerContainer

<code class="language-go hljs "><span class="hljs-comment">// Returns the JSON container information for the specified</span>
<span class="hljs-comment">// Docker container and request.</span>
<span class="hljs-keyword">func</span> (self *Client) DockerContainer(name <span class="hljs-typename">string</span>, query *v1.ContainerInfoRequest) (cinfo v1.ContainerInfo, err error) {
       u := self.dockerInfoUrl(name)
       ret := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]v1.ContainerInfo)
       <span class="hljs-keyword">if</span> err = self.httpGetJsonData(&ret, query, u, fmt.Sprintf(<span class="hljs-string">"Docker container info for %q"</span>, name)); err != <span class="hljs-constant">nil</span> {
              <span class="hljs-keyword">return</span>
       }
       <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(ret) !=<span class="hljs-number"> 1</span> {
              err = fmt.Errorf(<span class="hljs-string">"expected to only receive 1 Docker container: %+v"</span>, ret)
              <span class="hljs-keyword">return</span>
       }
       <span class="hljs-keyword">for</span> _, cont := <span class="hljs-keyword">range</span> ret {
              cinfo = cont
       }
       <span class="hljs-keyword">return</span>
}</code>

4)AllDockerContainers

<code class="language-go hljs "><span class="hljs-comment">// Returns the JSON container information for all Docker containers.</span>
<span class="hljs-keyword">func</span> (self *Client) AllDockerContainers(query *v1.ContainerInfoRequest) (cinfo []v1.ContainerInfo, err error) {
       u := self.dockerInfoUrl(<span class="hljs-string">"/"</span>)
       ret := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]v1.ContainerInfo)
       <span class="hljs-keyword">if</span> err = self.httpGetJsonData(&ret, query, u, <span class="hljs-string">"all Docker containers info"</span>); err != <span class="hljs-constant">nil</span> {
              <span class="hljs-keyword">return</span>
       }
       cinfo = <span class="hljs-built_in">make</span>([]v1.ContainerInfo,<span class="hljs-number"> 0</span>, <span class="hljs-built_in">len</span>(ret))
       <span class="hljs-keyword">for</span> _, cont := <span class="hljs-keyword">range</span> ret {
              cinfo = <span class="hljs-built_in">append</span>(cinfo, cont)
       }
       <span class="hljs-keyword">return</span>
}</code>

文章参考:http://blog.opskumu.com/cadvisor.html

本文转载自
cAdvisor 介绍


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明cAdvisor 介绍
喜欢 (0)
[]
分享 (0)