Detailed explanation of k8s resource management method (declarative, declarative)

Table of contents

1: Declarative resource management method

2: View basic information

1. View information

2. Create

 3. Delete

4. Type of service

Three: Project examples

1. Create kubectl create command

2. Issue the kubectl expose command

 3. Operate on the node node and check the load balancing port

 4. Update kubectl set

5. Roll back kubectl rollout 

6. Delete kubectl delete

Four: Declarative management method

Summarize


1: Declarative resource management method

1. The only entrance for kubernetes cluster management cluster resources is to call the apiserver interface through the corresponding method
2. kubectl is the official CLI command line tool, used to communicate with apiserver, organize and convert the commands entered by the user on the command line into The information that apiserver can identify is an effective way to manage various k8s resources.
3. kubectl command list
kubectl --help
k8s Chinese documentation: http://docs.kubernetes.org.cn/683.html
4. Right It is more convenient to add, delete and check resources, but it is not easy to modify them.

//查看版本信息    kubectl version

//查看资源对象简写    kubectl api-resources

//查看集群信息    kubectl cluster-info

//配置kubectl自动补全    source <(kubectl completion bash)

 

//node节点查看日志     journalctl -u kubelet -f

 

2: View basic information

kubectl get <resource> [-o wide|json|yaml] [-n namespace]
获取资源的相关信息,-n 指定命令空间,-o 指定输出格式
resource可以是具体资源名称,如pod nginx-xxx;也可以是资源类型,如pod;或者all(仅展示几种核心资源,并不完整)
--all-namespaces 或 -A :表示显示所有命名空间,
--show-labels :显示所有标签
-l app :仅显示标签为app的资源
-l app=nginx :仅显示包含app标签,且值为nginx的资源

1. View information

//查看 master 节点状态
kubectl get componentstatuses
kubectl get cs

//查看命名空间
kubectl get namespace
kubectl get ns
//命令空间的作用:用于允许不同 命名空间 的 相同类型 的资源 重名的

//查看default命名空间的所有资源
kubectl get all [-n default]

//查看命名空间kube-public 中的pod 信息
kubectl get pods -n kube-public
NAME                       READY   STATUS    RESTARTS   AGE
nginx-a1-d47f99cb6-hv6gz   1/1     Running   0          24m

//kubectl exec可以跨主机登录容器,docker exec 只能在容器所在主机上登录
kubectl exec -it nginx-a1-d47f99cb6-hv6gz bash -n kube-public

2. Create

//创建命名空间app
kubectl create ns app
kubectl get ns

//在命名空间kube-public 创建副本控制器(deployment)来启动Pod(nginx-wl)
kubectl create deployment nginx-wl --image=nginx  -n kube-public


//扩缩容
kubectl scale deployment nginx-wl --replicas=2 -n kube-public	# 扩容
kubectl scale deployment nginx-wl --replicas=1 -n kube-public	# 缩容

 3. Delete

//删除命名空间app
kubectl delete namespace app
kubectl get ns	

//删除(重启)pod资源,由于存在deployment/rc之类的副本控制器,删除pod也会重新拉起来
kubectl delete pod nginx-wl-d47f99cb6-hv6gz -n kube-public

//若pod无法删除,总是处于terminate状态,则要强行删除pod
kubectl delete pod <pod-name> -n <namespace> --force --grace-period=0
#grace-period表示过渡存活期,默认30s,在删除pod之前允许pod慢慢终止其上的容器进程,从而优雅退出,0表示立即终止pod
		
//删除副本控制器
kubectl delete deployment nginx-a1 -n kube-public
kubectl delete deployment/nginx-a1 -n kube-public

4. Type of service

●ClusterIP : Provides a virtual IP within the cluster for Pod access (default type of service)

●NodePort : Open a port on each Node for external access. Kubernetes will open a port on each Node and the port of each Node is the same. Programs outside the Kubernetes cluster can use NodeIp:NodePort. Access Service.
Each port can only be one service, and the port range can only be 30000-32767.

