9-資源限制

9-資源限制

了解stress工具

stress是一個壓力測試工具

docker run -it ubuntu:16.04

此時會進入ubuntu內,這時我們安裝 stress工具

apt-get update && apt-get install -y stress

查看help

stress --help
Usage: stress [OPTION [ARG]] ...-?, --help         show this help statement--version      show version statement-v, --verbose      be verbose-q, --quiet        be quiet-n, --dry-run      show what would have been done-t, --timeout N    timeout after N seconds--backoff N    wait factor of N microseconds before work starts-c, --cpu N        spawn N workers spinning on sqrt()-i, --io N         spawn N workers spinning on sync()-m, --vm N         spawn N workers spinning on malloc()/free()--vm-bytes B   malloc B bytes per vm worker (default is 256MB)--vm-stride B  touch a byte every B bytes (default is 4096)--vm-hang N    sleep N secs before free (default none, 0 is inf)--vm-keep      redirty memory instead of freeing and reallocating-d, --hdd N        spawn N workers spinning on write()/unlink()--hdd-bytes B  write B bytes per hdd worker (default is 1GB)
-? 顯示幫助信息
-v 顯示版本號
-q 不顯示運行信息
-n,--dry-run 顯示已經完成的指令執行情況
-t --timeout N 指定運行N秒后停止--backoff N 等待N微妙后開始運行
-c --cpu 產生n個進程 每個進程都反復不停的計算隨機數的平方根
-i --io  產生n個進程 每個進程反復調用sync(),sync()用于將內存上的內容寫到硬盤上
-m --vm n 產生n個進程,每個進程不斷調用內存分配malloc和內存釋放free函數--vm-bytes B 指定malloc時內存的字節數 (默認256MB)--vm-hang N 指示每個消耗內存的進程在分配到內存后轉入休眠狀態,與正常的無限分配和釋放內存的處理相反,這有利于模擬只有少量內存的機器
-d --hadd n 產生n個執行write和unlink函數的進程--hadd-bytes B 指定寫的字節數,默認是1GB--hadd-noclean 不要將寫入隨機ASCII數據的文件Unlink時間單位可以為秒s,分m,小時h,天d,年y,文件大小單位可以為K,M,G

重點說幾個重要的參數

  • --vm 創建幾個進程
  • --vm-bytes 創建的內存大小

例子1.

stress --vm 1

結果

root@aec1c5bc8396:/# stress --vm 1
stress: info: [221] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd

此時stress會反復創建 256MB 內存 然后釋放。

這里log不是很詳細,可以加--verbose查看詳細log輸出

例子2.

stress --vm 1 --vm-bytes 500000M --verbose

此時會報錯,因為分配的內存超出了虛擬機的內存。

我們可以看看虛擬機的內存。

top
Tasks:   2 total,   1 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   498892 total,   219204 free,   172276 used,   107412 buff/cache
KiB Swap:  2097148 total,  2084092 free,    13056 used.   286196 avail Mem 

我們可以看到虛擬機的內存為 498892KB

構建一個stress的鏡像

  1. 創建一個文件夾

    mkdir stress
    
  2. 在stress文件夾內創建Dockerfile

    FROM ubuntu:16.04
    RUN apt-get update && apt-get install -y stress
    ENTRYPOINT ["/usr/bin/stress"]
    
  3. 構建鏡像

    docker build -t ubuntu-stress .
    
  4. 測試鏡像使用

    docker run -it --rm ubuntu-stress --vm 1
    

    此時這個容器就可以當做stress命令使用了。

容器的資源配置

這個資源是 例如 cpu 內存(包含虛擬內存)。

在創建容器的時候是可以指定一些參數的,我們來看一下有哪些參數。

