首页 > 编程语言 >k8s源码分析3-kubectl命令行设置7大命令分组

k8s源码分析3-kubectl命令行设置7大命令分组

时间:2022-11-20 19:44:59浏览次数:45  
标签:kubectl Commands resource 命令 源码 resources Basic k8s ioStreams

本节重点总结 :

  • 设置cmd工厂函数f,主要是封装了与kube-apiserver交互客户端
  • 用cmd工厂函数f创建7大分组命令 ,如下
  1. 基础初级命令 Basic Commands (Beginner):
  2. 基础中级命令 Basic Commands (Intermediate):
  3. 部署命令 Deploy Commands:
  4. 集群管理分组 Cluster Management Commands:
  5. 故障排查和调试 Troubleshooting and Debugging Commands:
  6. 高级命令 Advanced Commands:
  7. 设置命令 Settings Commands

架构图

kubectl_cmd_group.png

设置参数-替换方法

	flags := cmds.PersistentFlags()
	flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags

	// Normalize all flags that are coming from other packages or pre-configurations
	// a.k.a. change all "_" to "-". e.g. glog package
	flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)

	addProfilingFlags(flags)

	flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code")


设置kubeconfig相关的命令行

	kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
	kubeConfigFlags.AddFlags(flags)
	matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
	matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())

设置cmd工厂函数f,主要是封装了与kube-apiserver交互客户端

  • 后面的子命令都使用这个f创建
	f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

创建proxy子命令

	proxyCmd := proxy.NewCmdProxy(f, ioStreams)
	proxyCmd.PreRun = func(cmd *cobra.Command, args []string) {
		kubeConfigFlags.WrapConfigFn = nil
	}

创建7大分组命令

1. 基础初级命令 Basic Commands (Beginner):

  • 代码
		{
			Message: "Basic Commands (Beginner):",
			Commands: []*cobra.Command{
				create.NewCmdCreate(f, ioStreams),
				expose.NewCmdExposeService(f, ioStreams),
				run.NewCmdRun(f, ioStreams),
				set.NewCmdSet(f, ioStreams),
			},
		},
  • 对应的输出

Basic Commands (Beginner):
  create        Create a resource from a file or from stdin.
  expose        Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run           Run a particular image on the cluster
  set           Set specific features on objects
  • 释义
    • create 代表创建资源
    • expose 将一种资源暴露成service
    • run 运行一个镜像
    • set 在对象上设置一些功能

2. 基础中级命令 Basic Commands (Intermediate):

	{
			Message: "Basic Commands (Intermediate):",
			Commands: []*cobra.Command{
				explain.NewCmdExplain("kubectl", f, ioStreams),
				get.NewCmdGet("kubectl", f, ioStreams),
				edit.NewCmdEdit(f, ioStreams),
				delete.NewCmdDelete(f, ioStreams),
			},
		},
  • 打印的help效果
Basic Commands (Intermediate):
  explain       Documentation of resources
  get           Display one or many resources
  edit          Edit a resource on the server
  delete        Delete resources by filenames, stdin, resources and names, or by resources and label selector
  • 释义
    • explain 获取资源的文档
    • get 展示资源
    • edit 编辑资源
    • delete 删除资源

3. 部署命令 Deploy Commands:

		{
			Message: "Deploy Commands:",
			Commands: []*cobra.Command{
				rollout.NewCmdRollout(f, ioStreams),
				scale.NewCmdScale(f, ioStreams),
				autoscale.NewCmdAutoscale(f, ioStreams),
			},
		},
  • 输出为
  rollout       Manage the rollout of a resource
  scale         Set a new size for a Deployment, ReplicaSet or Replication Controller
  autoscale     Auto-scale a Deployment, ReplicaSet, or ReplicationController

  • 释义
    • rollout 滚动更新
    • scale 扩缩容
    • autoscale 自动扩缩容

4. 集群管理分组 Cluster Management Commands:

		{
			Message: "Cluster Management Commands:",
			Commands: []*cobra.Command{
				certificates.NewCmdCertificate(f, ioStreams),
				clusterinfo.NewCmdClusterInfo(f, ioStreams),
				top.NewCmdTop(f, ioStreams),
				drain.NewCmdCordon(f, ioStreams),
				drain.NewCmdUncordon(f, ioStreams),
				drain.NewCmdDrain(f, ioStreams),
				taint.NewCmdTaint(f, ioStreams),
			},
		},
  • 输出

Cluster Management Commands:
  certificate   Modify certificate resources.
  cluster-info  Display cluster info
  top           Display Resource (CPU/Memory/Storage) usage.
  cordon        Mark node as unschedulable
  uncordon      Mark node as schedulable
  drain         Drain node in preparation for maintenance
  taint         Update the taints on one or more nodes

  • 释义
    • certificate 管理证书
    • cluster-info 展示集群信息
    • top 展示资源消耗top
    • cordon 将节点标记为不可用
    • uncordon 将节点标记为可用
    • drain 驱逐pod
    • taint 设置节点污点

5.故障排查和调试 Troubleshooting and Debugging Commands:

		{
			Message: "Troubleshooting and Debugging Commands:",
			Commands: []*cobra.Command{
				describe.NewCmdDescribe("kubectl", f, ioStreams),
				logs.NewCmdLogs(f, ioStreams),
				attach.NewCmdAttach(f, ioStreams),
				cmdexec.NewCmdExec(f, ioStreams),
				portforward.NewCmdPortForward(f, ioStreams),
				proxyCmd,
				cp.NewCmdCp(f, ioStreams),
				auth.NewCmdAuth(f, ioStreams),
				debug.NewCmdDebug(f, ioStreams),
			},
		},
  • 输出
Troubleshooting and Debugging Commands:
  describe      Show details of a specific resource or group of resources
  logs          Print the logs for a container in a pod
  attach        Attach to a running container
  exec          Execute a command in a container
  port-forward  Forward one or more local ports to a pod
  proxy         Run a proxy to the Kubernetes API server
  cp            Copy files and directories to and from containers.
  auth          Inspect authorization
  debug         Create debugging sessions for troubleshooting workloads and nodes
  • 释义
    • describe 展示资源详情
    • logs 打印pod中容器日志
    • attach 进入容器
    • exec 在容器中执行命令
    • port-forward 端口转发
    • proxy 运行代理
    • cp 拷贝文件
    • auth 检查鉴权
    • debug 打印debug

6. 高级命令 Advanced Commands:

  • 代码
		{
			Message: "Advanced Commands:",
			Commands: []*cobra.Command{
				diff.NewCmdDiff(f, ioStreams),
				apply.NewCmdApply("kubectl", f, ioStreams),
				patch.NewCmdPatch(f, ioStreams),
				replace.NewCmdReplace(f, ioStreams),
				wait.NewCmdWait(f, ioStreams),
				kustomize.NewCmdKustomize(ioStreams),
			},
		},
  • 输出


Advanced Commands:
  diff          Diff live version against would-be applied version
  apply         Apply a configuration to a resource by filename or stdin
  patch         Update field(s) of a resource
  replace       Replace a resource by filename or stdin
  wait          Experimental: Wait for a specific condition on one or many resources.
  kustomize     Build a kustomization target from a directory or a remote url.

  • 释义
    • diff 对比当前和应该运行的版本
    • apply 应用变更或配置
    • patch 更新资源的字段
    • replace 替换资源
    • wait 等待资源的特定状态
    • kustomize 从目录或远程 url 构建 kustomization 目标。

7. 设置命令 Settings Commands

  • 代码
		{
			Message: "Settings Commands:",
			Commands: []*cobra.Command{
				label.NewCmdLabel(f, ioStreams),
				annotate.NewCmdAnnotate("kubectl", f, ioStreams),
				completion.NewCmdCompletion(ioStreams.Out, ""),
			},
		},
  • 输出
Settings Commands:
  label         Update the labels on a resource
  annotate      Update the annotations on a resource
  completion    Output shell completion code for the specified shell (bash or zsh)

  • 释义
    • label 打标签
    • annotate 更新注释
    • completion 在shell上设置补全

本节重点总结 :

  • 设置cmd工厂函数f,主要是封装了与kube-apiserver交互客户端
  • 用cmd工厂函数f创建7大分组命令 ,如下
  1. 基础初级命令 Basic Commands (Beginner):
  2. 基础中级命令 Basic Commands (Intermediate):
  3. 部署命令 Deploy Commands:
  4. 集群管理分组 Cluster Management Commands:
  5. 故障排查和调试 Troubleshooting and Debugging Commands:
  6. 高级命令 Advanced Commands:
  7. 设置命令 Settings Commands

标签:kubectl,Commands,resource,命令,源码,resources,Basic,k8s,ioStreams
From: https://www.cnblogs.com/Dev0ps/p/16909311.html

相关文章

  • k8s集群可观测性
    k8s集群健康来源当把应用迁移到Kubernetes之后,要如何去保障应用的健康与稳定呢?其实很简单,可以从两个方面来进行增强:首先是提高应用的可观测性;第二是提高应用的可恢复......
  • nydusd 源码理解(一)
    “尝试通过nydus[1]源码理解工作流程。可能由于代码变动导致和本文记录的内容有出入。1.环境准备gitclonehttps://github.com/dragonflyoss/image-service.gitc......
  • JDK源码分析实战系列-PriorityQueue
    完全二叉树一棵深度为k的有n个结点的二叉树,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与满二叉树中编号为i的结点在二叉树中的位置相同,则......
  • k8s中pod、node、容器了解
    转自:https://juejin.cn/post/7037030068704051214,https://www.jianshu.com/p/94ee7e741d521.容器容器是docker技术中的概念:docker包括三个概念:镜像(image),容器(container)......
  • libusb系列-007-Qt下使用libusb1.0.26源码
    libusb系列-007-Qt下使用libusb1.0.26源码文章目录​​libusb系列-007-Qt下使用libusb1.0.26源码​​​​摘要​​​​安装编译环境​​​​确认需要的文件​​​​开始编译......
  • k8s超卖了解
    转自:https://testerhome.com/topics/31561?order_by=like&,https://fafucoder.github.io/2020/11/05/kubernetes-qos/https://github.com/bmwx4/k8s-in-practice/blob/mast......
  • 使用Gradle编译Spring源码
    最近想研究下spring的源码,决定先把源码跑起来,在此记录一下本次遇到的问题。本次使用的工具有:git、IDEA2021社区版、jdk17、gradle7.5.1从BuildfromSource得知,需要git和......
  • Seata 1.5.2 源码学习(Client端)
    在上一篇中通过阅读Seata服务端的代码,我们了解到TC是如何处理来自客户端的请求的,今天这一篇一起来了解一下客户端是如何处理TC发过来的请求的。要想搞清楚这一点,还得从Globa......
  • k8s:服务账号service-account相关参数设置:【重点一篇文章】
    为何写这篇文章?!主要在k8s中,serviceaccount是很要要的一个安全特性,官方资料、以及网上资料对这款的相关参数配置又语焉不详。这里也是自己的理解和解释。如有不对,请指......
  • K8s系列---【K8s常用命令】
    K8s常用命令1.查看所有node节点状态kubectlgetnodeskubectlgetnode-A 2.查看所有pod状态#查看所有命名空间下的podkubectlgetpod-A#默认查看default默认命名空间下......