栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

docker基础

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

docker基础

docker基础

文章目录
  • docker基础
    • docker的基本概念
      • 1.Docker的定义
      • 2.优点
      • 3.Docker的用途
    • 容器的工作方式
    • CGroups
    • lxc的常用操作

docker的基本概念

Docker的概念与特点

1.Docker的定义

Docker是以Docker容器为资源分割和调度的基本单位,封装整个软件运行时环境,为开发者和系统管理员设计,用于构建、发布和运行分布式应用的平台。它是一个跨平台、可移植且简单易用的容器解决方案。Docker的源代码托管在GitHub.上, 基于Go语言开发,并遵从Apache 2.0协议。Docker可在容器内部快速自动化地部署 应用,并通过操作系统内核技术(namespace、 CGroups等 )为容器提供资源隔离与安全保障。

Docker本身并不是容器,它是创建容器的工具,是应用容器引擎。

Docker技术的三大核心概念,分别是:

镜像(Image)

容器(Container)

仓库(Repository)

2.优点

(1)更快的交付和部署

(2)高效的资源利用和隔离

(3)环境标准化和版本控制

(4)更轻松的迁移和扩展

(5)更简单的维护和更新管理

Docker不同于传统的虚拟机,Docker容器是使用Docker弓|擎而不是管理程序来执行的。它只包含应用程序及依赖库,基于Libcontainer运行在宿主机上,因此容器比虚拟机小,并且由于主机内核的共享,可以更快地启动,具有更好的性能、更少的隔离和更好的兼容性。启动容器只需几秒即可完成。由于Docker轻量、资源占用少,使得Docker可以轻易地应用到构建标准化的应用中。

3.Docker的用途

(1)简化配置

(2)代码管理化管理(3)开发人员的生产化(4)隔离应用

(5)整合服务器

(6)调试能力

(7)多租户环境

(8)快速部署

容器的工作方式

(1)通过namespace对不同的容器实现了隔离,namespace允许一个进程及 其子进程从 共享的宿主机内核资源(挂载点、进程列表等)中获得一个仅自 己可见的隔离区域,让同一个 namespace下的所有进程感知彼此变化,对外界进程一无所知, 仿佛运行在一个独占的操作 系统中- -样。

(2)通过Control Groups (CGroups) 隔离宿主机上的物理资源,如CPU、内存、磁盘I/O和网络带宽。使用CGroups还可以为资源设置权重、计算使用量、操控任务(进程或线程)启停等。

(3)使用镜像管理功能,利用Docker的镜像分层、写时复制、内容寻址、联合挂载技术实现了一套完整的容器文件系统及运行环境,结合镜像仓库,镜像可以快速下载和共享,以便在多环境中部署

CGroups

控制组(CGroups)是Linux内核的一个特性,用来对共享资源进行隔离、限制、审计等。只有能控制分配到容器的资源,Docker才能避免多个容器同时运行时的系统资源竞争。

控制组可以提供对容器的内存、CPU、磁盘IO等资源进行限制。

CGroups能够限制的资源有:

(1) blkio: 为每个块设备设置I/O限制,如磁盘、光盘和USB等设备。

(2) cpu:使用调度程序提供对CPU的cgroup任务访问。

(3) cpuacct:自动生成cgroup任务的CPU资源使用报告。

(4) cpuset: 为cgroup中的任务分配独立CPU (在多核系统中)和内存节点。 (5) devices: 允许或拒绝cgroup任务访问设备。

(6) freezer: 暂停和恢复cgroup任务。

(7) memory:设置每个cgroup任务使用的内存限制,并自动生成内存资源使用报告 (8) net cls:标记每个网络包以供cgroup任务使用。

(9) ns: 命名空间子系统。

具体来看,控制组提供如下功能:

  • 资源限制(Resource Limitting)组可以设置为不超过设定的内存限制。比如:内存子系统可以为进行组设定一个内存使用上限,一旦进程组使用的内存达到限额再申请内存,就会发出Out of Memory警告
  • 优先级(Prioritization)通过优先级让一些组优先得到更多的CPU等资源
  • 资源审计(Accounting)用来统计系统实际上把多少资源用到合适的目的上,可以使用cpuacct子系统记录某个进程组使用的CPU时间
  • 隔离(Isolation)为组隔离命名空间,这样一个组不会看到另一个组的进程、网络连接和文件系统
  • 控制(Control)挂起、恢复和重启等操作
lxc的常用操作
经过实践目前lxc无法在centos8上使用,所以我们以centos7为

