kubectl resource management command-declarative

Table of contents

1. Declarative object management

1. Basic concepts

2. Basic command usage

3. View basic information (kubectl get)

4. Operations such as additions and deletions

5. Log in to the container in the pod

6. Expand and shrink the pod of the pod controller

7. Delete the replica controller

2. Create a project instance

1. Create kubectl create command

2. Release the kubectl expose command

3. Update kubectl set

4. Rollback kubectl rollout 

5. Delete kubectl delete

3. Canary release

1. Update the deployment version and configure the suspension deployment

2. Expose the updated Pod’s port separately

3. Make sure there are no problems with the updated pod and continue updating.

Edit

4. Check the latest updates

1. Declarative object management

1. Basic concepts

  • The only entry point for kubernetes cluster management resources is to call the apiserver interface through the corresponding method.
  • kubectl is the official CTL command line tool, used to communicate with the apiserver, organize and convert the commands entered by the user on the command line into information that the apiserver can recognize, thereby realizing an effective way to manage various k8s resources.

kubectl command

  • kubectl is a command line tool for kubernetes clusters. It can manage the cluster itself and install and deploy containerized applications on the cluster.

The kubectl command syntax format is as follows

  • All content in kubernetes is abstracted into resources, which can be viewed throughkubectl api-resources
kubectl [command] [type] [name] [flags]

command: //指定要对资源执行的操作,例如:create、get、delete

type: //指定资源类型,比如deployment、pod、service

name: //指定资源的名称,名称大小写敏感

flags: //指定额外的可选参数

2. Basic command usage

kubectl logs nginx-cdb6b5b95-fjm2x
#查看访问日志

kubectl describe deployment nginx-wl -n kube-public
#查看nginx-wl的pod控制器的详细信息,

kubectl describe pods 【pod名称】 -n kube-public
#查看某个pod的详细信息

kubectl version
#查看版本信息(服务端和客户端版本都可查看到)

kubectl api-resources
#查看资源对象简写以及版本

kubectl cluster-info
#查看集群信息

source <(kubectl completion bash)
//可以将此条命令放入到环境变量中,每次开机都会执行自动补全命令
#配置kubectl 自动补全
###命令放在/etc/profile 或者/root/.bashrc

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

kubectl get pods -w
#持续监听pod的状态信息

3. View basic information (kubectl get)

kubectl get <resource> [-o wide|json|yaml] [-n namespace]
#获取资源的相关信息,-o指定输出格式,-n 指定命令空间

//<resource> :可以是具体资源名称,如pods
//nginx-xxx: 也可以是资源类型,如pod,或all(仅展示几种核心资源,并不完整)

Case presentation

kubectl get pods
#查看当前默认命令空间(defaults)的pods信息

kubectl get pods -n kube-system
#查看指定的kube-system命名空间的资源

kubectl get pods -owide
#查看默认命名空间的pod详细信息

kubectl get pods -A 或者 kubectl get pods --all-namespace
#查看所有命名空间的pod资源信息

kubecrl get pods -A -owide
#查看所有命名空间下的pod资源信息,并显示详细信息

kubectl get pods --show-labels 
#查看当前默认的命令空间的下的pods的标签类型信息

kubectl get pods --show-labels -A
#查看所有命令空间下的标签信息

kubectl get pods -A -l app
#查看所有命名空间下,仅显示标签为app的资源

kubectl get pods -l app=nginx-ydq
#查看默认命名空间下的标签为nginx-ydq的资源

kubectl get cs
#查看服务组件状态信息

kubectl get nodes 
#查看node节点集群的信息

kubectl get ns
#查看命令空间资源(用于允许不同 命名空间 的 相同类型 的资源 重名的)

kubectl get deployment
#查看deployment的pod控制器

kubectl get all
#查看默认命名空间下的所有资源

kebectl get svc 
#查看service信息

Summary of commonly used command parameters

--all-namespace 或 -A   //表示显示所有的命令空间
--show-labels //显示所有标签
-l app //仅显示标签为app的资源
-n  //指定命令空间
-owide //显示详细信息

4. Operations such as additions and deletions

Create pod resources

  • There are two types of pod resources to create, one is autonomous pod and the other is  controller pod
  • The number of copies cannot be specified for autonomous pod creation. When a pod dies, it will not be rebuilt and can be deleted directly.
  • Controller pod The number of copies can be set when creating, and when the pod dies, the pod controller will be re-created. To delete it, you can only delete the pod controller.
