当前位置:网站首页>Kubernetes stateless application expansion and contraction capacity
Kubernetes stateless application expansion and contraction capacity
2022-07-06 01:34:00 【a_ small_ cherry】
brief introduction : Before , We have learned how to deploy applications from the command line , In this article, we learn how to pass yaml Configuration file for application deployment , And expand and shrink the applied capacity .

Image download 、 Domain name resolution 、 Time synchronization please click Alibaba open source mirror site
Kubernetes object
This paragraph is for reference kubernetes The official manual Learning notes of , It is suggested to have a preliminary understanding , If you have understood relevant concepts , This paragraph can be skipped , Look directly at the following operations .
Kubernetes object Is a persistent entity .Kubernetes Use these entities to represent the state of the entire cluster .
The following information is described :
- Which containerization applications are running ( And where Node On )
- Resources that can be used by applications
- Strategies for application runtime performance , For example, restart strategy 、 Upgrade strategy , And fault tolerance strategy
Once the object is created ,k8s The cluster will start to work continuously to ensure that the object conforms to the desired state .
Object specification (Spec) And state (Status)
Every Kubernetes Object contains two nested object fields , They are responsible for managing the configuration of objects : object spec and object status . spec It's necessary , It describes the Expected state (Desired State) —— The characteristics of the desired object . status Describes the The actual state (Actual State) , It is from Kubernetes Provided and updated by the system . At any moment ,Kubernetes The control surface has been trying to manage the actual state of the object to match the expected state .
describe Kubernetes object
In general use yaml To describe a k8s object ,yaml It is a language specially used to write configuration .
The basic rules of grammar are as follows :
- Case sensitive
- Use indentation to indicate hierarchy
- Indenting is not allowed tab, Only spaces are allowed
- The number of indented spaces doesn't matter , Just align the elements at the same level to the left
- '#' Notation
Use yaml describe k8s object , The following required fields are required :
- apiVersion - The... Used to create the object Kubernetes API Version of
- kind - The type of object you want to create
- metadata - Data that helps identify object uniqueness , Including a name character string 、UID And optional namespace
- spec - Describes the Expected state (Desired State),k8s The cluster will continue to ensure that objects conform to the described state .
management Kubernetes object
management k8s There are three ways to object

Imperative command (Imperative commands)
This is the way we used above , Operate directly on the command line .
Such as :
kubectl create deployment nginx --image nginxAdvantages over object configuration :
- The order is simple , Easy to learn and easy to remember .
- The command changes the cluster in one step .
Disadvantages compared to object configuration :
- Commands are not integrated with the change review process .
- Command does not provide an audit trail associated with the change .
- In addition to real-time content , The command does not provide a record source .
- The command does not provide a template for creating new objects .
Imperative object configuration (Imperative object configuration)
Operation instructions and configuration files are required .
Such as :
kubectl create -f nginx.yamlAdvantages over imperative commands :
- Object configurations can be stored in the source control system , such as Git.
- Object configuration can be integrated with process , For example, checking for updates before pushing and auditing .
- Object configuration provides a template for creating new objects .
Disadvantages compared with imperative commands :
- Object configuration requires a basic understanding of object architecture .
- Object configuration requires extra steps to write YAML file .
Advantages over declarative object configuration :
- Imperative object configuration behavior is easier to understand .
- from Kubernetes 1.5 Version start , Imperative object configuration is more mature .
Disadvantages compared to declarative object configuration :
- Imperative object configuration is more suitable for files , Rather than a directory .
- Updates to active objects must be reflected in the configuration file , Otherwise, it will be lost in the next replacement .
Declarative object configuration (Declarative object configuration)
Configure with declarative objects , Specified actions that do not need to be displayed in the command , In this way, you can put the configuration file in the directory , Perform different operations on the files in the directory .
such as :
kubectl apply -f configs/Advantages over imperative object configuration :
- Changes made to the active object, even if not incorporated into the configuration file , It will be preserved .
- Declarative object configuration better supports operations on directories and automatically detects the operation type of each file ( establish , repair , Delete ).
Disadvantages compared with imperative object configuration :
- Declarative object configuration is difficult to debug and the result is difficult to understand when an exception occurs .
- Use diff The resulting partial updates create complex merge and patch operations .
Practical operation
establish Deployment
establish node-deployment.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-deployment
labels:
app: node
spec:
replicas: 3
selector:
matchLabels:
app: node
template:
metadata:
labels:
app: node
spec:
containers:
- name: node
image: registry.cn-hangzhou.aliyuncs.com/larswang/hello-node:1.0
ports:
- containerPort: 80establish
kubectl apply -f node-deployment.yamlsee deployments
kubectl get deployments
see pods
kubectl get pods --show-labels
The zoom Deployment
Expand to ten copies
kubectl scale deployment.v1.apps/node-deployment --replicas=10Extension complete view deployments and pods situation
kubectl get deployments
kubectl get pods --show-labels
Shrink to three copies
kubectl scale deployment.v1.apps/node-deployment --replicas=3Now you can see that among 7 individual pod be in Terminating state . After a while , Look again , There's only... Left 3 Are running Of pod.

