docker & k8s

Docker

docker到底与一般的虚拟机有什么不同呢?
我们知道一般的linux系统即GNU/Linux系统包括两个部分,linux系统内核+GNU提供的大量自由软件,而centos就是众多GNU/Linux系统中的一个。
虚拟机会在宿主机上虚拟出一个完整的操作系统与宿主机完全隔离,是一个重量级的系统,而doker利用linux系统的namespace等特性使用宿主机的内核+自己的GNU外壳虚拟出一个轻量级的linux系统,也能实现与宿主机的隔离。所以,我们使用docker pull下来的操作系统例如centos pull下来的只有GNU外壳,不包含linux内核,所以体积很小。
在这里插入图片描述

分层镜像

镜像分层的一大好处就是共享资源,例如有多个镜像都来自与同一个base镜像,那么在docker host值需要存储一份base镜像。内存中之需要加载一份host,即可为多个容器服务。即使多个容器共享一个base镜像,当某个容器修改了base镜像,如修改了/etc/下的配置文件,其他容器也不会受到影响,这就是容器的写时复制机制。
在这里插入图片描述
当容器启动后,一个新的可写层容器层被加载岛镜像的顶部,,所有对容器的修改动作,都只会发生在容器层,只有容器层是可写的,其余镜像层都是只读的。
在这里插入图片描述

文件操作 说明
添加文件 在容器中创建文件是,新文件会被添加到容器层
读取文件 在容器中读取某个文件时,Dokcer会自上而下依次再各镜像中查找此文件,一旦找到,立即将其复制到容器层
修改文件 在容器中修改已存在的文件时,Docker会自上而下依次在各镜像层中查找此文件,一旦找到,就会将其复制到容器层,然后修改
删除文件 在容器中删除文件时,Docker也是从上往下依次在镜像层中查找此文件,找到后,会在容器层记录下此操作

基本概念

  • image:容器打包的镜像文件
  • container:容器运行实例
  • docker registry:镜像提交的远程仓库
  • xxx.tar:镜像导出的本地文件
  • dockerfile:构建镜像的命令行文件(类似shell脚本)

生命周期

docker生命周期

基本操作

dockerfile

主要组成部分:

  • 基础镜像信息 FROM centos
  • 制作镜像操作指令RUN yum install openssh-server -y
  • 容器启动时执行指令 CMD /bin/bash

基本指令:

  • FROM 基础镜像
  • MAINTAINER 维护者信息
  • RUN 构建容器时执行的命令
  • ADD 从宿主机copy文件给容器,会自动解压
  • COPY 作用和ADD一样,无自动解压
  • WORKDIR 设置shell窗口的当前工作目录
  • VOLUME 将容器某些文件目录挂载在宿主机的目录位置(保证该目录下文件存储在宿主机而不是容器)
  • EXPOSE对外暴露的端口
  • CMD 容器启动之后要干的事
  • ENTRYPOINT 类似与CMD,但是和CMD不同的是,docker run 容器 后面的的参数可以追加在ENTRPOINT后面,而如果使用CMD的话,则会完全覆盖CMD,因此,一般使用ENTRYPOINT来设置固定的命令,而在docker run命令中追加可变的参数

容器

使用

docker run ${image} 

来运行容器,使容器处于前台运行状态,但是如果容器内什么事情也没有做,会自动退出。
如:

daitian@DaiT-Home:~$ docker run centos
daitian@DaiT-Home:~$ docker ps -a | grep centos
b62dde459b45   centos    "/bin/bash"              4 minutes ago   Exited (0) 4 minutes ago                                       objective_darwin
14193bbb3f25   centos    "/bin/bash"              7 hours ago     Exited (127) 7 hours ago                                       friendly_euler

可以看出容器直接exited。
但是像nginx这种,会直接将服务运行在前台

daitian@DaiT-Home:~$ docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/05/14 10:53:00 [notice] 1#1: using the "epoll" event method
2023/05/14 10:53:00 [notice] 1#1: nginx/1.23.4
2023/05/14 10:53:00 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/05/14 10:53:00 [notice] 1#1: OS: Linux 5.15.79.1-microsoft-standard-WSL2
2023/05/14 10:53:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/05/14 10:53:00 [notice] 1#1: start worker processes
2023/05/14 10:53:00 [notice] 1#1: start worker process 29
2023/05/14 10:53:00 [notice] 1#1: start worker process 30
2023/05/14 10:53:00 [notice] 1#1: start worker process 31
2023/05/14 10:53:00 [notice] 1#1: start worker process 32
2023/05/14 10:53:00 [notice] 1#1: start worker process 33
2023/05/14 10:53:00 [notice] 1#1: start worker process 34
2023/05/14 10:53:00 [notice] 1#1: start worker process 35
2023/05/14 10:53:00 [notice] 1#1: start worker process 36
2023/05/14 10:53:00 [notice] 1#1: start worker process 37
2023/05/14 10:53:00 [notice] 1#1: start worker process 38
2023/05/14 10:53:00 [notice] 1#1: start worker process 39
2023/05/14 10:53:00 [notice] 1#1: start worker process 40
2023/05/14 10:53:00 [notice] 1#1: start worker process 41
2023/05/14 10:53:00 [notice] 1#1: start worker process 42
2023/05/14 10:53:00 [notice] 1#1: start worker process 43
2023/05/14 10:53:00 [notice] 1#1: start worker process 44
2023/05/14 10:53:00 [notice] 1#1: start worker process 45
2023/05/14 10:53:00 [notice] 1#1: start worker process 46
2023/05/14 10:53:00 [notice] 1#1: start worker process 47
2023/05/14 10:53:00 [notice] 1#1: start worker process 48

使用exec进入正在运行的容器

docker exec ${容器名称} command(ex:bash) 

tips:直接用root用户进入容器
docker exec -ti --user root {
    
    容器名} /bin/bash 

K8s

容器化时代,容器大规模部署,导致其难以维护,因此出现了k8s

  • 基于容器对应用的发布管理,更新,升级,降级
  • 负载均衡,服务发现
  • 跨机器,跨地区的网络模式
  • 自动扩缩容功能
  • 针对如nginx无状态服务的运行组件,如mysql等有状态服务的运行组件
  • 支持丰富的插件

参考视视频:添加链接描述

猜你喜欢

转载自blog.csdn.net/qq_15098623/article/details/130657235