[Cloud Native] Use of kubernetes controller deployment

 

 

Table of contents

​edit

1 Controller Controller

1.1 What is Controller

1.2 Common Controller Controller

1.3 How does the Controller manage Pods

2 Deployment

2.1 create a deployment

2.2 view deployment

2.3 scaling deployment

2.4 rollback deployment

2.5 delete deployment


1 Controller Controller

Official Website: Controller | Kubernetes

1.1 What is Controller

Kubernetes 通常不会直接创建 Pod, 而是通过 Controller 来管理 Pod 的。The deployment characteristics of the Pod are defined in the Controller, such as how many copies there are, what Node it runs on, and so on . In layman's terms, it can be considered that the Controller is an object used to manage Pods. Its core role can be summed up in one sentence:通过监控集群的公共状态,并致力于将当前状态转变为期望的状态。

Popular definition: controller can manage pods to make pods more capable of operation and maintenance

1.2 Common Controller Controller

  • DeploymentIs the most commonly used Controller. Deployments can manage multiple copies of Pods and ensure that Pods are running as expected.

    • ReplicaSet realizes the multi-copy management of Pod. ReplicaSet is automatically created when using Deployment, that is to say, Deployment manages multiple copies of Pod through ReplicaSet, and we usually do not need to use ReplicaSet directly.

  • DaemonsetUsed in scenarios where each Node runs at most one Pod copy. As its name suggests, a DaemonSet is typically used to run daemons.

  • StatefulesetIt can ensure that the name of each copy of the Pod is unchanged throughout the life cycle, while other Controllers do not provide this function. When a pod fails and needs to be deleted and restarted, the name of the pod will change, and StatefuleSet will ensure that the replicas are started, updated or deleted in a fixed order.

  • JobIt is used for applications that are deleted after running, while Pods in other Controllers usually run continuously for a long time.

1.3 How does the Controller manage Pods

注意: Controller 通过 label 关联起来 Pods

image-20230307105007568

2 Deployment

Official address: Deployments | Kubernetes

A Deployment provides declarative update capabilities for Pods and ReplicaSets.

You are responsible for describing the target state in the Deployment , and the Deployment Controller (Controller) changes the actual state at a controlled rate to make it the desired state. You can define a Deployment to create a new ReplicaSet, or delete an existing Deployment and adopt its resources through a new Deployment.

2.1 create a deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels: 
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec: 
      containers:
        - name: nginx
          image: nginx:1.19
          ports:
            - containerPort: 80

2.2 view deployment

# 部署应用
$ kubectl apply -f app.yaml
# 查看 deployment
$ kubectl get deployment
# 查看 pod
$ kubectl get pod -o wide
# 查看 pod 详情
$ kubectl describe pod pod-name
# 查看 deployment 详细
$ kubectl describe deployment 名称
# 查看 log
$ kubectl logs pod-name
# 进入 Pod 容器终端, -c container-name 可以指定进入哪个容器。
$ kubectl exec -it pod-name -- bash
# 输出到文件
$ kubectl get deployment nginx-deployment -o yaml >> test.yaml
  • NAMELists the names of Deployments in the namespace.

  • READYDisplays the number of "copies" of the application available. The displayed pattern is "Ready Count/Expected Count".

  • UP-TO-DATEShows the number of replicas that have been updated to achieve the desired state.

  • AVAILABLEDisplays the number of copies of the app available to the user.

  • AGEShows how long the application has been running.

Note that the desired number of replicas is .spec.replicasset according to the field 3.

2.3 scaling deployment

# 查询副本
$ kubectl get rs|replicaset
# 伸缩扩展副本
$ kubectl scale deployment nginx --replicas=5

2.4 rollback deployment

illustrate:

Deployment go-live is only triggered when the Deployment Pod template (i.e. .spec.template) changes, such as the template's tags or container image being updated. Other updates (such as expanding or shrinking the Deployment) will not trigger the go-live action.

# 查看上线状态
$ kubectl rollout status [deployment nginx-deployment | deployment/nginx]
# 查看历史
$ kubectl rollout history deployment nginx-deployment
# 查看某次历史的详细信息
$ kubectl rollout history deployment/nginx-deployment --revision=2
# 回到上个版本
$ kubectl rollout undo deployment nginx-deployment
# 回到指定版本
$ kubectl rollout undo deployment nginx-deployment --to-revision=2
# 重新部署
$ kubectl rollout restart deployment nginx-deployment
# 暂停运行,暂停后,对 deployment 的修改不会立刻生效,恢复后才应用设置
$ kubectl rollout pause deployment ngixn-deployment
# 恢复
$ kubectl rollout resume deployment nginx-deployment

2.5 delete deployment

# 删除 Deployment
$ kubectl delete deployment nginx-deployment
$ kubect delete -f nginx-deployment.yml
# 删除默认命名空间下全部资源
$ kubectl delete all --all
# 删除指定命名空间的资源
$ kubectl delete all --all -n 命名空间的名称

Guess you like

Origin blog.csdn.net/weixin_53678904/article/details/132183842