lxc-checkconfig
//检查系统环境是否满足容器使用要求;

lxc-create
//创建lxc容器;
格式:lxc-create -n NAME -t TEMPLATE_NAME

lxc-start
//启动容器;
格式:lxc-start -n NAME -d

lxc-info
//查看容器相关的信息;
格式:lxc-info -n NAME

lxc-console
//附加至指定容器的控制台;
格式:lxc-console -n NAME -t NUMBER

lxc-stop
//停止容器;

lxc-destory
//删除处于停机状态的容器;

lxc-snapshot
//创建和恢复快照;

//退出容器方式:

//安装一个epel源
[root@lch ~]# yum -y install epel-release
//安装LXC软件包和依赖包
[root@lch ~]# yum -y install lxc lxc-templates bridge-utils lxc-libs libcgroup libvirt perl debootstrap
	lxc           #主程序包
	lxc-templates #lxc的配置模板
	bridge-utils  #网桥管理工具
	lxc-libs      #lxc所需的库文件
	libcgroup     #cgroup是为Linux内核提供任务聚集和划分的机制,通过一组参数集合将一些任务组织成一个或多个子系统。
	libvirt       #管理Linux的虚拟化功能所需的服务器端守护程序。 需要针对特定驱动程序的管理程序。

//启动服务
[root@lch ~]# systemctl start lxc libvirtd                                                  
[root@lch ~]# systemctl status lxc




//检查系统是否满足容器使用要求
[root@lch ~]#  lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.10.0-862.el7.x86_64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
newuidmap is not installed
newgidmap is not installed
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled

--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

//查看当前系统中可用的LXC模板容器
[root@lch ~]# ls /usr/share/lxc/templates/
lxc-alpine     lxc-busybox  lxc-debian    lxc-gentoo        lxc-oracle  lxc-ubuntu
lxc-altlinux   lxc-centos   lxc-download  lxc-openmandriva  lxc-plamo   lxc-ubuntu-cloud
lxc-archlinux  lxc-cirros   lxc-fedora    lxc-opensuse      lxc-sshd

//创建LXC容器
[root@lch ~]# lxc-create -t centos -n myhost1

