docker+k8s
1 简介
1.1 docker是什么
- docker是开源项目,诞生于2013年初,由dotCloud公司发布的基于Google公司的Go语言实现
- 后加入Linux基金会,遵从Apache2.0协议,项目代码在Github维护
- docker项目的目标是实现轻量级的操作系统虚拟化解决方案
- docker的基础是Linux容器(LXC)等技术,在LXC上进一步封装,让用户不需要关心容器管理,操作简便
1.2 为什么要用docker
1.2.1 docker容器虚拟化的好处
- 脱离底层物理硬件的限制,更方便的在网络上传播,任何时间任何地点都可获取可使用
- 新型的创建分布式应用程序,快速分发部署
- 通过容器来打包应用、解耦应用和运行平台
- 更加节约时间,且降低部署过程中出现问题的风险
1.2.2 docker在开发和运维中的优势
-
更快的交付和部署
使用docker开发人员可以使用镜像来快速构建一套标准的开发环境
开发完之后,测试和运维人员可以直接使用完全相同的环境来部署代码
只要是开发测试过的代码就可以确保在生产环境无缝运行
-
更高的利用资源
运行docker不需要额外的虚拟化管理程序的支持
docker是内核级的虚拟化,可以实现更高的性能,同时对资源的额外需求低
-
更轻松地迁移和扩展
docker容器几乎可以在任意的平台运行,包括物理机、虚拟机、公有云、私有云、PC等
支持主流的操作系统发行版本
高兼容性可以让用户在不同的平台之间轻松的迁移应用
-
更轻松的管理和更新
使用dockerfile只需小的配置修改就可以替代大量的更新工作
1.2.3 docker与虚拟机比较
- docker容器启动快,启动和停止可实现秒级,相比传统虚拟机的分钟级要快很多
- docker容器对系统资源需求少,一台主机上可以同时运行数千个Docker容器
- docker通过类似git设计理念的操作来方便用户获取、分发和更新应用镜像,存储复用,增量更新
- docker通过Dockerfile支持灵活的自动化创建和部署机制,可以提高工作效率,并标准化流程
特性 | 容器 | 虚拟机 |
---|---|---|
启动速度 | 秒级 | 分钟级 |
性能 | 接近原生 | 较好 |
内存 | MB级 | GB级 |
硬盘适应 | MB级 | GB级 |
运行密度 | 单台主机支持上千个 | 单台主机支持几个 |
隔离性 | 安全隔离 | 完全隔离 |
迁移 | 优秀 | 一般 |
1.3 Docker与虚拟化
- Docker及其他容器技术都属于操作系统虚拟化范畴,操作系统虚拟化最大特点是不需要额外supervisor支持
- 传统方式是在硬件层面实现虚拟化,需要有额外的虚拟机管理应用和虚拟机操作系统层
- Docker容器是在操作系统层面实现虚拟化,直接复用本地主机的操作系统,因此更加轻量级
2 Docker概念与使用
2.1 docker三大核心概念
2.1.1 镜像(Image)
2.1.2 容器(Container)
- 镜像(Image)和容器(Container),就像是面向对象的程序设计中的类和实例一样
- 镜像是静态的定义,容器是镜像运行的实体
- 容器可以被创建、启动、停止、删除、暂停等
2.1.3 仓库(Repository)
- 用来保存镜像的仓库
- 当我们构建好自己的镜像后需要放在仓库中。需要启动一个镜像时可以在仓库中下载下来
2.2 Docker安装(CentOS)
2.2.1 更换系统yum源
# 查看操作系统内核版本
[root@ccc ~]# uname -a
Linux ccc 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# 备份原来的yum源
[root@ccc ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 刷新yum源缓存
[root@ccc ~]# yum makecache
@H_512_404@
2.2.2 安装所需基础软件
# 安装必要的一些系统工具
[root@ccc ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 下载新的CentOS-Base.repo 到 /etc/yum.repos.d/
[root@ccc ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
@H_512_404@
2.2.3 安装yum源
# 安装yum源
[root@ccc ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
@H_512_404@
2.2.4 安装并更新Docker-CE
[root@ccc ~]# yum makecache fast
[root@ccc ~]# yum -y install docker-ce
@H_512_404@
2.2.5 启动并设置开机自启
[root@ccc ~]# systemctl enable --Now docker
@H_512_404@
2.2.6 测试启动
[root@ccc ~]# docker info
[root@ccc ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-11-30 21:57:57 CST; 1min 1s ago
@H_512_404@
2.3 Docker安装(ubuntu)
2.3.1 查看版本信息
# 第一种方式
root@alvin-test-os:~# uname -a
# 第二种方式
root@alvin-test-os:~# cat /proc/version
@H_512_404@
2.3.2 更新系统
root@alvin-test-os:~# apt-get update
@H_512_404@
2.3.3 安装基础软件
root@alvin-test-os:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common
@H_512_404@
2.3.4 安装GPG证书
root@alvin-test-os:~# curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - OK
@H_512_404@
2.3.5 写入软件源信息
root@alvin-test-os:~# sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
@H_512_404@
2.3.6 更新并安装Docker-CE
root@alvin-test-os:~# sudo apt-get -y update
@H_512_404@
2.3.7 启动并检验
root@alvin-test-os:~# docker version
@H_512_404@
2.4 第一个Docker实例
[root@ccc ~]# docker run -d --rm --name Nginx -p 80:80 Nginx
Unable to find image 'Nginx:latest' locally
latest: Pulling from library/Nginx
bb79b6b2107f: Pull complete
5a9f1c0027a7: Downloading [==============> ] 7.457MB/26.49MB
5a9f1c0027a7: Downloading [======================> ] 12.17MB/26.49MB
166a2418f7e8: Download complete
1966ea362d23: Download complete
@H_512_404@
3 使用Docker镜像
3.1 获取镜像
3.1.1 获取镜像pull
[root@ccc ~]# docker pull busyBox:latest
或
[root@ccc ~]# docker pull docker.io/library/busyBox:latest
@H_512_404@
@H_357_502@3.1.2 字段解释
[root@ccc ~]# docker pull Nginx:1.17
1.17: Pulling from library/Nginx
afb6ec6fdc1c: Pull complete
b90c53a0b692: Pull complete
11fa52a0fdc0: Pull complete
Digest: sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
Status: Downloaded newer image for Nginx:1.17
docker.io/library/Nginx:1.17
@H_512_404@
3.1.3 补充
3.2 查看镜像信息
3.2.1 镜像列表images
- docker images
- docker images ls
[root@ccc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Nginx latest bc9a0695f571 5 days ago 133MB
busyBox latest dc3bacd8b5ea 6 days ago 1.23MB
@H_512_404@
- REPOSITORY镜像来源:来自哪个仓库,默认来自:hub.docker.com
- TAG镜像标签:如1.17、latest
- IMAGE ID镜像ID:例如bc9a0695f571
- CREATED时间段:如5 days ago
- SIZE镜像大小:133MB
3.2.2 images支持的选项
[root@ccc ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
Nginx latest bc9a0695f571 5 days ago 133MB
busyBox latest dc3bacd8b5ea 6 days ago 1.23MB
Nginx 1.17 9beeba249f3e 6 months ago 127MB
@H_512_404@
- --digest=true|false 列出镜像的数字摘要值
[root@ccc ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
Nginx latest sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3 bc9a0695f571 5 days ago 133MB
busyBox latest sha256:9f1c79411e054199210b4d489ae600a061595967adb643cd923f8515ad8123d2 dc3bacd8b5ea 6 days ago 1.23MB
Nginx 1.17 sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699 9beeba249f3e 6 months ago 127MB
@H_512_404@
- -q 只显示镜像ID
[root@ccc ~]# docker images -q
bc9a0695f571
dc3bacd8b5ea
9beeba249f3e
@H_512_404@
3.3 推送镜像push
[root@Centos7 docker]# docker push registry.cn-hangzhou.aliyuncs.com/alvinos/py15-Nginx:1.19.2
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/alvinos/py15-Nginx]
908cf8238301: Pushed
eabfa4cd2d12: Pushed
60c688e8765e: Pushed
f431d0917d41: Pushed
07cab4339852: Pushed
1.19.2: digest: sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19 size: 1362
@H_512_404@
3.4 为镜像添加tag
[root@ccc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Nginx latest bc9a0695f571 5 days ago 133MB
busyBox latest dc3bacd8b5ea 6 days ago 1.23MB
Nginx 1.17 9beeba249f3e 6 months ago 127MB
[root@ccc ~]# docker tag busyBox:latest mybusyBox:latest
[root@ccc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Nginx latest bc9a0695f571 5 days ago 133MB
mybusyBox latest dc3bacd8b5ea 6 days ago 1.23MB
busyBox latest dc3bacd8b5ea 6 days ago 1.23MB
Nginx 1.17 9beeba249f3e 6 months ago 127MB
@H_512_404@
3.5 查看详细信息inspect
[root@ccc ~]# docker inspect busyBox
[
{
"Id": "sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843","RepoTags": [
"busyBox:latest","mybusyBox:latest"
],...
"Metadata": {
"LastTagTime": "2020-11-30T22:39:16.14474717+08:00"
}
}
]
@H_512_404@
3.6 查看镜像历史history
[root@ccc ~]# docker history busyBox
IMAGE CREATED CREATED BY SIZE COMMENT
dc3bacd8b5ea 6 days ago /bin/sh -c #(nop) CMD ["sh"] 0B
<missing> 6 days ago /bin/sh -c #(nop) ADD file:7f51bbea8802a227e… 1.23MB
@H_512_404@
3.7 搜索镜像
3.7.1 搜索镜像search
[root@ccc ~]# docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted,interactive,objec… 5661 [OK]
django Django is a free web application framework,… 1021 [OK]
...
@H_512_404@
3.7.2 search支持的选项
[root@ccc ~]# docker search python -f stars=300
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted,objec… 5661 [OK]
django Django is a free web application framework,… 1021 [OK]
@H_512_404@
- --limit 限制输出结果
[root@ccc ~]# docker search python --limit 3
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted,objec… 5661 [OK]
nikolaik/python-nodejs Python with Node.js 55 [OK]
circleci/python Python is an interpreted,objec… 41
@H_512_404@
[root@ccc ~]# docker search python --limit 3 --no-trunc
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted,object-oriented,open-source programming language. 5661 [OK]
nikolaik/python-nodejs Python with Node.js 55 [OK]
circleci/python Python is an interpreted,open-source programming language. 41
@H_512_404@
3.8 删除rmi与清理prune镜像
3.8.1 删除镜像rmi
[root@ccc ~]# docker rmi redis
Untagged: redis:latest
Untagged:
redis@sha256:5b98e32b58cdbf9f6b6f77072c4915d5ebec43912114031f37fa5fa25b032489
@H_512_404@
# -f 强制删除镜像
[root@ccc ~]# docker rmi -f redis
Untagged: redis:latest
Untagged:
redis@sha256:5b98e32b58cdbf9f6b6f77072c4915d5ebec43912114031f37fa5fa25b032489
@H_512_404@
3.8.2 清理镜像
# 查看原来的镜像
[root@ccc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Nginx latest bc9a0695f571 5 days ago 133MB
busyBox latest dc3bacd8b5ea 6 days ago 1.23MB
mybusyBox latest dc3bacd8b5ea 6 days ago 1.23MB
Nginx 1.17 9beeba249f3e 6 months ago 127MB
# -a 删除所有的未被使用的镜像
[root@ccc ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: Nginx:1.17
...
@H_512_404@
# -f 强制删除镜像而不进行提示
alvin@AlvindeMacBook-Pro: docker image prune -a -f
Deleted Images:
untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/swoole:latest
...
@H_512_404@
3.9 构建镜像commit
- docker commit [参数] [容器ID | 容器名称:版本号]
参数:
-a : 维护者
-m : 简介
-p : 保存镜像时,镜像暂停运行
@H_512_404@
3.10 保存镜像
3.10.1 针对容器
-
export和import针对点是容器,将本机的容器导出为镜像包
-
export 针对容器保存镜像
docker export [容器名或ID] > [压缩包名称]
例如 docker export 3302aab7a252 > Nginx.tar
-
import 针对export保存的压缩包
# 使用export保存容器为镜像
[root@ccc ~]# docker export daf9c3656be3 > Nginx.tar
[root@ccc ~]# ll | grep Nginx.tar
-rw-r--r-- 1 root root 135117824 9 月 24 20:51 Nginx.tar
# 使用import导入包为镜像
[root@ccc ~]# docker import Nginx.tar test/Nginx:v1
sha256:02107323de1b074c5d2034b01eff855fec5922b45776c2721882d100ba6dd15b
[root@ccc ~]# docker images | grep test
test/Nginx v1 02107323de1b 22 seconds ago 131MB
@H_512_404@
3.10.2 针对镜像
-
save和load针对点是镜像,将本机的镜像导入、导出为镜像包
-
save针对镜像保存镜像
docker save [镜像名或ID] > [压缩包名称]
-
load导入镜像
docker load < [压缩包名称]
# 使用save保存镜像
[root@ccc ~]# docker save 6858809bf669 > busyBox.tar
[root@ccc ~]# ll | grep busy
-rw-r--r-- 1 root root 1458176 9 月 24 21:01 busyBox.tar
# 使用save保存多个镜像
[root@ccc ~]# docker save -o test.tar busyBox Nginx:1.18.0
[root@ccc ~]# docker load < test.tar
Loaded image: busyBox:latest
Loaded image: Nginx:1.18.0
# 使用load导入镜像
[root@ccc ~]# docker load < busyBox.tar
[root@ccc ~]# docker load -i busyBox.tar
Loaded image ID:
sha256:6858809bf669cc5da7cb6af83d0fae838284d12e1be0182f92f6bd96559873e3
[root@ccc ~]# docker images | grep 685880
busyBox latest 6858809bf669 2 weeks ago 1.23MB
@H_512_404@
3.10.3 区别
- export导出的镜像文件体积小于save保存的镜像
- import可以指定镜像名称,load不可以
- save可以将多个镜像打包到一个文件中,export不支持
- import导入的镜像会丢失所有历史记录和元数据信息,无法回滚
- load加载的镜像没有丢失镜像的历史,可以回滚
4 仓库Repository
-
docker login [参数] [仓库URL]
-
docker login --username=xxx xxx.com
-
docker push [仓库URL]/[命名空间]/[仓库名称]:[版本号]
5 使用Docker容器
5.1 创建容器run
5.1.1 格式
- docker run [option] image [cmd]
5.1.2 参数
- 容器启动参数
-d : 以守护进程的方式运行
-p : 指定端口映射(格式:宿主主机端口:容器向外暴露的端口)
docker run -d -p 8899:80 Nginx:1.19.2
-P : 随机端口映射
docker run -d -P Nginx:1.19.2
--name: 指定容器的名称(同一台宿主主机上的docker名称不能重复)
将容器的名称解析到Docker DNS
docker run -d --name Nginx_name -P Nginx:1.19.2
--rm:当一个容器结束了它的生命周期,就立即删除
docker run -d --rm --name Nginx_rm Nginx:1.19.2
-v: 映射存储卷(可以映射文件及文件夹)
docker run -d -v /root/test:/usr/share/Nginx/html -P Nginx:1.19.2
-i : 打开标准输出
-t : 创建一个伪终端
-e : 在容器内设置一个环境变量
docker run -d -e Nginx_NAME=Nginx Nginx:1.19.2
@H_512_404@
[root@alvin-test-os ~]# docker run -d --name Nginx -p 80:80 Nginx
Unable to find image 'Nginx:latest' locally
latest: Pulling from library/Nginx
852e50cd189d: Pull complete
a29b129f4109: Pull complete
b3ddf1fa5595: Pull complete
c5df295936d3: Pull complete
232bf38931fc: Pull complete
Digest: sha256:c3a1592d2b6d275bef4087573355827b200b00ffc2d9849890a4f3aa2128c4ae
Status: Downloaded newer image for Nginx:latest
6381d29d6e0ec3f6b01cf1aabb58b799ee88acf1a722e251807c9cb44e73a3e0
@H_512_404@
5.2 查看运行的容器ps
5.2.1 格式
- docker ps 查看正在运行的容器
5.2.2 参数
- -a 查看容器(包括已经停止了的容器)
- -q 只查看容器ID
[root@ccc ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
140d8682230f 9beeba249f3e "Nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:32769->80/tcp tender_cohen
85113c409717 bc9a0695f571 "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:32768->80/tcp suspicIoUs_germain
[root@ccc ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
140d8682230f 9beeba249f3e "Nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:32769->80/tcp tender_cohen
85113c409717 bc9a0695f571 "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:32768->80/tcp suspicIoUs_germain
a9d93f8e8df2 busyBox "sh" 4 hours ago Exited (0) 4 hours ago competent_hawking
[root@ccc ~]# docker ps -q
140d8682230f
85113c409717
@H_512_404@
5.3 停止容器stop
- docker stop [容器名称 | 容器ID]
- docker stop $(docker ps -q)
5.4 进入容器
5.4.1 attach
docker attach [容器名或ID]
[root@ccc ~]# docker attach Nginx
127.0.0.1 - "GET / HTTP/1.1" 308 171 "-" "curl/7.59.0" 0.000 - .
@H_512_404@
5.4.2 exec
docker exec [参数] [容器名或ID] [命令]
- 相当于在容器中执行一个命令
[root@ccc ~]# docker exec -it Nginx /bin/bash
Nginx [ / ]$
Nginx [ / ]$
@H_512_404@
5.4.3 nsenter
- 需配合docker inspect使用
[root@ccc ~]# nsenter --target $( docker inspect -f {{.State.Pid}}
Nginxv1 ) --mount --uts --ipc --net --pid
mesg: ttyname Failed: No such device
@H_512_404@
5.4.4 ssh
- 使用Docker容器不建议使用ssh进入Docker容器内
5.5 删除容器rm
5.5.1 格式
- docker rm
[root@ccc ~]# docker rm Nginx
@H_512_404@
5.5.2 强制删除-f
[root@ccc ~]# docker rm -f Nginx
Nginx
@H_512_404@
5.6 复制
5.6.1 容器-->宿主主机
- docker cp [容器ID:容器内文件路径] 宿主主机路径
[root@ccc ~]# docker cp 726b695a337c:/opt/start .
[root@ccc ~]# ls | grep start
Start
@H_512_404@
5.6.2 宿主主机-->容器
- docker cp 宿主主机路径 [容器ID:容器内文件路径]
[root@ccc ~]# docker cp start 726b695a337c:/root
[root@ccc ~]# docker exec 726b695a337c ls /root
start
@H_512_404@
6 Docker网络
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。