容器检查点 (Róngqì jiǎnchádiǎn)
引言
这个检查点旨在帮助您评估我们关于容器的入门文章所学到的知识,我们在这些文章中介绍了容器生态系统以及两种常见的容器解决方案:Docker和Kubernetes。您可以使用这个检查点来评估自己在这些主题上的知识,复习重要术语和命令,并找到持续学习的资源。
容器化是在操作系统级别上隔离开发环境以创建可移植运行时环境的过程。容器共享主机资源,能够在可预测和可控的环境中运行应用程序。使用容器对基础架构进行抽象和应用隔离,使您能够高效和一致地扩展开发和测试流程。
在这个检查点中,你会找到两个部分,它们从引言文章中综合了核心观点:一个部分总结了容器生态系统的目的,另一个部分涉及运行命令,分别包含了Docker和Kubernetes的特定子部分。在这些部分中,有互动组件可以帮助你测试知识。在这个检查点的最后,你将找到继续学习容器的机会。
资源
- Introduction to Containers
- How To Install and Use Docker on Ubuntu 22.04
- How To Install and Use Docker Compose on Ubuntu 22.04
- How To Use docker exec to Run Commands in a Docker Container
- How To Share Data between Docker Containers on Ubuntu 22.04
- How To Set Up a Private Docker Registry on Ubuntu 20.04
- An Introduction to Kubernetes
- How To Use minikube for Local Kubernetes Development and Testing
什么是容器?
容器为应用程序开发提供了一个可预测且受控的环境。当人们提到容器时,通常指的是容器引擎。
要理解容器,熟悉一些关键的容器化工具是很重要的。
Info
对以下术语进行解释,然后使用下拉功能来检查你的答案。
容器引擎
容器引擎是容器化的完整解决方案,引擎包括容器、容器运行时、容器镜像以及构建它们的工具。引擎还可能包括容器镜像注册表和容器编排。Docker是最常用的容器引擎之一。
容器镜像
容器镜像是提供给容器环境构建的模板。当运行一个镜像时,镜像的只读层与可调整的读写层相叠加,用于修改每个容器实例。
容器运行时
容器运行时管理容器的启动和执行。容器运行时分为两个组:开放容器倡议(OCI)和容器运行时倡议。
开放容器倡议提供容器格式的规范和标准。OCI提供了运行容器的基准,例如常用的runc。
容器运行时倡议专注于容器编排。
容器编排
容器编排涉及到资源分配、配置、扩展、调度、部署、监控等内容,通常用于自动化容器部署。
许多开发人员喜欢容器化,因为它具有可移植性和可预测的性能。将基础架构抽象化使得在多台机器上以完全相同的方式测试应用程序成为可能。这种基准环境的稳定性赋予开发人员协作和远程工作的能力。
Info
检查自己
使用容器时有哪些常见目标?
为了确保强大的开发和生产环境,您可能希望满足以下需求:
可预测和可移植的性能
使用和内存的高效率
状态不可持久性,但有持久数据的选项
具有集群网络功能的隔离容器
在容器测试期间记录错误和输出
应用程序和基础架构解耦
专业化和微服务
当从相同的基础镜像创建容器时,它们提供了一个可靠的环境,能够满足你们的开发和测试需求。这种一致性使你能够根据应用程序的开发情况随时创建和销毁容器。
如果您正在处理需要在容器内保存持久数据的长期项目,您可以挂载一个卷以进行持久数据存储和容器间数据共享。
Kubernetes和Docker是两种常见的容器解决方案。在下面的部分中,您可以通过在入门文章中使用这两种选择,评估您对它们之间的区别了解到的内容。
Info
自我检查
Docker和Kubernetes之间有什么区别?
Docker是一个容器引擎,通常用于同时运行一个或两个容器化应用程序。
Kubernetes是一个编排软件,可以使您能够在规模上运行许多重叠的容器。Kubernetes最初使用Docker运行时,但现在主要使用containerd抽象层。
什么是Docker?
使用Docker,可以在容器中管理应用程序进程。Docker通常作为容器引擎使用,并可在各种操作系统上运行。
您可以通过映射端口或者允许Docker选择一个随机未使用的端口,将Docker容器映射到主机上。这一过程简化了您的测试环境。如果想了解更多有关使用Docker的信息,您可以参阅《Docker生态系统:常见组件介绍》。
通过使用特定术语来支持系统设置,Docker能够评估您对其术语的了解。
Info
以下是关于 Docker 的相关术语,帮助你了解每个术语的含义。
Docker Compose
Docker Compose 是一个命令行工具,可以通过一个 YAML 文件来定义和运行多容器应用环境。YAML 配置文件将定义环境设置,通常包括一个 Web 服务器环境、端口重定向和共享卷。
Docker Hub
Docker Hub 是由 Docker 公司管理的特定镜像注册表,他们是 Docker 项目背后的公司。
Docker 镜像
Docker 镜像是构建容器的模板。有许多常用的镜像可供使用。
Docker 卷
Docker 卷可用于在容器之间持久化数据,包括已停止或已删除的容器。
Swarm
Docker Swarm 模式是一种用于管理集群的方法,这个集群被称为 Swarm。SwarmKit 是一个基于 Go 语言构建的开源编排工具包,可以用于 Swarm 模式集群。这与之前的名为 Docker Swarm 的工具是不同的,后者现在被称为 Swarm Classic,它指的是 Docker 的早期原生集群方案。Swarm Classic 曾经是一种用于大规模集群的选项。
Docker容器提供了一个可移植和一致的运行环境,通常可部署到Kubernetes集群中。
什么是Kubernetes?
使用Kubernetes,您可以在机群中运行和管理容器化应用程序和服务。Kubernetes经常用于满足容器化需求的扩展。
使用 Kubernetes 的特定术语来支持其系统设置。通过了解 Kubernetes 术语来评估您的知识水平。
Info
请定义以下术语,然后使用下拉菜单来核对你的答案。
部署
一个部署是使用复制集来进行周期管理的Kubernetes工作负载。
复制集用于管理Pod,类似于复制控制器,用于定义扩展副本Pod的模板。
节点
集群中的服务器被称为节点,其中一个节点充当集群的控制平面,其他服务器作为工作节点进行功能运行。每个节点都包括容器运行时,用于在该节点上管理应用程序和容器中的服务。
Pod
被作为单个应用程序进行控制的容器被分组为Pod。Pod可由复制控制器或复制集创建。
您的 Kubernetes 集群包括一个中央服务器和节点,其中包含特定的组件,帮助集群正常运行并且使服务器之间能够相互通信。要更详细了解 Kubernetes 架构,请参阅《Kubernetes 简介》。
为了实现集群中服务器的网络配置便利,Kubernetes在控制平面上需要特定的组件,这些组件也可以在节点之间进行访问,包括:
- A key-value store that can be distributed across nodes, such as etcd.
- An API server (kube-apiserver) that can be used to configure workloads and send commands.
- A controller manager (kube-controller-manager) to manage workload, perform tasks, and regulate the cluster.
- A scheduler (kube-scheduler) to assign workloads to specific nodes.
- A cloud controller (cloud-controller-manager) to interact with the cloud provider, resources, and services on behalf of the cluster.
节点还需要特定的组件与中央服务器通信并运行其分配的工作负载,包括:
- A container runtime on each node, such as Docker or runc.
- A communication service like kubelet to and from the control plane; kubelet uses a manifest to to define the workload it receives and manage the node’s work state.
- A small proxy, most commonly kube-proxy, to forward requests to containers.
有专门用于Docker和Kubernetes的命令行工具,每个工具都有自己的语法。当部署后,您还可以使用Docker Desktop或通过Web浏览器访问的Kubernetes仪表板来管理容器。
使用命令行
你开始使用Linux命令行是通过我们的云服务器入门文章,使用有关网页服务器解决方案的文章配置了一个网页服务器,并通过有关数据库的文章管理了你的数据库。
在容器的介绍中,通过诸如以下命令,您继续通过命令行开发熟悉度:
- cat to display a file.
- chmod to set permissions with a new tool.
- curl to transfer data with a specified location (the URL).
- echo to display a string passed as an argument.
- env to print all environment variables.
- exit to close the interactive Docker shell.
- mkdir to create new directories.
- pwd to print the present working directory.
- systemctl to manage the Docker daemon.
- tail to print the last ten lines of a file.
- watch to run a specific command consistently (every two seconds by default).
- whoami to print the current user’s username.
你还曾使用不同容器引擎的独特命令语法,并使用Homebrew软件包管理器为你的Kubernetes集群安装了minikube。在下面的部分中,你将回顾你在Docker和Kubernetes的入门文章中运行的命令。
运行Docker命令
在介绍Docker的文章中,你在Ubuntu服务器上安装和管理了一个Docker容器。默认情况下,只有root用户可以运行docker命令,你可以在前面添加sudo权限或者将用户添加到docker用户组中。
你使用了docker命令,用来传递选项、子命令和参数给你的Docker容器。
- docker exec to run commands in an active container, using the –workdir flag to specify the directory that a command should be run in, the –user flag to run a command as a different user, and the -e flag to pass an environment variable into the container or the –env-file flag to specify a .env file.
- docker images to review the images you have downloaded to your system.
- docker info to access system-wide information.
- docker ps to review active containers running on your system, using the -a switch to view all containers, both active and inactive, and the -l switch to view the latest container you created.
- docker rename to rename your container.
- docker rm with the container ID or name to remove a container.
- docker run to start a container from a specified image, using the combined -it switches for interactive shell access.
- docker search to search for images available on Docker Hub and docker pull to download a specified image from the registry.
- docker start with the container ID or name to start a stopped container.
- docker stop with the container ID or name to stop a running container.
- docker tag to rename a created image.
- docker volume to manage your data volumes.
Info
使用下拉功能获取答案。
docker和docker exec命令有什么区别?
docker exec命令用于运行程序和检查当前正在运行的容器,而你可能会使用docker run命令来创建新的容器。
什么是Dockerfile?
你可以使用docker build命令使用Dockerfile构建镜像,尽管在这些介绍性文章中你没有运行过该命令。Dockerfile用于构建镜像,而使用docker compose运行YAML配置文件则会管理编排。
你还使用了–help选项来访问各种子命令可用的选项,例如以下开关。
- –name to give a name to a container.
- –rm to create a container that removes itself when it’s stopped.
- -d will detach the container from the terminal to run it in the background.
- -v to manage your Docker Volume by naming it or to create a bindmount (when specifying a / or ~/).
Docker还提供了Docker Compose的命令行工具来管理多容器环境。你可以设置一个YAML配置文件,创建一个带有端口重定向和共享卷的Web服务器环境,并运行以下命令来管理你的容器:
- docker compose up runs the containerized environment.
- docker compose ps provides information about running containers and port redirection.
- docker compose logs accesses the logs for your container.
- docker compose pause pauses the container and docker compose unpause resumes it.
- docker compose stop will stop the container.
- docker compose down removes the containers, networks, and volumes associated with the environment.
Info
使用下拉功能获取答案。
使用docker命令和运行docker compose之间的主要区别是什么?
docker命令在命令行中运行所有子命令,而docker compose则运行一个YAML文件以提供配置数据,该数据可用于多个容器环境。使用docker compose启动的容器还可以共享网络和数据卷。
Docker Compose作为一个有益工具的原因是什么?
当您有一个较大的部署,同时运行多个容器时,您可以编写一个YAML文件来设置容器配置,然后运行docker compose来对所有组件发出命令并一起控制它们。结果,docker compose能够帮助您在应用程序增长并且需要一个强大的协调设置时扩展容器管理。
通过私有Docker镜像仓库和Docker Hub管理您的图像,您运行了以下命令:
- docker login to log in with the -u switch for your username.
- docker commit to commit a new Docker image with the -m switch to provide a commit message and the -a switch to specify the author.
- docker push to push your images, including to your own repository.
- docker pull to pull your images to a new machine.
对于持久性数据,您还可以设置一个Docker Volume。您可以使用docker volume命令与create子命令来创建一个新的卷,使用inspect子命令来验证系统上的卷,使用ls子命令来列出卷,并在卷名后附加:ro以获得只读权限。接下来,您可以在Docker容器和主机之间共享数据。
Docker是一种常见的容器引擎,Kubernetes是一个可以运行Docker容器引擎的编排平台。
运行Kubernetes命令
在介绍Kubernetes的文章中,你使用Docker框架运行minikube伴侣,模拟了在单台机器上运行的Kubernetes集群,这使你能够访问Kubernetes集群的浏览器仪表板。
你运行了以下 minikube 命令:
- minikube start to start the tool and enable kubectl, optionally using the -p or –profile option to specify a cluster.
- minikube dashboard to access the Kubernetes dashboard with automatic port forwarding, using the –url option to aid port forwarding from a remote server via SSH tunneling.
- minikube service with a specified service and the –url option to retrieve a URL for a service that is running.
- minikube config to manage your cluster with subcommands like set memory, get profile,
- minikube delete to delete the service in order to redeploy it.
- minikube mount to mount a directory from your local file system into the cluster temporarily, using the local_path:minikube_host_path syntax to specify which directory and where in the container.
- minikube profile with a specified cluster to switch the active profile.
你还使用了 kubectl 命令和以下子命令:
- kubectl get pods for a list of all the pods running in your cluster, using the -A argument to find all namespaces.
- kubectl create deployment to create a named deployment as a service, using the –image option to call a specified remote image.
- kubectl expose deployment to expose the named deployment, specifying a port with the –port option and the –type option.
- kubectl get service to check if a specified service is running.
- kubectl get nodes to list the active node(s) in the cluster, using the –kubeconfig option to specify a different YAML configuration file.
要使用托管的 Kubernetes 服务,您可以查看 Silicon Cloud Kubernetes 快速入门文档。
接下来是什么?
通过对容器和流行的数据库容器引擎有更深入的了解,您可以将开发环境容器化,从而在构建应用程序时实现一致性。这对于一个复杂的主题来说,可能看起来太简单了,但现在您至少有一个甚至两个容器生态系统可以进行实验、应用构建和项目扩展。
要构建更多的 Docker 生态系统,您可以接下来按照这些教程来操作:
- Containerizing a Node.js Application for Development With Docker Compose
- How To Secure a Containerized Node.js Application with Nginx, Let’s Encrypt, and Docker Compose
如果您喜欢立即开始操作,请尝试Silicon Cloud Marketplace 的Docker一键解决方案,它会启动一个已安装 Docker 的vServer(云主机)。您还可以查阅Silicon Cloud产品文档中的Container Images部分,了解如何部署容器镜像。
为了继续发展您的Kubernetes集群,您可以参考以下文章:
- Architecting Applications for Kubernetes
- Modernizing Applications for Kubernetes
- Best Practices for Rearchitecting Monolithic Applications to Microservices
- Our series From Containers to Kubernetes with NodeJS or with Django
如果您希望释放资源以进行开发,您可以迁移到Silicon Cloud托管的Kubernetes(DOKS)服务。您还可以使用Silicon Cloud容器注册表(DOCR)来提供对Docker容器和Silicon Cloud Kubernetes集群的额外支持。
凭借你对容器的新掌握,你已经做好了继续进行云端安全措施的旅程的准备。如果你还没有看过我们关于云服务器、Web服务器和数据库的入门文章,现在就去看一下吧。