当前位置: 首页 > news >正文

k8s常用命令介绍

一、Namespace(命名空间)

1.1 基本概念

Namespace是Kubernetes中用于资源隔离的逻辑分组机制,它允许在同一个物理集群中创建多个虚拟集群。不同Namespace中的资源名称可以相同,这为多团队、多环境(如dev/staging/prod)共享集群提供了便利。

1.2 常用命令

创建Namespace
# 通过命令行直接创建
kubectl create namespace <namespace-name># 通过YAML文件创建
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:name: <namespace-name>
EOF
查看Namespace
# 查看所有Namespace
kubectl get namespaces
# 或简写为
kubectl get ns# 查看特定Namespace的详细信息
kubectl describe namespace <namespace-name>
删除Namespace
kubectl delete namespace <namespace-name>
在特定Namespace中执行命令
# 查看某Namespace下的所有资源
kubectl get all -n <namespace-name># 在特定Namespace中创建资源
kubectl apply -f <file.yaml> -n <namespace-name>
设置默认Namespace
# 查看当前上下文
kubectl config current-context# 修改当前上下文的默认Namespace
kubectl config set-context --current --namespace=<namespace-name>

二、Pod(容器组)

2.1 基本概念

Pod是Kubernetes中最小的可部署单元,它包含一个或多个紧密关联的容器(通常是一个主容器和多个sidecar容器),共享存储、网络和运行规范。

2.2 常用命令

创建Pod
# 通过命令行直接创建
kubectl run <pod-name> --image=<image-name> --namespace=<namespace-name># 通过YAML文件创建
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:name: <pod-name>namespace: <namespace-name>
spec:containers:- name: <container-name>image: <image-name>ports:- containerPort: 80
EOF
查看Pod
# 查看所有Pod
kubectl get pods --all-namespaces
# 或简写为
kubectl get po -A# 查看特定Namespace中的Pod
kubectl get pods -n <namespace-name># 查看Pod详细信息
kubectl describe pod <pod-name> -n <namespace-name># 查看Pod日志
kubectl logs <pod-name> -n <namespace-name># 查看多容器Pod中特定容器的日志
kubectl logs <pod-name> -c <container-name> -n <namespace-name>
与Pod交互
# 进入Pod中的容器
kubectl exec -it <pod-name> -n <namespace-name> -- /bin/bash# 在多容器Pod中进入特定容器
kubectl exec -it <pod-name> -c <container-name> -n <namespace-name> -- /bin/bash# 在Pod中执行命令
kubectl exec <pod-name> -n <namespace-name> -- <command>
删除Pod
kubectl delete pod <pod-name> -n <namespace-name>
调试命令
# 查看Pod的事件
kubectl get events -n <namespace-name># 查看Pod的资源使用情况
kubectl top pod <pod-name> -n <namespace-name>

三、Deployment(部署)

3.1 基本概念

Deployment是声明式管理Pod副本集的更高级抽象,它支持滚动更新、回滚、扩缩容等操作,是管理无状态应用的首选方式。

3.2 常用命令

创建Deployment
# 通过命令行创建
kubectl create deployment <deployment-name> --image=<image-name> -n <namespace-name># 通过YAML文件创建
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:name: <deployment-name>namespace: <namespace-name>
spec:replicas: 3selector:matchLabels:app: <app-label>template:metadata:labels:app: <app-label>spec:containers:- name: <container-name>image: <image-name>ports:- containerPort: 80
EOF
查看Deployment
# 查看所有Deployment
kubectl get deployments -A
# 或简写为
kubectl get deploy -A# 查看特定Deployment的详细信息
kubectl describe deployment <deployment-name> -n <namespace-name># 查看Deployment关联的ReplicaSet
kubectl get replicasets -n <namespace-name># 查看Deployment关联的Pod
kubectl get pods -l app=<app-label> -n <namespace-name>
更新Deployment
# 更新镜像
kubectl set image deployment/<deployment-name> <container-name>=<new-image-name> -n <namespace-name># 编辑Deployment配置
kubectl edit deployment <deployment-name> -n <namespace-name># 应用YAML文件更新
kubectl apply -f <deployment-file.yaml> -n <namespace-name>
扩缩容
# 扩展副本数
kubectl scale deployment <deployment-name> --replicas=5 -n <namespace-name># 自动扩缩容(需安装metrics-server)
kubectl autoscale deployment <deployment-name> --min=2 --max=10 --cpu-percent=80 -n <namespace-name>
回滚与历史
# 查看发布历史
kubectl rollout history deployment/<deployment-name> -n <namespace-name># 回滚到上一个版本
kubectl rollout undo deployment/<deployment-name> -n <namespace-name># 回滚到特定版本
kubectl rollout undo deployment/<deployment-name> --to-revision=2 -n <namespace-name>
删除Deployment
kubectl delete deployment <deployment-name> -n <namespace-name>