●LoadBalancer : Map the LoadBalancer to the LoadBalancer address provided by the cloud service provider by setting it. This usage is only used in scenarios where the Service is set up on the cloud platform of a public cloud service provider. Accessed through an external load balancer, usually deploying LoadBalancer on a cloud platform requires additional costs.
After the service is submitted, Kubernetes will call CloudProvider to create a load balancing service for you on the public cloud, and configure the IP address of the proxied Pod to the load balancing service as the backend.

●externalName : Maps the service name to a DNS domain name, which is equivalent to the CNAME record of the DNS service. It is used to allow the Pod to access resources outside the cluster. It does not bind any resources itself.

targetPort is the container mapped port inside the cluster. The client forwards the targetPort through clusterIP:port and then forwards it to the inside of the container; when accessed by an external client, it forwards to the targetPort through nodeIP:nodePort and then forwards it to the inside of the container.

containerPort is the port inside the container and is also kubectl create deployment --port (clusterip port) --target-port (container port) --name <custom resource name> --type (svc resource type)

Three: Project examples

Project life cycle: Create-->Publish-->Update-->Rollback-->Delete

1. Create kubectl create command

●创建并运行一个或多个容器镜像。
●创建一个deployment 或job 来管理容器。
kubectl create --help

//启动 nginx 实例,暴露容器端口 80,设置副本数 3
kubectl create deployment nginx --image=nginx:1.14 --port=80 --replicas=3
 
kubectl get pods
kubectl get all

2. Issue the kubectl expose command

●将资源暴露为新的 Service。
kubectl expose --help

//为deployment的nginx创建service,并通过Service的80端口转发至容器的80端口上,Service的名称为nginx-service,类型为NodePort
kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort

------------------------------------------------------------------------------------------
Kubernetes 之所以需要 Service,一方面是因为 Pod 的 IP 不是固定的(Pod可能会重建),另一方面则是因为一组 Pod 实例之间总会有负载均衡的需求。
Service 通过 Label Selector 实现的对一组的 Pod 的访问。
对于容器应用而言,Kubernetes 提供了基于 VIP(虚拟IP) 的网桥的方式访问 Service,再由 Service 重定向到相应的 Pod。

//查看pod网络状态详细信息和 Service暴露的端口
kubectl get pods,svc -o wide

/查看关联后端的节点
kubectl get endpoints

//查看 service 的描述信息
kubectl describe svc nginx

 3. Operate on the node node and check the load balancing port


//在 node01 节点上操作,查看负载均衡端口
yum install ipvsadm -y
ipvsadm -Ln
//外部访问的IP和端口
TCP  192.168.80.11:44847 rr
  -> 172.17.26.3:80               Masq    1      0          0         
  -> 172.17.36.2:80               Masq    1      0          0         
  -> 172.17.36.3:80               Masq    1      0          0     
//pod集群组内部访问的IP和端口
TCP  10.0.0.189:80 rr
  -> 172.17.26.3:80               Masq    1      0          0         
  -> 172.17.36.2:80               Masq    1      0          0         
  -> 172.17.36.3:80               Masq    1      0          0         
  
//在 node02 节点上操作,同样方式查看负载均衡端口
yum install ipvsadm -y
ipvsadm -Ln
TCP  192.168.80.12:44847 rr
  -> 172.17.26.3:80               Masq    1      0          0         
  -> 172.17.36.2:80               Masq    1      0          0         
  -> 172.17.36.3:80               Masq    1      0          0         
 
TCP  10.0.0.189:80 rr
  -> 172.17.26.3:80               Masq    1      0          0         
  -> 172.17.36.2:80               Masq    1      0          0         
  -> 172.17.36.3:80               Masq    1      0          0         

curl 10.0.0.189
curl 192.168.80.11:44847
//在master01操作 查看访问日志
kubectl logs nginx-cdb6b5b95-fjm2x
kubectl logs nginx-cdb6b5b95-g28wz
kubectl logs nginx-cdb6b5b95-x4m24

 

 4. Update kubectl set

