设为首页 收藏本站
查看: 1130|回复: 0

[经验分享] Linux 第83天 Docker基础

[复制链接]

尚未签到

发表于 2019-2-20 11:11:48 | 显示全部楼层 |阅读模式
  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
  将指定的容器映射至主机所有地址的一个动态端口
  -p :
  将容器端口映射至指定主机主机端口
  -p ::
  将指定的容器端口映射至主机指定的IP的动态端口
  -p ::
  将指定的容器端口映射至主机指定的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的镜像是分层构建的,
  下层的镜像对于上层来说是只读的,只有在顶层才是可写层,所以当修改下层的镜像文件
  内容时,会产生写时复制,即将下层的文件复制至可写层,并在可写层做修改,但下层的
  原文件还是存在的,只是对上层隐藏了




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-674824-1-1.html 上篇帖子: 使用docker搭建附带kuptun的S 下篇帖子: Docker Volume
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表