zoukankan      html  css  js  c++  java
  • InitContainer

    InitContainer

    初始化容器

    在Pod中,容器是共享存储资源和网络资源的,所以Init Container容器产生的数据是可以被其他容器作用到的。初始化容器有点类似于postStart 钩子的作用,在Pod没有启动之前,为应用容器做一些准备工作,但是跟钩子的启动处于不同的阶段,在Pod生命周期图中可以明显看出。Pod中可以启动一个或多个容器,也可以有一个或多个initContainer 容器。

    initContainer 容器和普通容器几乎一样,除了一下两点:

    • 总是运行到完成
    • 必须在Pod启动下一个容器之前运行完成

    如果init容器启动失败,容器根据restartPolicy策略重启,如果有多个,它们会按顺序,一次执行一个,每个init容器都必须运行成功了才会运行下一个。init容器不支持readnessProbe,因为它们必须在Pod就绪前运行完成。

    initContainer能做什么?

    • 等他其他容器Ready: 比如web服务器需要等待mysql数据库服务器启动完成,可以启动一个initContainer 容器检测mysql服务启动,成功后退出,运行主容器
    • 环境构建:为主容器运行提供配置和初始化等操作。
    • 可以包含运行使用工具:处于安全考虑,在镜像中是不建议包含这些工具的。
    • 构建工具和代码:在镜像中没有,而服务需要用到的工具可以用init Container构建
    • 构建和部署分离: 不需要重新将两步打包成一个镜像
    • 提供一种阻塞容器启动的方式:必须在initContainer容器启动成功后,才会运行下一个容器,保证了一组条件运行成功的方式

    示例

    apiVersino: v1
    kind: Pod
    metadata:
      name: myapp
    spec:
      initContainers:
      - name: init-server
        image: busybox
        command: ["/bin/bash","-c","chown -R apache:apache /var/www"]
        volumeMount:
        - name: wwwroot
          mountPath: /var/www
      containers:
      - name: my-service
        image: httpd
        containerPort:
        - name: http
          port: 80
          protocol: TCP
        volumeMount:
        - name: wwwroot
          mountPath: /var/www
      volumes:
      - name: wwwroot
        hostPath:
          path: /home/wwwroot
    

    可以通过init Container修改主容器应用中的共享存储文件权限,让/var/www 路径成为apache用户和组拥有。这是我们利用initContainer起到的作用之一。

    官方文档示例

    apiVersion: v1
    kind: Pod
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: busybox
        command: ['sh', '-c', 'echo The app is running! && sleep 3600']
      initContainers:
      - name: init-myservice
        image: busybox
        command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
      - name: init-mydb
        image: busybox
        command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
    
  • 相关阅读:
    20190613 安装Anaconda
    20190613 一个SQL问题
    20190604 宠物引发的乱想
    windows下的vim打开文件乱码解决
    【题解】 「NOI2019」弹跳 KDT优化建图+最短路+剪枝+卡空间 LOJ3159
    【题解】 [AH/HNOI2017]单旋 LCT+splay
    【题解】 CF1400E Clear the Multiset 笛卡尔树+贪心+dp
    【题解】 CF1404C Fixed Point Removal 线段树+树状数组+带悔贪心
    【题解】 CF1251E2 Voting (Hard Version) 带悔贪心
    【题解】 CF1418G Three Occurrences hash+双指针+差分
  • 原文地址:https://www.cnblogs.com/h-gallop/p/11809606.html
Copyright © 2011-2022 走看看