四、Service(服务)

4.1 基本概念

Service是定义一组Pod访问策略的抽象,它为Pod提供稳定的IP地址和DNS名称,并实现负载均衡。主要类型包括:

  • ClusterIP:默认类型,集群内部访问
  • NodePort:通过节点端口暴露服务
  • LoadBalancer:通过云提供商的负载均衡器暴露服务
  • ExternalName:将服务映射到外部DNS名称

4.2 常用命令

创建Service
# 通过命令行创建
kubectl expose deployment <deployment-name> --type=ClusterIP --port=80 --target-port=8080 -n <namespace-name># 通过YAML文件创建
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:name: <service-name>namespace: <namespace-name>
spec:selector:app: <app-label>ports:- protocol: TCPport: 80targetPort: 8080type: ClusterIP
EOF
查看Service
# 查看所有Service
kubectl get services -A
# 或简写为
kubectl get svc -A# 查看特定Service的详细信息
kubectl describe service <service-name> -n <namespace-name># 查看Service的Endpoints(关联的Pod)
kubectl get endpoints <service-name> -n <namespace-name>
测试Service访问
# 在集群内部访问Service
kubectl run curl-test --image=radial/busyboxplus:curl -i --tty --rm
# 然后在容器内执行
curl http://<service-name>.<namespace-name>.svc.cluster.local# 获取Service的ClusterIP
kubectl get svc <service-name> -n <namespace-name> -o jsonpath='{.spec.clusterIP}'
删除Service
kubectl delete service <service-name> -n <namespace-name>
特殊类型Service
NodePort
# 创建NodePort Service
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:name: <service-name>namespace: <namespace-name>
spec:selector:app: <app-label>ports:- protocol: TCPport: 80targetPort: 8080type: NodePort
EOF# 查看分配的节点端口
kubectl get svc <service-name> -n <namespace-name> -o jsonpath='{.spec.ports[0].nodePort}'
LoadBalancer
# 创建LoadBalancer Service(需云提供商支持)
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:name: <service-name>namespace: <namespace-name>
spec:selector:app: <app-label>ports:- protocol: TCPport: 80targetPort: 8080type: LoadBalancer
EOF# 查看外部IP(可能需要等待云提供商分配)
kubectl get svc <service-name> -n <namespace-name> -w

五、Ingress(入口)

5.1 基本概念

Ingress是管理外部访问集群服务的API对象,通常通过HTTP/HTTPS路由提供。它需要与Ingress Controller(如Nginx、Traefik、ALB等)配合使用,实现:

  • 基于主机名或路径的路由
  • TLS终止
  • 负载均衡
  • 虚拟主机支持

5.2 常用命令

