Skip to content

03 - 搭建本地 K8s 环境 & kubectl 基础

启用 Docker Desktop 内置的 K8s

步骤

  1. 打开 Docker DesktopSettingsKubernetes
  2. 勾选 Enable Kubernetes
  3. 点击 Apply & Restart
  4. 等待安装完成(状态栏显示绿色 "Kubernetes running")

验证

bash
# 检查 kubectl 是否可用
kubectl version

# 查看集群信息
kubectl cluster-info

# 查看节点(Docker Desktop 只有一个节点)
kubectl get nodes

# 查看所有命名空间
kubectl get namespaces

# 查看系统组件 Pod
kubectl get pods -n kube-system

kubectl 基础操作

kubectl 是与 K8s 集群交互的命令行工具,语法格式:

kubectl [动词] [资源类型] [资源名] [参数]

常用动词

动词说明示例
get列出资源kubectl get pods
describe查看详细信息kubectl describe pod my-pod
create创建资源kubectl create -f pod.yaml
apply创建或更新资源(声明式,推荐)kubectl apply -f pod.yaml
delete删除资源kubectl delete pod my-pod
edit编辑资源kubectl edit deployment web
logs查看日志kubectl logs my-pod
exec在容器中执行命令kubectl exec -it my-pod -- sh
port-forward端口转发kubectl port-forward pod/my-pod 8080:80
scale调整副本数kubectl scale deployment web --replicas=3

资源类型缩写

全称缩写
podspo
servicessvc
deploymentsdeploy
replicasetsrs
namespacesns
configmapscm
persistentvolumeclaimspvc
nodesno
ingressesing

常用查询技巧

bash
# 查看所有资源类型
kubectl api-resources

# 多种资源一起查
kubectl get pods,svc,deploy

# 所有命名空间
kubectl get pods -A
kubectl get pods --all-namespaces

# 输出格式
kubectl get pods -o wide          # 更多列(IP、节点等)
kubectl get pods -o yaml          # YAML 格式
kubectl get pods -o json          # JSON 格式
kubectl get pods -o name          # 只输出名称

# 标签筛选
kubectl get pods -l app=web
kubectl get pods -l 'env in (prod,staging)'

# 排序
kubectl get pods --sort-by=.metadata.creationTimestamp

# 监听变化
kubectl get pods -w

命名空间(Namespace)

命名空间是集群内部的逻辑隔离,类似于文件系统的目录。

bash
# 默认命名空间
# default    - 用户资源的默认空间
# kube-system - K8s 系统组件
# kube-public - 公开信息
# kube-node-lease - 节点心跳

# 创建命名空间
kubectl create namespace dev
kubectl create namespace staging

# 在指定命名空间操作
kubectl get pods -n dev
kubectl apply -f pod.yaml -n dev

# 设置默认命名空间(避免每次都加 -n)
kubectl config set-context --current --namespace=dev

实操练习:第一个 Pod

bash
# 用命令式方式运行一个 Pod(快速测试)
kubectl run my-nginx --image=nginx:alpine

# 查看 Pod
kubectl get pods
kubectl get pods -o wide

# 查看详情
kubectl describe pod my-nginx

# 查看日志
kubectl logs my-nginx

# 进入 Pod
kubectl exec -it my-nginx -- sh

# 端口转发访问
kubectl port-forward pod/my-nginx 8080:80
# 浏览器打开 http://localhost:8080

# 删除 Pod
kubectl delete pod my-nginx

用 YAML 创建(声明式,推荐)

yaml
# 03-k8s-architecture/manifests/first-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:alpine
    ports:
    - containerPort: 80
bash
# 应用 YAML
kubectl apply -f first-pod.yaml

# 查看
kubectl get pods

# 删除
kubectl delete -f first-pod.yaml

kubectl 配置(Kubeconfig)

kubectl 的配置文件在 ~/.kube/config,管理多个集群时尤为重要:

bash
# 查看当前配置
kubectl config view

# 查看当前上下文(连接的集群)
kubectl config current-context

# 列出所有上下文
kubectl config get-contexts

# 切换上下文(将来连接阿里云 ACK 时需要)
kubectl config use-context docker-desktop

常用别名设置(提效)

~/.zshrc 中添加:

bash
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deploy'
alias kga='kubectl get all'
alias kaf='kubectl apply -f'
alias kdf='kubectl delete -f'
alias kdp='kubectl describe pod'
alias kl='kubectl logs'
alias ke='kubectl exec -it'

下一步

Phase 4 - K8s 核心资源