一、initcontainers
initcontainer(初始化容器)是Kubernetes中的一种特殊容器,它主要是用来初始化或者准备一些特定的环境,以便在主容器运行之前进行配置。initcontainer与主容器运行在同一个Pod中,但它会在主容器运行之前被启动。
initcontainer与主容器共享同一个Volume和Pod的网络命名空间。
二、initcontainers spark operator
使用Spark Operator时,经常需要在Spark应用程序运行之前,准备一些环境,比如将JAR文件下载到容器内部。这时候就可以使用initcontainers。下面是部署Spark Operator时使用initcontainers的代码示例:
apiVersion: "sparkoperator.k8s.io/v1beta2" kind: SparkApplication metadata: name: pi spec: type: Scala mode: cluster image: "gcr.io/spark-operator/spark:v3.1.1" imagePullPolicy: Always mainClass: org.apache.spark.examples.SparkPi mainApplicationFile: "local:///opt/spark/examples/jars/spark-examples_2.12-3.1.1.jar" sparkVersion: "3.1.1" restartPolicy: type: OnFailure onFailureRetries: 3 onFailureRetryInterval: 10 onSubmissionFailureRetries: 5 onSubmissionFailureRetryInterval: 20 driver: cores: 1 coreLimit: "1200m" memory: "512m" labels: version: 3.1.1 serviceAccount: spark InitContainers: - name: download-files image: busybox command: ['sh', '-c', 'wget http://path/to/files.tgz -O - | tar xz -C /'] executor: cores: 1 instances: 1 memory: "512m" labels: version: 3.1.1 InitContainers: - name: download-files image: busybox command: ['sh', '-c', 'wget http://path/to/files.tgz -O - | tar xz -C /']
三、initcontainer挂载持久化
initcontainers可以挂载configMap或者secrets,以获取在主容器中需要使用到的配置信息或者机密数据。
下面是一个挂载configMap的initcontainers的示例:
spec: containers: - image: nginx name: nginx volumeMounts: - mountPath: /etc/nginx/nginx.conf name: config-volume subPath: nginx.conf initContainers: - name: config-nginx image: busybox command: ['sh', '-c', 'echo "server {\n listen 80;\n}" > /data/nginx.conf'] volumeMounts: - mountPath: /data name: config-volume volumes: - name: config-volume configMap: name: nginx-config
四、initcontainer k8s
initcontainers支持Kubernetes中大多数的存储类型,比如emptyDir、configMap、secret和persistentVolumeClaim等。下面是一个使用emptyDir的示例:
spec: volumes: - name: shared-logs emptyDir: {} initContainers: - name: create-volume-dir image: busybox command: ['sh', '-c', 'mkdir /var/shared-logs'] volumeMounts: - mountPath: /var/shared-logs name: shared-logs containers: - image: nginx name: nginx volumeMounts: - mountPath: /var/log/nginx name: shared-logs
五、initcontainer telnet
可以使用telnet来测试容器之间的网络连接。下面是使用CentOS作为示例进行测试:
spec: containers: - name: web image: nginx - name: net image: centos command: - "/bin/sh" - "-c" - "yum install -y telnet && echo telnet nginx 80 && telnet nginx 80"
六、initcontainers怎么更新
initcontainers的更新方式与普通容器类似,可以通过Kubernetes的kubectl命令或者Deployment进行更新。
下面是一个使用Deployment进行更新initcontainers的示例:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: initContainers: - name: init1 image: busybox command: ['sh', '-c', 'echo 123 > /tmp/test'] volumeMounts: - name: data mountPath: /tmp containers: - name: nginx image: nginx ports: - containerPort: 80 name: http volumeMounts: - name: data volumes: - name: data emptyDir: {}
七、initcontainer重启
initcontainer与主容器同生共死,它们共用一个Pod,如果主容器需要重启,那么initcontainer也会一起重启。
八、initcontainers skywalking
可以使用SkyWalking对initcontainer进行监控。
下面是一个使用SkyWalking监控initcontainer的示例:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx annotations: "skywalking/nginx": "true" "skywalking/init": "true" spec: initContainers: - name: init1 image: busybox command: ['sh', '-c', 'echo 123 > /tmp/test'] volumeMounts: - name: data mountPath: /tmp containers: - name: nginx image: nginx ports: - containerPort: 80 name: http volumeMounts: - name: data volumes: - name: data emptyDir: {}
九、initcontainer 检测端口
可以使用curl或者nc等工具来检测端口的可用性。
下面是一个使用nc检测端口的initcontainers示例:
spec: initContainers: - name: wait-for-etcd image: centos command: ['sh', '-c', 'until nc -z etcd 2379; do sleep 1; done;'] containers: - name: my-container image: my-image
总结
通过此篇文章,我们对initcontainer有了更深入的理解。initcontainers可以在主容器运行之前,进行一些初始化或者准备工作,比如获取configMap、secrets,挂载卷、检测端口等。同时,initcontainers也可以使用Spark Operator进行部署,并且支持Kubernetes中的多种存储类型。