kubectl create ns app
#创建命名空间app

kubectl delete ns app
#删除命名空间app

kubectl create deployment nginx-wl --image=nginx --port=80 --replicas=2 -n kube-public 
#在命名空间kube-public创建副本控制器(deployment)来启动pod,并创建2个副本(pod名称它会随机)
//删除它只能删除的pod控制器,删除控制器后,pod控制器下面的所有pod都会删除
###注意!!!!pod控制器,不能单独创建,基本是和创建pod一起制定创建pod控制器。

kubectl run nginx-wl2 --image=nginx -n kube-public
#直接创建自主式pod(挂掉后不会重建,没有pod控制器)
//可以直接删除这个pod,它没有pod控制器

kubectl describe deployment nginx-wl -n kube-public
#查看nginx-ydq的pod控制器的详细信息,

kubectl describe pods 【pod名称】 -n kube-public
#查看某个pod的详细信息

kubectl delete deployment nginx-wl -n kube-public
#删除pod控制器,(里面所有的pod都会被删除)

kubectl delete pod nginx-wl2 -n kube-public
#删除自主式pod。

kubectl delete pod 【pod名称】 -n 【命名空间】 --force --grace-period=0
#如pod无法删除,总是处于terminate状态,则要强行删除
//grace-period 表示过度存活期,默认30s,在删除pod之前允许POD慢慢终止其上的容器进程,从而优雅退出,0表示立即终止

5. Log in to the container in the pod

kubectl exec -it 【pod名称】 bash
#可以跨主机登录到指定的pod中的容器中,docker exec只能在容器所在主机上登录

6. Expand and shrink the pod of the pod controller

The number of arbitrary pods can be created or reduced based on the specified number.

kubectl scale deployment nginx-wl --replicas=4 -n kube-public
#扩容

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

7. Delete the replica controller

kubectl delete deployment nginx-wl -n kube-public
kubectl delete deployment/nginx-wl -n kube-public

2. Create a project instance

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

1. Create kubectl create command

  • Create and run one or more container images.
  • Create a deployment or job to manage containers.
//帮助
kubectl create --help  

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

2. Release the kubectl expose command

Why does Kubernetes use Service:

  • On the one hand, it is because the IP of the Pod is not fixed (the Pod may be rebuilt)
  • On the other hand, there is always a need for load balancing between a group of Pod instances.

For container applications, Kubernetes provides a VIP (virtual IP)-based bridge method to access the Service, and then the Service redirects to the corresponding Pod.

type 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 access the Service through NodeIp:NodePort. Each port can only be one service, and the port range can only be 30000-32767.
  • LoadBalancer: Map to the LoadBalancer address provided by the cloud service provider by setting the LoadBalancer. 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 Pods to access resources outside the cluster. It is not bound by itself. any resources.

Expose the resource as a new 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


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

3. Update kubectl set

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

#查看当前 nginx 的版本号
curl -I 192.168.247.10:30801

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

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

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

#再看 nginx 的版本号
curl -I 192.168.247.10:30801

4. Rollback 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

5. Delete kubectl delete

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

#删除service
kubectl delete svc/nginx-service

kubectl get all

3. Canary release

The Deployment controller supports custom control of the scrolling rhythm during the update process, such as "pause" or "resume" update operations. For example, the update process is paused immediately after the first batch of new Pod resources are created. At this time, only a part of the new version of the application exists, and the main part is still the old version. Then, filter a small portion of user requests and route them to the new version of the Pod application, and continue to observe whether it can run stably and in the desired way. After confirming that there is no problem, continue to complete the remaining rolling update of Pod resources, otherwise roll back the update operation immediately. This is called a canary release.

1. Update the deployment version and configure the suspension deployment

kubectl set image deployment/nginx nginx=nginx:1.14 && kubectl rollout pause deployment/nginx

kubectl rollout status deployment/nginx  #观察更新状态

2. Expose the updated Pod’s port separately

##创建一个新的service
kubectl expose deployment nginx-zz --name=hew-nginx.servicet=80 --target-port=80 --type=NodePort

##查看相关信息
kubectl get endpoints

##获取到新Pod的子标签
kubectl get pod --show-labels -owide、

##获取新service的yaml文件
kubectl edit svc new-nginx 

verify:

3. Make sure there are no problems with the updated pod and continue updating.

kubectl rollout resume deployment/nginx

4. Check the latest updates

kubectl get pods -w 

Guess you like

Origin blog.csdn.net/weixin_46254171/article/details/134041842