Kubernetes 学习环境搭建手册 (Ubuntu 25.04)
架构
Kubernetes (K8S) 是一个开源的容器编排系统,采用主从架构,主要组件包括:
控制平面 (Control Plane):
- API Server: 集群的统一入口,提供 RESTful API 接口,所有组件都通过它与集群交互
- etcd: 高可用的分布式键值存储,持久化保存所有集群数据(唯一有状态组件)
- Scheduler: 负责资源调度,根据策略将 Pod 绑定到合适的工作节点
- Controller Manager: 运行各种控制器的主进程(如 Deployment 控制器、Node 控制器等),确保集群状态符合预期声明
工作节点 (Worker Nodes):
- Kubelet: 节点上的核心代理,负责与 API Server 通信并管理本节点容器的生命周期
- Kube-proxy: 实现 Service 抽象,通过 iptables/IPVS 维护网络规则,实现服务发现和负载均衡
- 容器运行时: 负责镜像管理和容器运行(推荐 containerd,Docker 已被弃用)
步骤
-
环境准备
# 更新系统 sudo apt update && sudo apt upgrade -y # 安装必要工具 sudo apt install -y curl apt-transport-https ca-certificates
-
安装容器运行时 (containerd)
# 安装containerd sudo apt install -y containerd # 配置 containerd sudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config.toml sudo systemctl restart containerd
-
安装 kubeadm, kubelet 和 kubectl
# 添加Kubernetes仓库 sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list # 安装组件 sudo apt update sudo apt install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl
-
初始化集群
# 初始化控制平面 sudo kubeadm init --pod-network-cidr=10.244.0.0/16 # 配置kubectl mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown (id -u):(id -g) $HOME/.kube/config
-
安装网络插件 (Flannel)
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
-
允许主节点调度 Pod (单节点集群)
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
配置
-
日志管理
# 查看Pod日志 kubectl logs <pod-name>
-
存储配置
创建 PersistentVolume (PV):
apiVersion: v1 kind: PersistentVolume metadata: name: pv-local spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data" persistentVolumeReclaimPolicy: Retain
创建 PersistentVolumeClaim (PVC):
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-local spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
kubectl get pv kubectl get pvc kubectl describe pvc pvc-local
数据库部署 (MySQL 示例)
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: pvc-local
动态伸缩
手动伸缩:
kubectl scale deployment <deployment-name> --replicas=3 -n <namespace>
# 不带命名空间的话,就用 default
自动伸缩 (HPA):
# 1. 部署服务(资源限制)
# resources:
# requests:
# cpu: "100m"
# 2. 安装 metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 如果遇到证书错误
kubectl patch deployment metrics-server -n kube-system --type='json' -p='[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
# 3. 创建 HPA
kubectl autoscale deployment <deployment-name> --cpu-percent=50 --min=1 --max=5
# 4. 生成负载测试(新终端)
kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://hpa-test; done"
# 5. 观察HPA
watch kubectl get hpa
常用命令速查
- 查看节点状态
kubectl get nodes - 查看所有 Pod
kubectl get pods -A - 查看服务
kubectl get services - 查看部署
kubectl get deployments - 进入 Pod 容器
kubectl exec -it-- /bin/bash - 删除资源
kubectl delete