一个简单的类似 jquery的 ajax get 函数

function get(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.overrideMimeType('text/plain; charset=x-user-defined');

    xhr.onreadystatechange = function(e) {
      if (this.readyState == 4 && this.status == 200) {
            callback(this.responseText);
      }
    };
    xhr.send();
}

docker cgroup - [docker cookbook] 读书笔记 2

Control Groups (cgroups) provide resource limitations and accounting for containers. From the Linux Kernel documentation:

Control Groups provide a mechanism for aggregating/partitioning sets
of tasks, and all their future children, into hierarchical groups with
specialized behaviour.

In simple terms, they can be compared to the ulimit shell command or the setrlimit system call. Instead of setting the resource limit to a single process, cgroups allow the limiting of resources to a group of processes.

Control groups are split into different subsystems, such as CPU, CPU sets, memory block I/O, and so on. Each subsystem can be used independently or can be grouped with others. The features that cgroups provide are:

  1. Resource limiting: For example, one cgroup can be bound to specific
    CPUs, so all processes in that group would run off given CPUs only
  2. Prioritization: Some groups may get a larger share of CPUs
  3. Accounting: You can measure the resource usage of different
    subsystems for billing
  4. Control: Freezing and restarting groups

Some of the subsystems that can be managed by cgroups are as follows:

  • blkio: It sets I/O access to and from block devices such as disk,
    SSD, and so on
  • Cpu: It limits access to CPU
  • Cpuacct: It generates CPU resource utilization
  • Cpuset: It assigns the CPUs on a multicore system to tasks in a
    cgroup
  • Devices: It devises access to a set of tasks in a cgroup
  • Freezer: It suspends or resumes tasks in a cgroup
  • Memory: It sets limits on memory use by tasks in a cgroup

There are multiple ways to control work with cgroups. Two of the most popular ones are accessing the cgroup virtual filesystem manually and accessing it with the libcgroup library.

docker namespace

There are different types of namespaces and each one of them isolates applications from each other. They are created using the clone system call. One can also attach to existing namespaces.

  1. The pid namespace allows each container to have its own process
    numbering. Each pid forms its own process hierarchy. A parent
    namespace can see the children namespaces and affect them, but a
    child can neither see the parent namespace nor affect it.
  2. The net namespace allows us to have different network interfaces on
    each container, like port. Each net namespace has its own routing
    table and firewall rules.
  3. ipc namespace sepratate IPC (Inter Process Communication) between
    different container's process;
  4. with mnt namespace, a container can have its own set of mounted
    filesystems and root directories, enhenance chroot.
  5. With uts namespace, we can have different hostnames for each
    container.
  6. With user namespace support, we can have users who have a nonzero ID
    on the host but can have a zero ID inside the container.

There are ways to share namespaces between the host and container and container and container.

摘自book: docker cookbook, 第一章 introduction and Installation, 第一节 Introduction

[using docker] 读书笔记 4

1) It’s important to set the USER statement in all your Dockerfiles (or change user within any ENTRYPOINT / CMD scripts). If you don’t do this, your processes will be running as root within the container. As UIDs are the same within a container and on the host, should an attacker manage to break the container, they will have root access to the host machine.

2) 查看container 的CPU, 内存, 网络使用情况
docker stats $(docker inspect -f {{.Name}} $(docker ps -q))

3) cAdvisor aggregates and processes various stats and also makes these available through a REST API, for further processing and storage.