Copy /var/cache/lxc/centos/x86_64/7/rootfs to /var/lib/lxc/myhost1/rootfs ... 
Copying rootfs to /var/lib/lxc/myhost1/rootfs ...//生成虚拟系统的根,文件默认路径在/var/lib/lxc/myhost1下
sed:无法读取 /var/lib/lxc/myhost1/rootfs/etc/init/tty.conf:没有那个文件或目录
Storing root password in '/var/lib/lxc/myhost1/tmp_root_pass'  
chpasswd:无法打开 /etc/passwd
正在终止用户 root 的密码。
passwd: Libuser error at line: 425 - Error replacing `/etc/passwd': 权限不够.
passwd: 错误
sed:无法读取 /var/lib/lxc/myhost1/rootfs/etc/rc.sysinit:没有那个文件或目录
sed:无法读取 /var/lib/lxc/myhost1/rootfs/etc/rc.d/rc.sysinit:没有那个文件或目录

Container rootfs and config have been created.
Edit the config file to check/enable networking setup.

The temporary root password is stored in:

        '/var/lib/lxc/myhost1/tmp_root_pass'  //这个文件保存了主机的初始root密码


The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible.  If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):

        chroot /var/lib/lxc/myhost1/rootfs passwd //可以使用这个命令修改初始root密码

//lxc主机所生成的文件默认路径在/var/lib/lxc/主机名/目录下
[root@lch ~]# tree /var/lib/lxc/myhost1/ -L 1
/var/lib/lxc/myhost1/
├── config
├── rootfs
└── tmp_root_pass

//修改密码,这里遇到点问题,需要关闭防火墙selinux,不然修改不了 换了一个主机,密码要8位以上
[root@lch ~]# chroot /var/lib/lxc/myhost1/rootfs passwd
更改用户 root 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

//启动
[root@lch ~]# lxc-start -n myhost1
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected virtualization lxc.
Detected architecture x86-64.

Welcome to CentOS Linux 7 (Core)!

Running in a container, ignoring fstab device entry for /dev/root.
Cannot add dependency job for unit display-manager.service, ignoring: Unit not found.
[  OK  ] Reached target Remote File Systems.
[  OK  ] Started Forward Password Requests to Wall Directory Watch.
[  OK  ] Reached target Local Encrypted Volumes.
[  OK  ] Created slice Root Slice.
[  OK  ] Listening on /dev/initctl Compatibility Named Pipe.
[  OK  ] Listening on Journal Socket.
[  OK  ] Created slice System Slice.
         Starting Read and set NIS domainname from /etc/sysconfig/network...
[  OK  ] Listening on Delayed Shutdown Socket.
[  OK  ] Created slice User and Session Slice.
[  OK  ] Reached target Slices.
         Starting Remount Root and Kernel File Systems...
         Starting Journal Service...
[  OK  ] Created slice system-getty.slice.
         Mounting POSIX Message Queue File System...
[  OK  ] Started Dispatch Password Requests to Console Directory Watch.
[  OK  ] Reached target Paths.
         Mounting Huge Pages File System...
[  OK  ] Reached target Swap.
[  OK  ] Started Journal Service.
[  OK  ] Mounted POSIX Message Queue File System.
[  OK  ] Mounted Huge Pages File System.
[  OK  ] Started Read and set NIS domainname from /etc/sysconfig/network.
[  OK  ] Started Remount Root and Kernel File Systems.
         Starting Flush Journal to Persistent Storage...
[  OK  ] Reached target Local File Systems (Pre).
         Starting Configure read-only root support...
         Starting Rebuild Hardware Database...
<46>systemd-journald[16]: Received request to flush runtime journal from PID 1
[  OK  ] Started Flush Journal to Persistent Storage.
[  OK  ] Started Configure read-only root support.
         Starting Load/Save Random Seed...
[  OK  ] Reached target Local File Systems.
         Starting Rebuild Journal Catalog...
         Starting Create Volatile Files and Directories...
         Starting Mark the need to relabel after reboot...
[  OK  ] Started Load/Save Random Seed.
[  OK  ] Started Rebuild Journal Catalog.
[  OK  ] Started Mark the need to relabel after reboot.
[  OK  ] Started Create Volatile Files and Directories.
         Starting Update UTMP about System Boot/Shutdown...
[  OK  ] Started Update UTMP about System Boot/Shutdown.
[  OK  ] Started Rebuild Hardware Database.
         Starting Update is Completed...
[  OK  ] Started Update is Completed.
[  OK  ] Reached target System Initialization.
[  OK  ] Listening on D-Bus System Message Bus Socket.
[  OK  ] Reached target Sockets.
[  OK  ] Reached target Basic System.
         Starting Permit User Sessions...
         Starting Login Service...
         Starting LSB: Bring up/down networking...
         Starting OpenSSH Server Key Generation...
[  OK  ] Started D-Bus System Message Bus.
[  OK  ] Started Daily Cleanup of Temporary Directories.
[  OK  ] Reached target Timers.
[  OK  ] Started Permit User Sessions.
         Starting Cleanup of Temporary Directories...
[  OK  ] Started Command Scheduler.
[  OK  ] Started Console Getty.
[  OK  ] Reached target Login Prompts.
[  OK  ] Started Login Service.
[  OK  ] Started Cleanup of Temporary Directories.
[  OK  ] Started OpenSSH Server Key Generation.

CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

myhost1 login: root  //输入之前创建的账户密码
Password: 

[root@myhost1 ~]# 
[root@myhost1 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
11: eth0@if12:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether fe:e5:ea:0c:fb:4b brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.122.202/24 brd 192.168.122.255 scope global dynamic eth0
       valid_lft 3533sec preferred_lft 3533sec
    inet6 fe80::fce5:eaff:fe0c:fb4b/64 scope link 
       valid_lft forever preferred_lft forever
       
[root@myhost1 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  3.9G   14G  23% /
devtmpfs                 476M     0  476M   0% /dev
tmpfs                    488M     0  488M   0% /dev/shm
tmpfs                    488M  6.3M  482M   2% /run
tmpfs                    488M     0  488M   0% /sys/fs/cgroup
tmpfs                     98M     0   98M   0% /run/user/0


//可以ssh直接连接
[root@localhost ~]# ssh 192.168.122.202
The authenticity of host '192.168.122.202 (192.168.122.202)' can't be established.
ECDSA key fingerprint is SHA256:hvfEka4oBUjMvE38Yju9sAuRhNM/6lRSWjIp7KoPTWE.
ECDSA key fingerprint is MD5:d3:81:8c:55:72:f7:29:3d:68:67:dc:63:9d:c0:ec:7d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.202' (ECDSA) to the list of known hosts.
root@192.168.122.202's password: 
Last login: Wed Dec  1 08:11:34 2021
[root@myhost1 ~]# 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830304.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号