K8S鈎子操作


簡介

我們知道,K8S可以在應用容器啟動之前先執行一些預定義的操作,比如事先生成一些數據,以便於應用容器在啟動的時候使用。這種方式可以通過init container技術實現,具體可以參考《Kubernetes init container》

那么事實上,在實際生產中,還有一種需求,就是我們需要在應用容器啟動后執行一些初始化操作,比如設置容器的dns參數等,說到這里就不得不多提一句,k8s到目前為止尚不支持通過為kubelet添加參數的方式為應用容器設置dns的options。事實上我們在生產中之所以使用到本篇文檔所說的這種鈎子,就是為了在應用容器啟動后為其設置一個dns的options。

除了為容器添加啟動后的鈎子之外,還可以為容器添加銷毀之前的鈎子。

配置

定義啟動后和銷毀前鈎子示例:

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"]

下面是一個在啟動后執行多條指令的示例:

apiVersion: v1
kind: Pod
metadata:
  name: example-app
spec:
  containers:
    - name: example-app
      image: hub.breezey.top/library/example-app:v1.0
      imagePullPolicy: Always
      lifecycle:
        postStart:
          exec:
            command:
              - "sh"
              - "-c"
              - >
                /bin/echo '123456' > /data/apps/rsync.pwd;
                chmod 400 /data/apps/rsync.pwd;
                /usr/bin/rsync -avzLu --password-file=/data/apps/rsync.pwd www@$(RSYNC_SERVER)::apps/example-app /data/apps/ > /dev/stdout 2>&1;
      env:
        - name: RSYNC_SERVER
          value: 192.168.0.100


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM