日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Docker-----网络模式与资源控制管理

發布時間:2023/12/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Docker-----网络模式与资源控制管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 一、docker的四種網絡
    • 二、docker自定義網絡
    • 三:Docker資源控制(cpu、內存、IO資源控制)

一、docker的四種網絡

1、host

  • 在容器內部創建的兩個容器A,B,通過docker 0(相當于小型的局域網,為內部容器的網關)進行內部通信;與外部通信地址映射Nat模式物理網卡ens33
  • 如果兩個內部容器的服務不同,但端口一樣,進行外部通信時,進行端口映射成不同的端口進行通信

2、container

  • 創建的容器不會創建自己的網卡、設置IP等,而是和一個指定地容器共享IP、端口范圍
  • 這個模式指定新創建的容器和已經存在的一個容器共享一network namespace,而不是和宿主機共享,新創建的容器不會創建自己的網卡,配置自己的IP,而是和一個指定地容器共享IP、端口范圍等。同樣,兩個容器除了網絡方面,其他的如文件系統、進程列表還是隔離的。兩個容器的進程可以通過loo網卡設備通信

3、None

  • 該模式關閉了容器的網絡功能
  • 這種網絡模式下容器只有lo回環網口,沒有其他的網卡。none 模式可以在容器創建時通過-network=none參數指定
  • 這種類型的網絡無法聯網,但是封閉的網絡能很好的保證容器的安全性,提高了安全性。

4、Bridge

  • 此模式會為每一個容器分配、設置IP等,并將容器連接到一個docker虛擬網橋,通過docker0網橋及iptables的nat表配置與宿主機通信
  • 當Docker進程啟動時,會在主機上創建一個名為docker0的虛擬網橋,此主機上啟動的Docker容器會連接到這個虛擬網橋上。虛擬網橋的工作方式和物理交換機類似,這樣主機上的所有容器就通過交換機連在了一個二層網絡中
  • 從docker0子網中分配一個IP給容器使用,并設置docker0的IP地址為容器的默認網關。在主機上創建一對虛擬網卡veth pair設備,Docker將veth pair設備的一端掛載在新創建的容器中,并命名為ethO (容器的網卡),另一端放在主機中,以vethxxx這樣類似的名字命名,并將這個網絡設備加入到docker0網橋中。可以通過brctl show命令查看。

總結

host模式 -net= host 容器和宿主機共享Network namespace(網絡名稱空間)containeb模式 -net=container.NAME_or_ID 容器和另外一個容器共享Network namespace(共享ip地址和端口范圍)none模式 -net= none 容器有獨立的Network namespace,但并沒有對其進行任何網絡設置,無法與外部進行交互。如分配veth pair和網橋連接,配置IP等。bridge模式 -net= bridge (默認為該模式)docker 0為虛擬網橋,所有的容器會連接到虛擬網橋上(因為虛擬網橋為它們的虛擬網關),虛擬網橋會結合iptable的規則去進行地址映射,把容器地址段映射為宿主機的地址段,用于和宿主機通訊,最后讓宿主機連接外網。注:以上不需要動手配置,真正需要配置的是自定義網絡

二、docker自定義網絡

1、查看網絡列表

