Linux 第83天 Docker基礎
Linux 第83天 Docker基礎概念
時間: 20181105
個人小站: www.winthcloud.top
目錄
容器概念
What is a container?
Linux Namespaces (核心裡實現的特性,併為上層提供了呼叫介面)
What's LXC? Docker的前生 LXC
OCI (Open Container Initiative) runC
Docker architecture
Docker objects
Docker Hub
Aufs
Docker Data Volume
Docker container networking
安裝docker
docker 常用命令
容器概念
碎片化的容器知識
虛擬化級別
主機級
type I
直接執行在硬體上hypervizor
程式經過兩層核心和硬體互動
vmware有一款VMware vSphere Hypervisor
Proxmox VE等
type II
在系統層上通過虛擬機器軟體主機上再建立kernel和使用者空間
如windows的vmware,virturalbox等
隔離級別高 消耗資源較大
容器級
一個核心多個名稱空間(使用者空間) 實現應用層隔離 使用者空間的隔離
當物理機出現故障時方便於遷移
linux裡的兩顆樹
程序樹
檔案系統樹
root
在核心中存在
Linux namespaces 容器級虛擬需要提供的隔離資源(核心空間中實現,系統呼叫介面)
Mount root system(chroot)
程序樹 PID 特權空間的子程序init
Network ip 命令建立名稱空間來隔離
IPC 程序間通訊隔離
UTS 主機名
USER 隔離
Cgroup 控制給容器資源配額 如記憶體,cpu
What is a container?
Container technology, also known as just a container, is a method to package
an application so it can be run, with its dependencies, isolated from other
process.
A container is a standard unit of software that packages up code and all
its dependencies so the application runs quickly and reliably from one
computing environment to another.
Package Software into Standardized Unit For Development, Shipment and
Deployment.
Linux Namespaces (核心裡實現的特性,併為上層提供了呼叫介面)
Cgroup (Cgroup root directory) 一個可以實現資源配額的名稱空間
IPC (System V IPC, POSIX manage queues)
Network (Network devices, stacks, ports, etc.)
Mount (Mount points)
PID (Process IDs)
User (User and group IDs)
UTS (Hostname and NIC domain name)
Cgroup (Control Groups)
blkio 塊裝置 io
cpu
cpuacct
cpuset
devices
freezer
memory
perf_event: 對cgroup中的任務進行統一效能測試
net_cls: cgroup中的任務建立資料報文的類別識別符號
What's LXC? Docker的前生 LXC
LXC is a userspace interface for the Linux kernel containment feature.
Current LXC uses the following kernel features to contain process.
Kernel namespace (ipc, uts, mount, pid,network and user)
Apparmor and SELinux profiles 應用層安全
Seccomp policies 核心安全計算模式
Chroots (using pivot_root)
Kernel capabilities
CGroups (control groups)
LXC containers are often considered as something in the middle between a
chroot and a full fledged virtual machine.
The goal of LXC is to create an environment as close as possible to a
standard Linux installation but without the need for a separate kernel.
LXC Architecture
Containers(share host kernel->libraries->app)
LXC
Libraries, glibc (libc, uclib or bionic)
Kernel namespace (ipc, uts, mount, pid,network and user)
底層為核心,以及在核心所提供的特性namespace,並封裝成系統呼叫介面
由於其難以供一般程式設計師來操作並呼叫,所以在其上又封裝了一層庫介面
然後再上一層便是LXC它是呼叫庫呼叫介面,然後在此之上便可建立一個個容器
Linux Containers have emerged as a key opensource application application
packaging and delivery technology, combining lightweight application
isolation with the flexibility of image-based deployment method.
CentOS 7 implements Linux Containers using core technologies such as
Control Groups (Cgroups) for Resource Management, Namespaces for Process
Isolation, SELinux for Security, enabling secure multi-tenancy and reducing
the potential for security exploits.
lxc, containerd, openvz, systemd-nspawn, runC
OCI (Open Container Initiative)
由Linux基金會主導於2015年6月創立
旨在圍繞容器格式和執行時制定的一個開放的工業化標準
Contain two specifications
the Runtime Specification (runtime-spec)
the Image Specification (image-spec)
The Runtime Specification outlines how to run a "filesystem bundle" that
is unpacked on disk.
At a high-level an OCI implementation would download an OCI Image then
unpack that image into an OCI Runtime filesystem bundle.
runC
OCF: Open Container Format
runC is a CLI tool for spawning and running containers according to
the OCI specification.
Containers are started as a child process of runC and can be
embedded into various other systems without having to run a daemon.
runC is built on libcontainer, the same container technology
powering millions of Docker Engine installations.
Docker architecture
The Docker daemon
The Docker daemon (dockerd) listens for Docker API requests and manages
Docker object such as images, containers, networks and volumes.
The Docker client
The docker client (docker) is the primary way that many Docker users
interact with Docker.
The docker command uses the Docker API.
Docker registries
A Docker registry stores Docker images.
Docker Hub and Docker Cloud are public registries that anyone can use,
and Docker is configured to look for images on Docker Hub by default.
You can even run your own private registry.
Docker objects
When you use Docker, you are creating and using images, containers, networks
volumes, plugins, and other objects.
IMAGE
an image is read-only template with instructions for creating a
Docker container. Often, an image is based on another image, with
some additional customization.
You might create your own images or you might only use those created
by others and published in a registry.
CONTAINERS
A container is a runnable instance of an image.
You can create, run, stop, move, or delete a container using the
Docker API or CLI.
You can connect a container to one or more networks, attach storage
to it, or even create a new image based on its current state.
Aufs
Advanced multi-layered unification filesystem 高階多層統一檔案系統
用於為Linux檔案系統實現"聯合掛載"
aufs是之前的UnionFS的重新實現,2006年由Junjiro Okajima開發
Docker最初使用aufs作為容器檔案系統層,它目前仍作為儲存後端之一來支援
aufs的競爭產品是overlayfs,後者自從3.18版本開始被合併到Linux核心
但是要注意Centos7目前的核心是3.10.X 所以導致有一部分使用者群體選擇了ubuntu
docker的分層映象,除了aufs,docker還支援btrfs,devicemapper和vfs等
在ubuntu系統下,docker預設ubuntu的aufs,而在Centos7上,用的是devicemapper
Docker Hub
Docker Hub provides the following major features
Image Repositories
Find and pull images from community and official libraries, and
manage, push to and pull from private image libraries to which you
have access.
Automated Builds
Automatically create new images when you make changes to a source
code repository.
Webhooks
A feature of Automated Builds, Webhooks let you trigger actions
after a successful push to a repository.
Organizations
Create work groups to manage access to image repositories.
GitHub and Bitbuckert Integration
Add the Hub and your Docker Images to your current workflows.
倉庫結構
倉庫名:版本tag
索引
認證
倉庫提供者
hub.docker.com
gcr.io
quay.io
dev.aliyun.com
docker 只允許執行一個程序 和子程序
容器所產生的日誌直接傳送在控制檯上
主管理器可以直接有相關命令將其控制檯上的日誌輸出出來
docker 容器裡的程序執行在前臺
Docker Registry分類
Sponsor Registry
Mirror Registry
Vendor Registry
Private Registry
Docker Data Volume
Data volumes privide several useful features for persistent or shared data
Data volumes can be shared and reused among containers
Changes to a data volume are made directly.
Changes to a data volume will not be included when you update an image
Data volumes persist even if the container itself is deleted.
Volume於容器初始化之時即會建立,由base image提供的卷中的資料會於此期間完成複製
Volume的初衷是獨立於容器的生命週期實現資料持久化,因此刪除容器之時不會刪除卷,
也不會對哪怕未被引用的卷做回收操作。
Volume types
Docker有兩種型別的卷,每種型別都在容器中存在一個掛載點,但其在宿主機上的位置
有所不同。
Bind mount volume
a volume that points to a user-specified location on the host file
system.
Docker-managed volume
the Docker daemon creates managed volumes in a portion of the host's
file system that's owned by Docker.
在容器中使用Volumes
為docker run 命令使用-v選項即可使用volume
Docker-managed volume
docker run -it -name bbox1 -v /data busybox
docker inspect -f {{.Mounts}} bbox1
檢視bbox1容器的卷、卷標識及掛載的主機目錄
Bind-mount Volume
docker run -it -v HOSTDIR:VOLUMEDIR --name b2 busybox
docker inspect -f {{.Mounts}} b2
(所繫結的卷容器和主機都可看到)
Sharing volumes
There are two ways to share volumes between containers.
多個容器的卷使用同一個主機目錄
docker run -it --name c1 -v /docker/vol:/data busybox:latest
docker run -it --name c2 -v /docker/vol:/data busybox:latest
複製使用其它容器的卷,為docker run命令使用--volumes-from選項
docker run -it --name b1 -v /docker/v1:/data busybox:latest
docker run -it --name b2 --volumes-from b1 busybox:latest
Docker container networking
Docker is concerned with two types of networking:
single-host virtual networks
provide container isolation
multi-host networks
provide an overlay where any container on a participating host can
have its own routable IP address from any other container in the
network.
Bridged containers
橋接式容器一般擁有兩個介面:一個環回介面和一個連線至主機上某橋裝置的乙太網介面
docker daemon啟動時預設會建立一個名為docker0的網路橋,並且建立的容器為橋接
式容器,其乙太網介面橋接至docker0網橋
--net bridge 即為將容器介面新增至docker
docker0橋為NET橋,因此,橋接式容器可通過此橋介面訪問外部網路,但防火牆規則阻止
了一切外部網路訪問橋接式容器的請求
docker run --rm --net bridge busybox:latest ifconfig -a
--hostname HOSTNAME 選項為容器指定主機名如
docker run --rm --net bridge --hostname mysky.com busybox:latest \
nslookup mysky.com
--dns DNS_SERVER_IP 選項能為容器指定要使用的DNS伺服器地址
docker run --rm --dns 172.16.0.1 busybox:latest nslookup mysky.com
--add-host HOSTNAME:IP 選項為容器指定本機名解析項
docker run --rm --dns 172.16.0.1 --add-host "mysky.com:192.168.1.1" \
nslookup mysky.com
Closed containers
不參與網路通訊,運行於此類容器中的程序僅能訪問本地迴環介面
僅適用於程序無須網路通訊的場景中,例如備份、程序診斷及各種離線任務等
docker run --rm --net none busybox:latest ifconfig -a
Opening inbound communication
Docker0為NAT橋,因此容器一般獲得的是私有地址
可以把容器想像為宿主機NAT服務背後的主機
如果開放容器或其上的服務為外部網路訪問,需要在宿主機上為其定義DNAT規則
對宿主機某IP地址的訪問全部對映給某容器地址
-A PREROUTING -d 主機IP -j DNAT --to-distination 容器IP
對宿主機某IP地址的某埠訪問對映給某容器地址的某埠
-A PREROUTING -d 主機IP -p {tcp|udp} --dport 主機埠 \
-j DNAT --to-destination 容器IP:port
docker run命令使用-p選項即可實現埠對映,無須手動新增規則
-p
-p <containerPort>
將指定的容器對映至主機所有地址的一個動態埠
-p <hostPort>:<containerPort>
將容器埠對映至指定主機主機埠
-p <ip>::<containerPort>
將指定的容器埠對映至主機指定的IP的動態埠
-p <ip>:<hostPort>:<containerPort>
將指定的容器埠對映至主機指定的IP和埠
動態埠指隨機埠,具體的對映結果可使用docker port命令檢視
-P 選項或--publish-all 將容器的所有計劃要暴露埠全部對映至主機埠
計劃要暴露的埠使用--expose選項指定
docker run -d -P --expose 2222 --expose 3333 --name web \
busybox:latest /bin/httpd -p 2222 -f
docker port web
如果不想使用預設的docker0橋介面,或者需要修改此橋介面的網路屬性,
可通過docker daemon命令使用-b, --bip, --fixed-cidr,
--default-gateway, --dns以及--mtu等選項進行設定
Joined containers
聯盟式容器是指使用某個已存在容器的網路介面的容器,介面被聯盟內的各容器共享
使用;因此,聯盟式容器彼此間網路完全無隔離,例如
建立一個監聽於2222埠的http服務容器
docker run -d -it --rm -p 2222 busybox:latest /bin/httpd \
-p 2222 -f
建立一個聯盟式容器,並檢視其監聽的埠
docker run -it --rm --net container:web --name joined \
busybox:latest netstat -tan
聯盟式容器彼此間雖然共享同一個網路名稱空間,但其它名稱空間User, Mount, PID,
IPC, UTS還是隔離的
聯盟式容器彼此間存在埠衝突的可能性,因此通常只會在多個容器上的程式需要通過
loopback介面互相通訊、或對某已存在的容器的網路屬性進行監控時才使用此種模式的
網路模型
Open containers
開放式容器 共享主機網路名稱空間的容器,它們對主機的網路名稱空間擁有全部的訪問
許可權,包括訪問那些關鍵性服務,這對宿主機安全性有很大潛在威脅,注意會共享所有主
機的介面
--net host 選項即可建立開放式容器 如
docker run -it --rm --net host busybox:latest /bin/sh
安裝docker
安裝包
需要repo資源 Extra, docker-ce,可以去阿里找
yum install docker-ce
使用aliyun加速器
建立aliyun賬號dev.aliyun.com
建立名稱空間,建立倉庫,在其下方有映象加速器新增方式
按照其方法新增即可
修改預設的dockerd服務啟動時的設定
vim /usr/lib/systemd/system/docker.service
在[service]段裡新增如下一行
ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
修改網路引數
vim /etc/sysctl.d/docker.conf
此時便可啟動服務
systemctl start docker
docker 常用命令
docker version
docker info
docker image
ls -a -q
pull
push
rm
inspect
tag
save -o
load -i
docker container
commit -a -c -p
cp
top
exec container_name ifconfig
logs
ps
stats
start
stop
inspect
pause
kill
rm
commit
port
run --name container_name
-d
--rm
-i inactive
-t tty
-e ENV
--network host,none,container:Container_name,bridge
-p,publish
-v HOSTDIR:CONTAINERDIR
--volumes-from
-h
docker network
create
-d {bridge|null|host|macvlan|overlay} docker info plugins
--gateway
--ingress
--ip-range
--subnet
ls
rm
connect
disconnect
inspect
-f
prune
docker volume
inspect
ls
總結
1. docker 起初是基於lxc提供的介面來做底層建立容器。
2. runC 是一個容器建立和執行的一個標準規範,docker的映象是分層構建的,
下層的映象對於上層來說是隻讀的,只有在頂層才是可寫層,所以當修改下層的映象檔案
內容時,會產生寫時複製,即將下層的檔案複製至可寫層,並在可寫層做修改,但下層的
原檔案還是存在的,只是對上層隱藏了