kubectl là gì?
kubectl là một CLI giúp bạn tương tác với Kubernetes clusters . Chúng ta sử dụng kubectl để triển khai các ứng dụng cũng như kiểm tra và quản lý các tài nguyên của cluster.
kubectl tương tác với API server được chạy trên control plane node.
Kubernetes (Kubectl) CheatSheet
Quản lý cluster
Command DESCRIPTION kubectl cluster-info Get cluster information. kubectl get nodes Views all the nodes present in the cluster.
Quản lý node
Command Description kubectl get nodes List all nodes in the cluster. kubectl describe node <node-name> Describe a specific node. kubectl drain <node-name> Drain a node for maintenance. kubectl uncordon <node-name> Uncordon a node after maintenance. kubectl label node <node_name> <key>=<value> You can label the node by using key-value pair. kubectl label node <node_name> <label_key>- You can remove the label which is already attached to the node.
Quản lý namespace
Command Description kubectl describe namespace <namespace-name> Describe a namespace. kubectl create namespace <namespace-name> Create a namespace. kubectl get namespaces List all namespaces. kubectl config set-context –current –namespace=<namespace-name> Switch to a different namespace. kubectl delete namespace <namespace-name> Delete a namespace. kubectl edit namespace <namespace_name> Edit and update the namespace definition.
Tạo tài nguyên
Command Description kubectl apply -f <resource.yaml> Create or Update a resource from a YAML file. kubectl create <resource-type> Create an object imperatively. kubectl edit <resource-type> -o yaml Edit a resource configure in YAML format.
Xem tài nguyên
Command Description kubectl get <resource-type> List all resources of a specific type. kubectl get <resource-type> -o wide List all resources with additional details. kubectl describe <resource-type> <resource-name> Describe a specific resource. kubectl get <resource-type> –all-namespaces List all resources in all namespaces. kubectl get <resource-type> -l <label-selector> List resources with a specific label selector. kubectl get <resource-type> -n <namespace> List all resources in a specific namespace.
Xóa tài nguyên
Command Description kubectl delete <resource-type> <resource-name> Delete a resource. kubectl delete <resource-type> –all Delete all resources of a specific type. kubectl delete -f <resource.yaml> Delete the resource from a YAML file. kubectl delete <resource-type> -n <namespace> Delete resources in a specific namespace.
Scaling tài nguyên
Command Description kubectl scale deployment <deployment-name> –replicas=<count> Scale a deployment. kubectl scale statefulset <statefulset-name> –replicas=<count> Scale a statefulset. kubectl scale replicaset <replicaset-name> –replicas=<count> Scale a replica set.
Quản lý pod
Command Description kubectl create -f <pod.yaml> Create a pod from a YAML file. kubectl get pods List all pods in the cluster. kubectl describe pod <pod-name> Describe a specific pod. kubectl logs <pod-name> Get logs from a pod. kubectl logs -f <pod-name> Stream logs from a pod. kubectl exec -it <pod-name> <command> Exec into a pod. kubectl delete pod <pod-name> Delete a pod. kubectl create pod <pod-name> Create a pod with the name. kubectl get pod -n <namespace_name> List all pods in a namespace.
Quản lý deployment và rollout
Command Description kubectl create deployment <deployment-name> –image=<image-name> Create a deployment. kubectl get deployments List all deployments. kubectl describe deployment <deployment-name> Describe a specific deployment. kubectl set image deployment/<deployment-name> <container-name>=<new-image-name> Update a deployment’s image. kubectl rollout status deployment/<deployment-name> Rollout status of a deployment. kubectl rollout pause deployment/<deployment-name> Pause a deployment rollout. kubectl rollout resume deployment/<deployment-name> Resume a deployment rollout. kubectl rollout undo deployment/<deployment-name> Rollback a deployment to the previous revision. kubectl rollout undo deployment/<deployment-name> –to-revision=<revision-number> Rollback a deployment to a specific revision. kubectl delete deployment <deployment-name> Delete deployment name.
Quản lý replicasets
Command Description kubectl create -f <replicaset.yaml> Create a ReplicaSet. kubectl get replicasets List all ReplicaSets. kubectl describe replicaset <replicaset-name> Describe a specific ReplicaSet. kubectl scale replicaset <replicaset-name> –replicas=<count> Scale a ReplicaSet.
Quản lý service
Command Description kubectl create service <service-type> <service-name> –port=<port> Create a service. kubectl get services List all services. kubectl expose deployment <deployment-name> –port=<port> Expose a deployment as a service. kubectl describe service <service-name> Describe a specific service. kubectl delete service <service-name> Delete a service. kubectl get endpoints <service-name> Get information about a service.
Quản lý configmaps và secrets
Command Description kubectl create configmap <configmap-name> –from-file=<path-to-file> Create a config map from a file. kubectl create secret <secret-type> <secret-name> –from-literal=<key>=<value> Create a secret. kubectl get configmaps List all config maps. kubectl get secrets List all secrets. kubectl describe configmap <configmap-name> Describe a specific config map. kubectl describe secret <secret-name> Describe a specific secret. kubectl delete secret <secret_name> Delete a specific secret. kubectl delete configmap <configmap-name> Delete a specific config map.
Quản lý mạng
Command Description kubectl port-forward <pod-name> <local-port>:<pod-port> Port forward to a pod. kubectl expose deployment <deployment-name> –type=NodePort –port=<port> Expose a deployment as a NodePort service. kubectl create ingress <ingress-name> –rule=<host>/<path>=<service-name> –<service-port> Create an Ingress resource. kubectl describe ingress <ingress-name> Get information about an Ingress. kubectl get ingress <ingress-name> -o jsonpath='{.spec.rules[0].host}’ Retrieves the most value from the first rule of the specified Ingress resource.
Quản lý lưu trữ
Command Description kubectl create -f <pv.yaml> Create a PersistentVolume. kubectl get pv List all PersistentVolumes. kubectl describe pv <pv-name> Describe a specific PersistentVolume. kubectl create -f <pvc.yaml> Create a PersistentVolumeClaim. kubectl get pvc List all PersistentVolumeClaims. kubectl describe pvc <pvc-name> Describe a specific PersistentVolumeClaim.
Quản lý statefulsets
Command Description kubectl create -f <statefulset.yaml> Create a StatefulSet. kubectl get statefulsets List all StatefulSets. kubectl describe statefulset <statefulset-name> Describe a specific StatefulSet. kubectl scale statefulset <statefulset-name> –replicas=<count> Scale a StatefulSet.
Giám sát và gỡ lỗi
Command Description kubectl get events Check cluster events. kubectl get component statuses Get cluster component statuses. kubectl top nodes Get resource utilization of nodes. kubectl top pods Get resource utilization of pods. kubectl debug <pod-name> -it –image=<debugging-image> Enable container shell access debugging.
Viết tắt trong kubectl
Bạn có thể muốn biết các tên viết tắt của một số tài nguyên trên kubectl giúp bạn dễ dàng thao tác hơn và làm code của bạn trông gọn gàng hơn.
Tên rút gọn Tên đầy đủ csr certificatesigningrequests cs componentstatuses cm configmaps ds daemonsets deploy deployments ep endpoints ev events hpa horizontalpodautoscalers ing ingresses limits limitranges ns namespaces no nodes pvc persistentvolumeclaims pv persistentvolumes po pods pdb poddisruptionbudgets psp podsecuritypolicies rs replicasets rc replicationcontrollers quota resourcequotas sa serviceaccounts svc services