背景说明

使用Windows对Kubernetes资源进行操作时由于kubetctl.exe无法像Linux使用kubectl进行操作且需要配置环境变量导致和Linux操作体验不一致,Windows也有alias类似的命令,就是:doskey

解决方案

文件位置

按照一定的方式安装好kubectl.exe后定位文件位置
image.png
由上可以看到文件位置:C:\Program Files\Docker\Docker\resources\bin\kubectl.exe

脚本编写

编写文件alias_key.bat

  1. @REM 设置别名为:ls
  2. @doskey ls=dir /b $*
  3. @REM kubectl.exe工具设置别名为:kubectl
  4. @doskey kubectl="C:\Program Files\Docker\Docker\resources\bin\kubectl.exe" $*
  • 等号左边是右边的别名
  • $*表示这个命令还可能有其他参数
  • @表示执行这条命令时不显示这条命令本身

编写文件aliaskey.reg

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%USERPROFILE%\\alias_key.bat"

脚本注册

把脚本文件放到用户主目录%USERPROFILE%
image.png
双击运行文件aliaskey.reg

别名执行

Microsoft Windows [版本 10.0.19042.1165]
(c) Microsoft Corporation。保留所有权利。

C:\Users\86183>kubectl
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

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

Basic Commands (Intermediate):
  explain       Get documentation for a resource
  get           Display one or many resources
  edit          Edit a resource on the server
  delete        Delete resources by file names, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout       Manage the rollout of a resource
  scale         Set a new size for a deployment, replica set, or replication controller
  autoscale     Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:

环境变量

%ALLUSERSPROFILE% =C:\ProgramData
%CommonProgramFiles% =C:\Program Files\Common Files
%CommonProgramFiles(x86)% =C:\Program Files (x86)\Common Files
%CommonProgramW6432% =C:\Program Files\Common Files
%COMPUTERNAME% =MyPC
%ComSpec% =C:\WINDOWS\system32\cmd.exe
%HOMEDRIVE% =C:
%HOMEPATH% =\Users\用户名
%LOGONSERVER% =\\MicrosoftAccount
%OS% =Windows_NT
%ProgramW6432% =C:\Program Files  
%PUBLIC% =C:\Users\Public 
%TEMP% =C:\Users\用户名\AppData\Local\Temp
%TMP% =C:\Users\用户名\AppData\Local\Temp
%USERDOMAIN% =MyPC 
%USERNAME% =用户名
%USERPROFILE% =C:\Users\用户名
%SystemRoot% =C:\WINDOWS
%SystemDrive% =C:
%APPDATA% =C:\Users\用户名\AppData\Roaming
%LOCALAPPDATA% =C:\Users\用户名\AppData\Local
%windir% =C:\WINDOWS
%Path% =C:\Windows\system32;C:\Windows; 
%ProgramData% =C:\ProgramData
%ProgramFiles% =C:\Program Files
%ProgramFiles(x86)% =C:\Program Files (x86)