每个知识点学习目标:了解(know)、理解(understand)、掌握(grasp)、复习(review)
主要讲解物流大数据项目项目介绍和项目解决方案。
1、项目介绍 行业背景、项目背景:数据量大(海量数据、大规模数据)和业务复杂 本项目基于一家大型物流公司研发的智慧物流大数据平台 物流实时大屏系统:ClickHouse数据库 + 服务接口SpringCloud + NodeJS/Vue 2、项目解决方案 物流快读:业务流程,6个部分组成,中间4个部分 快递单:tbl_express_bill,运单:tbl_waybill,客户表、快递员表、仓库表、车辆运输表 逻辑架构 业务数据:数据库RDBMs(Oracle、MySQL) 采集数据:OGG、Canal 数据缓冲:Kafka 实时ETL:StructuredStreaming 数据存储:Kudu(离线报表和即席查询Impala)、Es(物流信息检索)、ClickHouse(实时大屏和服务接口) 资源统一调度:YARN、Azkaban 数据流转图 业务数据存储 大数据存储与分析 技术选项,为什么选择这些框架?? Kafka,大数据流式处理 计算Spark,目前来说,能够使用Spark搞定,就不用Flink 非功能描述 服务器选项:云主机还是物理机 集群规模:按照数据量计算和规划 服务器资源规划 本次学习项目时,服务器说明
主要讲解
Docker
容器基本使用
1、Docker 容器是什么 虚拟化是什么 容器是什么 虚拟机与容器比较 2、Docker 框架安装部署 Docker容器引擎框架架构 C/S 安装,在线联网yum安装 启动服务及加速配置 3、Docker容器命令使用 镜像image 容器container
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dJSI4rek-1621332867773)(/img/image-20210518151229534.png)]
随着大数据平台型产品方向的深入应用实践和Docker开源社区的逐渐成熟,业界有不少的大数据研发团队开始使用Docker。
Docker会让大数据平台部署更加简单快捷、让研发和测试团队集成交付更加敏捷高效、让产线环境的运维更加有质量保障。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ib8AROFk-1621332867775)(/img/1615533027804.png)]
虚拟机
可以实现一些开发、测试与运维之间的问题,但是存在很多缺陷:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4fFmiDQQ-1621332867778)(/img/1615533188596.png)]
官网:https://www.docker.com/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xtZtQCqN-1621332867779)(/img/1615533217031.png)]
虚拟化(Virtualization)
,在Windows系统上安装VMWare虚拟化软件
,可以将物理机器资源虚拟化到机器上(VM),在虚拟机上安装操作系统,运行应用程序。在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源,予以抽象、转换后呈现出来,打破实体结构间的不可切割的障碍,使用户可以比原来的组态更好的方式来应用这些资源。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ONUmfiYA-1621332867780)(/img/1615533757293.png)]
虚拟化简单讲,就是把一台
物理计算机
虚拟成多台逻辑计算机
,每个逻辑计算机里面可以运行不同的操作系统,相互不受影响,这样就可以充分利用硬件资源。
- 1)、类型一、直接在硬件资源上安装虚拟化软件,创建虚拟机,安装操作系统,运行应用
- 2)、类型二、硬件之上安装操作系统,再安装虚拟化软件,创建虚拟机,安装操作系统,运行应用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cLUgOdMh-1621332867780)(/img/1615533906190.png)]
Docker是一个开源的应用
容器引擎(框架)
,创建容器,部署运行应用,比如创建容器Contanier,运行MySQL数据库。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B0k34sYO-1621332867781)(/img/1615535123251.png)]
Docker将应用及依赖打包到容器Contanier(
轻量级,可移植
),发布Deploy到任何流向linux服务器运行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Iwy8mh7m-1621332867781)(/img/1615535380570.png)]
Docker是一种容器技术,
解决软件跨环境迁移的问题
=
1)、什么是虚拟机
虚拟机是一个计算机系统的仿真,简单来说,虚拟机可以实现在一台物理计算机上模拟多台计算机运行任务。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-saPEktny-1621332867782)(/img/1615535594325.png)]
VMs也消耗大量系统资源,每个VM不仅运行一个OS的完整copy并且需要所有硬件的虚拟化copy,这消耗大量RAM和CPU。
- 2)、容器是
将操作系统虚拟化
- 容器是在操作系统之上,每个容器
共享OS内核
,执行文件和库等- 容器是非常轻量的,仅仅MB水平并且几秒即可启动
- VM相比,
容器仅需OS、支撑程序和库文件
便可运行应用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OucMoLhO-1621332867782)(/img/1615535739222.png)]
虚拟机VM和容器Contanier比较
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ucxzGyfx-1621332867783)(/img/1615535830164.png)]
Docker是一个
客户端-服务端(C/S)架构
程序,Docker客户端只需要向Docker服务端或者守护进程发出请求,服务端或者守护进程完成所有工作返回结果。
- 1)、Docker 服务端(Server),运行以后就是一个守护进程(后台运行),类比MySQL数据库服务
- 启动Docker 服务
- 2)、Docker 客户端(Client),向服务端发送请求指令,Server端接收以后,执行操作,完成以后返回结果给客户端,类比mysql
- 使用
docker
命令,操作容器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M14iYzsC-1621332867783)(/img/1612077125442.png)]
在同一台宿主机器上运行Docker守护进程或者客户端,Docker 容器引擎提供命令行:
docker
,就是客户端Client,可以向Docker 服务端发送请求
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w5LrFMb5-1621332867784)(/img/1612077210810.png)]
docker引擎是一个c/s结构的应用,主要组件见下图:
- 1)、
镜像image
:模板,相当Java中定义类- 2)、
容器Contanier
:实例,相当Java中基于类创建对象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jd80HhjP-1621332867784)(/img/1615536410014.png)]
1、Server是一个常驻进程
2、REST API 实现了client和server间的交互协议
3、CLI 实现容器和镜像的管理,为用户提供统一的操作界面
Docker使用C/S架构,Client 通过接口与Server进程通信实现
容器的构建,运行和发布
。client和server可以运行在同一台集群,也可以通过跨主机实现远程通信。构建(build):创建镜像、运行(run):创建容器和启动、发布(Deploy):应用运行和迁移。
1)、镜像Image
- 自定义
镜像文件:DockerFile
,build构建出镜像- 可以从注册中心仓库中拉取镜像,最大公开镜像仓库:
hub.docker.com
docker pull elasticsearch2)、容器Contanier
- 从镜像中创建容器,运行应用。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pz5zcwS0-1621332867785)(/img/1615537043833.png)]
- 1)、Docker 镜像
- Docker 镜像(Image)就是一个只读的模板。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bEvQXU5n-1621332867785)(/img/1615537480347.png)]
镜像(Image)就是一堆只读层(read-only layer)的统一视角
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-coKJo8K3-1621332867786)(/img/1615537538645.png)]
右边可以看到多个只读层,它们重叠在一起,除了最下面一层,其它层都会有一个指针指向下一层。
统一文件系统(Union File System)技术能够将不同的层整合成一个文件系统,为这些层提供了一个统一的视角,这样就隐藏了多层的存在,在用户的角度看来,只存在一个文件系统。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y13ewDWk-1621332867786)(/img/1615537623172.png)]
镜像采用分层的架构,更多为了更好利用资源,进行重用(各层可以被重复使用)。
Docker容器引擎(框架),Docker Server服务端和Docker Client客户端,将其安装在一台机器上。
直接解压提供压缩包【CentOS7.7.zip
】,导入至VMWare虚拟软件中,启动虚拟机。
导入虚拟机以后,启动虚拟机时,选择已经导入移动该虚拟机:192.168.88.10, node1.itcast.cn,root/123456
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ERqSO9Lx-1621332867787)(/img/1612079957733.png)]
修改虚拟机为命令行模式(启动后为桌面模式):
[root@node1 ~]# systemctl get-default graphical.target [root@node1 ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. [root@node1 ~]# [root@node1 ~]# systemctl get-default multi-user.target [root@node1 ~]# shutdown -r now
将Docker安装到Centos上,注意:建议安装在Centos7.x以上的版本。整个安装步骤参考教案即可,采用在线yum源方式安装,所以需要连接网络。
官网:https://docs.docker.com/engine/install/centos/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RjacIz1M-1621332867787)(/img/1615538753532.png)]
安装完成以后,启动Docker服务:
systemctl start docker
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bzQGDU1y-1621332867788)(/img/1615539483022.png)]
鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,配置加速器来解决,配置阿里云加速器。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OCQqtvrv-1621332867788)(/img/1615539708242.png)]
https://yeu2889p.mirror.aliyuncs.com
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://yeu2889p.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
Docker 引擎服务启动、停止和关闭命令:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YHyKcumD-1621332867788)(/img/1615539901884.png)]
使用Docker创建容器Contanier,运行CentOS 7操作系统。
[root@node1 ~]# docker pull centos:7 7: Pulling from library/centos 2d473b07cdd5: Pull complete Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e Status: Downloaded newer image for centos:7 docker.io/library/centos:7 [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 8652b9f0cb4c 3 months ago 204MB
[root@node1 ~]# docker run -di --name=mycentos centos:7 21cc295413f0730ecdc64605081bab53775f982c56163713c8d0360e6a5f015d [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 11 seconds ago Up 10 seconds mycentos
[root@node1 ~]# docker exec -it mycentos /bin/bash [root@21cc295413f0 /]# [root@21cc295413f0 /]# hostname 21cc295413f0 [root@21cc295413f0 /]# [root@21cc295413f0 /]# vi aa.txt [root@21cc295413f0 /]# [root@21cc295413f0 /]# more a aa.txt anaconda-post.log [root@21cc295413f0 /]# more aa.txt Hello Docker!
已经安装部署Docker引擎:
systemctl start docker
[root@node1 ~]# ps -ef|grep docker root 1851 1 0 08:56 ? 00:00:01 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Docker 容器引擎,提供客户端:
docker
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TosUUUDw-1621332867789)(/img/1615600508387-1621314383188.png)]
[root@node1 ~]# docker version Client: Docker Engine - Community Version: 20.10.5 API version: 1.41 Go version: go1.13.15 Git commit: 55c4c88 Built: Tue Mar 2 20:33:55 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.5 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 363e9a8 Built: Tue Mar 2 20:32:17 2021 OS/Arch: linux/amd64 [root@node1 ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) Server: Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 1 Server Version: 20.10.5 Storage Driver: overlay2 Backing Filesystem: xfs [root@node1 ~]# docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: app* Docker App (Docker Inc., v0.9.1-beta3) builder Manage builds buildx* Build with BuildKit (Docker Inc., v0.5.1-docker) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
在Docker容器引擎中,最重要命令就是:
镜像命令和容器命令
。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S7nkl6RZ-1621332867790)(/img/1615600776170-1621314383188.png)]
首先,看一下镜像相关常用命令:搜索镜像
search
、拉取镜像pull
、查看镜像list
及删除镜像rmi
从注册中心仓库中获取镜像,对镜像进行管理操作
注册中心,仓库地址:https://hub.docker.com
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-riF5fcv3-1621332867790)(/img/1615600863236-1621314383188.png)]
# 搜索镜像image [root@node1 ~]# clear [root@node1 ~]# docker search elasticsearch NAME DESCRIPTION STARS OFFICIAL AUTOMATED elasticsearch Elasticsearch is a powerful open source sear… 4894 [OK] nshou/elasticsearch-kibana Elasticsearch-7.10.1 Kibana-7.10.1 125 [OK] itzg/elasticsearch Provides an easily configurable Elasticsearc… 71 [OK] elastichq/elasticsearch-hq Official Docker image for ElasticHQ: Elastic… 70 [OK]
搜索镜像时,更多在WEB 界面搜索
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V0MKSEYn-1621332867790)(/img/1615601089585-1621314383188.png)]
当搜索到镜像以后,可以仓库中将镜像拉取pull到本地镜像
每个镜像image,都有版本tag,所以在拉取镜像时,需要选择合适的版本tag,当拉取镜像时,不指定tag版本,默认情况下将会拉取最新tag版本镜像。
[root@node1 ~]# docker pull elasticsearch:7.11.1 7.11.1: Pulling from library/elasticsearch 7a0437f04f83: Downloading [===> ] 5.934MB/75.18MB 744c7f8ba15d: Downloading [======================> ] 10.09MB/22.29MB a2e5f03a9eab: Download complete edcdb0cfb01b: Downloading [> ] 539.6kB/324.5MB
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uYleQsL4-1621332867791)(/img/1615601240320-1621314383189.png)]
当镜像拉取到本地以后,需要查看镜像和删除镜像
- 1)、查看镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E3xZfbsS-1621332867792)(/img/1615601300360-1621314383189.png)]
- 2)、删除镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kO7DiDjt-1621332867794)(/img/1615601348789-1621314383189.png)]
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 8652b9f0cb4c 3 months ago 204MB [root@node1 ~]# [root@node1 ~]# docker rmi 8652b9f0cb4c [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# docker rmi centos:7 [root@node1 ~]#
实际开发中,创建容器运行应用之前,首先获取对应镜像,往往都是从注册中心仓库中拉取,当将镜像拉取到本地以后,就可以创建容器。
1)、容器查看,本地有多少容器,有的运行,有的时停止的
Docker 中容器Container运行以线程方式运行,所以使用ps查看
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hsVtOfWn-1621332867795)(/img/1615601617510-1621314383189.png)]
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [root@node1 ~]# [root@node1 ~]# docker ps -f status=exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos
2)、容器创建,有2种方式,容器创建时需要指定使用镜像
image(模板) -> container(实例)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B9YEx5Af-1621332867795)(/img/1615601873246-1621314383189.png)]
- 方式一:启动交互式容器,
docker run -it --name=centos 镜像名称:标签 /bin/bash
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9HwV3udu-1621332867796)(/img/1615601963095-1621314383189.png)]
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [root@node1 ~]# [root@node1 ~]# docker run -it centos:7 /bin/bash [root@46188ebaf87f /]# hostname 46188ebaf87f [root@46188ebaf87f /]# [root@46188ebaf87f /]# exit exit [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 46188ebaf87f centos:7 "/bin/bash" 32 seconds ago Exited (0) 6 seconds ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos
当创建交互式容器时,创建完成以后,直接运行容器,并且进入容器中,如果退出容器,自动将容器关闭。
- 2)、方式二:守护式方式创建容器,
docker run -di --name=容器名称 镜像名称:标签
- 采用守护式方法创建容器以后,需要登录容器(相当于登录操作系统)
- 命令:
docker exec -it 容器名称(或者容器id) /bin/bash
[root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# [root@node1 ~]# docker run -di --name=mycentos2 centos:7 1741ea301b3f7c60992cd7b29a29747f15c46c6be6a916bbad213e16018a3303 [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 6 seconds ago Up 6 seconds mycentos2 [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# docker exec -it mycentos2 /bin/bash [root@1741ea301b3f /]# [root@1741ea301b3f /]# hostname 1741ea301b3f [root@1741ea301b3f /]#
实际项目中,创建容器时采用守护方式创建,使用exec进入容器进行操作。
当容器创建完成以后,有时需要停止容器运行、启动容器运行,相关命令如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QS0vOnKp-1621332867796)(/img/1615602427828-1621314383189.png)]
可以重启容器(先停止,再启动)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zgt4EtsI-1621332867797)(/img/1615602461159-1621314383189.png)]
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 4 minutes ago Up 4 minutes mycentos2 e56430030fff centos:7 "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago centos7 46188ebaf87f centos:7 "/bin/bash" 8 minutes ago Exited (0) 7 minutes ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [root@node1 ~]# [root@node1 ~]# docker start mycentos mycentos [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 4 minutes ago Up 4 minutes mycentos2 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Up 3 seconds mycentos [root@node1 ~]# [root@node1 ~]# docker stop mycentos2 mycentos2 [root@node1 ~]# [root@node1 ~]# docker kill 21cc295413f0 21cc295413f0 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 5 minutes ago Exited (137) 25 seconds ago mycentos2 e56430030fff centos:7 "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago centos7 46188ebaf87f centos:7 "/bin/bash" 9 minutes ago Exited (0) 8 minutes ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 6 seconds ago mycentos
当使用Docker 容器引擎,创建容器以后,每个容器运行时,都有IP地址,属于Docker容器内部IP地址。
[root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4148cf3b3dea centos:7 "/bin/bash" 3 minutes ago Up 3 minutes centos7 827ff98c2a29 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes centos [root@node1 ~]# [root@node1 ~]# ifconfig docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:d4ff:fef5:9869 prefixlen 64 scopeid 0x20<link> ether 02:42:d4:f5:98:69 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 13 bytes 1768 (1.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.88.10 netmask 255.255.255.0 broadcast 192.168.88.255 inet6 fe80::20c:29ff:fea0:85 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:a0:00:85 txqueuelen 1000 (Ethernet) RX packets 485440 bytes 672923357 (641.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 129820 bytes 8330903 (7.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 48 bytes 4080 (3.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 48 bytes 4080 (3.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth94d4a57: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::3408:a4ff:fe46:7687 prefixlen 64 scopeid 0x20<link> ether 36:08:a4:46:76:87 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vetha19c80a: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::cc15:1fff:fe6d:991d prefixlen 64 scopeid 0x20<link> ether ce:15:1f:6d:99:1d txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:f6:83:da txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如何查看容器中IP地址呢?
[root@node1 ~]# docker inspect centos [ { "Id": "827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1", "Created": "2021-03-13T03:02:42.354813578Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 53376, "ExitCode": 0, "Error": "", "StartedAt": "2021-03-13T03:02:47.515567445Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf", "ResolvConfPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/resolv.conf", "HostnamePath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/hostname", "HostsPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/hosts", "LogPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1-json.log", "Name": "/centos", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7-init/diff:/var/lib/docker/overlay2/c907f57265207bf28d834e05f41753b198a5b3df2b84ddd064e053d741e3ad5f/diff", "MergedDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/merged", "UpperDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/diff", "WorkDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "827ff98c2a29", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos:7", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201113", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "958164ab6fd78db5b500d75ec0c798318a7d1a89cc4e1de3e168e3a062e7f8d9", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/958164ab6fd7", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "1da3cb4de7247d1002b27ced094ea5d7a6a2a2c23d8157b77461cbee886e8f1c", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "051cdc01a196f22b35b248f863b7212fa2256adb4f1decf877a09bd05bdd4d59", "EndpointID": "1da3cb4de7247d1002b27ced094ea5d7a6a2a2c23d8157b77461cbee886e8f1c", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]
删除容器:当容器不再被使用时,可以直接删除容器Contanier
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FCMbnl9X-1621332867797)(/img/1615605219378-1621314383190.png)]
当创建容器以后,并且运行时,包含IP地址,提供相关网络适配器将宿主机IP地址转换为容器IP地址。此外,如果容器中运行服务,需要暴露端口号,需要设置容器端口号与宿主机端口号映射。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D1VleSbB-1621332867798)(/img/image-20210518175026918.png)]
接下来,采用Docker容器部署数据库和服务:MySQL数据库。
步骤:①搜索镜像、②拉取镜像、③查看镜像、④启动容器、⑤停止容器、⑥移除容器
[root@node1 ~]# docker search mysql [root@node1 ~]# docker pull centos/mysql-57-centos7 Using default tag: latest latest: Pulling from centos/mysql-57-centos7 d8d02d457314: Pull complete a11069b6e245: Pull complete 596303fb1aa3: Pull complete a29499e779a7: Pull complete 17d1a52c2e00: Pull complete ed24591227fe: Pull complete de0ad46e3ed9: Pull complete c62e4a984a9c: Pull complete 01d54c6bda68: Pull complete Digest: sha256:e08ee4d43b7356607685b69bde6335e27cf20c020f345b6c6c59400183882764 Status: Downloaded newer image for centos/mysql-57-centos7:latest docker.io/centos/mysql-57-centos7:latest [root@node1 ~]# docker images centos/mysql-57-centos7 latest f83a2938370c 17 months ago 452MB
命令:
docker run -dt --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7
其中在创建容器时,需要指定端口号映射
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ltmoqlxA-1621332867799)(/img/1615605653675-1621314383190.png)]
指定端口号映射方式:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yvHCKJnr-1621332867799)(/img/1615605683345-1621314383190.png)]
[root@node1 ~]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7 b3da50687358586f0b0a430ba138b0a080b39dbf244de4f92152919f51a3dae9 [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3da50687358 centos/mysql-57-centos7 "container-entrypoin…" 40 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp mysql [root@node1 ~]# docker exec -it mysql /bin/bash bash-4.2$ ps PID TTY TIME CMD 202 pts/0 00:00:02 bash 211 pts/0 00:00:00 ps bash-4.2$ bash-4.2$ ps -ef|grep mysql mysql 1 0 2 03:22 ? 00:00:02 /opt/rh/rh-mysql57/root/usr/libexec/mysqld --defaults-file=/etc/my.cnf mysql 202 0 12 03:23 pts/0 00:00:02 /bin/bash mysql 212 202 0 03:23 pts/0 00:00:00 ps -ef mysql 213 202 0 03:23 pts/0 00:00:00 grep mysql
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ihuJbg5a-1621332867800)(/img/1615605915549-1621314383190.png)]
-e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7
b3da50687358586f0b0a430ba138b0a080b39dbf244de4f92152919f51a3dae9
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3da50687358 centos/mysql-57-centos7 “container-entrypoin…” 40 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp mysql
[root@node1 ~]# docker exec -it mysql /bin/bash
bash-4.2$ ps
PID TTY TIME CMD
202 pts/0 00:00:02 bash
211 pts/0 00:00:00 ps
bash-4.2$
bash-4.2$ ps -ef|grep mysql
mysql 1 0 2 03:22 ? 00:00:02 /opt/rh/rh-mysql57/root/usr/libexec/mysqld --defaults-file=/etc/my.cnf
mysql 202 0 12 03:23 pts/0 00:00:02 /bin/bash
mysql 212 202 0 03:23 pts/0 00:00:00 ps -ef
mysql 213 202 0 03:23 pts/0 00:00:00 grep mysql
[外链图片转存中...(img-ihuJbg5a-1621332867800)]