教程 > k8s 教程 > 阅读:45

kubernetes(k8s) kubectl 命令——迹忆客-ag捕鱼王app官网

kubectl 控制 kubernetes 集群。 它是 kubernetes 的关键组件之一,在设置完成后可以在任何机器上的工作站上运行。 它具有管理集群中节点的能力。

kubectl 命令用于交互和管理 kubernetes 对象和集群。 在本章中,我们将通过 kubectl 讨论 kubernetes 中使用的一些命令。

kubectl annotate - 它更新资源上的注释。

$kubectl annotate [--overwrite] (-f filename | type name) key_1=val_1 ...
key_n = val_n [--resource-version = version]

例如

kubectl annotate pods tomcat description = 'my frontend'

kubectl api-versions - 它在集群上打印支持的 api 版本。

$ kubectl api-version;

kubectl apply - 它能够通过文件或标准输入配置资源。

$ kubectl apply –f 

kubectl attach − 这会将东西附加到正在运行的容器上。

$ kubectl attach  –c 
$ kubectl attach 123456-7890 -c tomcat-conatiner

kubectl autoscale − 这用于自动扩展定义的 pod,例如部署、副本集、复制控制器。

$ kubectl autoscale (-f filename | type name | type/name) [--min = minpods] --
max = maxpods [--cpu-percent = cpu] [flags]
$ kubectl autoscale deployment foo --min = 2 --max = 10

kubectl cluster-info − 它显示集群信息。

$ kubectl cluster-info

kubectl cluster-info dump − 它转储有关集群的相关信息以进行调试和诊断。

$ kubectl cluster-info dump
$ kubectl cluster-info dump --output-directory = /path/to/cluster-state

kubectl config − 修改 kubeconfig 文件。

$ kubectl config 
$ kubectl config –-kubeconfig 

kubectl config current-context − 它显示当前上下文。

$ kubectl config current-context
#deploys the current context

kubectl config delete-cluster − 从 kubeconfig 中删除指定的集群。

$ kubectl config delete-cluster 

kubectl config delete-context − 从 kubeconfig 中删除指定的上下文。

$ kubectl config delete-context 

kubectl config get-clusters − 显示 kubeconfig 中定义的集群。

$ kubectl config get-cluster
$ kubectl config get-cluster 

kubectl config get-contexts − 描述一个或多个上下文。

$ kubectl config get-context 

kubectl config set-cluster − 在 kubernetes 中设置集群条目。

$ kubectl config set-cluster name [--server = server] [--certificateauthority =
path/to/certificate/authority] [--insecure-skip-tls-verify = true]

kubectl config set-context − 在 kubernetes 入口点中设置上下文入口。

$ kubectl config set-context name [--cluster = cluster_nickname] [--
user = user_nickname] [--namespace = namespace]
$ kubectl config set-context prod –user = vipin-mishra

kubectl config set-credentials − 在 kubeconfig 中设置用户条目。

$ kubectl config set-credentials cluster-admin --username = vipin --
password = uxfgweu9l35qcif

kubectl config set − 在 kubeconfig 文件中设置单个值。

$ kubectl config set property_name property_value

**kubectl config unset **− 它取消设置 kubectl 中的特定组件。

$ kubectl config unset property_name property_value

kubectl config use-context − 在 kubectl 文件中设置当前上下文。

$ kubectl config use-context 

kubectl config view

$ kubectl config view
$ kubectl config view –o jsonpath='{.users[?(@.name == "e2e")].user.password}'

kubectl cp − 将文件和目录复制到容器和从容器复制。

$ kubectl cp  
$ kubectl cp /tmp/foo :/tmp/bar -c 

kubectl create − 通过文件名或标准输入创建资源。 为此,接受 json 或 yaml 格式。

$ kubectl create –f 
$ cat  | kubectl create –f -

同样,我们可以使用 create 命令和 kubectl 创建多个列出的内容。

  • deployment
  • namespace
  • quota
  • secret docker-registry
  • secret
  • secret generic
  • secret tls
  • serviceaccount
  • service clusterip
  • service loadbalancer
  • service nodeport

kubectl delete − 按文件名、标准输入、资源和名称删除资源。

$ kubectl delete –f ([-f filename] | type [(name | -l label | --all)])

kubectl describe − 描述 kubernetes 中的任何特定资源。 显示资源或一组资源的详细信息。

$ kubectl describe  
$ kubectl describe pod tomcat

