2.4 kubectl命令行设置7大命令分组

devtools/2025/1/24 1:16:19/

本节重点总结 :

  • 设置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 packageflags.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 Servicerun           Run a particular image on the clusterset           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 resourcesget           Display one or many resourcesedit          Edit a resource on the serverdelete        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 resourcescale         Set a new size for a Deployment, ReplicaSet or Replication Controllerautoscale     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 infotop           Display Resource (CPU/Memory/Storage) usage.cordon        Mark node as unschedulableuncordon      Mark node as schedulabledrain         Drain node in preparation for maintenancetaint         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 resourceslogs          Print the logs for a container in a podattach        Attach to a running containerexec          Execute a command in a containerport-forward  Forward one or more local ports to a podproxy         Run a proxy to the Kubernetes API servercp            Copy files and directories to and from containers.auth          Inspect authorizationdebug         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 versionapply         Apply a configuration to a resource by filename or stdinpatch         Update field(s) of a resourcereplace       Replace a resource by filename or stdinwait          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 resourceannotate      Update the annotations on a resourcecompletion    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

http://www.ppmy.cn/devtools/153006.html

相关文章

在K8S中,如果后端NFS存储的IP发送变化如何解决?

在Kubernetes中,如果后端NFS存储的IP地址发生了变化,您需要更新与之相关的Peristent Volume(PV)或Persistent Volume Claim(PVC)以及StorageClass中关于NFS服务器IP的配置信息,确保K8S集群内的Pod能够正确连接到新的NFS存储位置。解决方案如下…

大语言模型的语境中“越狱”和思维链

大语言模型的语境中“越狱”和思维链 越狱(Jailbreaking) 含义:在大语言模型的语境中,“越狱”是指用户试图绕过语言模型的安全限制和使用规则,让模型生成违反伦理道德、包含有害内容(如暴力、歧视、恶意软件代码等)的输出。这些安全限制是由模型开发者设置的,目的是确…

栈和队列(C语言)

目录 数据结构之栈 定义 实现方式 基本功能实现 1)定义,初始化栈 2)入栈 3)出栈 4)获得栈顶元素 5)获得栈中有效元素个数 6)检测栈是否为空 7)销毁栈 数据结构之队列 定义 实现方…

MongoDB深度解析与实践案例

MongoDB深度解析与实践案例 在大数据与云计算时代,NoSQL数据库以其灵活的数据模型、水平扩展能力和高性能,成为了众多开发者与企业数据存储的首选。MongoDB,作为NoSQL数据库的领军者,凭借其面向文档的存储方式、强大的查询语言以…

windows下本地部署安装hadoop+scala+spark-【不需要虚拟机】

注意版本依赖【本实验版本如下】 Hadoop 3.1.1 spark 2.3.2 scala 2.11 1.依赖环境 1.1 java 安装java并配置环境变量【如果未安装搜索其他教程】 环境验证如下: C:\Users\wangning>java -version java version "1.8.0_261" Java(TM) SE Runti…

机器学习 vs 深度学习

目录 一、机器学习 1、实现原理 2、实施方法 二、深度学习 1、与机器学习的联系与区别 2、神经网络的历史发展 3、神经网络的基本概念 一、机器学习 1、实现原理 训练(归纳)和预测(演绎) 归纳: 从具体案例中抽象一般规律…

第11章:Python TDD实现货币类加法运算初步

写在前面 这本书是我们老板推荐过的,我在《价值心法》的推荐书单里也看到了它。用了一段时间 Cursor 软件后,我突然思考,对于测试开发工程师来说,什么才更有价值呢?如何让 AI 工具更好地辅助自己写代码,或许…

深入理解 Windows Server 的核心功能:现代 IT 架构的基石

深入理解 Windows Server 的核心功能:现代 IT 架构的基石 在现代 IT 基础架构中,Windows Server 一直扮演着不可或缺的角色。它不仅是一个强大的服务器操作系统,更是企业级解决方案的核心支柱。从中小型企业到跨国公司,Windows Server 提供了从身份管理到高可用性的一系列…