docker run --help
Options:--add-host list                  Add a custom host-to-IP mapping (host:ip)-a, --attach list                    Attach to STDIN, STDOUT or STDERR--blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)--blkio-weight-device list       Block IO weight (relative device weight) (default [])--cap-add list                   Add Linux capabilities--cap-drop list                  Drop Linux capabilities--cgroup-parent string           Optional parent cgroup for the container--cidfile string                 Write the container ID to the file--cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period--cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota--cpu-rt-period int              Limit CPU real-time period in microseconds--cpu-rt-runtime int             Limit CPU real-time runtime in microseconds-c, --cpu-shares int                 CPU shares (relative weight)--cpus decimal                   Number of CPUs--cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)-d, --detach                         Run container in background and print container ID--detach-keys string             Override the key sequence for detaching a container--device list                    Add a host device to the container--device-cgroup-rule list        Add a rule to the cgroup allowed devices list--device-read-bps list           Limit read rate (bytes per second) from a device (default [])--device-read-iops list          Limit read rate (IO per second) from a device (default [])--device-write-bps list          Limit write rate (bytes per second) to a device (default [])--device-write-iops list         Limit write rate (IO per second) to a device (default [])--disable-content-trust          Skip image verification (default true)--dns list                       Set custom DNS servers--dns-option list                Set DNS options--dns-search list                Set custom DNS search domains--entrypoint string              Overwrite the default ENTRYPOINT of the image-e, --env list                       Set environment variables--env-file list                  Read in a file of environment variables--expose list                    Expose a port or a range of ports--group-add list                 Add additional groups to join--health-cmd string              Command to run to check health--health-interval duration       Time between running the check (ms|s|m|h) (default 0s)--health-retries int             Consecutive failures needed to report unhealthy--health-start-period duration   Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)--health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)--help                           Print usage-h, --hostname string                Container host name--init                           Run an init inside the container that forwards signals and reaps processes-i, --interactive                    Keep STDIN open even if not attached--ip string                      IPv4 address (e.g., 172.30.100.104)--ip6 string                     IPv6 address (e.g., 2001:db8::33)--ipc string                     IPC mode to use--isolation string               Container isolation technology--kernel-memory bytes            Kernel memory limit-l, --label list                     Set meta data on a container--label-file list                Read in a line delimited file of labels--link list                      Add link to another container--link-local-ip list             Container IPv4/IPv6 link-local addresses--log-driver string              Logging driver for the container--log-opt list                   Log driver options--mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)-m, --memory bytes                   Memory limit--memory-reservation bytes       Memory soft limit--memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap--memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)--mount mount                    Attach a filesystem mount to the container--name string                    Assign a name to the container--network string                 Connect a container to a network (default "default")--network-alias list             Add network-scoped alias for the container--no-healthcheck                 Disable any container-specified HEALTHCHECK--oom-kill-disable               Disable OOM Killer--oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)--pid string                     PID namespace to use--pids-limit int                 Tune container pids limit (set -1 for unlimited)--privileged                     Give extended privileges to this container-p, --publish list                   Publish a container's port(s) to the host-P, --publish-all                    Publish all exposed ports to random ports--read-only                      Mount the container's root filesystem as read only--restart string                 Restart policy to apply when a container exits (default "no")--rm                             Automatically remove the container when it exits--runtime string                 Runtime to use for this container--security-opt list              Security Options--shm-size bytes                 Size of /dev/shm--sig-proxy                      Proxy received signals to the process (default true)--stop-signal string             Signal to stop a container (default "SIGTERM")--stop-timeout int               Timeout (in seconds) to stop a container--storage-opt list               Storage driver options for the container--sysctl map                     Sysctl options (default map[])--tmpfs list                     Mount a tmpfs directory-t, --tty                            Allocate a pseudo-TTY--ulimit ulimit                  Ulimit options (default [])-u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])--userns string                  User namespace to use--uts string                     UTS namespace to use-v, --volume list                    Bind mount a volume--volume-driver string           Optional volume driver for the container--volumes-from list              Mount volumes from the specified container(s)-w, --workdir string                 Working directory inside the container

內內存的限制

參數

  • --memory
  • --memory-swap

如果我們只是限制了memory而沒有限制 memory-swap 那么 memory-swap 會和 memory 一樣。

列子1.

限定 200M 內存

docker run --memory=200M ubuntu-stress --vm 1 --verbose

壓力測試 500M

docker run --memory=200M ubuntu-stress --vm 1 --verbose --vm-bytes 500M

結果

stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [6] forked
stress: dbug: [6] allocating 524288000 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (415) <-- worker 6 got signal 9
stress: WARN: [1] (417) now reaping child worker processes
stress: FAIL: [1] (421) kill error: No such process
stress: FAIL: [1] (451) failed run completed in 1s

cpu限制

