k8spod管理
·
1、命名空间管理
命名空间用于集群资源隔离,k8s 内置 default、kube‑system、kube‑public、kube‑node‑lease 等 ns。
# 查看命名空间
kubectl get namespaces
# 创建命名空间
kubectl create namespace timinglee
# 删除命名空间(会删除ns内所有资源)
kubectl delete namespaces timinglee
2、命令式 Pod 基础操作
# 查看pod,-o wide 显示节点、PodIP
kubectl get pods -o wide
# 命令式创建pod
kubectl run lee --image nginx:latest
# 查看pod详细事件(排错核心,ImagePullBackOff镜像拉取失败看这里)
kubectl describe pods error
# 删除单个pod
kubectl delete pods error
# 删除当前命名空间所有pod
kubectl delete pods --all
3、kubectl 常用实操命令
#create:命令式创建deployment
kubectl create deployment webcluster --replicas 2 --image myapp:v1
#edit:直接编辑在线资源yaml
kubectl edit deployments.apps webcluster
#patch:json片段修改资源
kubectl patch deployments.apps webcluster -p '{"spec":{"replicas":1}}'
#expose:创建service暴露应用
kubectl expose deployment webcluster --port 80 --target-port 80
#logs:查看pod容器日志
kubectl logs pods/webcluster-77c87d9946-gh9v7
#attach:连接容器标准输入输出
kubectl attach pods/testpod -it
#exec:进入容器执行命令(最常用)
kubectl exec -it pods/testpod -c testpod -- /bin/bash
#cp:宿主机和pod之间拷贝文件
kubectl cp testpod:/usr/share/nginx/html/index.html /mnt/test
kubectl cp /mnt/index.html testpod:/usr/share/nginx/html/index.html
#rollout:版本更新、重启、历史
kubectl rollout status deployment webcluster
kubectl rollout restart deployment webcluster
kubectl rollout history deployment webcluster
#scale:扩缩副本数
kubectl scale deployment webcluster --replicas 4
#label:增删标签,`key-`代表删除标签
kubectl label pods webcluster-9787d97f6-zl6q2 app-
kubectl label pods webcluster-9787d97f6-zl6q2 app=webcluster
4、YAML 声明式编写 Pod
Pod 多容器
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
containers:
- image: myapp:v1
name: myapp1
- image: busyboxplus:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
hostPort 端口映射
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
containers:
- image: myapp:v1
name: myapp1
ports:
- name: http
containerPort: 80 #容器内部端口
hostPort: 80 #宿主机节点端口
protocol: TCP
env 环境变量注入
apiVersion: v1
kind: Pod
metadata:
labels:
run: mysql
name: mysql
spec:
containers:
- image: mysql:8.0
name: mysql8
env:
- name: MYSQL_ROOT_PASSWORD
value: lee
nodeSelector 指定调度节点
apiVersion: v1
kind: Pod
metadata:
labels:
run: mysql
name: mysql
spec:
nodeSelector:
kubernetes.io/hostname: k8s-node2
containers:
- image: mysql:8.0
name: mysql8
hostNetwork 共享宿主机网络栈
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
5、Pod QoS 资源服务质量等级
BestEffort:不配置 requests/limits,优先级最低
resources: {} #不写resources字段
Burstable:requests < limits,优先级中等
resources:
limits:
cpu: 700m
memory: 200M
requests:
cpu: 500m
memory: 100M
Guaranteed:requests 与 limits 完全相等,优先级最高
resources:
limits:
cpu: 500m
memory: 100M
requests:
cpu: 500m
memory: 100M
6、restartPolicy 容器重启策略(Pod 级别)
Always:无论容器怎么退出,总是重启(deployment 默认)
OnFailure:容器异常退出(返回码≠0)才重启;正常退出不重启
Never:无论退出状态,永远不重启(Job 常用)
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
restartPolicy: OnFailure
containers:
- image: busybox:latest
name: busybox
command: ["/bin/sh","-c","sleep 30"]
7、Pod 生命周期
init 容器
initContainers:初始化容器,必须全部成功执行完毕后,才启动业务容器,常用于等待依赖、初始化配置。
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
initContainers:
- name: busybox
image: busybox:latest
command:
- /bin/sh
- -c
- "until test -e /testfile;do echo wating for myservice; sleep 2;done"
containers:
- image: myapp:v1
name: webserver
restartPolicy: Always
livenessProbe 存活探针
检测容器是否正常运行;探测失败,k8s重启容器。支持 tcpSocket、httpGet、exec。
containers:
- image: myapp:v1
name: testpod
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3 #启动后多久第一次探测
periodSeconds: 1 #探测间隔
timeoutSeconds: 1 #探测超时
readinessProbe 就绪探针
检测业务是否就绪;探测失败,不会重启容器,会把 pod 从 service 的 endpoint 摘除,不再接收流量。
containers:
- image: myapp:v1
name: webserver
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 3
periodSeconds: 2
timeoutSeconds: 1
8、Deployment 版本升级与回退
#升级镜像
kubectl set image deployments webcluster myapp=myapp:v2
#记录更新说明,写入revision
kubectl annotate deployment webcluster kubernetes.io/change-cause="myappv2" --overwrite
#查看版本历史
kubectl rollout history deployment webcluster
#回退到指定版本
kubectl rollout undo deployment webcluster --to-revision=1
#暂停更新
kubectl rollout pause deployment webcluster
#恢复更新
kubectl rollout resume deployment webcluster
更多推荐

所有评论(0)