K8S鉤子操做

簡介

咱們知道,K8S能夠在應用容器啓動以前先執行一些預約義的操做,好比事先生成一些數據,以便於應用容器在啓動的時候使用。這種方式能夠經過init container技術實現,具體能夠參考《Kubernetes init container》html

那麼事實上,在實際生產中,還有一種需求,就是咱們須要在應用容器啓動後執行一些初始化操做,好比設置容器的dns參數等,說到這裏就不得很少提一句,k8s到目前爲止尚不支持經過爲kubelet添加參數的方式爲應用容器設置dns的options。事實上咱們在生產中之因此使用到本篇文檔所說的這種鉤子,就是爲了在應用容器啓動後爲其設置一個dns的options。nginx

除了爲容器添加啓動後的鉤子以外,還能夠爲容器添加銷燬以前的鉤子。api

配置

定義啓動後和銷燬前鉤子示例:post

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]

下面是一個在啓動後執行多條指令的示例:ui

apiVersion: v1
kind: Pod
metadata:
  name: dyland-lua-api-pre
spec:
  containers:
    - name: dyland-lua-api-pre
      image: hub.dz11.com/op-base/openresty:1.11.2.4
      imagePullPolicy: Always
      lifecycle:
        postStart:
          exec:
            command:
              - "sh"
              - "-c"
              - >
                /bin/echo 'options single-request-reopen' >> /etc/resolv.conf;
                /bin/echo '123456' > /home/www/server/rsync.pwd;
                chmod 400 /home/www/server/rsync.pwd;
                /usr/bin/rsync -avzLu --password-file=/home/www/server/rsync.pwd www@$(RSYNC_SERVER)::pre-lua/dyland-lua-api.pre.wh03 /home/www/server/ > /dev/stdout 2>&1;
      env:
        - name: RSYNC_SERVER
          value: 10.1.51.109
相關文章
相關標籤/搜索