●更改现有应用资源一些信息。
kubectl set --help

//获取修改模板
kubectl set image --help
Examples:
  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

//查看当前 nginx 的版本号
curl -I http://192.168.80.11:44847
curl -I http://192.168.80.12:44847

//将nginx 版本更新为 1.15 版本
kubectl set image deployment/nginx nginx=nginx:1.15

//处于动态监听 pod 状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后依次类推
kubectl get pods -w

---------------------------------------------------------------------------------------------
#滚动更新详解:
kubectl get all
DESIRED:表示期望的状态是 10 个 READY 的副本
CURRENT:表示当前副本的总数: 即8 个日副本 + 5 个新副本
UP_TO-DATE:表示当前已经完成更新的副本数: 即 5个新副本
AVAILABLE:表示当前处于 READY 状态的副本数: 即8个日副本。

kubectl describe deployment/nginx
滚动更新通过参数 maxSurge 和 maxUnavailable 来控制副本替换的数量
maxSurge:此参数控制滚动更新过程中副本总数的超过 DESIRED 的上限。maxSurge 可以是具体的整数(比如 3),也可以是百分百,向上取整。maxSurge 默认值为 25%。
例如,DESIRED 为 10,那么副本总数的最大值为 10 + 10 * 25% = 13,即 CURRENT 为 13。

maxUnavailable:此参数控制滚动更新过程中,不可用的副本相占 DESIRED 的最大比例。maxUnavailable 可以是具体的整数(比如 3),也可以是百分百,向下取整。 maxUnavailable 默认值为 25%。
例如,DESIRED 为 10,那么可用的副本数至少要为 10 - 10 * 25% = 8,即 AVAILABLE 为 8。

因此 maxSurge 值越大,初始创建的新副本数量就越多;maxUnavailable 值越大,初始销毁的旧副本数量就越多。

理想情况下,DESIRED 为 10 的滚动更新的过程应该是这样的:
首先创建 3 个新副本使副本总数达到 13 个。
然后销毁 2 个旧副本使可用的副本数降到 8 个。
当这 2 个旧副本成功销毁后,可再创建 2 个新副本,使副本总数保持为 13 个。
当新副本通过 Readiness 探测后,会使可用副本数增加,超过 8。
进而可以继续销毁更多的旧副本,使可用副本数回到 8。
旧副本的销毁使副本总数低于 13,这样就允许创建更多的新副本。
这个过程会持续进行,最终所有的旧副本都会被新副本替换,滚动更新完成。
---------------------------------------------------------------------------------------------

//再看更新好后的 Pod 的 ip 会改变
kubectl get pods -o wide

//再看 nginx 的版本号
curl -I http://192.168.80.11:44847
curl -I http://192.168.80.12:44847

5. Roll back kubectl rollout 

●对资源进行回滚管理
kubectl rollout --help

//查看历史版本
kubectl rollout history deployment/nginx 

//执行回滚到上一个版本
kubectl rollout undo deployment/nginx

//执行回滚到指定版本
kubectl rollout undo deployment/nginx --to-revision=1

//检查回滚状态
kubectl rollout status deployment/nginx

6. Delete kubectl delete

//删除副本控制器
kubectl delete deployment/nginx

//删除service
kubectl delete svc/nginx-service

kubectl get all

Four: Declarative management method

1. Suitable for modifying resources
2. The declarative resource management method relies on the resource configuration manifest file to manage resources. The resource configuration
manifest file has two formats: yaml (human-friendly, easy to read), json (easy to parse through the API interface) )
3. Resource management is defined in advance in a unified resource configuration list, and then applied to the k8s cluster through declarative commands. 4.
Grammar format: kubectl create/apply/delete -f xxxx.yaml
 

//查看资源配置清单
kubectl get deployment nginx -o yaml

//解释资源配置清单
kubectl explain deployment.metadata

kubectl get service nginx -o yaml
kubectl explain service.metadata

//修改资源配置清单并应用
方法一:离线修改:
修改yaml文件,并用 kubectl apply -f xxxx.yaml 文件使之生效
注意:当apply不生效时,先使用delete清除资源,再apply创建资源

kubectl get service nginx -o yaml > nginx-svc.yaml
vim nginx-svc.yaml				#修改port: 8080
kubectl delete -f nginx-svc.yaml
kubectl apply -f nginx-svc.yaml
kubectl get svc

方法二:在线修改:
直接使用 kubectl edit service nginx 在线编辑资源配置清单并保存退出即时生效(如port: 888)
PS:此修改方式不会对yaml文件内容修改


//删除资源配置清单
陈述式删除:
kubectl delete service nginx

声明式删除:
kubectl delete -f nginx-svc.yaml


 

 

 

Summarize

Declarative resource management
kubectl create <resource type> <resource name> -n namespace [options]
                                                  --image=mirror --replicas=number of replicas --port=container port

kubectl get <resource type|all> [resource name] -n namespace -o wide|yaml|json -w

kubectl describe <resource type> <resource name> -n namespace

kubectl delete <resource type> <resource name>|--all -n namespace [--force --grace-period=0]
                                                           Terminate Pod running immediately and forcefully delete resources

kubectl exec -it -n namespace <Pod resource name> [-c container name] sh|bash

kubectl logs -n namespace <Pod resource name> [-c container name] [-p]

kubectl scale -n namespace deployment <resource name> --replicas=number of replicas   

kubectl expose -n namespace deployment <resource name> --name <custom svc resource name> --port <clusterIP port> --target-port <container port> --type <svc type> kubectl create
svc <svc resource type> <resource name> --tcp=<clusterIP port>:<container port>

kubectl set image deployment <resource name> <container name>=<image name>

kubectl rollout history deployment <resource name>
kubectl rollout undo deployment <resource name> [--to-revision= ]
kubectl rollout status deployment <resource name>


There are 4 types of service:
ClusterIP: The default service resource type, which provides clusterIP for internal access to the K8S cluster.
NodePort: A port will be opened on each Node node. Users inside and outside the K8S cluster can access the service through NodeIP: NodePort. And its associated Pod
LoadBalancer: Use the public cloud's LB service and service for mapping. Users can use the IP address of the public cloud LB service to forward the request to the Node node, and then access the service and its associated Pod ExternalName through NodeIP:NodePort
. : Equivalent to making an alias for a domain name or IP. Pod can access related external services through this service.


The port of the service:
port: The port used by the clusterIP of the service resource
nodePort: The port opened on each Node node (default range is 30000~32767) defined in the NodePort type service. targetPort: The service will be
sent to the port or The nodePort request is forwarded to the container port of the backend Pod.

containerPort: the container port specified when creating the Pod

Inside the K8S cluster http://clusterIP:port --> podIP:containerPort
Outside the K8S cluster http://nodeIP:nodePort --> podIP:containerPort


Application release strategy:
blue-green release,
rolling release
, grayscale release/canary release
kubectl set image deployment <resource name> <container name>=<image name> && kubectl rollout pause deployment <resource name>
kubectl rollout resume deployment <resource Name>

25% max unavailable During the rolling update process, the number of destroyed Pods does not exceed 25% of the expected number of copies, rounded down 25%
max surge During the rolling update process, the number of new Pods does not exceed 25% of the expected number of copies, rounded up Rounding

The expected number of Pod copies is 10, the number of destroyed ones is 2, and the number of new Pods is 3. The number of Pods will remain consistent at 8 ~ 13 during the entire update process.

Declarative resource management
kubectl apply|create -f XXX.yaml
kubectl delete -f XXX.yaml

kubectl edit <resource type> <resource name>

kubectl explain <resource type>.<first-level field>.<second-level field>...
 

Guess you like

Origin blog.csdn.net/A1100886/article/details/132152232