Bảng tra cứu nhanh Kubernetes / kubectl
Tài liệu tra cứu kubectl có thể tìm kiếm và in: contexts, pods, deployments, services, config và secrets, scaling và khắc phục sự cố. Miễn phí.
kubectl basics
13kubectl get pods
kubectl get pods -o wide
kubectl get all
kubectl describe pod my-pod
kubectl logs my-pod
kubectl logs -f my-pod
kubectl logs my-pod -c sidecar
kubectl logs my-pod --previous
kubectl exec -it my-pod -- sh
kubectl exec my-pod -- env
kubectl get pod my-pod -o yaml
kubectl explain deployment.spec
kubectl api-resources
Contexts & namespaces
11kubectl config get-contexts
kubectl config current-context
kubectl config use-context prod
kubectl get namespaces
kubectl get pods -n kube-system
kubectl get pods -A
kubectl config set-context --current --namespace=dev
kubectl create namespace staging
kubectl delete namespace staging
kubectl config view --minify
export KUBECONFIG=~/.kube/other-config
Creating & applying resources
12kubectl apply -f app.yaml
kubectl apply -f ./manifests/
kubectl apply -k ./overlays/prod
kubectl delete -f app.yaml
kubectl create deployment web --image=nginx
kubectl run tmp --image=busybox -it --rm -- sh
--dry-run=client -o yaml
kubectl diff -f app.yaml
kubectl edit deployment web
kubectl patch deploy web -p '{"spec":{"replicas":3}}'
kubectl label pod my-pod env=prod
kubectl annotate pod my-pod note="checked"
Pods & debugging
13kubectl get pods -w
kubectl get pods -l app=web
kubectl get events --sort-by=.lastTimestamp
kubectl top pods
kubectl port-forward pod/my-pod 8080:80
kubectl cp my-pod:/var/log/app.log ./app.log
kubectl debug my-pod -it --image=busybox
kubectl debug node/my-node -it --image=busybox
kubectl delete pod my-pod
kubectl delete pod my-pod --grace-period=0 --force
kubectl get pod my-pod -o jsonpath='{.status.phase}'
CrashLoopBackOff
ImagePullBackOff
Deployments & rollouts
12kubectl get deployments
kubectl set image deploy/web web=nginx:1.27
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout undo deploy/web
kubectl rollout undo deploy/web --to-revision=2
kubectl rollout restart deploy/web
kubectl rollout pause deploy/web
kubectl rollout resume deploy/web
kubectl scale deploy/web --replicas=5
maxSurge: 1, maxUnavailable: 0
kubectl get rs
Services & networking
12kubectl get svc
kubectl expose deploy/web --port=80 --target-port=8080
type: ClusterIP
type: NodePort
type: LoadBalancer
kubectl get endpoints web
kubectl port-forward svc/web 8080:80
kubectl get ingress
web.default.svc.cluster.local
kubectl run curl --image=curlimages/curl -it --rm -- sh
kubectl get networkpolicies
sessionAffinity: ClientIP
Config & secrets
11kubectl create configmap app-cfg --from-literal=ENV=prod
kubectl create configmap app-cfg --from-file=config.ini
kubectl create secret generic db --from-literal=pass=s3cret
kubectl get secret db -o jsonpath='{.data.pass}' | base64 -d
envFrom:
- configMapRef:
name: app-cfg
env:
- name: DB_PASS
valueFrom:
secretKeyRef:
name: db
key: pass
volumes:
- name: cfg
configMap:
name: app-cfg
kubectl create secret tls web-tls --cert=tls.crt --key=tls.key
kubectl create secret docker-registry regcred --docker-server=...
imagePullSecrets: [{name: regcred}]
kubectl rollout restart deploy/web
Scaling & autoscaling
11kubectl scale deploy/web --replicas=10
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70
kubectl get hpa
resources:
requests:
cpu: 100m
memory: 128Mi
resources:
limits:
cpu: 500m
memory: 256Mi
cpu: 100m
kubectl top pods --sort-by=memory
OOMKilled (exit code 137)
kubectl get pdb
minAvailable: 1
kubectl scale deploy/web --replicas=0
Nodes & cluster admin
12kubectl get nodes
kubectl describe node my-node
kubectl top nodes
kubectl cordon my-node
kubectl drain my-node --ignore-daemonsets
kubectl uncordon my-node
kubectl taint nodes my-node key=value:NoSchedule
nodeSelector: { disktype: ssd }
kubectl cluster-info
kubectl version
kubectl auth can-i delete pods
kubectl get componentstatuses
YAML manifest skeletons
9apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
apiVersion: v1
kind: ConfigMap
metadata:
name: app-cfg
data:
APP_ENV: production
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup
spec:
schedule: "0 3 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: job
image: busybox
command: ["sh", "-c", "echo run"]
restartPolicy: OnFailure
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
# kubectl create deploy web --image=nginx --dry-run=client -o yaml
Troubleshooting one-liners
13kubectl get pods -A | grep -v Running
kubectl get events -A --sort-by=.lastTimestamp | tail -20
kubectl describe pod my-pod | grep -A5 Events
kubectl logs -l app=web --tail=50
kubectl get pods --field-selector=status.phase=Failed
kubectl delete pods --field-selector=status.phase=Succeeded
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
kubectl get deploy web -o jsonpath='{.spec.template.spec.containers[0].image}'
kubectl rollout status deploy/web --timeout=120s
kubectl exec -it my-pod -- nslookup web
kubectl get pod my-pod -o yaml | grep -A5 lastState
kubectl api-resources --verbs=list -o name | xargs -n1 kubectl get -n dev
watch kubectl get pods
Không có mục nào khớp với “:q”.
Giới thiệu về Bảng tra cứu nhanh Kubernetes / kubectl
Bảng tra cứu Kubernetes này đặt kubectl vào một trang có thể tìm kiếm: kubectl cơ bản, context và namespace, tạo và áp dụng tài nguyên, pod và gỡ lỗi, deployment và rollout, service và mạng, cấu hình và secret, mở rộng và tự động mở rộng, node và quản trị cụm, khung manifest YAML, và các lệnh một dòng để khắc phục sự cố.
Nó được xây quanh các lệnh bạn chạy khi chịu áp lực: mô tả pod đang crash-loop, theo dõi log từ container trước đó, exec vào container đang chạy, quay lui deployment, port-forward một service, với các cờ được nêu rõ thay vì để mặc kubectl --help.
Như mọi bảng tra cứu trong nhóm này, nó miễn phí và chạy phía máy khách: lọc hàng trực tiếp bằng ô tìm kiếm, nhảy giữa các phần bằng mục lục cố định, sao chép bất kỳ lệnh nào chỉ với một cú nhấp và in trang để tham khảo cạnh terminal của bạn.
Cách sử dụng Bảng tra cứu nhanh Kubernetes / kubectl
- Mở bảng và xem các phần, từ kubectl cơ bản đến Các lệnh một dòng khắc phục sự cố.
- Tìm một từ khóa như logs, rollout hoặc port-forward để lọc mọi hàng trực tiếp.
- Nhảy tới Khung manifest YAML khi bạn cần một Deployment, Service hoặc ConfigMap khởi đầu.
- Nhấp vào một lệnh hoặc biểu tượng sao chép của nó để sao chép thẳng vào terminal của bạn.
- Dùng In để có bản giấy của toàn bộ tài liệu tham khảo kubectl.