Skip to content
kubectl-basics logo

kubectl 基础操作

kubectl-basics

kubectl 基础操作与常用命令

SKILL.md

Full skill instructions

kubectl 基础操作

概述

kubectl 是 Kubernetes 的命令行工具,用于与集群进行交互。本 skill 涵盖日常运维中最常用的命令。

集群信息

# 集群信息
kubectl cluster-info
kubectl version

# 节点信息
kubectl get nodes
kubectl describe node <node-name>

# 上下文切换
kubectl config get-contexts
kubectl config use-context <context-name>

资源查看

常用资源

# Pod
kubectl get pods
kubectl get pods -o wide
kubectl get pods -A                    # 所有命名空间

# Deployment
kubectl get deployments
kubectl get deploy -o yaml

# Service
kubectl get services
kubectl get svc

# 所有资源
kubectl get all
kubectl get all -n <namespace>

输出格式

kubectl get pods -o wide               # 详细信息
kubectl get pods -o yaml               # YAML 格式
kubectl get pods -o json               # JSON 格式
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

资源操作

创建与删除

# 从文件创建
kubectl apply -f manifest.yaml
kubectl create -f manifest.yaml

# 删除资源
kubectl delete -f manifest.yaml
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --force --grace-period=0

编辑与更新

# 编辑资源
kubectl edit deployment <name>

# 打补丁
kubectl patch deployment <name> -p '{"spec":{"replicas":3}}'

# 扩缩容
kubectl scale deployment <name> --replicas=5

Pod 调试

# 查看日志
kubectl logs <pod-name>
kubectl logs <pod-name> -c <container>
kubectl logs <pod-name> -f              # 实时跟踪
kubectl logs <pod-name> --previous      # 上一个容器

# 进入容器
kubectl exec -it <pod-name> -- /​bin/​bash
kubectl exec -it <pod-name> -c <container> -- sh

# 端口转发
kubectl port-forward <pod-name> 8080:80
kubectl port-forward svc/<service> 8080:80

常见场景

场景 1:快速排查 Pod 问题

# 1. 查看 Pod 状态
kubectl get pod <name> -o wide

# 2. 查看事件
kubectl describe pod <name> | grep -A 20 Events

# 3. 查看日志
kubectl logs <name> --tail=100

场景 2:滚动更新

# 更新镜像
kubectl set image deployment/<name> container=image:tag

# 查看更新状态
kubectl rollout status deployment/<name>

# 回滚
kubectl rollout undo deployment/<name>
kubectl rollout history deployment/<name>

快捷别名

# 推荐添加到 ~/​.bashrc 或 ~/​.zshrc
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deploy'
alias kd='kubectl describe'
alias kl='kubectl logs'
alias ke='kubectl exec -it'

故障排查

状态可能原因排查命令
Pending资源不足/调度问题kubectl describe pod
CrashLoopBackOff应用崩溃kubectl logs --previous
ImagePullBackOff镜像拉取失败kubectl describe pod
OOMKilled内存超限检查 resources.limits

More DevOps & CI/CD skills

Project scaffolding, deployment configuration, and CI/CD setup for Google ADK agents.

6K 472.6K
View

Set up tracing, logging, and monitoring for deployed ADK agents across Cloud Trace, BigQuery, and third-party platforms.

6K 472.6K
View

Enterprise Azure infrastructure architect generating Bicep or Terraform from workload descriptions.

1.5K 454.3K
View
azure-kubernetes logo
DevOps & CI/CD

azure-kubernetes

Plan and configure production-ready Azure Kubernetes Service clusters with Day-0 and Day-1 best practices.

1.5K 447.1K
View

Raw mechanical interfaces fusing Swiss typographic print with military terminal aesthetics. Rigid grids, extreme type scale contrast, utilitarian color, analog degradation effects. For data-heavy dashboards, portfolios, or editorial sites that need to feel like declassified blueprints.

92.7K 354.9K
View
just-scrape logo
DevOps & CI/CD

just-scrape

Web search, scraping, extraction, crawling, and monitoring via ScrapeGraph AI CLI.

69 245K
View

Skill for working with Firebase Hosting (Classic). Use this when you want to deploy static web apps, Single Page Apps (SPAs), or simple microservices. Do NOT use for Firebase App Hosting.

462 159.7K
View

Deploy and manage web apps with Firebase App Hosting. Use this skill when deploying Next.js/Angular apps with backends.

462 159.1K
View
deploy-to-vercel logo
DevOps & CI/CD

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

31.9K 146.6K
View
programmatic-seo logo
DevOps & CI/CD

programmatic-seo

Build SEO-optimized pages at scale using templates, data, and proven playbook patterns.

53.3K 140.1K
View

Design and build isolated, reusable Convex backend components with clear boundaries and app-facing wrappers.

63 123.5K
View

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".

31.9K 116.1K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
I
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View