0、kubectl 命令幫助
# kubectl --help 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 使用 replication controller, service, deployment 或者 pod 並暴露它作為一個新的Kubernetes Service run 在集群中運行一個指定的鏡像 set 為 objects 設置一個指定的特征 Basic Commands (Intermediate): explain 查看資源的文檔 get 顯示一個或更多 resources edit 在服務器上編輯一個資源 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 為 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 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: diff Diff live version against would-be applied version apply 通過文件名或標准輸入流(stdin)對資源進行配置 patch 使用 strategic merge patch 更新一個資源的 field(s) replace 通過 filename 或者 stdin替換一個資源 wait Experimental: Wait for a specific 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: 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 Provides utilities for interacting with plugins. version 輸出 client 和 server 的版本信息 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).
1、kubect create 創建一個資源從一個文件或標准輸入
kubectl create deployment nginx --image=nginx:1.14 kubectl create -f my-nginx.yaml
2、kubectl run 在集群中運行一個指定的鏡像
kubectl run nginx --image=nginx:1.16 --port=80 --replicas=1
3、kubectl expose 創建Service對象以將應用程序"暴露"於網絡中
kubectl expose deployment/nginx --type="NodePort" --port=80 --name=nginx
4、kubectl get 顯示一個或更多resources資源
kubectl get cs # 查看集群狀態 kubectl get nodes # 查看集群節點信息 kubectl get ns # 查看集群命名空間 kubectl get svc -n kube-system # 查看指定命名空間的服務 kubectl get pod <pod-name> -o wide # 查看Pod詳細信息 kubectl get pod <pod-name> -o yaml # 以yaml格式查看Pod詳細信息 kubectl get pods # 查看資源對象,查看所有Pod列表 kubectl get rc,service # 查看資源對象,查看rc和service列表 kubectl get pod,svc,ep --show-labels # 查看pod,svc,ep能及標簽信息 kubectl get all --all-namespaces # 查看所有的命名空間
5、kubectl clster-info 顯示集群信息
kubectl cluster-info # 查看集群狀態信息
6、kubectl describe 描述資源對象
kubectl describe nodes <node-name> # 顯示Node的詳細信息 kubectl describe pods/<pod-name> # 顯示Pod的詳細信息
7、kubectl scale pod擴容與縮容
kubectl scale deployment nginx --replicas 5 # 擴容 kubectl scale deployment nginx --replicas 3 # 縮容
8、查看服務器上支持的API資源
# 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 leases coordination.k8s.io true Lease events ev events.k8s.io true Event 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 ingresses ing networking.k8s.io true Ingress networkpolicies netpol networking.k8s.io true NetworkPolicy runtimeclasses node.k8s.io false RuntimeClass 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 csidrivers storage.k8s.io false CSIDriver csinodes storage.k8s.io false CSINode storageclasses sc storage.k8s.io false StorageClass volumeattachments storage.k8s.io false VolumeAttachment
9、生成yaml資源配置文件方法
# 用run命令生成yaml文件 kubectl create deployment nginx --image=nginx:1.14 -o yaml --dry-run > my.deploy.yaml # 用get命令導出yaml文件 kubectl get deploy nginx-deployment -o yaml --export > my.deploy.yaml # Pod容器的字段拼寫忘記了 kubectl explain pods.spec.containers