k8s中kubectl陈述式资源管理

目录

1、 理论

1.1、 管理k8s核心资源的三种基本方法 :

1.1.1陈述式的资源管理方法:

1.1.1.1、优点:

1.1.1.2、缺点:

1.1.2、声明式资源管理方法

1.1.3、GUI式资源管理方法

1.2、陈述式资源管理方法

2. 对资源的增、删、查操作

2.1、查看版本信息

2.2、查看资源对象简写

2.3、查看集群信息

2.4、配置kubectl自动补全

2.5、node节点查看日志

2.7、显示所有的命名空间,详细信息

2.9、yaml格式的

2.10、创建标签为ky29

2.11查看通过标签创建的信息

2.12、 显示所有的·app标签

​编辑2.13、查看 master 节点状态

2.14、查看命令空间

2.15、查看kube-system命名空间的所有资源

2.16、查看命名空间为default下面,ky29的信息

2.17、创建命名空间app

2.18、删除命名空间app

2.19、在命名空间ky10中创建一个创建一个名为"ky15-nginx"的部署,并使用镜像nginx

2.20、描述某个资源的详细信息

2.21、查看命名空间kuy10中的pod 信息

2.22、kubectl exec可以跨主机登录容器,docker exec 只能在容器所在主机上登录

2.23、删除pod资源

3、项目的生命周期

3.1、创建    kubectl create命令

3.2、发布 kubectl expose命令

3.2.2、流程:.

3.2.3、.小实验

3.3、更新    kubectl set更改现有应用资源一些信息。

3.3.2、获取修改模板

3.3.3、查看当前 nginx 的版本号

3.3.4、将nginx 版本更新为 1.15 版本

3.4、回滚    kubectl rollout 对资源进行回滚管理

3.4.2、 查看历史版本

3.4.3、执行回滚到上一个版本

3.4.4、查看历史版本

3.4.5、检查回滚状态

3.5、删除    kubectl delete

3.5.1、删除副本控制器

3.5.2、删除pod

3.5..3、删除service

3.6、 总结:


 

1、 理论

1.1、 管理k8s核心资源的三种基本方法 :

1.1.1陈述式的资源管理方法:

主要依赖命令行工具kubectl进行管理

1.1.1.1、优点:

可以满足90%以上的使用场景

对资源的增、删、查操作比较容易

1.1.1.2、缺点:

命令冗长,复杂,难以记忆

特定场景下,无法实现管理需求
对资源的修改麻烦,需要patch来使用json串更改

1.1.2、声明式资源管理方法

主要依赖统一资源配置清单进行管理

1.1.3、GUI式资源管理方法

主要依赖图形化操作界面进行管理

1.2、陈述式资源管理方法

  1. kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用 apiserver 的接口
  2. kubectl 是官方的CLI命令行工具,用于与 apiserver 进行通信,将用户在命令行输入的命令,组织并转化为 apiserver 能识别的信息,进而实现管理 k8s 各种资源的一种有效途径
  3. kubectl 的命令大全
  4. 对资源的增、删、查操作比较方便,但对改的操作就不容易了

 k8s中文文档:http://docs.kubernetes.org.cn/683.html

2. 对资源的增、删、查操作

2.1、查看版本信息

kubectl version

2.2、查看资源对象简写

kubectl api-resources

2.3、查看集群信息

kubectl cluster-info

2.4、配置kubectl自动补全

1、直接命令行输出

source <(kubectl completion bash)

2、进入配置文件进行自动配置

vim /root/.bashrc
source <(kubectl completion bash)

2.5、node节点查看日志

journalctl -u kubelet -f

2.6、基本信息查看

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的资源
 

2.7、显示所有的命名空间,详细信息

kubectl get --all namespces -A pod

2.8、查看pod wide格式相关详细信息

kubectl get pod -owide

2.9、yaml格式的

2.10、创建标签为ky29

kubectl create deployment ky29 --image=soscscs/myapp:v1

 

2.11查看通过标签创建的信息

kubectl get pod 

2.12、 显示所有的·app标签

2.13、查看 master 节点状态

kubectl get componentstatuses
kubectl get cs

2.14、查看命令空间

命令空间的作用:用于允许不同 命令空间 的 相同类型 的资源 重名的

kubectl get namespace
kubectl get ns

2.15、查看kube-system命名空间的所有资源

kubectl get all -n default

2.16、查看命名空间为default下面,ky29的信息

2.17、创建命名空间app

kubectl create ns app
kubectl get ns

创建命名空间ky10,并查看所有的命名空间

2.18、删除命名空间app

kubectl delete namespace app
kubectl get ns	

2.19、在命名空间ky10中创建一个创建一个名为"ky15-nginx"的部署,并使用镜像nginx

创建成功

2.20、描述某个资源的详细信息

kubectl describe pod ky15-nginx -n ky10

2.21、查看命名空间kuy10中的pod 信息

kubectl get pods -n ky10

2.22、kubectl exec可以跨主机登录容器,docker exec 只能在容器所在主机上登录

kubectl exec -it ky15-nginx bash -n ky10

2.23、删除pod资源

kubectl get pod -o wide
kubectl delete deployments.apps ky29

2.24、扩缩容:

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

3、项目的生命周期

创建-->发布-->更新-->回滚-->删除

3.1、创建    kubectl create命令

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

启动 nginx 实例,暴露容器端口 80,设置副本数 3

kubectl create deployment ky30 --image=nginx:1.14 --port=80 --replicas=3
kubectl get pods
kubectl get all

