原文:https://kubernetes.io/docs/tutorials/kubernetes-basics/deploy-intro/
目标:
- 学习什么是应用部署
- 通过kubectl来部署你的第一个应用到k8s
Kubernetes Deployments(K8s部署)
Once you have a running Kubernetes cluster, you can deploy your containerized applications on top of it. To do so, you create a Kubernetes Deployment configuration. The Deployment instructs Kubernetes how to create and update instances of your application. Once you've created a Deployment, the Kubernetes master schedules mentioned application instances onto individual Nodes in the cluster.
开始我们创建好了Kubernetes集群,现在我们就可以部署我们的应用上去了,要部署应用需要先创建 Kubernetes Deployment configuration
(K8部署配置),本文由公众号"BlockChain栈"翻译制作
。这个配置将指导k8s如何创建,更新你的应用。当你创建好了一个部署后,k8s的master schedules
会吧你的应用实例推送到每个节点。
Once the application instances are created, a Kubernetes Deployment Controller continuously monitors those instances. If the Node hosting an instance goes down or is deleted, the Deployment controller replaces it.This provides a self-healing mechanism to address machine failure or maintenance.
当我们的应用实例创建好后,k8s Deployment Controller会一直监视这些实例。如果这些部署了应用的节点挂掉了或者被删除,Deployment Controller会替换掉他们。这提供了一种自修复的机制来处理机器故障或维护。
In a pre-orchestration world, installation scripts would often be used to start applications, but they did not allow recovery from machine failure. By both creating your application instances and keeping them running across Nodes, Kubernetes Deployments provide a fundamentally different approach to application management.
在pre-orchestration
的世界中,安装脚本通常被用于启动应用,但是它不能让已经失败的机器恢复。K8s Deployments
提供了一个全新管理应用的方法:通过创建应用程序实例并让它们跨节点运行。
Deploying your first app on Kubernetes
You can create and manage a Deployment by using the Kubernetes command line interface,
Kubectl
. Kubectl uses the Kubernetes API to interact with the cluster. In this module, you'll learn the most common Kubectl commands needed to create Deployments that run your applications on a Kubernetes cluster.
你可以用k8s命令行接口(Kubectl
)来创建和管理一个Deployment
, Kubectl
是使用k8s API来和集群进行交互的。本文由公众号"BlockChain栈"翻译制作
。在这节,你可以学习到在Kubernetes集群上运行应用程序所需的最常见的Kubectl
命令。
When you create a Deployment, you'll need to specify the container image for your application and the number of replicas that you want to run. You can change that information later by updating your Deployment; Modules 5 and 6 of the bootcamp discuss how you can scale and update your Deployments.
当你创建了一个Deployment
,你需要为应用程序指定容器镜像,并指定要运行的副本的数量。你也可以在以后更新Deployment
时修改这些信息。在第5节和第6节中,我们将会讨论如何来修改和更新你的Deployments
.
For our first Deployment, we'll use a
Node.js
application packaged in a Docker container. The source code and theDockerfile
are available in the GitHub repository for the Kubernetes Bootcamp.
在第一个Deployment中,我们将会用到一个包含Node.js应用包的Docker
容器,源码和Dockerfile
(生成Docker镜像的配置文件)在GitHub repository 中。
Now that you know what Deployments are, let's go to the online tutorial and deploy our first app!
现在你肯定知道了什么是Deployments
了吧,让我们开始在线部署我们的第一个app吧!
以下内容是在线实验部分文档内容
kubectl basics
Like minikube, kubectl comes installed in the online terminal. Type
kubectl
in the terminal to see its usage.The common format of a kubectl command is:kubectl
action resource This performs the specified action (likecreate
,describe
) on the specified resource (likenode
,container
). You can use--help
after the command to get additional info about possible parameters (kubectl get nodes --help
).
像minikube一样,kubectl 也是安装在我们的命令行终端上。在终端中输入kubectl
来查看它的使用说明。kubectl
命令的公共格式是:kubectl
操作资源,它会指定的资源(如节点、容器)上执行指定的操作(如创建、描述)。你可以使用--help
跟在命令之后获得关于可能参数的额外信息(如kubectl get node --help
)。
Check that kubectl is configured to talk to your cluster, by running the kubectl version command:
要想检查kubectl是否已经在你的集群中配置好了,可以使用 kubectl vesion 命令
To view the nodes in the cluster, run the
kubectl get nodes
command:
要想查看集群中的节点,可以使用 kubectl get nodes
命令。
Here we see the available
nodes
(1 in our case). Kubernetes will choose where to deploy our application based on Node available resources.
这里我们可以看到可用的node
(只有一个)。k8s会选择从中选择节点来部署我们的资源。
Deploy our app
Let’s run our first app on Kubernetes with the kubectl run command. The run command creates a new deployment. We need to provide the deployment name and app image location (include the full repository url for images hosted outside Docker hub). We want to run the app on a specific port so we add the--port parameter:
让我们使用 kubectl run
来运行我们的第一个app。run
命令会创建一个新的部署。我们需要提供部署的name
和应用的镜像位置(包括在Docker中心外托管的图像的完整存储库url
)。如果想让我们的app运行在一个特殊的端口上可以使用--port
参数:
kubectl run kubernetes-bootcamp \
--image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080
Great! You just deployed your first application by creating a deployment. This performed a few things for you:
1.searched for a suitable node where an instance of the application could be run (we have only 1 available node)
2.scheduled the application to run on that Node
3.configured the cluster to reschedule the instance on a new Node when needed
好了,我们刚才通过创建一个deployment
来部署了我们的第一个应用。它执行了如下的几个事情:
1.寻找一个合适的节点,来给应用程序的实例运行(我们只有一个可用节点)。
2.调度应用程序在该节点上运行
3.配置集群,以便在需要时将实例重新安排到新节点上
To list your deployments use the get deployments command:
使用如下命令可以列出你的deployments:
kubectl get deployments
We see that there is 1 deployment running a single instance of your app. The instance is running inside a Docker container on your node.
我们可以看到有1个deployment
正在运行应用程序的一个实例,实例运行在节点上的Docker容器中。
Pods
that are running inside Kubernetes are running on a private, isolated network.By default they are visible from other pods and services within the same kubernetes cluster, but not outside that network.When we use kubectl , we're interacting through an API endpoint to communicate with our application.
Pods
是运行在k8s内部的一个私有的独立的网络中.默认情况下,它们可以在相同的k8s集群中的其他pod
和服务中可见,但是对外的网络是不可见的。当我们使用kubectl
时,我们通过API endpoint
来与我们的应用程序进行通信。
We will cover other options on how to expose your application outside the kubernetes cluster in Module 4.
在模块4中,我们将介绍如何在kubernetes集群之外公开应用程序的其他选项
The
kubectl
command can create a proxy that will forward communications into the cluster-wide, private network. The proxy can be terminated by pressingcontrol-C
and won't show any output while its running.
kubectl
命令可以创建一个代理来将通信转发到集群范围的私有网络。代理可以通过按control - c
来终止,并且在运行时不会显示任何输出。
kubectl proxy
We now have a connection between our host (the online terminal) and the Kubernetes cluster. The proxy enables direct access to the API from these terminals.
我们现在在主机(在线终端)和Kubernetes集群之间建立了连接。代理支持从这些终端直接访问API。
You can see all those APIs hosted through the proxy endpoint, now available at throughhttp://localhost:8001. For example, we can query the version directly through the API using thecurlcommand:
现在可以使用http://localhost:8001
看到所有这些api托管的代理端点。例如,我们可以使用命令直接通过API查询版本
curl http://localhost:8001/version
The API server will automatically create an endpoint for each pod,based on the pod name, that is also accessible through the proxy.First we need to get the Pod name, and we'll store in the environment variable POD_NAME:
API服务器将根据pod名称自动为每个pod创建一个endpoint
.这也可以通过代理访问。首先,我们需要获取Pod名称,然后在存储在环境变量POD_NAME
中:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
Now we can make an HTTP request to the application running in that pod:
现在我们可以对运行在该pod
中的应用程序进行HTTP
请求:
curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/$POD_NAME/
The url is the route to the API of the Pod.
这个url就是这个Pod的API的route。
Note: Check the top of the terminal. The proxy was run in a new tab (Terminal 2), and the recent commands were executed the original tab (Terminal 1). The proxy still runs in the second tab, and this allowed our curl command to work usinglocalhost:8001.
Note:检查终端的顶部。代理在一个新选项卡(Terminal2)中运行,而最近的命令执行了原始的选项卡(Terminal1),代理仍然在第二个选项卡中运行,因此我们的curl
命令可以使用localhost:8001
。
本文由公众号BlockChain栈
翻译制作。欢迎各位大佬关注微信公众号BlockChain栈