创建Ingress
# 通过YAML文件创建(需要先安装Ingress Controller)
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: <ingress-name>namespace: <namespace-name>annotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:rules:- host: <your-domain.com>http:paths:- path: /pathType: Prefixbackend:service:name: <service-name>port:number: 80
EOF# 创建带TLS的Ingress
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: <ingress-name>namespace: <namespace-name>
spec:tls:- hosts:- <your-domain.com>secretName: <tls-secret-name>rules:- host: <your-domain.com>http:paths:- path: /pathType: Prefixbackend:service:name: <service-name>port:number: 80
EOF
查看Ingress
# 查看所有Ingress
kubectl get ingress -A
# 或简写为
kubectl get ing -A# 查看特定Ingress的详细信息
kubectl describe ingress <ingress-name> -n <namespace-name>
管理TLS证书
# 创建TLS Secret(需要已有证书文件)
kubectl create secret tls <tls-secret-name> \--cert=<path-to-cert.pem> \--key=<path-to-key.pem> \-n <namespace-name># 查看TLS Secret
kubectl get secret <tls-secret-name> -n <namespace-name> -o yaml
删除Ingress
kubectl delete ingress <ingress-name> -n <namespace-name>
常见Ingress Controller相关命令
Nginx Ingress Controller
# 安装Nginx Ingress Controller(使用Helm)
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm install ingress-nginx ingress-nginx/ingress-nginx -n ingress-nginx --create-namespace# 查看Ingress Controller Pod
kubectl get pods -n ingress-nginx# 查看Ingress Controller日志
kubectl logs -l app.kubernetes.io/name=ingress-nginx -n ingress-nginx
测试Ingress
# 获取Ingress Controller的外部IP(可能需要根据部署方式调整)
kubectl get svc ingress-nginx-controller -n ingress-nginx# 临时添加本地hosts解析(如果使用域名)
echo "$(kubectl get svc ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}') <your-domain.com>" | sudo tee -a /etc/hosts# 测试访问
curl http://<your-domain.com>

六、综合示例

6.1 完整应用部署流程

# 1. 创建Namespace
kubectl create namespace my-app# 2. 创建Deployment
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:name: my-appnamespace: my-app
spec:replicas: 3selector:matchLabels:app: my-apptemplate:metadata:labels:app: my-appspec:containers:- name: my-appimage: nginx:1.21ports:- containerPort: 80
EOF# 3. 创建Service
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:name: my-appnamespace: my-app
spec:selector:app: my-appports:- protocol: TCPport: 80targetPort: 80type: ClusterIP
EOF# 4. 创建Ingress
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: my-appnamespace: my-appannotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:rules:- host: my-app.example.comhttp:paths:- path: /pathType: Prefixbackend:service:name: my-appport:number: 80
EOF# 5. 验证所有资源
kubectl get all,ingress -n my-app
http://www.dtcms.com/a/296604.html

相关文章:

  • 飞腾D3000PBF和UBOOT配置说明
  • Android15或AndroidU广播的发送流程
  • 阿里云ECS坑之dnf-makecache系统软件更新检测服务
  • Java面试宝典:Spring专题二
  • Access开发一键删除Excel指定工作表
  • Golang实现 - 实现只有表头的 Excel 模板,并在指定列添加了下拉框功能。生成的 Excel 文件在打开时,指定列的单元格会显示下拉选项
  • 笔记/使用Excel进行财务预测
  • 【超完整图文】在 Ubuntu 环境下安装 Qt Creator 4.7.0(较旧版本)
  • 亿级流量短剧平台架构演进:高并发场景下的微服务设计与性能调优
  • IP 证书全面解析:功能、类型与申请指南
  • 神经网络实战案例:用户情感分析模型
  • iview 部分用法
  • PyTorch常用Tensor形状变换函数详解
  • Spring中的循环依赖:解密、破局与架构启示
  • 第21章 常用的用户调查分析方法
  • 08 rk3568 模拟smi mdio RTL8367RB
  • 详解FreeRTOS开发过程(五)-- 系统内核控制函数及任务相关API函数
  • 遥感滑坡识别分割数据集labelme格式1893张1类别
  • 【java计算日期属于本月第几周通用方法】
  • 用生成模型解开视网膜图像的表征|文献速递-医学影像算法文献分享
  • 黄山派lvgl8学习笔记(3)导入陀螺仪传感器数据
  • 解决VSCode中“#include错误,请更新includePath“问题
  • 深度分析Java内存结构
  • 基础NLP | 01 机器学习 深度学习基础介绍
  • JavaScript 文件在页面渲染中的加载机制详解
  • CF每日5题(1500-1600)
  • Unity3D + VR头显 × RTSP|RTMP播放器:构建沉浸式远程诊疗系统的技术实践
  • Springboot宠物用品商城的设计与实现
  • 深入理解 eMMC RPMB 与 OP-TEE 在 Linux 系统中的应用开发
  • 云祺容灾备份系统AWS S3对象存储备份与恢复实操手册