Init container容器Volume文件复制脚本(共享存储)

Deprecated


1、由于卷的挂载是直接在容器当前目录之上的,即容器原本目录下文件被隐藏,等umount才可见,因此,如果不希望发生把原本目录下的文件也影藏掉,需要通过复制容器,两个容器共享存储,然后通过向辅助容器挂载volume,然后通过这个容器,把新挂载的文件copy到共享目录,完成文件替换。





2、复制文件的脚本(/volume,/share为辅助容器下目录,即使和应用容器同名也不会影响)    。。。其实简单的一句  copy /volume/* /share 就够了(最初考虑通过参数传入两个目录)


#!/bin/sh
vs="volume is empty!"
ss="sharedir is empty!"
volume="/volume"
share="/share"
#get the file from the volume and copy all of them to the share directory
set -e
echo ">volume path:"$volume
echo ">shareDir:"$share
echo "-----------"
if [ ! -z "$volume" ];then
 if [ ! -d "$volume" ];then
  echo "  [$volume] is't a directory,exit ..."
  exit 0
 fi
else
 echo "  no volume assigned,exit ..."
 exit 0
fi
if [ ! -z "$share" ];then
 if [ ! -d "$share" ];then
  echo "  [$share] isn't a directory,exit ..."
  exit 0
 fi
else
 echo "  no sharedir assigned,exit ..."
 exit 0
fi
#copying start
echo ">>$volume before copy:"
ls $volume
echo ">>$share before copy"
ls $share
cp $volume/* $share/
echo "------------"
if [ $? -eq 0 ];then
 echo ">copy success!"
 echo ">> $share after copy:"

3、Kubernetes1.3版本init container的初始化容器方式还处于alpha阶段,需要荣国annotation的形式生命在pod中,备份yaml文件如下:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-init
  annotations:
    pod.alpha.kubernetes.io/init-containers: '[
        {
            "name": "install",
            "image": "{repository}/library/initcontainer:1.1",
            "command": ["/bin/sh", "-c", "/run.sh /share /work-dir"],
            "volumeMounts": [
                {
                    "name": "volume",
                    "mountPath": "/volume"
                },
                {
                     "name":"workdir",
                     "mountPath":"/share"
                }
            ]
        }
    ]'
spec:
  containers:
  - name: nginx
    image: {repository}/library/nginx:1.9.14-alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - name: workdir
      mountPath: /usr/share/nginx/daata
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}
  - name : volume
    hostPath:
     path: /home/mypace
/hostpath

4、辅助容器Dockerfile备份(from busybox,该镜像比较小,但是能够执行基本的sh命令)

FROM {repository}/library/busybox:1.24

COPY run.sh /
RUN chmod +x /run.sh

CMD ["/bin/sh", "-c", "/run.sh"]
5、如何运行多进程的容器
while [[ true ]]; do
sleep 1
done
一种方法是使用 Shell脚本 ,另一种方法是使用进程管理工具 Supervisor 详细

你可能感兴趣的:(k8s,docker)