Deployment of Kubernetes study notes (6)

Deployment concept

Kubernetes Deployment is a controller object in Kubernetes that manages the deployment of applications. It manages and automatically updates the application's ReplicaSets and ensures that the application has a certain number of instances available at any one time.

Deployments provide some great features like automatically restarting failed instances, automatically scaling up and down the number of instances, and rolling updates that can upgrade from one version to another. These features make Deployment ideal for deploying and managing large distributed applications in production environments.

By using Kubernetes Deployment, it is easy to define the target state of the application deployment, such as the desired number of instances and application images, and let Kubernetes automatically convert it to the actual state. This makes application deployment more efficient and reliable.

Components of a Kubernetes deployment

A Kubernetes Deployment is a resource object that describes the desired state of an application and how to manage a containerized application. It consists of the following components:
Pod Template: Describes container images, environment variables, storage volumes, etc.
ReplicaSet: manages the number of copies of the Pod.
Update strategy: Control the update strategy of Deployment, including rolling update, fast update, etc.
Rollback strategy: If the update fails, the control rolls back to the previous version.
Autoscaling: Increase or decrease the number of replicas based on demand.
Service: Expose the Pod to the outside world by providing a fixed IP address and DNS name.
Together, these pieces allow users to manage a containerized application and ensure it is always in the desired state.

Create and manage Deployments

1. Create a Deployment using a resource list

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example # deployment名称
spec:
  replicas: 3 # 副本数,也就是pod数量
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx # 容器名称
        image: nginx:1.14.2 # 容器镜像
        ports:
        - containerPort: 80 # 容器端口

Example explanation:
In this example, a workload resource named nginx-deployment is created, the number of pods specified is 3, the pod container image is nginx, the image version is nginx:1.14.2, and the container port is 80

1.1 Use kubectl apply -f <resource list file name> to create resources

kubectl apply -f  /k8s_yaml/deployment-example.yaml

1.2 Create a deployment resource using kubectl

kubectl create deployment <deployment-name> --image=<image-name>:<tag>

2. View deployment resources

kubectl get deployments.apps deployment-example
# 输出以下信息
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment-example   3/3     3            3           657d

When examining a Deployment in a cluster, the fields displayed are:
NAME lists the name of the Deployment in the namespace.
READY shows the number of available "copies" of the application. The displayed pattern is "Ready Count/Expected Count".
UP-TO-DATE shows how many replicas have been updated to reach the desired state.
AVAILABLE shows how many copies of the application are available to the user.
AGE shows how long the application has been running.
2. Use kubectl get deployments.apps <deployment name> to get the created deployment resources

3. Update the deployment

# 方法1
kubectl set image deployment.v1.apps/deployment-example nginx=nginx:1.16.1
# 方法2
kubectl set image deployment/deployment-example nginx=nginx:1.16.1

4. View the deployment status

kubectl rollout status deployment deployment-example

# 命令输出结果
deployment "deployment-example" successfully rolled out

5. Delete the deployment

kubectl delete deployments.apps my-deployment 
# 命令输出结果
deployment.apps "my-deployment" deleted

6. Edit deployment, yaml file

kubectl edit deployment/deployment-example
# 编辑保存后输出结果
deployment.apps/nginx-deployment edited

7. Get deployment details

kubectl describe deployments deployment-example
# 命令输出结果
Name:                   deployment-example
Namespace:              default
CreationTimestamp:      Sun, 25 Apr 2021 10:53:37 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 24
                        kubectl.kubernetes.io/last-applied-configuration:
                          {
    
    "apiVersion":"apps/v1","kind":"Deployment","metadata":{
    
    "annotations":{
    
    },"name":"deployment-example","namespace":"default"},"spec":{
    
    "repli...
Selector:               app=deployment-example
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:       app=deployment-example
  Annotations:  redeploy-timestamp: 1675647913508
  Containers:
   deployment-container:
    Image:      nginx:1.16.1
    Port:       <none>
    Host Port:  <none>
    Limits:
      cpu:     2
      memory:  2Gi
    Requests:
      cpu:        20m
      memory:     100Mi
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   deployment-example-d9df5cfc8 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  34m   deployment-controller  Scaled up replica set deployment-example-d9df5cfc8 to 3

8. Roll back the online version

# 首先查看发布版本
kubectl rollout history deployment/deployment-example
# 回滚到上一个版本
kubectl rollout undo deployment/deployment-example
# 回滚到指定版本
kubectl rollout undo deployment/deployment-example --to-revision=2
# 命令执行输出结果
deployment.apps/deployment-example rolled back

9. Expand and shrink the number of pods

# 通过指定replicas数量来控制pod数量从而达到扩缩容目的
kubectl scale deployment/deployment-example --replicas=10

10. Suspend deployment online activities

# 执行完此命令后deployment更新操作将不被允许
kubectl rollout pause deployment/deployment-example 
# 执行完此命令后取消暂停动作
kubectl rollout resume deployment/deployment-example

Guess you like

Origin blog.csdn.net/Habo_/article/details/128984998