參數

  • --cpu-shares 是一個相對權重。例如: 有兩個容器,一個設定了10,一個設定了5,這樣的話,兩個容器如果占滿了cpu,那么這個個數的比例就是權重,10個cpu的百分比是5個cpu的百分比的兩倍。

例子1.

  1. 打開三個虛擬機的窗口,通過vagrant ssh

  2. 第一個窗口執行top命令

  3. 第二個執行ubuntu-stress容器

    docker run --name test2 --cpu-shares=5 ubuntu-stress --cpu 1
    

    此時我們查看第一個窗口的top

    8875 root      20   0    7472     96      0 R 99.7  0.0   0:06.99 stress
    

    cpu已經占用已經快 100% 了。

  4. 第三個窗口執行ubuntu-stress容器

    docker run --name test1 --cpu-shares=10 ubuntu-stress --cpu 1
    

    此時我們查看第一個窗口的top

     8956 root      20   0    7472     96      0 R 66.1  0.0   0:03.54 stress                                                                                     
    8875 root      20   0    7472     96      0 R 33.2  0.0   1:21.26 stress
    

    我們可以看到 8875 進程的cpu占用率編程 33 了,另一個是 66,大約是兩倍的關系。

所以 --cpu-shares 的含義并不是指定使用了多少顆cpu,而是一個比例,默認值為 1024,最小為2,只有在容器競爭資源的時候才有意義,如果只有一個容器,即使設置為2,那么也是占用當前設備的 100% cpu資源。

限制可用的 CPU 個數

當前系統為 2 個cpu

為容器分配2個cpu,并且使用一個cpu

  1. 在其中一個終端執行
docker run --rm --cpus=2 ubuntu-stress --cpu 1
  1. 另外一個終端執行top
 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                   5120 root      20   0    7472     96      0 R  99.7  0.0   0:54.48 stress

查看宿主cpu使用情況
在這里插入圖片描述

也可以通過 top 命令 然后按 1 查看。

可以看到 5120 進程 占用了 100%,此時只有一個cpu被占用,那么我們使用2個cpu做一下測試

docker run --rm --cpus=2 ubuntu-stress --cpu 2

查看top

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                   5234 root      20   0    7472     96      0 R  99.7  0.0   0:13.47 stress                                                                                    5233 root      20   0    7472     96      0 R  99.3  0.0   0:13.38 stress 

可以看到 2 個進程消耗了cpu 100%,說明兩個cpu都被充分利用。

指定固定的 CPU

通過 --cpus 選項我們無法讓容器始終在一個或某幾個 CPU 上運行,但是通過 --cpuset-cpus 選項卻可以做到!這是非常有意義的,因為現在的多核系統中每個核心都有自己的緩存,如果頻繁的調度進程在不同的核心上執行勢必會帶來緩存失效等開銷。下面我們就演示如何設置容器使用固定的 CPU,下面的命令為容器設置了 --cpuset-cpus 選項,指定運行容器的 CPU 編號為 1:

docker run --rm --cpuset-cpus="1" ubuntu-stress --cpu 1

top 終端內按 數字 1

%Cpu0  :  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

可以看到是在編號為 1 的cpu上運行。

資源限制的底層技術支持

  • Namespace : 做隔離 pid, net, ipc, mnt, uts
  • Control groups : 做資源限制
  • Union file systems : Container 和 image的分層

隔離性 Linux namespace

