Docker
Docker
讲师:马永亮(马哥)
http://www.magedu.com
Container
•容器是一种基础工具;泛指任何可以用于容纳其它物品的工具,可以
部分或完全封闭,被用于容纳、储存、运输物品;物体可以被放置在
容器中,而容器则可以保护内容物;
•人类使用容器的历史至少有十万年,甚至可能有数百万年的历史;
•容器的类型
• 瓶 - 指口部比腹部窄小、颈长的容器。
• 罐 - 指那些开口较大、一般为近圆筒形的器皿。
• 箱 - 通常是立方体或圆柱体。形状固定。
• 篮 - 以条状物编织而成。
• 桶 - 一种圆柱形的容器。
• 袋 - 柔性材料制成的容器,形状会受内容物而变化。
• 瓮 - 通常是指陶制,口小肚大的容器。
• 碗 - 用来盛载食物的容器。
• 柜 - 指一个由盒组成的家俱。
• 鞘 - 用于装载刀刃的容器。
LXC
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
Virtualization and Container
namespaces
clone(), setns()
•Control Groups, CGroups
•LinuX Container
• lxc-create, template
Moby, CNCF
•nmp
•machine+swarm+compose
•mesos+marathon
•kubernetes -> k8s
•libcontainer->runC
kvm and lxc
Namespaces and Container
Linux Namespaces
Linux Namespaces and CGroups
•Namespaces
• Mount namespaces:挂载点
• UTS namespaces:主机名与域名
• IPC namespaces:信号量、消息队列和共享内存
• PID namespaces:进程号
• Network namespaces:网络设备、网络栈、端口等
• User namespaces:用户和组
Control Groups (cgroups)
•cgroups
• blkio:块设备IO
• cpu:CPU
• cpuacct:CPU资源使用报告
• cpuset:多处理器平台上的CPU集合
• devices:设备访问
• freezer:挂起或恢复任务
• memory:内存用量及报告
• perf_event:对cgroup中的任务进行统一性能测试
• net_cls:cgroup中的任务创建的数据报文的类别标识符
filesystem namespace
CGroups
What's LXC?
•LXC is a userspace interface for the Linux kernel containment features.
•Current LXC uses the following kernel features to contain processes
• Kernel namespaces (ipc, uts, mount, pid, network and user)
• Apparmor and SELinux profiles
• Seccomp policies
• Chroots (using pivot_root)
• Kernel capabilities
• CGroups (control groups)
What's LXC?
•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
lxc简单应用
•安装
• lxc, lxc-templates
•命令
• lxc-checkconfig, lxc-ls, lxc-create, lxc-start, lxc-console, lxc-stop, lxc-info, lxc-
clone, lxc-snapshot, lxc-destroy, …
• WebGUI: lxc-webpanel
• http://lxc-webpanel.github.io/
• yum install python-flask
• git clone https://github.com/lxc-webpanel/LXC-Web-Panel.git
• python LXC-Web-Panel/lwp.py
再谈容器
•Linux Containers have emerged as a key open source application packaging
and delivery technology, combining lightweight application isolation with the
flexibility of image-based deployment methods.
•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
Linux Container Architecture
Docker
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
What is docker
•docker中的容器
• lxc -> libcontainer -> runC
OCI
•Open Container Initiative
• 由Linux基金会主导于2015年6月创立
• 旨在围绕容器格式和运行时制定一个开放的工业化标准
• contains 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
aufs, overlayfs
dm:
devicemapper
Moby
docker-ee
dcoker-ce
•/bin/ls
• ls /etc
• ls /var
•镜像:静态
•容器:动态,生命周期
docker architecture
•The Docker daemon
• The Docker daemon (dockerd) listens for Docker API requests and manages Docker
objects 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.
repository, repo
nginx
tag
nginx:1.15
nginx:latest
nginx:1.4
nginx:stable
nginx:1.10
Docker objects
•When you use Docker, you are creating and using images, containers,
networks, volumes, plugins, and other objects.
• IMAGES
• An image is a 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.
Docker Host and Registry
安装及使用docker
•依赖的基础环境
• 64 bits CPU
• Linux Kernel 3.10+
• Linux Kernel cgroups and namespaces
•CentOS 7
• “Extras” repository
•Docker Daemon
• systemctl start docker.service
•Docker Client
• docker [OPTIONS] COMMAND [arg...]
Investigating the Docker environment
•docker version
• The version option shows which versions of different Docker components are installed
Investigating the Docker environment
•docker info
• The info option lets you see how many local containers and images there are, as well as
information on the size and location of Docker storage areas
Registry options
•When asked to search for or pull images, the docker command uses the
Docker registry (docker.io) to complete those activities
• In RHEL and RHEL Atomic Host, this entry in the /etc/sysconfig/docker
file causes the Red Hat registry (registry.access.redhat.com) to be used first:
•If you wanted to add a private registry that you installed yourself, just add
another --add-registry. For example:
Registry options(2)
•If you want to prevent users from pulling images from the Docker registry,
uncomment the BLOCK_REGISTRY entry so it appears as follows:
•To access a registry that uses https protocol for security, but is not set up with
certificates for authentication, you can still access that registry by defining it as
an insecure registry in the /etc/sysconfig/docker file. For example:
常用操作
• docker search:Search the Docker Hub for images
• docker pull:Pull an image or a repository from a registry
• docker images:List images
• docker create:Create a new container
• docker start:Start one or more stopped containers
• docker run:Run a command in a new container
• docker attach:Attach to a running container
• docker ps:List containers
• docker logs:Fetch the logs of a container
• docker restart:Restart a container
• docker stop:Stop one or more running containers
• docker kill:Kill one or more running containers
• docker rm:Remove one or more containers
•alpine:
docker event state
Docker images
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
Docker: 码头工人
•overlay2
Registries
Docker Daemon
About Docker Images
•Docker镜像含有启动容器所需要的文件系统及其内容,因此,其用
于创建并启动docker容器
• 采用分层构建机制,最底层为bootfs,其之为rootfs
• bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以
节约内存资源;
• rootfs:位于bootfs之上,表现为docker容器的根文件系统;
• 传统模式中,系统启动之时,内核挂载rootfs时会首先将其挂载为“只读”模式,完整性自检完成
后将其重新挂载为读写模式;
• docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载 ”技术额外挂载一个“可写”层;
Docker Image Layer
•位于下层的镜像称为父镜像(parent image),最底层的称为基础镜像(base
image)
•最上层为“可读写”层,其下的均为“只读”层
Aufs
• advanced multi-layered unification filesystem:高级多层统一文件系统
• 用于为Linux文件系统实现“联合挂载”
• aufs是之前的UnionFS的重新实现,2006年由Junjiro Okajima开发;
• Docker最初使用aufs作为容器文件系统层,它目前仍作为存储后端之一来支持;
• aufs的竞争产品是overlayfs,后者自从3.18版本开始被合并到Linux内核;
• docker的分层镜像,除了aufs,docker还支持btrfs, devicemapper和vfs等
• 在Ubuntu系统下,docker默认Ubuntu的 aufs;而在CentOS7上,用的是devicemapper;
Devicemapper
•Device Mapper 是 Linux2.6 内核中支持逻辑卷管理的通用设备映射机制
,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的
内核架构
Devicemapper
•在内核中它通过一个一个模块化的 target driver 插件实现对 IO 请求的
过滤或者重新定向等工作,当前已经实现的 target driver 插件包括软
raid、软加密、逻辑卷条带、多路径、镜像、快照等
• 前一页图中 linear、mirror、snapshot、multipath 表示的就是这些 target driver
• 在这诸多“插件”中,有一种叫Thin Provisioning Snapshot,Docker正是使用了
Thin Provisioning的Snapshot的技术实现了类似auFS的分层镜像
Docker Registry
•启动容器时,docker daemon会试图从本地获取相关的镜像;本地镜像
不存在时,其将从Registry中下载该镜像并保存到本地;
• The Registry is a stateless, highly scalable server side application that stores and lets you
distribute Docker images.
Docker Registry 分类
•Registry用于保存docker镜像,包括镜像的层次结构和元数据
•用户可自建Registry,也可使用官方的Docker Hub
•分类
• Sponsor Registry:第三方的registry,供客户和Docker社区使用
• Mirror Registry:第三方的registry,只让客户使用
• Vendor Registry:由发布Docker镜像的供应商提供的registry
• Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry
Registry(repository and index)
•Repository
• 由某特定的docker镜像的所有迭代版本组成的镜像仓库
• 一个 Registry中可以存在多个Repository
• Repository可分为“顶层仓库”和“用户仓库”
• 用户仓库名称格式为“用户名/仓库名”
• 每个仓库可以包含多个Tag(标签) ,每个标签对应一个镜像
•Index
• 维护用户帐户、镜像的校验以及公共命名空间的信息
• 相当于为Registry提供了一个完成用户认证等功能的检索接口
Docker Registry
•Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“
私有”Registry上保存,供其他人员使用,例如“部署”到生产环境;
base image
Docker Hub
•Docker Hub is a cloud-based registry service which allows you to link to
code repositories, build your images and test them, stores manually pushed
images, and links to Docker Cloud so you can deploy images to your hosts.
•It provides a centralized resource for container image discovery, distribution
and change management, user and team collaboration, and workflow
automation throughout the development pipeline.
Docker Hub
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 Bitbucket Integration
• Add the Hub and your Docker Images to your current workflows.
•Github
• project, dockerfile
Getting images from remote Docker registries
•To get Docker images from a remote registry (such as your own Docker
registry) and add them to your local system, use the docker pull command:
• The <registry> is a host that provides the docker-distribution service on TCP <port>
(default: 5000)
• Together, <namespace> and <name> identify a particular image controlled by
<namespace> at that registry
• Some registries also support raw <name>; for those, <namespace> is optional
• When it is included, however, the additional level of hierarchy that <namespace> provides is useful
to distinguish between images with the same <name>
•docker pull nginx:1.12-alpine
• magedu/nginx:1.12-alpine
•docker load
• Load an image from a tar archive or STDIN
• Usage:docker load [OPTIONS]
• --input, -i:Read from tar archive file, instead of STDIN
• --quiet, -q:Suppress the load output
Docker
Data Volume
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
Why Data Volumes?
•Docker镜像由多个只读层叠加而成,启动容器时,Docker会加载只读镜
像层并在镜像栈顶部添加一个读写层
•如果运行中的容器修改了现有的一个已经存在的文件,那该文件将会
从读写层下面的只读层复制到读写层,该文件的只读版本仍然存在,
只是已经被读写层中该文件的副本所隐藏,此即“写时复制(COW)”机
制
Why Data Volume?
•关闭并重启容器,其数据不受影响;但删除Docker容器,则其更改将会
全部丢失
•存在的问题
• 存储于联合文件系统中,不易于宿主机访问;
• 容器间数据共享不便
• 删除容器其数据会丢失
•解决方案:“卷(volume)”
• “卷”是容器上的一个或多个“目录”,此类目录可绕过联合文件系统,与宿主机
上的某目录“绑定(关联)”
Data volumes
•Data volumes provide several useful features for persistent or shared data
• Volume于容器初始化之时即会创建,由base image提供的卷中的数据会于此期间
完成复制
• 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的初衷是独立于容器的生命周期实现数据持久化,因此删除容
器之时既不会删除卷,也不会对哪怕未被引用的卷做垃圾回收操作;
Data volumes
•卷为docker提供了独立于容器的数据管理机制
• 可以把“镜像”想像成静态文件,例如“程序”,把卷类比为动态内容,例如“数据
”;于是,镜像可以重用,而卷可以共享;
• 卷实现了“程序(镜像)”和“数据(卷)”分离,以及“程序(镜像)”和“制作镜像的主机
”分离,用户制作镜像时无须再考虑镜像运行的容器所在的主机的环境;
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 bbox2 busybox
• ~]# docker inspect -f {{.Mounts}} bbox2
Sharing volumes
•There are two ways to share volumes between containers
• 多个容器的卷使用同一个主机目录,例如
• ~]# docker run –it --name c1 -v /docker/volumes/v1:/data busybox
• ~]# docker run –it --name c2 -v /docker/volumes/v1:/data busybox
• 复制使用其它容器的卷,为docker run命令使用--volumes-from选项
• ~]# docker run -it --name bbox1 -v /docker/volumes/v1:/data busybox
• ~]# docker run -it --name bbox2 --volumes-from bbox1 busybox
Docker Networking
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
OVS: OpenVSwitch, SDN
6种名称空间:UTS, User, Mount, IPC,
Pid, Net
C1 C2 C3 C4
C5 C6
S2
H1
H2
Overlay Network
C1
C5
Web3 Web2 Web1
User, Mount, Pid User, Mount, Pid
UTS, Net, IPC UTS, Net, IPC
172.18.0.77 172.18.0.78
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
The local Docker network
topology
•The default local Docker network topology and two attached containers
Four network container archetypes
Bridged containers
•桥接式容器一般拥有两个接口:一个环回接口和一个连接至主机上某
桥设备的以太网接口
•docker daemon启动时默认会创建一个名为docker0的网络桥,并且创建
的容器为桥接式容器,其以太网接口桥接至docker0
• --net bridge即为将容器接口添加至docker0桥
•docker0桥为NAT桥,因此,桥接式容器可通过此桥接口访问外部网络
,但防火墙规则阻止了一切从外部网络访问桥接式容器的请求
• ~]# docker run --rm --net bridge busybox:latest ifconfig -a
Closed containers
•不参与网络通信,运行于此类容器中的进程仅能访问本地环回接口
•仅适用于进程无须网络通信的场景中,例如备份、进程诊断及各种离
线任务等
• ~]# docker run --rm --net none busybox:latest ifconfig -a
Bridged containers
•可以为docker run命令使用
• “--hostname HOSTNAME”选项为容器指定主机名,例如
• ~]# docker run --rm --net bridge --hostname bbox.magedu.com busybox:latest nslookup
bbox.magedu.com
• “--dns DNS_SERVER_IP”选项能够为容器指定所使用的dns服务器地址,例如
• ~]# docker run --rm --dns 172.16.0.1 busybox:latest nslookup docker.com
• “--add-host HOSTNAME:IP”选项能够为容器指定本地主机名解析项,例如
• ~]# docker run --rm --dns 172.16.0.1 --add-host "docker.com:172.16.0.100" busybox:latest
nslookup docker.com
Opening inbound communication
•Docker0为NAT桥,因此容器一般获得的是私有网络地址
•可以把容器想像为宿主机NAT服务背后的主机
•如果开放容器或其上的服务为外部网络访问,需要在宿主机上为其定
义DNAT规则,例如
• 对宿主机某IP地址的访问全部映射给某容器地址
• 主机IP 容器IP
• -A PREROUTING -d 主机IP -j DNAT --to-destination 容器IP
• 对宿主机某IP地址的某端口的访问映射给某容器地址的某端口
• 主机IP:PORT 容器IP:PORT
• -A PREROUTING -d 主机IP -p {tcp|udp} --dport 主机端口 -j DNAT --to-destination 容器IP:容
器端口
•为docker run命令使用-p选项即可实现端口映射,无须手动添加规则
docker0
Opening inbound communication
•-p选项的使用格式
• -p <containerPort>
• 将指定的容器端口映射至主机所有地址的一个动态端口
• -p <hostPort>:<containerPort>
• 将容器端口<containerPort>映射至指定的主机端口<hostPort>
• -p <ip>::<containerPort>
• 将指定的容器端口<containerPort>映射至主机指定<ip>的动态端口
• -p <ip>:<hostPort>:<containerPort>
• 将指定的容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
• “动态端口”指随机端口,具体的映射结果可使用docker port命令查看
Opening inbound communication
•“-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
等还是隔离的
• 联盟式容器彼此间存在端口冲突的可能性,因此,通常只会在多个容器上的程序需
要程序loopback接口互相通信、或对某已存的容器的网络属性进行监控时才使用此
种模式的网络模型
Open containers
•开放式容器共享主机网络名称空间的容器,它们对主机的网络名称空
间拥有全部的访问权限,包括访问那些关键性服务,这对宿主机安全
性有很大潜在威胁
•为docker run命令使用“--net host”选项即可创建开放式容器,例如:
• ~]# docker run -it --rm --net host busybox:latest /bin/sh
Dockerfile
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
镜像相关的操作
•镜像的生成途径
• Dockerfile
• 基于容器制作
About Dockerfile
•Dockerfile is nothing but the source code for building Docker images
• Docker can build images automatically by reading the instructions from a Dockerfile
• A Dockerfile is a text document that contains all the commands a user could call on
the command line to assemble an image
• Using docker build users can create an automated build that executes several
command-line instructions in succession
Dockerfile Format
•Format
• # Comment
• INSTRUCTION arguments
•The instruction is not case-sensitive
• However, convention is for them to be UPPERCASE to distinguish them from
arguments more easily
•Docker runs instructions in a Dockerfile in order
•The first instruction must be `FROM` in order to specify the Base
Image from which you are building
Environment replacement
•Environment variables (declared with the ENV statement) can also be used in
certain instructions as variables to be interpreted by the Dockerfile
•Environment variables are notated in the Dockerfile either
with $variable_name or ${variable_name}
•The ${variable_name} syntax also supports a few of the
standard bash modifiers
• ${variable:-word} indicates that if variable is set then the result will be that value.
If variable is not set then word will be the result.
• ${variable:+word} indicates that if variable is set then word will be the result,
otherwise the result is the empty string.
.dockerignore file
•Before the docker CLI sends the context to the docker daemon, it looks for a
file named .dockerignore in the root directory of the context
•If this file exists, the CLI modifies the context to exclude files and directories
that match patterns in it
•The CLI interprets the .dockerignore file as a newline-separated list of
patterns similar to the file globs of Unix shells
Dockerfile Instructions
•FROM
• FROM指令是最重的一个且必须为Dockerfile文件开篇的第一个非注释行,用于
为映像文件构建过程指定基准镜像,后续的指令运行于此基准镜像所提供的运
行环境
• 实践中,基准镜像可以是任何可用镜像文件,默认情况下,docker build会在
docker主机上查找指定的镜像文件,在其不存在时,则会从Docker Hub Registry
上拉取所需的镜像文件
• 如果找不到指定的镜像文件,docker build会返回一个错误信息
• Syntax
• FROM <repository>[:<tag>] 或
• FROM <resository>@<digest>
• <reposotiry>:指定作为base image的名称;
• <tag>:base image的标签,为可选项,省略时默认为latest;
Dockerfile Instructions
•MAINTANIER
• 用于让Dockerfile制作者提供本人的详细信息
• Dockerfile并不限制MAINTAINER指令可在出现的位置,但推荐将其放置于
FROM指令之后
• Syntax
• MAINTAINER <authtor's detail>
• <author's detail>可是任何文本信息,但约定俗成地使用作者名称及邮件地址
•If you wanted to add a private registry that you installed yourself, just add
another --add-registry. For example:
Registry options(2)
•If you want to prevent users from pulling images from the Docker registry,
uncomment the BLOCK_REGISTRY entry so it appears as follows:
•To access a registry that uses https protocol for security, but is not set up with
certificates for authentication, you can still access that registry by defining it as
an insecure registry in the /etc/sysconfig/docker file. For example:
Registry configurations
Creating a reverse proxy
Configuring HTTPS (TLS) on the reverse proxy
Thank You!
讲师:马永亮(马哥)
http://www.magedu.com
Kubernetes
讲师:马永亮(马哥) 主讲:马永亮(马哥)
QQ群:169777636
http://www.magedu.com 客服QQ:2813150558, 1661815153
http://www.magedu.com
http://mageedu.blog.51cto.com
CRUSH
•RedHat
• GlusterFS
• Ceph
•masters/nodes
• master:
• apiserver、scheduler、controller-manager
• etcd
• node:
• kubelet(agent):
• kube-proxy
• docker(container engine)
• Registry:
• harbor
• Addons(附件)
• kube-dns
• UI: dashboard
• ……
•CNI:
• Node Network:Node IP
• Cluster Network:ClusterIP (ServiceIP)
• 不会配置于任何网络接口之上,它们定义在iptables或ipvs规则中;
• Pod Network:PodIP
•虚拟化网络模型实现:
• flannel(CoreOS)
• Project Calico
• Canel
• kube-rote
• weave network
• ……
•IaaS:
• 托管模型
• K8SaaS:
• VPS
•物理机
• Rancher 2.0
• OpenShift (PaaS)
• ……
•kubeadm(beta)
•kubespray, kopts
•ansible (roles)
Pod生命周期,启动、终止
nginx-svc
apiserver
kube-proxy
kubelet
ServiceIP:ServiceP
ort
ClusterIP
•Pod控制器:
• label selector:选定受控的Pod资源;
• PodTemplate:用于创建Pod资源的配置模板;
• replicas:副本数量
RESTful API
•http协议
•资源,对象
• uri
• /articles/devops/1
• /api/v1/pods/my-pod
• JSON
•CRUD
• PUT
• GET,HEAD
• POST, PATCH
• DELETE
JWT, RBAC
•www.magedu.com/test
•CLI: kubectl
•web GUI: dashboard
admission control,准入控
制
•Pod
• Controller
• Service, Ingress
•存储插件:Volume
•网络插件:Network Policy
•Authentication, Authorization, Admission
•helm
•kubectl
• 直接命令:run, expose, scale, set image,
• 资源配置文件:命令式(create)
• 资源配置文件:声明式(apply)
• 资源:(属性:值)
• apiVersion: groupname/version
• kind:种类,Pod/Service/Deployment/ReplicationController/...
• metadata:元数据, object
• name:名称
• namespace:名称空间,默认为default
• labels:标签
• annotations:注解
•Pod、Pod Controller、Service
• Pod Controller:管控各Pod对象
• Service:为某一类Pod应用提供固定的访问端点
Kubernetes
•Kubernetes is an open source system for managing containerized applications
across multiple hosts, providing basic mechanisms for deployment,
maintenance, and scaling of applications
•Kubernetes builds upon a decade and a half of experience at Google running
production workloads at scale using a system called Borg, combined with
best-of-breed ideas and practices from the community
Velocity
Adoption
Kubernetes Cluster
•A running Kubernetes cluster contains node agents (kubelet) and a cluster
control plane (AKA master), with cluster state backed by a distributed storage
system (etcd)
Kubernetes Architecture View
Kubernetes Architecture
•Clients, Master, Node, Registry
Kubernetes Master
•API Server, Scheduler, Controller-Manager Server
Kubernetes Node
•Kube-proxy, kubelet, Container runtime(docker, rkt, cri-o, frakti) , Pod
Kubernetes Architecture
Key Concepts of Kubernetes
•Pod - A group of Containers
•Labels - Labels for identifying pods
•Kubelet - Container Agent
•kube-proxy - A load balancer for Pods
•etcd - A metadata service
•cAdvisor - Container Advisor providers resource usage/performance statistics
•Replication Controller - Manages replication of pods
•Scheduler - Schedules pods in worker nodes
•API Server - Kubernetes API server
What is Pod?
•A group of one or more containers that are always co-located and co-
scheduled that share the context
•Containers in a pod share the same IP address, ports, hostname and storage
•Modeled like a virtual machine
• Each container represents one process
• Tightly coupled with other containers in the same pod
•Pods are scheduled in Nodes
•Fundamental unit of deployment in Kubernetes
What is a Pod?
•Containers within the same pod
communicate with each other using
IPC;
•Containers can find each other via
localhost;
•Each container inherits the name of
the pod
•Each pod has an IP address in a flat
shared networking space
•Volumes are shared by containers in
a pod
Deploying a Pod
Use cases for Pod
•Content management systems, file and data loaders, local cache managers, etc.
•Log and checkpoint backup, compression, rotation, snapshotting, etc.
•Data change watchers, log tailers, logging and monitoring adapters, event
publishers, etc.
•Proxies, bridges, and adapters
•Controllers, managers, configurators, and updaters
Replication Controller
•Ensures that a Pod or homogeneous set of Pods are always up and available
•Always maintains desired number of Pods
• If there are excess Pods, they get killed
• New pods are launched when they fail, get deleted, or terminated
•Creating a replication controller with a count of 1 ensures that a Pod is always
available
•Replication Controller and Pods are associated through Labels
Controllers
Services
•Kubernetes Pods are mortal, they are born and when they die, they are not
resurrected
•ReplicationControllers in particular create and destroy Pods dynamically (e.g.
when scaling up or down or when doing rolling updates)
•While each Pod gets its own IP address, even those IP addresses cannot be
relied upon to be stable over time
•This leads to a problem: if some set of Pods (let’s call them backends)
provides functionality to other Pods (let’s call them frontends) inside the
Kubernetes cluster, how do those frontends find out and keep track of which
backends are in that set?
Services
•A Kubernetes Service is an abstraction which defines a logical set of Pods and
a policy by which to access them - sometimes called a micro-service
•The set of Pods targeted by a Service is (usually) determined by a Label
Selector
•For Kubernetes-native applications, Kubernetes offers a
simple Endpoints API that is updated whenever the set of Pods in
a Service changes
•For non-native applications, Kubernetes offers a virtual-IP-based bridge to
Services which redirects to the backend Pods
Kubernetes Object: Service
Labels & Selectors
•Key/Value pairs associated with Kubernetes objects
•Used to organize and select subsets of objects
•Attached to objects at creation time but modified at any time
•Labels are the essential glue to associated one API object with other
• Replication Controller ->Pods
• Service -> Pods
• Pods->Nodes
Services
•An abstraction to define a logical set of Pods bound by a policy by to access
them
•Services are exposed through internal and external endpoints
•Services can also point to non-Kubernetes endpoints through a Virtual-IP-
Bridge
•Supports TCP and UDP
•Interfaces with kube-proxy to manipulate iptables
•Service can be exposed internal or external to the cluster
• A Service as a static API object
• virtual, but static IP
• no service discovery necessary
Services
•A group of pods that work together
• grouped by a selector
•Defines access policy
• "load balanced" or "headless"
•Gets a stable virtual IP and port
• sometimes called the service portal
• also a DNS name
•VIP is managed by kube-proxy
• watches all services
• updates iptables when backends change
•Hides complexity-ideal for non-native apps
kube-proxy
•Runs on each node - proxies UDP and TCP - does not understand HTTP
- provides load balancing - is just used to reach services
• The Kubernetes network proxy runs on each node
• This reflects services as defined in the Kubernetes API on each node and can do simple
TCP,UDP stream forwarding or round robin TCP,UDP forwarding across a set of
backends
• Service cluster ips and ports are currently found through Docker-links-compatible
environment variables specifying ports opened by the service proxy
• There is an optional addon that provides cluster DNS for these cluster Ips
• The user must create a service with the apiserver API to configure the proxy
Proxy-mode: userspace
•For each Service it opens a port (randomly chosen) on the local node
• Any connections to this “proxy port” will be proxied to one of the Service’s
backend Pods
Proxy-mode: iptables
•For each Service it installs iptables rules which capture traffic to
the Service’s clusterIP (which is virtual) and Port and redirects that traffic to
one of the Service’s backend sets
•For each Endpoints object it installs iptables rules which select a backend Pod
Discovering services
•Kubernetes supports 2 primary modes of finding a Service - environment
variables and DNS
•Environment variables
• When a Pod is run on a Node, the kubelet adds a set of environment variables for each active Service. It
supports both Docker links compatible variables and
simpler {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the
Service name is upper-cased and dashes are converted to underscores
•DNS
• An optional (though strongly recommended) cluster add-on is a DNS server
• The DNS server watches the Kubernetes API for new Services and creates a set of DNS records for
each
• If DNS has been enabled throughout the cluster then all Pods should be able to do name resolution
of Services automatically
What is Ingress?
•Typically, services and pods have IPs only routable by the cluster network.
All traffic that ends up at an edge router is either dropped or forwarded
elsewhere
•An Ingress is a collection of rules that allow inbound connections to reach the
cluster services
Ingress
•Ingress -- url based routing
•An Ingress is a collection of rules that allow inbound connections to reach the
cluster services.
Kubernetes networking
•On GCE(Google Compute Engine)/GKE
• GCE Advanced Routes (program the fabric)
• "Everything to 10.1.1.0/24, send to this VM"
•Plenty of other ways
• Cilium
• OVN (Open Virtual Networking)
• Calico
• Romana
• Flannel
• OVS
• Open Contrail
• Cisco Contiv
• Others...
Overlay network concept
Overlay network concept
Architecture in details
Kubernetes Objects
•Kubernetes Objects are persistent entities in the Kubernetes system
•Kubernetes uses these entities to represent the state of your cluster
•Specifically, they can describe:
• What containerized applications are running (and on which nodes)
• The resources available to those applications
• The policies around how those applications behave, such as restart policies, upgrades,
and fault-tolerance
Kubernetes Objects
•A Kubernetes object is a “record of intent”–once you create the object, the
Kubernetes system will constantly work to ensure that object exists
•By creating an object, you’re effectively telling the Kubernetes system what
you want your cluster’s workload to look like; this is your cluster’s desired
state
•To work with Kubernetes objects–whether to create, modify, or delete
them–you’ll need to use the Kubernetes API
•When you use the kubectl command-line interface, for example, the CLI
makes the necessary Kubernetes API calls for you; you can also use the
Kubernetes API directly in your own programs
Object Spec and Status
•Every Kubernetes object includes two nested object fields that govern the
object’s configuration: the object spec and the object status
• The spec, which you must provide, describes your desired state for the object–the
characteristics that you want the object to have
• The status describes the actual state for the object, and is supplied and updated by the
Kubernetes system
•At any given time, the Kubernetes Control Plane actively manages an object’s
actual state to match the desired state you supplied
Describing a Kubernetes Object
•When you create an object in Kubernetes, you must provide the object spec
that describes its desired state, as well as some basic information about the
object (such as a name)
•When you use the Kubernetes API to create the object (either directly or
via kubectl), that API request must include that information as JSON in the
request body
•Most often, you provide the information to kubectl in a .yaml file. kubectl
converts the information to JSON when making the API request
Deployment
•Deployment enables declarative updates for Pods and ReplicaSets
一个示例
• kubectl create -f ./deployment-example.yaml --record
当前
Deployment的
目标状态
Pod的数量
要创建的Pod的模板
列出Pod中的容器
,至少应该一个,
且不可被更新 容器的名字,作为
DNS_LAEL使用
Docker image
Required Fields
•In the .yaml file for the Kubernetes object you want to create, you’ll need to
set values for the following fields
• apiVersion - Which version of the Kubernetes API you’re using to create this object
• kind - What kind of object you want to create
• metadata - Data that helps uniquely identify the object, including a name string, UID,
and optional namespace
•各种类型对象及其相关属性文档:
https://kubernetes.io/docs/resources-reference/v1.5
Kubernetes Workloads资源类型
•WORKLOADS
• Container
• CronJob
• DaemonSet
• Deployment
• Job
• Pod
• ReplicaSet
• ReplicationController
• StatefulSet
关于马哥教育
• 博客:http://mageedu.blog.51cto.com
• 主页:http://www.magedu.com
• QQ:2813150558, 1661815153, 113228115
• QQ群:169777636, 279599283
Thank You!
讲师:马永亮(马哥)
http://www.magedu.com