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的鏡像是分層構建的,

下層的鏡像對於上層來說是隻讀的,只有在頂層纔是可寫層,所以當修改下層的鏡像文件

內容時,會產生寫時複製,即將下層的文件複製至可寫層,並在可寫層做修改,但下層的

原文件還是存在的,只是對上層隱藏了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章