【k8s笔录】容器bash启动删除缓慢问题

64 阅读1分钟

问题:Pod容器启动命令使用/bin/sh时,删除非常缓慢。

原因:sh并没有接收SIGTERM信号并处理。

解决方法:

  • 即使在prestop中kill -9 1也无法退出,因为linux 1号进程会忽略SIGKILL信号;
  • 在prestop中kill 1也无效,因为sh并没有接收SIGTERM信号并处理;
  • 在sh命令中捕捉SIGTERM并退出即可解决。
    • trap 'echo trap SIGTERM $(date +"%F %T");exit' SIGTERM;

示例:test-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    k8s-app: test
  name: test
  namespace: default
spec:
  serviceName: test
  replicas: 1
  selector:
    matchLabels:
      k8s-app: test
  template:
    metadata:
      labels:
        k8s-app: test
    spec:
      nodeSelector:
#        test: "true"
#        kubernetes.io/hostname: xxx
      hostNetwork: true
      containers:
        - name: test
          image: busybox:latest
          command:
            - /bin/sh
          args:
            - -c
            - trap 'echo trap SIGTERM $(date +"%F %T");exit' SIGTERM; while true; do echo 'Hello, Kubernetes'; sleep 10; done