Skip to content

Instantly share code, notes, and snippets.

@morsoli
Last active April 1, 2021 03:37
Show Gist options
  • Select an option

  • Save morsoli/683bd5aadaaafe62a107bed0840f74c9 to your computer and use it in GitHub Desktop.

Select an option

Save morsoli/683bd5aadaaafe62a107bed0840f74c9 to your computer and use it in GitHub Desktop.

常用命令

  1. 查看指定pod的日志
kubectl logs <pod_name>
kubectl logs -f <pod_name> #类似tail -f的方式查看(tail -f 实时查看日志文件 tail -f 日志文件log)
kubectl get pod ...
kubectl describe pod ...
kubectl edit deploy ...
  1. 查看指定pod中指定容器的日志
kubectl logs <pod_name> -c <container_name>
# 查看Docker容器日志
docker logs <container_id>
  1. 强制删除pod
  • 问题:kubelet delete pod之后总处于Terminating,无法移除
  • 解决:加参数 --force --grace-period=0,grace-period表示过渡存活期,默认30s,在删除POD之前允许POD慢慢终止其上的容器进程,从而优雅退出,0表示立即终止POD kubectl delete po <your-pod-name> -n <name-space> --force --grace-period=0
  1. 批量删除pod

kubectl get pod -n <name-space>| grep Terminating| awk '{print $1}' | xargs kubectl delete pod -n <name-space> --force --grace-period=0

  1. 重启pod

kubectl get pod pod_name -n <name-space> -o yaml | kubectl replace --force -f -

  1. pod环境开发
#!/bin/sh
pod_1=`kubectl get pod -n <name-space> | grep pod_name | grep Running | awk -F ' ' '{print $1}'`
pod_2=`kubectl get pod -n <name-space> | grep pod_name | grep Running | awk -F ' ' '{print $1}'`

for pod_name in $pod_1 $pod_2
do
    kubectl cp /root/ ${pod_name}:/root/ -n <name-space>
    kubectl exec -it $pod_name [执行pod server] -n <name-space>
done
  1. Kubernetes创建pod一直处于ContainerCreating解决方案
# 命令查看pod详情,会发现有错误:
kubectl describe

进阶使用

  1. 将 Pod 分配给节点 推荐的方法都是用标签选择器来进行选择
kubectl label nodes <node-name> <label-key>=<label-value>
kubectl get nodes --show-labels
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    label-key: label-value

kubectl get pods -o wide 并查看分配给 pod 的 “NODE” 来验证其是否有效

  1. 污点和容忍度 key和value字段来自要改变调度状态的 Pod 的属性
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  tolerations:
  - key: "example-key"
    operator: "Exists"
    effect: "NoSchedule"

3.Pod 的生命周期

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment