k8s之kubectl常用命令


一. Kubectl常用命令

node相關

kubectl get nodes -o wide

kubectl describe node node01

 

容器命令

kubectl get pods -A -o wide

kubectl describe pod edgedns-xxx -n openname

kubectl exec -it pod_name /bin/sh

kubectl exec mypod -c pod -it -- /bin/sh

kubectl logs mypod -f -n openname

KUBE_EDITOR="vim" kubectl edit pod af -n ngc

 

網絡

kubectl get subnets

三. kubectl命令行工具

3.1 kubectl

kubectl是Kubernetes集群的命令行工具,通過kubectl能夠對集群本身進行管理,並能夠在集群上進行容器化應用的安裝部署。

kubectl [command] [TYPE] [NAME] [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).

command:指定要對資源執行的操作,如create、get、describe和delete等。

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin. expose 使用replication controller, service, deployment或者pod並暴露它作為一個新的k8s Service run 在集群中運行一個指定的鏡像 set 為 objects 設置一個指定的特征 Basic Commands (Intermediate): explain 查看資源的文檔 get 顯示一個或更多 resources edit 在服務器上編輯一個資源 delete 根據filenames, stdin, resources and names, or by resources and label selector刪除資源 Deploy Commands: rollout Manage the rollout of a resource scale 為 Deployment, ReplicaSet, Replication Controller 或者 Job 設置一個新的副本數量 autoscale 自動調整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數量 Cluster Management Commands: certificate 修改 certificate 資源. cluster-info 顯示集群信息 top Display Resource (CPU/Memory/Storage) usage. cordon 標記 node 為 unschedulable uncordon 標記 node 為 schedulable drain Drain node in preparation for maintenance 耗盡node taint 更新一個或者多個 node 上的 taints Troubleshooting and Debugging Commands: describe 顯示一個指定 resource 或者 group 的 resources 詳情 logs 輸出容器在 pod 中的日志 attach Attach 到一個運行中的 container exec 在一個 container 中執行一個命令 port-forward Forward one or more local ports to a pod proxy 運行一個 proxy 到 Kubernetes API server cp 復制 files 和 directories 到 containers 和從容器中復制 files 和 directories. auth Inspect authorization Advanced Commands: apply 通過配置文件或標准輸入流(stdin)對資源進行配置 patch 使用 strategic merge patch 更新一個資源的 field(s) replace 通過 filename 或者 stdin替換一個資源 wait Experimental: Wait for one condition on one or many resources convert 在不同的 API versions 轉換配置文件 Settings Commands: label 更新在這個資源上的 labels annotate 更新一個資源的注解 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 修改 kubeconfig 文件 plugin Runs a command-line plugin version 輸出 client 和 server 的版本信息 

TYPE:指定資源類型,資源類型是大小寫敏感的,開發者能夠以單數、復數和縮略的形式。例如: 

kubectl get pod pod1
kubectl get pods pod1 kubectl get po pod1 

NAME:指定資源的名稱,名稱大小寫敏感。若省略名稱,則會顯示所有資源,例如:

kubectl get pods

flags:指定可選的參數。例如,可以使用-s或者--server參數指定kubernetes API server的地址和端口。

3.2 kubectl常用命令

kubectl作為kubernetes的命令行工具,主要的職責就是對集群中的資源的對象進行操作,這些操作包括對資源對象的創建、刪除和查看等。下表中顯示了kubectl支持的所有操作,以及這些操作的語法和描述信息:

kubectl編輯配置

kubectl edit (RESOURCE/NAME | -f FILENAME) [options]
$kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-test 2/3 3 2 4d18h nginx-test nginx app=nginx $kubectl edit deployment nginx-test deployment.extensions/nginx-test edited $ kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-test 2/2 2 2 4d18h nginx-test nginx app=nginx

kubectl輸出選項

kubectl默認的輸出格式為純文本格式,可以通過-o或者–output字段指定命令的輸出格式。

kubectl [command] [TYPE] [NAME] -o=<output format>
kubectl get pod nginx-test-xxxxxxxxx -o yaml

3.3 k8s支持的資源

$ kubectl api-resources

NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true Binding componentstatuses cs false ComponentStatus configmaps cm true ConfigMap endpoints ep true Endpoints events ev true Event limitranges limits true LimitRange namespaces ns false Namespace nodes no false Node persistentvolumeclaims pvc true PersistentVolumeClaim persistentvolumes pv false PersistentVolume pods po true Pod podtemplates true PodTemplate replicationcontrollers rc true ReplicationController resourcequotas quota true ResourceQuota secrets true Secret serviceaccounts sa true ServiceAccount services svc true Service mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition apiservices apiregistration.k8s.io false APIService controllerrevisions apps true ControllerRevision daemonsets ds apps true DaemonSet deployments deploy apps true Deployment replicasets rs apps true ReplicaSet statefulsets sts apps true StatefulSet tokenreviews authentication.k8s.io false TokenReview localsubjectaccessreviews authorization.k8s.io true LocalSubjectAccessReview selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview subjectaccessreviews authorization.k8s.io false SubjectAccessReview horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler cronjobs cj batch true CronJob jobs batch true Job certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest daemonsets ds extensions true DaemonSet deployments deploy extensions true Deployment ingresses ing extensions true Ingress networkpolicies netpol extensions true NetworkPolicy podsecuritypolicies psp extensions false PodSecurityPolicy replicasets rs extensions true ReplicaSet addons k3s.cattle.io true Addon helmcharts k3s.cattle.io true HelmChart listenerconfigs k3s.cattle.io true ListenerConfig networkpolicies netpol networking.k8s.io true NetworkPolicy poddisruptionbudgets pdb policy true PodDisruptionBudget podsecuritypolicies psp policy false PodSecurityPolicy clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding clusterroles rbac.authorization.k8s.io false ClusterRole rolebindings rbac.authorization.k8s.io true RoleBinding roles rbac.authorization.k8s.io true Role priorityclasses pc scheduling.k8s.io false PriorityClass storageclasses sc storage.k8s.io false StorageClass volumeattachments storage.k8s.io false VolumeAttachment

3.4 kubeconfig

kubectl默認會從$HOME/.kube目錄下查找文件名為config的文件,也能通過設置環境變量KUBECONFIG或者通過設置去指定其它 kubeconfig 文件。kubeconfig就是為訪問集群所作的配置。

在開啟了 TLS 的集群中,每當與集群交互的時候少不了的是身份認證,使用 kubeconfig(即證書) 和 token 兩種認證方式是最簡單也最通用的認證方式。

kubectl config SUBCOMMAND [options]
kubectl options
      --alsologtostderr[=false]: 同時輸出日志到標准錯誤控制台和文件。 --api-version="": 和服務端交互使用的API版本。 --certificate-authority="": 用以進行認證授權的.cert文件路徑。 --client-certificate="": TLS使用的客戶端證書路徑。 --client-key="": TLS使用的客戶端密鑰路徑。 --cluster="": 指定使用的kubeconfig配置文件中的集群名。 --context="": 指定使用的kubeconfig配置文件中的環境名。 --insecure-skip-tls-verify[=false]: 如果為true,將不會檢查服務器憑證的有效性, 這會導致你的HTTPS鏈接變得不安全。 --kubeconfig="": 命令行請求使用的配置文件路徑。 --log-backtrace-at=:0: 當日志長度超過定義的行數時,忽略堆棧信息。 --log-dir="": 如果不為空,將日志文件寫入此目錄。 --log-flush-frequency=5s: 刷新日志的最大時間間隔。 --logtostderr[=true]: 輸出日志到標准錯誤控制台,不輸出到文件。 --match-server-version[=false]: 要求服務端和客戶端版本匹配。 --namespace="": 如果不為空,命令將使用此namespace。 --password="": API Server進行簡單認證使用的密碼。 -s, --server="": Kubernetes API Server的地址和端口號。 --stderrthreshold=2: 高於此級別的日志將被輸出到錯誤控制台。 --token="": 認證到API Server使用的令牌。 --user="": 指定使用的kubeconfig配置文件中的用戶名。 --username="": API Server進行簡單認證使用的用戶名。 --v=0: 指定輸出日志的級別。 --vmodule=: 指定輸出日志的模塊,格式如下:pattern=N,使用逗號分隔。

 

參考:

1. Kubernetes中文手冊  kubernetes中文社區


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM