Skip to content
This repository has been archived by the owner on Oct 21, 2019. It is now read-only.

postStart preStop pod hook in lxcfs-daemonset.yaml to remount_lxcfs Does it work? #10

Open
JaeGerW2016 opened this issue Jun 28, 2019 · 3 comments

Comments

@JaeGerW2016
Copy link

JaeGerW2016 commented Jun 28, 2019

spec:
 ...
 template:
 ...
   spec:
   ... 
     containers:
     - name: lxcfs
       image: registry.cn-hangzhou.aliyuncs.com/denverdino/lxcfs:3.0.4
       imagePullPolicy: IfNotPresent
       securityContext:
         privileged: true
         capabilities:
           add:
             - SYS_ADMIN
       lifecycle:
         postStart:
             exec:
                command:
                - /bin/sh
                - /usr/local/bin/container_remount_lxcfs.sh
         preStop:
            exec:
               command:
               - /bin/fusermount -u /var/lib/lxcfs          
...

container_remount_lxcfs.sh

#!/bin/bash

LXCFS="/var/lib/lxcfs"

containers=$(docker ps | awk '{print $2}' | grep -v Name)
for container in $containers;do
        echo "remount $container"
        PID=$(docker inspect $container | grep Pid | awk '{print $2}' | awk -F ',' '{print $1}' )
        for file in meminfo cpuinfo stat uptime swaps diskstats;do
                nsenter --target $PID --mount --  mount -B "$LXCFS/proc/$file" "/proc/$file"
        done
 done
@JaeGerW2016 JaeGerW2016 changed the title postStart pod hook in lxcfs-daemonset.yaml to remount_lxcfs Does it work? postStart preStop pod hook in lxcfs-daemonset.yaml to remount_lxcfs Does it work? Jun 28, 2019
@denverdino
Copy link
Owner

Thanks for suggestion, it looks good, can you provide a PR for that.

@JaeGerW2016
Copy link
Author

@denverdino I tested the prestop and postStart hooks in my cluster to run the script. It has not been successful. I don't know what caused it.

@JaeGerW2016
Copy link
Author

JaeGerW2016 commented Jul 1, 2019

These are my yamls file contents
lxcfs-daemonset.yaml

apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
  name: lxcfs
  labels:
    app: lxcfs
spec:
  selector:
    matchLabels:
      app: lxcfs
  template:
    metadata:
      labels:
        app: lxcfs
    spec:
      hostPID: true
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: lxcfs
        image: 314315960/lxcfs-without-cleanup:3.0.4
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
          capabilities:
            add:
              - SYS_ADMIN
        lifecycle:
          postStart:
            exec:
              command: ["/usr/local/container_remount_lxcfs.sh"]
          preStop:
            exec:
              command: ["/usr/local/cleanup.sh"]
        volumeMounts:
        - name: cgroup
          mountPath: /sys/fs/cgroup
        - name: lxcfs
          mountPath: /var/lib/lxcfs
          mountPropagation: Bidirectional
        - name: usr-local
          mountPath: /usr/local
        - name: fusermount
          mountPath: /bin/fusermount
        - name: poststart
          mountPath: /usr/local/container_remount_lxcfs.sh
          subPath: container_remount_lxcfs.sh
        - name: prestop
          mountPath: /usr/local/cleanup.sh
          subPath: cleanup.sh
      volumes:
      - name: cgroup
        hostPath:
          path: /sys/fs/cgroup
      - name: usr-local
        hostPath:
          path: /usr/local
      - name: lxcfs
        hostPath:
          path: /var/lib/lxcfs
          type: DirectoryOrCreate
      - name: fusermount
        hostPath:
          path: /bin/fusermount
      - name: poststart
        configMap:
          name: remount-lxcfs
          defaultMode: 0755
          items:
          - key: container_remount_lxcfs.sh
            path: container_remount_lxcfs.sh
      - name: prestop
        configMap:
          name: remount-lxcfs
          defaultMode: 0755
          items:
          - key: cleanup.sh
            path: cleanup.sh

lxcfs-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: remount-lxcfs
data:
  container_remount_lxcfs.sh: |
    #!/bin/bash
    LXCFS="/var/lib/lxcfs"
    containers=$(docker ps | awk '{print $2}' | grep -v Name)
    for container in $containers;do
        echo "remount $container"
        PID=$(docker inspect $container | grep Pid | awk '{print $2}' | awk -F ',' '{print $1}' )
        for file in meminfo cpuinfo stat uptime swaps diskstats;do
                nsenter --target $PID --mount --  mount -B "$LXCFS/proc/$file" "/proc/$file"
        done
    done
  cleanup.sh: |
    #!/bin/bash
    nsenter -m/proc/1/ns/mnt fusermount -u /var/lib/lxcfs 2> /dev/null || true
    nsenter -m/proc/1/ns/mnt [ -L /etc/mtab ] || \
               sed -i "/^lxcfs \/var\/lib\/lxcfs fuse.lxcfs/d" /etc/mtab

Migrate the cleanup part of start.sh in the lxc-image directory to the cleanup.sh preStop hook script.

[root@localhost lxcfs-image]# diff start.sh start-default.sh 
3,4c3,7
< # Prepare
< mkdir -p /usr/local/lib/lxcfs /var/lib/lxcfs
---
> # Cleanup
> nsenter -m/proc/1/ns/mnt fusermount -u /var/lib/lxcfs 2> /dev/null || true
> nsenter -m/proc/1/ns/mnt [ -L /etc/mtab ] || \
>         sed -i "/^lxcfs \/var\/lib\/lxcfs fuse.lxcfs/d" /etc/mtab
> 
8a12,14
> 
> # Prepare
> mkdir -p /usr/local/lib/lxcfs /var/lib/lxcfs

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants