现现在,Kubernetes已经彻底改变了软件开发方式。Kubernetes做为一个管理容器化工做负载及服务的开源平台,其拥有可移植、可扩展的特性,并促进了声明式配置和自动化,同时它还证实了本身是管理复杂微服务的主要参与者。而Kubernetes之因此可以在业界被普遍采用,究其缘由是Kubernetes迎合了如下需求:node
可是,你是否考虑过如何在使用实际须要的资源的同时得到强大的容器编排平台?最佳资源利用的关键是知道须要扩展什么应用程序以及什么时候须要扩展应用程序。所以,在本文中,咱们将讨论和学习如何扩展Kubernetes容器,而且咱们将特别关注两类服务:kubectl和Horizontal Pod Autoscaler(HPA)。后端
在绝大部分状况下和Kubernetes交互是经过一个名为kubectl的命令行工具。kubectl主要用于和Kubernetes API进行通讯来建立、更新以及删除在Kubernetes内的工做负载。在下文中,咱们将提供一些常见的命令,你能够利用它们开始管理Kubernetes。api
大部分常见的kubectl命令都提供了要执行的特定操做或动做,好比建立、删除等。这一方法一般涉及解释描述Kubernetes中的对象(pod、服务、资源等)的文件(YAML或JSON)。这些文件可用于模板以及环境中的持续文件,而且帮助保持Kubernetes对声明式配置的关注。命令行所指定的操做将会传递到API Server,而后根据须要与Kubernetes中的后端服务进行通讯。下方的表格能够帮助你安装kubectl:app
请注意:随着新版本的发布,适用于Windows的kubectl的最佳版本会有所变化。想要找到目前最合适的二进制文件,请访问如下网址:微服务
https://storage.googleapis.com/kubernetes-release/release/stable.txt工具
并根据须要调整上述URL。学习
kubectl 句法测试
kubectl句法以下:google
kubectl [command] [TYPE] [NAME] [flags]复制代码
kubectl操做spa
如下示例能够帮助你熟悉运行经常使用的kubectl操做:
kubectl apply - Apply or Update a resource from a file or stdin.
# Create a service using the definition in example-service.yaml.
kubectl apply -f example-service.yaml
kubectl get - List one or more resources.
# List all pods in plain-text output format.
kubectl get pods
# List all pods in plain-text output format and include additional information (such as node name).
kubectl describe - Display detailed state of one or more resources, including the uninitialized ones by default.
# Display the details of the node with name <node-name>.
kubectl describe nodes <node-name>
kubectl delete - Delete resources either from a file, stdin, or specifying label selectors, names, resource selectors, or resources.
# Delete a pod using the type and name specified in the pod.yaml file.
kubectl delete -f pod.yaml
# Delete all the pods and services that have the label name=<label-name>.
kubectl delete pods,services -l name=<label-name>
kubectl logs - Print the logs for a container in a pod.
# Return a snapshot of the logs from pod <pod-name>.
kubectl logs <pod-name>
# Start streaming the logs from pod <pod-name>. This is similar to the 'tail -f' Linux command.
kubectl logs -f <pod-name>复制代码
以上都是kubectl中经常使用的操做,若是你想了解更多,能够查阅kubectl的官方指南。此外,咱们在往期的文章中也有介绍:
Pod水平自动伸缩(HPA)是Kubernetes的一个重要功能,它可让你配置集群以自动伸缩正在运行的服务。HPA实现为一种Kubernetes API资源和controller。资源决定controller的行为,controller会按期调整replication controller或部署中的副本数量,使观察到的平均CPU利用率与用户指定的目标相匹配。
同时,HPA实现为控制回路,其周期由controller manager的–horizontal-pod-autoscaler-sync-period标志控制(默认值为30秒)。
在每一个周期期间,controller manager 会根据每一个HPA定义中指定的指标来查询资源利用率。Controller manager会从资源指标API(针对per-pod资源指标)或自定义指标API(针对全部其余指标)中得到指标。
HPA controller将会经过两种不一样的方式获取指标:direct Heapster access和REST client access。当使用direct Heapster access时,HPA将会经过API server的服务代理子资源直接查询Heapster。请注意,Heapster须要部署在集群上并在kube-system命名空间中运行。
HPA的工做流程包含如下四个步骤,如图所示:
当你推出HPA时请考虑如下因素:
在本文中,咱们讨论了两种扩展Kubernetes应用程序的主要工具,二者都是全部Kubernetes服务的关键组件。咱们看到了如何安装而且使用不一样的功能,如应用、获取、删除、描述以及kubectl的日志等。同时,咱们回顾并了解有关Horizontal Pod Autoscaler的信息,例如它是如何工做的以及它对任意Kubernetes服务的重要性。在扩展微服务应用程序时,kubectl和HPA都是Kubernetes的重要功能。
在上个月发布的Rancher 2.3中,已经集成了HPA功能,能够在Rancher中经过UI使用。目前,Rancher 2.3也已经stable,若是想要更全面地了解Rancher 2.3关注咱们下周三晚上的Rancher K8S云课堂吧!
欢迎添加小助手(wx:rancher2),进官方技术群,了解更多Kubernetes使用攻略