Automatic recovery
To look at first pods list
kubectl get pods --show-labelsChoose one of them pod And delete
kubectl delete pod node-deployment-57df45c5bf-d8xstAfter the deletion is successful , Look again pods list
kubectl get pods --show-labels
You will find deleted pod No longer exists , however Deployment Created a new pod.
This is it. k8s It will always try to ensure that the running state of the cluster is consistent with the state described in the configuration .
obtain Deployment Description information
kubectl describe deployment node-deployment
You can see Deployment Current description of , And pod Historical changes .
NewReplicaSet:
- When you first create Deployment when , It creates a ReplicaSet (node-deployment-57df45c5bf) And created 3 Copies .
Events:
- First, expand the capacity to 10 individual pods
- It's shrunk 3 individual pods
- It's shrunk 1 individual pods
- It's expanded to 3 individual pods
see Deployment state
kubectl get deployment node-deployment -o yamlWith yaml Format display Deployment Configuration and current status of

summary
In this article, we introduce what is k8s object , And the use of yaml To configure , Created a Deployment.
adopt scale Yes Deployment Zoom , And demonstrates the manual deletion of a pod after ,k8s Keep the running state consistent with the description according to the description .
After the operation , Look back at the concept , There's a kind of Epiphany .
In this paper, from :Kubernetes Stateless application expansion and contraction - Alicloud developer community
边栏推荐
- ClickOnce 不支持请求执行级别“requireAdministrator”
- MCU lightweight system core
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
- 一图看懂!为什么学校教了你Coding但还是不会的原因...
- LeetCode 322. Change exchange (dynamic planning)
- leetcode刷题_平方数之和
- How does Huawei enable debug and how to make an image port
- How to get the PHP version- How to get the PHP Version?
- [flask] obtain request information, redirect and error handling
猜你喜欢
![[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。

黄金价格走势k线图如何看?

National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Folio.ink 免费、快速、易用的图片分享工具

Mongodb problem set

037 PHP login, registration, message, personal Center Design

3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)

Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步

【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化

leetcode刷题_反转字符串中的元音字母
随机推荐
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
现货白银的一般操作方法
How to get all sequences in Oracle database- How can I get all sequences in an Oracle database?
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
剑指 Offer 12. 矩阵中的路径
Basic process and testing idea of interface automation
Leetcode 208. Implement trie (prefix tree)
Loop structure of program (for loop)
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
[le plus complet du réseau] | interprétation complète de MySQL explicite
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
MCU lightweight system core
[flask] official tutorial -part3: blog blueprint, project installability
A glimpse of spir-v
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
yii中console方法调用,yii console定时任务
Force buckle 9 palindromes
Redis-字符串类型