3.2、发布 kubectl expose命令

将资源暴露为新的 Service。

kubectl expose --help

 为deployment的ky30创建service,并通过Service的80端口转发至容器的80端口上,类型为NodePort

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

service 的 type 类型:
●ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(service默认类型)

●NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过 NodeIp:NodePort 的方式Kubernetes集群外部的程序可以访问Service。
每个端口只能是一种服务,端口范围只能是 30000-32767。

●LoadBalancer:通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。这种用法仅用于在公有云服务提供商的云平台上设置Service的场景。通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
在service提交后,Kubernetes就会调用CloudProvider在公有云上为你创建一个负载均衡服务,并且把被代理的Pod的IP地址配置给负载均衡服务做后端。

●externalName:将service名称映射到一个DNS域名上,相当于DNS服务的CNAME记录,用于让Pod去访问集群外部的资源,它本身没有绑定任何的资源。
 

创建部署新的

kubectl create deployment nginx-ky30 --image=nginx:1.14 --port=80 --replicas=4

3.2.2、流程:.

先创建    kubectl create命令,创建并运行一个或多个容器镜像

发布    kubectl expose命令,将资源暴露为新的 Service。

查看pod网络状态详细信息和 Service暴露的端口

注意:不创建发布会失败,会报错

3.2.3、.小实验

kubectl exec可以跨主机登录容器

式样访问登录是否是轮询(每个容器都改一下)

 访问

查看关联后端的节点

kubectl describe svc nginx

查看 service 的描述信息

kubectl describe svc ky10

在master01操作 查看访问日志

kubectl logs ky10-56b94fcf84-fzppc

3.3、更新    kubectl set
更改现有应用资源一些信息。

kubectl set --help

Configure application resources

 These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          Update image of a pod template
  resources      Update resource requests/limits on objects with pod templates
  selector       Set the selector on a resource
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding

Usage:
  kubectl set SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands)

3.3.2、获取修改模板

kubectl set image --help
Configure application resources

 These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          Update image of a pod template
  resources      Update resource requests/limits on objects with pod templates
  selector       Set the selector on a resource
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding

Usage:
  kubectl set SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@master01 ~]# kubectl set image --help
Update existing container image(s) of resources.

 Possible resources include (case insensitive):

  pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)

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
  
  # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all
  
  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1
  
  # Print result (in yaml format) of updating nginx container image from local file, without hitting
the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Options:
      --all=false: Select all resources, including uninitialized ones, in the namespace of the
specified resource types
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or
map key is missing in the template. Only applies to golang and jsonpath output formats.
      --dry-run='none': Must be "none", "server", or "client". If client strategy, only print the
object that would be sent, without sending it. If server strategy, submit server-side request
without persisting the resource.
      --field-manager='kubectl-set': Name of the manager used to track field ownership.
  -f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a
server.
  -k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f
or -R.
      --local=false: If true, set image will NOT contact api-server but run locally.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
      --record=false: Record current kubectl command in the resource annotation. If set to false, do
not record the command. If set to true, record the command. If not set, default to updating the
existing annotation value only if one already exists.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you
want to manage related manifests organized within the same directory.
  -l, --selector='': Selector (label query) to filter on, not including uninitialized ones, supports
'=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
      --template='': Template string or path to template file to use when -o=go-template,
-o=go-template-file. The template format is golang templates
[http://golang.org/pkg/text/template/#pkg-overview].

Usage:
  kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ...
CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

3.3.3、查看当前 nginx 的版本号

 curl -I http://192.168.41.30:31924

3.3.4、将nginx 版本更新为 1.15 版本

kubectl set image deployment/ky10 nginx=nginx:1.15

更新世可能会有延迟 

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

kubectl get pods -w

3.4、回滚    kubectl rollout 
对资源进行回滚管理

kubectl rollout --help
Manage the rollout of a resource.
  
 Valid resource types include:

  *  deployments
  *  daemonsets
  *  statefulsets

Examples:
  # Rollback to the previous deployment
  kubectl rollout undo deployment/abc
  
  # Check the rollout status of a daemonset
  kubectl rollout status daemonset/foo

Available Commands:
  history     View rollout history
  pause       Mark the provided resource as paused
  restart     Restart a resource
  resume      Resume a paused resource
  status      Show the status of the rollout
  undo        Undo a previous rollout

Usage:
  kubectl rollout SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

3.4.2、 查看历史版本

kubectl rollout history deployment/nginx 

3.4.3、执行回滚到上一个版本

kubectl rollout undo deployment/ky10

版本回滚以前的IP地址

版本回滚以后的IP地址

3.4.4、查看历史版本

3.4.5、检查回滚状态

kubectl rollout status deployment/ky10

3.5、删除    kubectl delete

3.5.1、删除副本控制器

3.5.2、删除pod

 kubectl delete deployments/nginx-ky30

3.5..3、删除service

kubectl delete svc/nginx

​​​​​​​

3.6、 总结:

项目生命周期

创建

kubectl create   <资源类型> <资源名称> --image=<镜像名> [ --port= --replicas= }

发布

kubectl expose  <资源类型> <资源名称> --port=--target-porttype={ClusteIPINodePort}

 更新

kubectl set image <资源类型> <资源名称> 容器名=镜像名

回滚

kubectl rollout unde  <资源类型><资源名称> 默认是回滚到上一个版本
                                         --torevision= 可滚到你指定的版木

删除

kubectl delete <资源类型><资源名称>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值