分享交流
合作共赢!

Kubernetes/K8S基础使用方法总结【二】——kubectl命令

[root@master1 flannel]# 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 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

Deploy Commands:
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

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

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

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 using strategic merge patch
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
convert Convert config files between different API versions
kustomize Build a kustomization target from a directory or a remote url.

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)

Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information

Usage:
kubectl [flags] [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).

一、标签选择器:

标签选择器可以在查看kubernetes对象资源时,如pods, nodes等进行过滤。查看标签信息:

[root@master1 ~]# kubectl get nodes --show-labels -n kube-system
[root@master1 ~]# kubectl get pods --show-labels -n kube-system

1.一般标签选择器

1.等值关系:=, ==, !=

2.集合关系:

  • KEY in (VALUE1,VALUE2…)
  • KEY notin (VALUE1,VALUE2…)
  • KEY
  • !KEY

使用实例:

  • kubectl get pods -l label1,label2…
  • kubectl get pods -l key1=value1,key2!=value2
  • kubectl get pods -l “key1 in (value1,value2,value3…)”
  • kubectl get pods -l “key1 notin (value1,value2,value3…)”

2.内嵌标签选择器

很多资源支持内嵌字段定义其使用的标签选择器:

1.matchlabels: 直接给定键值

2.matchExpressions: 基于给定的表达式来定义使用标签选择器,使用格式:{key:”KEY”,operator:”OPERATOR”,values:[VAL1,VAL2,VAL3,…]}

operator: 操作符,In,NotIn(values字段值必须为非空列表);Exists, NotExists(values字段值必须为空列表)

3.常用命令:

  • kubectl version: 查看版本信息
  • kubectl cluster-info: 查看集群信息
  • kubectl delete pods pod_name: 删除pod_name的pod资源
  • kubectl get pods –show-labels: 查看所有pod中的标签信息
  • kubectl get pods -L labelname: 显示label标签下的所有标签值
  • kubectl get pods -l label1[,label2,…]: 显示label标签的pod信息
  • kubectl label pods podname key=vakye: pod资源添加标签
  • kubectl label pods podname key=vakye –overwrite: pod资源修改标签
  • kubectl get pods -w: 动态监控pods信息
  • kuebctl  create serviceaccount mysa -o yaml –dry-run: 可以以yaml格式输出需要编写的yaml文件内容,可以重定向到某个文件中
  • kubectl get pods podName -o yaml –export: 将podName名称的pod资源以yaml的格式导出其配置信息
赞(0) 打赏
未经允许不得转载:琼杰笔记 » Kubernetes/K8S基础使用方法总结【二】——kubectl命令
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