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

CommandDESCRIPTION
kubectl cluster-infoGet cluster information.
kubectl get nodesViews all the nodes present in the cluster.

Quản lý node

CommandDescription
kubectl get nodesList 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

CommandDescription
kubectl describe namespace <namespace-name> Describe a namespace.
kubectl create namespace <namespace-name>Create a namespace.
kubectl get namespacesList 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

CommandDescription
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 yamlEdit a resource configure in YAML format.

Xem tài nguyên

CommandDescription
kubectl get <resource-type>List all resources of a specific type.
kubectl get <resource-type> -o wideList all resources with additional details.
kubectl describe <resource-type> <resource-name>Describe a specific resource.
kubectl get <resource-type> –all-namespacesList 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

CommandDescription
kubectl delete <resource-type> <resource-name>Delete a resource.
kubectl delete <resource-type> –allDelete 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

CommandDescription
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

CommandDescription
kubectl create -f <pod.yaml>Create a pod from a YAML file.
kubectl get podsList 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

CommandDescription
kubectl create deployment <deployment-name> –image=<image-name>Create a deployment.
kubectl get deploymentsList 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

CommandDescription
kubectl create -f <replicaset.yaml>Create a ReplicaSet.
kubectl get replicasetsList 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

CommandDescription
kubectl create service <service-type> <service-name> –port=<port>Create a service.
kubectl get servicesList 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

CommandDescription
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 configmapsList all config maps.
kubectl get secretsList 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

CommandDescription
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ữ

CommandDescription
kubectl create -f <pv.yaml>Create a PersistentVolume.
kubectl get pvList all PersistentVolumes.
kubectl describe pv <pv-name>Describe a specific PersistentVolume.
kubectl create -f <pvc.yaml>Create a PersistentVolumeClaim.
kubectl get pvcList all PersistentVolumeClaims.
kubectl describe pvc <pvc-name>Describe a specific PersistentVolumeClaim.

Quản lý statefulsets

CommandDescription
kubectl create -f <statefulset.yaml>Create a StatefulSet.
kubectl get statefulsetsList 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

CommandDescription
kubectl get eventsCheck cluster events.
kubectl get component statusesGet cluster component statuses.
kubectl top nodesGet resource utilization of nodes.
kubectl top podsGet 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ọnTên đầy đủ
csrcertificatesigningrequests
cscomponentstatuses
cmconfigmaps
dsdaemonsets
deploydeployments
ependpoints
evevents
hpahorizontalpodautoscalers
ingingresses
limitslimitranges
nsnamespaces
nonodes
pvcpersistentvolumeclaims
pvpersistentvolumes
popods
pdbpoddisruptionbudgets
psppodsecuritypolicies
rsreplicasets
rcreplicationcontrollers
quotaresourcequotas
saserviceaccounts
svcservices

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.
x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security