Kubernetes共享挂载点问题

时间:2018-10-19 08:35:53

标签: nginx kubernetes

我有一个带有logrotate和nginx容器的Pod:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-log
  labels:
    app: nginx-log

spec:
  containers:

  - name: logrotate
    image: path/to/logrotate:latest
    volumeMounts:
    - name: logs
      mountPath: /var/log

  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /var/log/nginx

  volumes:
  - name: logs
    emptyDir: {}

我遇到的问题是nginx的mountPath是/ var / log还是/ var / log / nginx,logrotate容器总是在/ var / log /中看到nginx日志,而不是在/ var / log / nginx /中。是否可以强制nginx在emptyDir中创建nginx /文件夹并在那里登录?

2 个答案:

答案 0 :(得分:0)

我认为Init Containers是一个很好的用途,可以用来在Pod完全部署之前运行任务。

一个例子如下:

apiVersion: v1  
kind: Pod  
metadata:  
  name: nginx-log  
spec:  
  containers:

  - name: logrotate
    image: path/to/logrotate:latest
    volumeMounts:
    - name: logs
      mountPath: /var/log

  - name: nginx  
    image: nginx:latest  
    ports:
    - containerPort: 80    
    volumeMounts:  
    - name: logs  
      mountPath: /var/log/nginx  

  initContainers:  
  - name: logs  
    image: busybox  
    command: ["mkdir","-p","/var/log/nginx"]  
    volumeMounts:  
    - name: logs  
      mountPath: /var/log/nginx 

  volumes:
  - name: logs
    emptyDir: {}

您还可以阅读Using InitContainers to pre-populate Volume data in Kubernetes

答案 1 :(得分:0)

好的,我可以使用它

var url = $"https://developer.api.autodesk.com/oss/v2/buckets/{bucketKey}/objects/{objectName}";

        using (var httpClient = GetAuthorizedHttpClient("data:write"))
        {
            httpClient.Timeout = TimeSpan.FromMinutes(120);
            var request = new HttpRequestMessage(HttpMethod.Put, url);

            fileStream.Position = 0;

            request.Content = new StreamContent(fileStream);                

            var response = httpClient.SendAsync(request).Result;

            var responseContent = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
                throw new Exception($"Failed to upload object: {response.ReasonPhrase}");

            var result = JsonConvert.DeserializeObject<ObjectUploadResult>(response.Content.ReadAsStringAsync().Result);
            return result;
        }

Logrotate侧车取自:https://github.com/honestbee

在logrotate.tpl.conf中进行了少量修改:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-log
  labels:
    app: nginx-log

spec:
  containers:

  - name: logrotate
    image: path/to/image/logrotate:latest
    volumeMounts:
    - name: logs
      mountPath: /var/log

  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /var/log/nginx
      subPath: nginx

  volumes:
  - name: logs
    emptyDir: {}

和Dockerfile中的

${LOGROTATE_PATTERN1} 
${LOGROTATE_PATTERN2}
{
    rotate ${LOGROTATE_ROTATE}
    ${LOGROTATE_MODE}
    size ${LOGROTATE_SIZE}
    missingok
    nocompress
    su root root
 }