# 钩子

  • 容器启动后钩子
  • 容器终止前钩子

# 钩子定义方法

  • command
  • tcp
  • httpGet
  lifecycle:
    postStart:
      exec:
        command:
        - cat
        - /tmp/helthy
---
  lifecyle:
    postStart:
      tcpSocket:
        port: 8080
---
  lifecyle:
    postStart:
      httpGet:
        path: /
        port: 80
        host: 192.168.0.1
        scheme: HTTP # http 或https
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 钩子应用案例

apiVersion: v1
kind: pod
metadata:
  name: pod-hook-exec
  namespace: dev
spec:
  containers:
  - name: manin-container
    image: nginx:1.17.1
    port:
    - name: nginx-port
      containerPort: 80
    lifecycle:
      postStart:
        exec:
          # 启动nginx时修改nginx首页
          command: ['/bin/sh', '-c', 'echo postStart... > /user/share/nginx/html/index.html']
      preStop:
        exec:
          # 退出容器前执行nginx退出命令
          command: ['/usr/sbin/nginx', '-s', 'quit']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21