目录
CKA: Certified Kubernetes Administrator是关于k8s的认证,也是另一门更高认证CKS的基础。
k8s是一个用来管理容器的工具,所以必须要依赖于对应的容器技术(或者说容器运行时),docker就是其中之一(docker engine),其他的容器技术还有containerd等。在1.24版本之前默认使用docker,现在需要自己指定。而且docker也是将容器技术发扬光大的最大功臣(反正我是从docker开始接触容器的),而且其也是制定image(容器镜像)的标准制定者。
容器是什么
封装
容器(本文针对的就是docker, 所以下面默指docker) 就是一个打包封装,可以想象成一个集装箱,程序(应用)及其所需要的环境、依赖等等等等所有都打包成一个镜像,然后通过docker cli来进行管理(CS架构,有服务端dockerd, 并提供RestAPI来给客户端调用,客户端docker CLI来进行管理和操作)。所以镜像一旦做完,就可以实现到处运行,易扩展,且基于该镜像生成的容器(里面跑的应用)也都相同,而不会因为环境问题出现各种不一致。可以将镜像当成一个模具,出来的容器都一样。
沙盒
同时容器也是一种沙盒,一种隔离,通过名称空间(Namespace)和控制组(cgroup)来实现资源的隔离,使各个容器互不影响。
轻量”虚拟机“
之前的应用大部分跑在服务器或者虚拟机上,而且虚拟机也是一种封装和隔离,和容器技术很像,但容器是轻量级的虚拟机,并不需要在宿主机上安装虚拟操作系统,而是直接共享宿主机的内核(kernel),本质就是跑在宿主机上的一个进程。
vm: OS > Hypervisor > virtualOS (kernel + bin/lib) > App
docker:OS > DockerDeamon > App
相比于vm来说,启动更快,资源(cpu, memory, storage等)利用率更高。
概念
docker中重要的概念就5个,仓库(Registry),镜像(Image),容器(Container),网络(Network),存储卷(Volume)。
image是模板,container是基于镜像的运行实例,在其中可以跑各种应用,网络是容器之间进行通信的基础,volume则是数据永久存储的关键(因为容器结束销毁后运行时产生的数据都没了)。
分层存储
镜像并不像虚拟机中用的OS镜像一样是一个大文件,而是基于UnionFS联合文件系统技术的一组文件,也就是分层的概念,一个镜像是由多层组成的(基于是否在上一层基础上有文件改动),而且这些层都是只读层,在运行时会在其上新建一个可读写的容器层,所有数据及其改动都写在这个容器层上,容器一旦结束,则将这一层丢弃。在读取文件时也是从容器层开始至上而下的查找读取。比如运行一个Centos的容器,然后我在容器里删除了/etc下的某个文件,则docker并不是真正的将文件从镜像中删除,而是在容器层创建一个whiteout文件,将被删除的文件mark成删除了。同理修改/etc下的某个文件,也只是将该文件复制到容器层并进行修改。基于读取时是从上到下,所以最终看到的也就是容器层里修改后的文件(copy-on-write),但并不是真正的修改了下层的文件。只有容器层是可写的,其余层都是只读的。
操作
- pull - 从registry中拉取镜像
- push - 将本地镜像发布到registry中
- run - 运行指定镜像,从而生成一个基于该镜像的容器(也就是镜像的运行时,在镜像上新建了一层可写的容器层)
- attach (exec -it) - 进入指定的容器,attach只是该本地的流绑定到容器里,而exec则是在容器里运行个程序,-it是指以交互方式运行个tty终端
- start - 运行一个停止的容器
- stop - 停止一个正在运行的容器
- rm - 移除一个停止的容器,或者通过-f来强制移除正在运行的容器
还可以通过管理命令来管理对应的资源,比如docker image ls (简写为docker images)来查看本地的镜像,更多的命令可以参看help文档,多用就熟悉了,没啥好说的。
jtian@k8s-master:~$ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/jtian/.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 "/home/jtian/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/jtian/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/jtian/.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* Docker Buildx (Docker Inc., v0.8.2-docker)
compose* Docker Compose (Docker Inc., v2.6.0)
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
scan* Docker Scan (Docker Inc., v0.17.0)
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.
====================================================================
links:
Certified Kubernetes Administrator (CKA) | Cloud Native Computing Foundation (cncf.io)