kubectl drain − 这用于排空节点以进行维护。 它准备节点进行维护。 这会将节点标记为不可用,因此不应为其分配将要创建的新容器。

$ kubectl drain tomcat –force

kubectl edit − 它用于结束服务器上的资源。 这允许直接编辑可以通过命令行工具接收的资源。

$ kubectl edit 

kubectl exec − 这有助于在容器中执行命令。

$ kubectl exec pod <-c container > -- command < args...>
$ kubectl exec tomcat 123-5-456 date

kubectl expose − 这用于将 kubernetes 对象(例如 pod、复制控制器和服务)公开为新的 kubernetes 服务。 这有能力通过正在运行的容器或从 yaml 文件公开它。

$ kubectl expose (-f filename | type name) [--port=port] [--protocol = tcp|udp]
[--target-port = number-or-name] [--name = name] [--external-ip = external-ip-ofservice]
[--type = type]
$ kubectl expose rc tomcat –-port=80 –target-port = 30000
$ kubectl expose –f tomcat.yaml –port = 80 –target-port =

kubectl get − 此命令能够在集群上获取有关 kubernetes 资源的数据。

$ kubectl get [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columnsfile=...|
go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...]
(type [name | -l label] | type/name ...) [flags]

例如

$ kubectl get pod 
$ kubectl get service 

kubectl logs − 它们用于获取 pod 中容器的日志。 打印日志可以在 pod 中定义容器名称。 如果 pod 只有一个容器,则无需定义其名称。

$ kubectl logs [-f] [-p] pod [-c container]

例如

$ kubectl logs tomcat.
$ kubectl logs –p –c tomcat.8

kubectl port-forward − 它们用于将一个或多个本地端口转发到 pod。

$ kubectl port-forward pod [local_port:]remote_port
[...[local_port_n:]remote_port_n]
$ kubectl port-forward tomcat 3000 4000
$ kubectl port-forward tomcat 3000:5000

kubectl replace − 能够用文件名或标准输入替换资源。

$ kubectl replace -f filename
$ kubectl replace –f tomcat.yml
$ cat tomcat.yml | kubectl replace –f -

kubectl rolling-update − 在复制控制器上执行滚动更新。 通过一次更新 pod 将指定的复制控制器替换为新的复制控制器。

$ kubectl rolling-update old_controller_name ([new_controller_name] --
image = new_container_image | -f new_controller_spec)
$ kubectl rolling-update frontend-v1 –f freontend-v2.yaml

kubectl rollout − 它能够管理部署的推出。

$ kubectl rollout 
$ kubectl rollout undo deployment/tomcat

除了上述之外,我们还可以使用 rollout 执行多项任务,例如 -

  • rollout history
  • rollout pause
  • rollout resume
  • rollout status
  • rollout undo

kubectl run − run 命令可以在 kubernetes 集群上运行镜像。

$ kubectl run name --image = image [--env = "key = value"] [--port = port] [--
replicas = replicas] [--dry-run = bool] [--overrides = inline-json] [--command] --
[command] [args...]
$ kubectl run tomcat --image = tomcat:7.0
$ kubectl run tomcat –-image = tomcat:7.0 –port = 5000

kubectl scale − 它将扩展 kubernetes 部署、replicaset、复制控制器或作业的大小。

$ kubectl scale [--resource-version = version] [--current-replicas = count] --
replicas = count (-f filename | type name )
$ kubectl scale –-replica = 3 rs/tomcat
$ kubectl scale –replica = 3 tomcat.yaml

kubectl set image − 它更新 pod 模板的镜像。

$ kubectl set image (-f filename | type name)
container_name_1 = container_image_1 ... container_name_n = container_image_n
$ kubectl set image deployment/tomcat busybox = busybox ngnix = ngnix:1.9.1
$ kubectl set image deployments, rc tomcat = tomcat6.0 --all

kubectl set resources − 它用于设置资源的内容。 它使用 pod 模板更新对象的资源/限制。

$ kubectl set resources (-f filename | type name) ([--limits = limits & --
requests = requests]
$ kubectl set resources deployment tomcat -c = tomcat --
limits = cpu = 200m,memory = 512mi

kubectl top node − 它显示 cpu/内存/存储使用情况。 top 命令允许我们查看节点的资源消耗。

$ kubectl top node [node name]
the same command can be used with a pod as well.

查看笔记

扫码一下
查看教程更方便
网站地图