Kubernetes如何在挂载了卷(nfs)之后才运行容器

3
官方的nginx镜像由于我正在从nfs卷中挂载配置文件而无法启动。我正在尝试使用如下的bash脚本来解决这个问题,但是它并没有起作用。有什么建议吗?kubectl logs conteinerxxx -p返回nginx: invalid option: "off"。但是nginx -g "daemon off;"在我的shell上似乎运行良好。有更加惯用的方法吗?顺便说一下,这是DigitalOcean上的coreos集群。

Dockerfile

//REPLACE THE OLD `CMD ["nginx", "-g", "daemon off;"]`
ADD nginxinit.sh /nginxinit.sh
ENTRYPOINT ["./nginxinit.sh"]

bash

#!/usr/bin/env bash
until mountpoint -q /etc/nginx; do
    echo "$(date) - wainting for NGINX config files to be mounted..."
    sleep 1
done

nginx -g "daemon off;"

RC

apiVersion: v1
kind: ReplicationController
metadata:
  name: mypod
  labels:
    name: mypod
spec:
  replicas: 1
  selector:
    name: mypod
  template:
    metadata:
      labels:
        name: mypod
    spec:
      containers:
        - image: cescoferraro/nginx
          name: myfrontend
          volumeMounts:
          - mountPath: "/etc/nginx"
            name: nginx-nfs
      volumes:
        - name: nginx-nfs
          persistentVolumeClaim:
            claimName: nginx-nfs

PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-nfs
spec:
  capacity:
    storage: 32Mi
  accessModes:
    - ReadWriteMany
  nfs:
    # FIXME: use the right IP
    server: x.x.x.x
    path: "/nginx"

PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 32Mi

既然您正在从外部卷挂载配置文件,将“daemon off”部分放入配置文件中是否更容易呢?或者之后再添加它,例如:echo "daemon off;" >> /etc/nginx/nginx.conf - AdelaN
是的!我相信这个问题可以用Bash解决,尽管我没有花太多精力去解决它。我在想是否有一种Kubernetes的方式来处理这种情况。我将尝试在nginx配置文件中使用daemon off。目前,我正在使用一个nginx配置文件,我将其“docker cp”到我的nfs卷中。 - CESCO
1
嗨@CESCO,Pod不应该在挂载卷之前启动--如果是这样,那就是一个错误。您能多久重现此问题? - Paul Morie
@PaulMorie 我复制nginx文件的方式有问题。 "docker cp containerxxx:/etc/nginx/* /tmp" 没有复制conf.d目录(我太蠢了)。所以我修复了它,并且我的镜像可以正常工作了。我以为它会在官方镜像中工作,但实际上并没有。使用nginx镜像似乎可以重现这个问题。 - CESCO
1个回答

0
最好将配置放入configMap中。
      volumeMounts:
        - name: nfsvol
          mountPath: /srv/nfs/share
        - name: config-volume
          mountPath: /etc/nginx
  volumes:
    - name: nfsvol
      persistentVolumeClaim:
        claimName: nfs-pvc
    - name: config-volume
      projected:
        sources:
        - configMap:
            name: core-nginx-config
            items:
             - key: fastcgi_params
               path: fastcgi_params
             - key: mime.types
               path: mime.types
             - key: nginx.conf
               path: nginx.conf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接