每個用戶實例之間相互隔離, 互不影響。 一般的硬件虛擬化方法給出的方法是VM,而LXC給出的方法是container,更細一點講就是kernel namespace。其中pid、net、ipc、mnt、uts、user等namespace將container的進程、網絡、消息、文件系統、UTS(“UNIX Time-sharing System”)和用戶空間隔離開。

  1. pid namespace

    不同用戶的進程就是通過pid namespace隔離開的,且不同 namespace 中可以有相同pid。所有的LXC進程在docker中的父進程為docker進程,每個lxc進程具有不同的namespace。同時由于允許嵌套,因此可以很方便的實現 Docker in Docker。

  2. net namespace

    有了 pid namespace, 每個namespace中的pid能夠相互隔離,但是網絡端口還是共享host的端口。網絡隔離是通過net namespace實現的, 每個net namespace有獨立的 network devices, IP addresses, IP routing tables, /proc/net 目錄。這樣每個container的網絡就能隔離開來。docker默認采用veth的方式將container中的虛擬網卡同host上的一個docker bridge: docker0連接在一起。

  3. ipc namespace

    container中進程交互還是采用linux常見的進程間交互方法(interprocess communication - IPC), 包括常見的信號量、消息隊列和共享內存。然而同 VM 不同的是,container 的進程間交互實際上還是host上具有相同pid namespace中的進程間交互,因此需要在IPC資源申請時加入namespace信息 - 每個IPC資源有一個唯一的 32 位 ID。

  4. mnt namespace

    類似chroot,將一個進程放到一個特定的目錄執行。mnt namespace允許不同namespace的進程看到的文件結構不同,這樣每個 namespace 中的進程所看到的文件目錄就被隔離開了。同chroot不同,每個namespace中的container在/proc/mounts的信息只包含所在namespace的mount point。

  5. uts namespace

    UTS(“UNIX Time-sharing System”) namespace允許每個container擁有獨立的hostname和domain name, 使其在網絡上可以被視作一個獨立的節點而非Host上的一個進程。

  6. user namespace

    每個container可以有不同的 user 和 group id, 也就是說可以在container內部用container內部的用戶執行程序而非Host上的用戶。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536037.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536037.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536037.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

10-Docker 網絡

10-Docker 網絡 基礎網絡概念 數據傳輸通過數據包 兩臺筆記本之間通信是通過數據包通信的。想知道數據包是怎么打包的呢&#xff0c;就先要知道網絡分層的概念。 網絡分層 目前有兩種網絡分層的模型。 ISO/OSI 分層&#xff0c;也就是 7 層模型。TCP/IP 分層&#xff0c;也就…

11-Docker Bridge詳解

11-Docker Bridge詳解 容器之間是如何通信的&#xff1f; 操作前刪除test2的容器。 查看當前機器上docker的網絡 docker network lsNETWORK ID NAME DRIVER SCOPE 056d0ece100f bridge bridge local a…

12-容器之間link

12-容器之間link 這篇主要講 容器之間如何 link。 需要準備的是&#xff0c;創建兩個容器 test1 和 test2 通過 busybox。 在之前的課程中已經創建的只需要重新啟動即可。 什么情況下需要link 例如有一個容器是 web 服務器&#xff0c;并且跑在一個docker容器內&#xff0c;…

13-容器的端口映射

13-容器的端口映射 部署一個簡單web nginx容器 docker run -d --name web nginxnginx 默認的端口是 80 端口&#xff0c;此時我們是沒有辦法訪問的。 好的&#xff0c;通過前面的學習我們已經知道&#xff0c;這個 web 容器四連接到 bridge 網橋上的&#xff0c;那我們查看一…

14-容器網絡之host和none

14-容器網絡之host和none 在之前的小節&#xff0c;我們有看到過 host 和 none。 通過 docker network ls 查看。 none 網絡 刪除 test1 容器 docker stop test1 && docker rm test1創建 test1 容器并連接到none網絡 docker run -d --name test1 --network none busy…

15-多容器復雜應用的部署

15-多容器復雜應用的部署 此節主要是通過部署一個復雜的應用場景&#xff0c;進而練習容器的網絡相關知識。 創建一個flask-web應用 創建一個 flask-web 文件夾 mkdir flask-web在此文件夾內創建 app.py 文件 cd flask-web touch app.py編寫一個簡單的 web 程序 import os im…

16-多機器通信

16-多機器通信 回顧上節課的思考題。 flask-redis 想訪問 redis&#xff0c;該如何通信&#xff1f; 創建另外一臺linux虛擬機 直接拷貝Vagrantfile文件創建一個 centos 虛擬機 我們創建一個centos7-2文件夾&#xff0c;然后創建虛擬機 vagrant up創建的過程中提示選擇網絡接…

17-Docker的數據持久化介紹

17-Docker的數據持久化介紹 回顧容器與鏡像 容器是在鏡像之上創建的一層運行時層&#xff0c;這一層是可以讀寫的&#xff0c;我們能夠在容器內添加數據&#xff0c;讀取數據。 也就是說我們在創建容器的時候&#xff0c;我們能夠在容器內創建文件&#xff0c;安裝軟件等等&a…

18-數據持久化-Data Volume

