Cài đặt (Installation)

Cài đặt công cụ dòng lệnh kubectl để tương tác với API Kubernetes: https://kubernetes.io/docs/tasks/tools/#kubectl

Kích hoạt tự động hoàn thành trong bash:

kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl

Cờ toàn cục (Global flags)

Flag Mô tả
--namespace [namespace] Tên không gian tên để sử dụng
--context [context] Tên ngữ cảnh để sử dụng
--help Hiển thị thông tin về lệnh

Ngữ cảnh và cấu hình (Context and configuration)

Command Mô tả
kubectl config get-contexts Liệt kê tất cả ngữ cảnh
kubectl config current-context Hiển thị ngữ cảnh hiện tại
kubectl config use-context [context] Chuyển sang ngữ cảnh khác
kubectl config delete-context [context] Xóa ngữ cảnh được chỉ định

Hiển thị tài nguyên (Display resources)

Command Mô tả
kubectl get [resource] Liệt kê tài nguyên trong không gian tên hiện tại
kubectl get [resource] -o wide Liệt kê tài nguyên với chi tiết mở rộng
kubectl get [resource] -A Liệt kê tài nguyên trong tất cả không gian tên
kubectl get [resource] [name] Liệt kê một tài nguyên cụ thể
kubectl get [resource] -l [key1]=[value1] Liệt kê tài nguyên có nhãn [key1] chứa [value1]
kubectl get [resource] [name] -o yaml In tài nguyên cụ thể dưới dạng YAML
kubectl describe [resource] Hiển thị thông tin chi tiết về tài nguyên

Áp dụng tệp cấu hình (Apply configuration manifests)

Command Mô tả
kubectl apply -f [file] Áp dụng cấu hình từ một tệp
kubectl apply -f [dir] Áp dụng tất cả cấu hình trong thư mục
kubectl apply -k [dir] Áp dụng tài nguyên từ thư mục Kustomize

Tạo tài nguyên thủ công (Create resources manually)

Command Mô tả
kubectl run [name] --image=[image] Khởi động một pod
kubectl create deployment [name] --image=[image] Tạo một deployment
kubectl expose pod [pod] --port=[port] Tạo dịch vụ cho pod hiện có
kubectl expose deployment [name] --port=[port] Tạo dịch vụ cho deployment hiện có
kubectl create ingress [name] --rule=[host/path=svc:port] Tạo ingress định tuyến lưu lượng đến dịch vụ
kubectl create job [name] --from=cronjob/[name] Tạo job từ một cronjob
kubectl create cronjob [name] --schedule=[schedule] Tạo cronjob với lịch trình Cron
kubectl create secret generic [name] Tạo secret chứa cặp [key] và [value]
kubectl create secret docker-registry [name] --docker-username=[username] Tạo secret cho registry Docker

Tạo tệp YAML cấu hình (Generate YAML configuration manifests)

Command Mô tả
kubectl create deployment [name] --image=[image] --dry-run=client -o yaml Tạo tệp YAML cho deployment
kubectl expose deployment [name] --port=[port] --dry-run=client -o yaml Tạo tệp YAML cho dịch vụ của deployment

Chỉnh sửa tài nguyên (Edit resources)

Command Mô tả
kubectl edit [resource] [name] Chỉnh sửa tài nguyên trong trình soạn thảo
kubectl set image [resource] [name] [container]=[image] Cập nhật image của container trong pod

Đặt nhãn và chú thích (Set labels and annotations)

Command Mô tả
kubectl label [resource] [name] [key]=[value] Thêm nhãn cho tài nguyên
kubectl annotate [resource] [name] [key]=[value] Thêm chú thích cho tài nguyên

Xử lý tài nguyên (Manage resources)

Command Mô tả
kubectl scale deployment [name] --replicas=[n] Điều chỉnh số lượng bản sao của deployment
kubectl delete [resource] [name] Xóa một tài nguyên cụ thể
kubectl delete -f [file] Xóa tài nguyên từ tệp cấu hình

Tương tác với pod (Interact with pods)

Command Mô tả
kubectl logs [pod] Xem nhật ký của pod
kubectl logs -f [pod] Theo dõi nhật ký của pod theo thời gian thực
kubectl exec [pod] -- [command] Thực thi lệnh trong pod
kubectl exec -it [pod] -- bash Mở shell trong pod
kubectl port-forward [pod] [local-port]:[pod-port] Chuyển tiếp cổng từ pod đến máy cục bộ

Kiểm tra cụm (Inspect cluster)

Command Mô tả
kubectl cluster-info Hiển thị thông tin về cụm
kubectl top pod Hiển thị mức sử dụng tài nguyên của pod
kubectl top node Hiển thị mức sử dụng tài nguyên của node


Tải xuống cheat sheet

Kubernetes Cheat Sheet
Theo dõi
Thông báo của
0 Góp ý
Được bỏ phiếu nhiều nhất
Mới nhất Cũ nhất
Phản hồi nội tuyến
Xem tất cả bình luận