ARTICLE DETAIL

资讯详情

深耕网站SEO优化与搜索引擎排名提升的一线实战洞察。

k8spod管理

k8spod管理 1、命名空间管理命名空间用于集群资源隔离k8s 内置 default、kube‑system、kube‑public、kube‑node‑lease 等 ns。# 查看命名空间 kubectl get namespaces # 创建命名空间 kubectl create namespace timinglee # 删除命名空间会删除ns内所有资源 kubectl delete namespaces timinglee2、命令式 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 --all3、kubectl 常用实操命令#create命令式创建deployment kubectl create deployment webcluster --replicas 2 --image myapp:v1 #edit直接编辑在线资源yaml kubectl edit deployments.apps webcluster #patchjson片段修改资源 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 appwebcluster4、YAML 声明式编写 PodPod 多容器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 10000hostPort 端口映射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: TCPenv 环境变量注入apiVersion: v1 kind: Pod metadata: labels: run: mysql name: mysql spec: containers: - image: mysql:8.0 name: mysql8 env: - name: MYSQL_ROOT_PASSWORD value: leenodeSelector 指定调度节点apiVersion: v1 kind: Pod metadata: labels: run: mysql name: mysql spec: nodeSelector: kubernetes.io/hostname: k8s-node2 containers: - image: mysql:8.0 name: mysql8hostNetwork 共享宿主机网络栈apiVersion: v1 kind: Pod metadata: labels: run: testpod name: testpod spec: hostNetwork: true containers: - image: busybox:latest name: busybox command: - /bin/sh - -c - sleep 100005、Pod QoS 资源服务质量等级BestEffort不配置 requests/limits优先级最低resources: {} #不写resources字段Burstablerequests limits优先级中等resources: limits: cpu: 700m memory: 200M requests: cpu: 500m memory: 100MGuaranteedrequests 与 limits 完全相等优先级最高resources: limits: cpu: 500m memory: 100M requests: cpu: 500m memory: 100M6、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: AlwayslivenessProbe 存活探针检测容器是否正常运行探测失败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: 18、Deployment 版本升级与回退#升级镜像 kubectl set image deployments webcluster myappmyapp:v2 #记录更新说明写入revision kubectl annotate deployment webcluster kubernetes.io/change-causemyappv2 --overwrite #查看版本历史 kubectl rollout history deployment webcluster #回退到指定版本 kubectl rollout undo deployment webcluster --to-revision1 #暂停更新 kubectl rollout pause deployment webcluster #恢复更新 kubectl rollout resume deployment webcluster
返回列表