k8s 共享内存的方案

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: xxx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xxx
  template:
    metadata:
      labels:
        app: xxx
    spec:
      containers:
      - args:
        - infinity
        command:
        - sleep
        image: centos:7
        name: centos
        resources:
          limits:
            memory: 256Mi
        volumeMounts:
        - mountPath: /dev/shm
          name: cache-volume
      volumes:
      - emptyDir:
          medium: Memory
          sizeLimit: 128Mi
        name: cache-volume

注意事项

  • 将memory介质的emptyDir挂载到/dev/shm/
  • 配置container的memory limit
  • 配置memory介质的emptyDir的sizeLimit为 memory limit的50%

上述配置可以在admission controller中自动为用户配置,用户不需要显式的“请求开启共享内存”。