[root@server1 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 4c67ba8a5d4b bride bridge local bfbf3cdaf20a host host local f5b7dfaba49b none null local

2、根據鏡像創建容器并指定ip地址

  • 因為本地有鏡像,所以無效下載,直接進行操作
  • 會報錯:來自守護程序的錯誤響應,僅在用戶定義的網絡上支持用戶指定的IP地址
[root@server1 ~]# docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:7 /bin/bash 11ffd3d0a05f7def137ef2e1d6ed2d77f646032287dbe7b12c3210547860c2c3 docker: Error response from daemon: user specified IP address is supported on user defined networks only.

3、根據鏡像創建容器不指定ip地址

  • 會報錯:原因已經之前指定IP地址了
[root@server1 ~]# docker run -itd --name test2 --network bridge centos:7 /bin/bash 2f35af2c52a632ac10eefe6ef5836f268626f18aa3060083c9a917bc03823d5c[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2f35af2c52a6 centos:7 "/bin/bash" 2 seconds ago Up 1 second test2 11ffd3d0a05f centos:7 "/bin/bash" 2 minutes ago Created test1[root@server1 ~]# docker start 11ffd3d0a05f Error response from daemon: user specified IP address is supported on user defined networks only Error: failed to start containers: 11ffd3d0a05f #原因:創建運行容器手動指定IP地址,不允許,沒有權限,違背ip地址分配規則按順序配置
  • 查看已運行容器狀態
[root@server1 ~]# docker exec -it 2f35af2c52a6 /bin/bash [root@2f35af2c52a6 /]# yum -y install net-tools [root@2f35af2c52a6 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)RX packets 2475 bytes 13137917 (12.5 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 2461 bytes 136191 (132.9 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0loop txqueuelen 1 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

4、自定義網絡固定IP

[root@server1 ~]# docker network create --subnet=172.18.0.0/16 mysub 7b9ff03bf0a3c829b452b5970c2ffca6a44a70fe0e00ca93226514a6a5c224d9[root@server1 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 4c67ba8a5d4b bridge bridge local bfbf3cdaf20a host host local 7b9ff03bf0a3 mysub bridge local # 自定義 f5b7dfaba49b none null local

5、定義分配地址

[root@server1 ~]# docker run -itd --name test3 --net mysub --ip 172.18.0.100 centos:7 /bin/bash eda31a7db2875e735ade11389ffdd9edd790b6d907a02e0bba188d1f4689acbd[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eda31a7db287 centos:7 "/bin/bash" 4 seconds ago Up 4 seconds test3 2f35af2c52a6 centos:7 "/bin/bash" 14 minutes ago Up 14 minutes test2 11ffd3d0a05f centos:7 "/bin/bash" 16 minutes ago Created
  • 查看容器網絡地址
[root@server1 ~]# docker exec -it eda31a7db287 /bin/bash [root@eda31a7db287 /]# yum -y install net-tools [root@eda31a7db287 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.18.0.100 netmask 255.255.0.0 broadcast 172.18.255.255ether 02:42:ac:12:00:64 txqueuelen 0 (Ethernet)RX packets 1979 bytes 13111519 (12.5 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1967 bytes 109708 (107.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • 測試網關
[root@eda31a7db287 /]# ping 172.18.0.1 PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data. 64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.067 ms 64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.034 ms 64 bytes from 172.18.0.1: icmp_seq=3 ttl=64 time=0.036 ms
  • 測試另一個網橋網關
[root@eda31a7db287 /]# ping 172.17.0.1 PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data. 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.051 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.061 ms 64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.038 ms

三:Docker資源控制(cpu、內存、IO資源控制)

前言

  • 07年谷歌,可以控制資源分配通過操作系統內核,控制應用程序使用內存資源、cpu資源、 文件系統資源等等
  • cgroup是一種資源控制手段
  • 每個容器相當于一個進程

1、CPU使用率控制

  • cpu周期: 1s為一個周期的定律,參數值一般為100000 (CPU衡量單位是秒)
  • 假如需要給此容器分配cpu使用率的20%,則參數需要設置為20000,相當于每個周期分配給這個容器0.2s
  • cpu在一個時刻,只能給一個進程占用
cat /sys/fs/cgroup/qpu/docker/容器ID/qpu.cfs_ quota us [root@server1 ~]# cd /sys/fs/cgroup/cpu/docker/ [root@server1 docker]# ls 2f35af2c52a632ac10eefe6ef5836f268626f18aa3060083c9a917bc03823d5c cgroup.clone_children cgroup.event_control cgroup.procs cpuacct.stat cpuacct.usage cpuacct.usage_percpu cpu.cfs_period_us cpu.cfs_quota_us cpu.rt_period_us cpu.rt_runtime_us cpu.shares cpu.stat eda31a7db2875e735ade11389ffdd9edd790b6d907a02e0bba188d1f4689acbd notify_on_release tasks[root@server1 docker]# cd 2f35af2c52a632ac10eefe6ef5836f268626f18aa3060083c9a917bc03823d5c/ [root@server1 2f35af2c52a632ac10eefe6ef5836f268626f18aa3060083c9a917bc03823d5c]# ls cgroup.clone_children cpuacct.usage cpu.rt_period_us notify_on_release cgroup.event_control cpuacct.usage_percpu cpu.rt_runtime_us tasks cgroup.procs cpu.cfs_period_us cpu.shares cpuacct.stat cpu.cfs_quota_us cpu.stat [root@server1 2f35af2c52a632ac10eefe6ef5836f268626f18aa3060083c9a917bc03823d5c]# cat cpu.cfs_quota_us -1 # -1代表此容器可以使用的資源不受限制會引發問題,導致某個容器占用資源過大,影響其它容器的性能

2、動態查看cpu使用率

  • ①top
[root@server1 ~]# top
  • ②docker stats
[root@server1 ~]# docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS eda31a7db287 test3 0.00% 82.76MiB / 3.686GiB 2.19% 13.1MB / 111kB 139kB / 49.1MB 1 2f35af2c52a6 test2 0.00% 96.35MiB / 3.686GiB 2.55% 13.1MB / 136kB 110MB / 49.8MB 1# CONTAINER ID 容器ID # NAME 容器名稱 # CPU % CPU占用 # MEM USAGE 內存占用 # LIMIT 內存最大限制 # MEM % 內存使用率 # NET I/O IO控制 # BLOCK I/O BLOCK控制/也是對IO的控制 # PIDS PID

3、CPU 20%的限定

  • 方式一 在創建容器運行進行資源限制
  • ①創建容器
[root@server1 ~]# docker run -itd --name test4 --cpu-quota 20000 centos:7 /bin/bash 5906465b58b809e7a30a63c8fd0aa0f16114fe5d8f006ff5343e0a943b4f3d51
  • ②計算圓周率
[root@server1 ~]# docker exec -it 5906465b58b8 /bin/bash [root@5906465b58b8 /]# yum -y install bc [root@5906465b58b8 /]# echo "scale=5000;4*a(1)" | bc -l -q
  • ③另開一個新的會話窗口查看
[root@server1 ~]# top

  • 方式二 對已經存在且在運行時狀態的容器進行設置
  • ①查看運行容器
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5906465b58b8 centos:7 "/bin/bash" 13 minutes ago Up 13 minutes test4 eda31a7db287 centos:7 "/bin/bash" About an hour ago Up About an hour test3 2f35af2c52a6 centos:7 "/bin/bash" About an hour ago Up About an hour test2 11ffd3d0a05f centos:7 "/bin/bash" 2 hours ago Created test1
  • ②進入容器設置
  • echo “20000” > 容器完整ID/cpu.cfs quota us
[root@server1 ~]# cd /sys/fs/cgroup/cpu/docker/ [root@server1 docker]# cd eda31a7db2875e735ade11389ffdd9edd790b6d907a02e0bba188d1f4689acbd/ [root@server1 eda31a7db2875e735ade11389ffdd9edd790b6d907a02e0bba188d1f4689acbd]# echo "20000" > cpu.cfs_quota_us [root@server1 eda31a7db2875e735ade11389ffdd9edd790b6d907a02e0bba188d1f4689acbd]# cat cpu.cfs_quota_us 20000

4、設置容器的權重

  • 關閉運行中的容器,不讓它們占用資源
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5906465b58b8 centos:7 "/bin/bash" 22 minutes ago Up 22 minutes test4 eda31a7db287 centos:7 "/bin/bash" About an hour ago Up About an hour test3 2f35af2c52a6 centos:7 "/bin/bash" 2 hours ago Up 2 hours test2 11ffd3d0a05f centos:7 "/bin/bash" 2 hours ago Created test1[root@server1 ~]# docker stop 5906465b58b8 5906465b58b8 [root@server1 ~]# docker stop eda31a7db287 eda31a7db287 [root@server1 ~]# docker stop 2f35af2c52a6 2f35af2c52a6[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5906465b58b8 centos:7 "/bin/bash" 23 minutes ago Exited (137) 56 seconds ago test4 eda31a7db287 centos:7 "/bin/bash" About an hour ago Exited (137) 38 seconds ago test3 2f35af2c52a6 centos:7 "/bin/bash" 2 hours ago Exited (137) 9 seconds ago test2 11ffd3d0a05f centos:7 "/bin/bash" 2 hours ago Created
  • 按比例分配設置容器權重,此處權重是所有值相加然后看占用百分比
[root@server1 ~]# docker run -itd --name c1 --cpu-shares 512 centos:7 /bin/bash 24dce68faa2f79b279baca01796df378f8a0f901653a845aa2e593655328eea4[root@server1 ~]# docker run -itd --name c2 --cpu-shares 1024 centos:7 /bin/bash 7bb762fd67c304472e1360a301523672ebc3cdc9af0eb8aa832360d403382c23[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7bb762fd67c3 centos:7 "/bin/bash" 3 seconds ago Up 2 seconds c2 24dce68faa2f centos:7 "/bin/bash" 19 seconds ago Up 18 seconds
  • 復制兩個終端、分別進入容器后進行測試,主終端使用docker stats進行查看
  • ①查看容器ID
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7bb762fd67c3 centos:7 "/bin/bash" 8 minutes ago Up 8 minutes c2 24dce68faa2f centos:7 "/bin/bash" 9 minutes ago Up 9 minutes c1
  • ②兩個終端分別進入測試
[root@server1 ~]# docker exec -it 7bb762fd67c3 /bin/bash [root@7bb762fd67c3 /]# yum -y install epel-release [root@7bb762fd67c3 /]# yum -y install stress [root@7bb762fd67c3 /]# stress -c 4 # stress模擬滿載線程 [root@server1 ~]# docker exec -it 24dce68faa2f /bin/bash [root@24dce68faa2f /]# yum -y install epel-release [root@24dce68faa2f /]# yum -y install stress [root@24dce68faa2f /]# stress -c 4 # stress模擬滿載線程
  • ③查看容器資源占用情況
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK IO PIDS 7bb762fd67c3 c2 265.10% 121.1MiB / 3.686GiB 3.21% 21.3MB / 173kB 1.12MB 50MB 7 24dce68faa2f c1 132.93% 120.8MiB / 3.686GiB 3.20% 21.5MB / 281kB 8.19kB 50.1MB 7

5、限制容器使用的CPU (指定使用第2第4個)

  • 關閉所有運行的容器
[root@server1 ~]# docker stop 7bb762fd67c3 7bb762fd67c3 [root@server1 ~]# docker stop 24dce68faa2f 24dce68faa2f[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7bb762fd67c3 centos:7 "/bin/bash" 45 minutes ago Exited (137) 27 seconds ago c2 24dce68faa2f centos:7 "/bin/bash" 45 minutes ago Exited (137) 11 seconds ago c1 5906465b58b8 centos:7 "/bin/bash" About an hour ago Exited (137) 50 minutes ago test4 eda31a7db287 centos:7 "/bin/bash" 2 hours ago Exited (137) 50 minutes ago test3 2f35af2c52a6 centos:7 "/bin/bash" 2 hours ago Exited (137) 49 minutes ago test2 11ffd3d0a05f centos:7 "/bin/bash" 3 hours ago Created
  • 創建容器
[root@server1 ~]# docker run -itd --name c3 --cpuset-cpus 1,3 centos:7 /bin/bash[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d21509f83a1a centos:7 "/bin/bash" 3 hours ago Up 3 hours c3[root@server1 ~]# docker exec -it d21509f83a1a /bin/bash [root@d21509f83a1a /]# yum -y install epel-release [root@d21509f83a1a /]# yum -y install stress [root@d21509f83a1a /]# stress -c 2
  • 另開終端查看
[root@server1 ~]# top top - 04:13:17 up 9:45, 4 users, load average: 0.43, 0.15, 0.09 Tasks: 223 total, 3 running, 220 sleeping, 0 stopped, 0 zombie %Cpu0 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 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 %Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st %Cpu3 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

6、內存使用上限限制

  • 創建容器
[root@server1 ~]# docker run -itd --name c4 -m 512m centos:7 /bin/bash 4be6941bc10e3bf5603a4e56093e202c4ea78e93a8aecd450c7aac8e2de6e954 [root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4be6941bc10e centos:7 "/bin/bash" 4 seconds ago Up 3 seconds
  • 查看
[root@server1 ~]# docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 4be6941bc10e c4 0.00% 392KiB / 512MiB 0.07% 648B / 0B 0B / 0B 1

7、docker Io限制

  • docker的Io限制就是對block的Io進行約束
  • 控制數據量用的較多,其中控制Io次數使用的較少
--device-read-bps :限制讀某個設備的bps (數據量,比特率,每秒數據傳輸速率) docker run -itd --device-read-bps /dev/sda:30M centos:7 /bin/bash --device-write-bps:限制寫入某個設備的bps (數據量) docker run -itd --device-write-bps /dev/sda:30M centos:7 /bin/bash --device-read-iops限制讀某個設備的iops (次數) --device-write-iops 限制寫入某個設備的iops (次數)

8、for語句批量刪除

  • 批量刪除“exit”狀態容器
[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4be6941bc10e centos:7 "/bin/bash" 4 minutes ago Up 4 minutes c4 d21509f83a1a centos:7 "/bin/bash" 3 hours ago Up 3 hours c3 7bb762fd67c3 centos:7 "/bin/bash" 4 hours ago Exited (137) 3 hours ago c2 24dce68faa2f centos:7 "/bin/bash" 4 hours ago Exited (137) 3 hours ago c1 5906465b58b8 centos:7 "/bin/bash" 4 hours ago Exited (137) 4 hours ago test4 eda31a7db287 centos:7 "/bin/bash" 5 hours ago Exited (137) 4 hours ago test3 2f35af2c52a6 centos:7 "/bin/bash" 6 hours ago Exited (137) 4 hours ago test2 11ffd3d0a05f centos:7 "/bin/bash" 6 hours ago Created test1 [root@server1 ~]# for i in `docker ps -a | grep -i exit | awk '{print $1}'`;do docker rm -f $i;done 7bb762fd67c3 24dce68faa2f 5906465b58b8 eda31a7db287 2f35af2c52a6[root@server1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4be6941bc10e centos:7 "/bin/bash" 5 minutes ago Up 5 minutes c4 d21509f83a1a centos:7 "/bin/bash" 3 hours ago Up 3 hours c3

總結

以上是生活随笔為你收集整理的Docker-----网络模式与资源控制管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。