18-數據持久化-Data Volume Data Volume 使用場景。一般來講有些容器自己會產生一些數據&#xff0c;我們不想數據隨著容器的銷毀而銷毀&#xff0c;我們想保存數據&#xff0c;正常一般用在數據庫&#xff0c;比如我們想保存數據庫中的數據&#xff0c;這個時候我們就會使用 …

19-數據持久化-Bind Mounting

19-數據持久化-Bind Mounting Bind Mounting 與 Data Volume區別 Data Volume 需要在 Dockerfile 內聲明需要創建的 volume 目錄。 Bind Mounting 則不需要在 Dockerfile 聲明 volume&#xff0c;只需要在創建容器的時候&#xff0c;也就是 run 的時候聲明即可。 如: dock…

go自定義包教程

go自定義包教程 而是環境為 go version go1.11 linux/amd64linux mint 19 創建一個自定義的包 切換到src目錄下&#xff0c;創建demo目錄 cd src mkdir demo再創建smap 文件夾 cd demo mkdir smap創建包文件 cd smap touch map.gomap.go文件內容 package smapimport "…

在Linux中對硬盤進行分區、格式化和掛載

在Linux中對硬盤進行分區、格式化和掛載 我最近有一個全新的4-TB硬盤加入我的電腦。所以我需要在Linux中安裝它。為此&#xff0c;我需要執行以下操作&#xff1a; 分區格式化掛載檢查是否已安裝硬盤驅動器 分區 首先&#xff0c;在通過SATA和電源線將硬盤連接到計算機后&am…

Angular gitlab持續集成之runner配置

gitlab持續集成 安裝Runner 正常安裝 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64sudo chmod x /usr/local/bin/gitlab-runner sudo useradd --comment GitLab Runner --creat…

javascript編譯壓縮

javascript編譯 js是JavaScript 源碼文件&#xff0c; .min.js是壓縮版的js文件。 .min.js文件經過壓縮&#xff0c;相對編譯前的js文件體積較小&#xff0c;傳輸效率快。經過編碼將變量和函數原命名改為毫無意義的命名&#xff0c;以防止他人窺視和竊取 js 源代碼 在線版 g…

vscode設置中文

vscode設置中文 當我們安裝完成vscode后&#xff0c;會發現它是中文的&#xff0c;因為默認情況下&#xff0c;vscode使用的語言為英文(us)。 那很多同學是不喜歡英文的&#xff0c;不是看不懂&#xff0c;而是不方便哈。 修改語言配置文件 使用快捷鍵組合【CtrlShiftp】 在…

linux下編譯boost

linux下編譯boost 下載boot庫 官方下載地址 下載完成會解壓。 編譯 1.進入解壓后的文件夾內 cd boost_1_69_0 執行下面的語句 ./bootstrap.sh --with-librariesall --with-toolsetgcc–with-libraries 指定編譯哪些boost庫&#xff0c;all的話就是全部編譯&#xff0c;只想…

C++通過原子變量代替互斥量

C通過原子變量代替互斥量 廢話不多說&#xff0c;直接上代碼。 實現類似lock_guard功能 #include <atomic> #include <thread>class ClockGuard { public:ClockGuard(std::atomic_flag & atomic):m_lockedFlag(atomic){lock();}~ClockGuard(){unlock();}voi…

conky安裝配置

conky安裝配置 安裝conky sudo apt-get install conky-all修改配置 在主目錄下創建’.conkyrc’ 文件內容&#xff1a; # set to yes if you want Conky to be forked in the background background no cpu_avg_samples 2 net_avg_samples 2 out_to_console no # X font w…

Windows子系統安裝圖形界面

Windows子系統安裝圖形界面 有很多同學需要使用 linux 系統,但是又不想真正安裝一個 linux 系統。于是微軟大哥就把 ubuntu 系統集成到 windows 內了&#xff0c;你說 微軟 屌不屌&#xff1f; 言歸正傳。 安裝Xlaunch 因為這個是最靠譜的圖形化界面了&#xff0c;所以不解…

C++ 判斷類是否有某變量

C 判斷類是否有某變量 話不多說上代碼。 #define HAS_MEMBER(member)\template<typename T, typename... Args>struct has_member_##member\ {\private:\template<typename U> static auto Check(int) -> decltype(std::declval<U>().member(std::declv…