• 1. cAdvisor简介
  • 2. cAdvisor结构图
  • 3. Metrics
  • 4. cAdvisor源码
    • 4.1. cAdvisor入口函数
    • 4.2. cAdvisor Client的使用
    • 4.2.1 client定义
    • 4.2.2. client方法

    title: “[Kubernetes] cAdvisor介绍”
    catalog: true
    date: 2017-08-13 10:50:57
    type: “categories”
    subtitle:
    header-img:
    tags:

    • Kubernetes
      catagories:
    • Kubernetes

    1. cAdvisor简介

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

    2. cAdvisor结构图

    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

    1. func main() {
    2. defer glog.Flush()
    3. flag.Parse()
    4. if *versionFlag {
    5. fmt.Printf("cAdvisor version %s (%s)/n", version.Info["version"], version.Info["revision"])
    6. os.Exit(0)
    7. }
    8. setMaxProcs()
    9. memoryStorage, err := NewMemoryStorage()
    10. if err != nil {
    11. glog.Fatalf("Failed to initialize storage driver: %s", err)
    12. }
    13. sysFs, err := sysfs.NewRealSysFs()
    14. if err != nil {
    15. glog.Fatalf("Failed to create a system interface: %s", err)
    16. }
    17. collectorHttpClient := createCollectorHttpClient(*collectorCert, *collectorKey)
    18. containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient)
    19. if err != nil {
    20. glog.Fatalf("Failed to create a Container Manager: %s", err)
    21. }
    22. mux := http.NewServeMux()
    23. if *enableProfiling {
    24. mux.HandleFunc("/debug/pprof/", pprof.Index)
    25. mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
    26. mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
    27. mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
    28. }
    29. // Register all HTTP handlers.
    30. err = cadvisorhttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm)
    31. if err != nil {
    32. glog.Fatalf("Failed to register HTTP handlers: %v", err)
    33. }
    34. cadvisorhttp.RegisterPrometheusHandler(mux, containerManager, *prometheusEndpoint, nil)
    35. // Start the manager.
    36. if err := containerManager.Start(); err != nil {
    37. glog.Fatalf("Failed to start container manager: %v", err)
    38. }
    39. // Install signal handler.
    40. installSignalHandler(containerManager)
    41. glog.Infof("Starting cAdvisor version: %s-%s on port %d", version.Info["version"], version.Info["revision"], *argPort)
    42. addr := fmt.Sprintf("%s:%d", *argIp, *argPort)
    43. glog.Fatal(http.ListenAndServe(addr, mux))
    44. }

    核心代码:

    1. memoryStorage, err := NewMemoryStorage()
    2. sysFs, err := sysfs.NewRealSysFs()
    3. #创建containerManager
    4. containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient)
    5. #启动containerManager
    6. err := containerManager.Start()

    4.2. cAdvisor Client的使用

    1. import "github.com/google/cadvisor/client"
    2. func main(){
    3. client, err := client.NewClient("http://192.168.19.30:4194/") //http://<host-ip>:<port>/
    4. }

    4.2.1 client定义

    cadvisor/client/client.go

    1. // Client represents the base URL for a cAdvisor client.
    2. type Client struct {
    3. baseUrl string
    4. }
    5. // NewClient returns a new v1.3 client with the specified base URL.
    6. func NewClient(url string) (*Client, error) {
    7. if !strings.HasSuffix(url, "/") {
    8. url += "/"
    9. }
    10. return &Client{
    11. baseUrl: fmt.Sprintf("%sapi/v1.3/", url),
    12. }, nil
    13. }

    4.2.2. client方法

    1)MachineInfo

    1. // MachineInfo returns the JSON machine information for this client.
    2. // A non-nil error result indicates a problem with obtaining
    3. // the JSON machine information data.
    4. func (self *Client) MachineInfo() (minfo *v1.MachineInfo, err error) {
    5. u := self.machineInfoUrl()
    6. ret := new(v1.MachineInfo)
    7. if err = self.httpGetJsonData(ret, nil, u, "machine info"); err != nil {
    8. return
    9. }
    10. minfo = ret
    11. return
    12. }

    2)ContainerInfo

    1. // ContainerInfo returns the JSON container information for the specified
    2. // container and request.
    3. func (self *Client) ContainerInfo(name string, query *v1.ContainerInfoRequest) (cinfo *v1.ContainerInfo, err error) {
    4. u := self.containerInfoUrl(name)
    5. ret := new(v1.ContainerInfo)
    6. if err = self.httpGetJsonData(ret, query, u, fmt.Sprintf("container info for %q", name)); err != nil {
    7. return
    8. }
    9. cinfo = ret
    10. return
    11. }

    3)DockerContainer

    1. // Returns the JSON container information for the specified
    2. // Docker container and request.
    3. func (self *Client) DockerContainer(name string, query *v1.ContainerInfoRequest) (cinfo v1.ContainerInfo, err error) {
    4. u := self.dockerInfoUrl(name)
    5. ret := make(map[string]v1.ContainerInfo)
    6. if err = self.httpGetJsonData(&ret, query, u, fmt.Sprintf("Docker container info for %q", name)); err != nil {
    7. return
    8. }
    9. if len(ret) != 1 {
    10. err = fmt.Errorf("expected to only receive 1 Docker container: %+v", ret)
    11. return
    12. }
    13. for _, cont := range ret {
    14. cinfo = cont
    15. }
    16. return
    17. }

    4)AllDockerContainers

    1. // Returns the JSON container information for all Docker containers.
    2. func (self *Client) AllDockerContainers(query *v1.ContainerInfoRequest) (cinfo []v1.ContainerInfo, err error) {
    3. u := self.dockerInfoUrl("/")
    4. ret := make(map[string]v1.ContainerInfo)
    5. if err = self.httpGetJsonData(&ret, query, u, "all Docker containers info"); err != nil {
    6. return
    7. }
    8. cinfo = make([]v1.ContainerInfo, 0, len(ret))
    9. for _, cont := range ret {
    10. cinfo = append(cinfo, cont)
    11. }
    12. return
    13. }