▶︎
all
running...

简易沙盒系统设计

架构

infra

沙盒系统 架构基于 Kubernetes 设计,通过 CRD(CustomResourceDefinition) 实现沙盒的抽象对象,简化沙盒的管理流程,统一通过 k8s 的管理 API 来实现沙盒的“增删改查”

relationship

组件功能

Sandbox Controller (沙盒控制器)

Scheduler (调度器)

设计流程图

Controller

controller

Worker

Worker

Monitor (监控)

Kube Scale

Storage Controller (存储控制器)

Storage Policy Controller (存储策略控制器)

Life Cycle Controller (生命周期控制器)

Sandbox 对象

apiVersion: runtime.your_website/v1alpha1
kind: Sandbox
metadata:
  name: 'demo-sandbox' # 通过scheduler创建时会自动生成
  labels:
    puid: b91eb5a9-4bad-4edd-9365-57b4a4105ee8 # 冗余字段
  annotation:
    runtime.your_website/group-refix: pc
spec:
  type: python
  version: 1.0.15
  token: d352640a0b498a5510bbc4f136da4d2c # Auto Generate or sepecific
  network:
    type: https # http/https/tcp
  ttl: 3600 # set sandbox life time with second, '0' is keep alive (default:3600)
  resource:
    requests:
      cpu: 10m
      memory: 10Mi
    limits:
      cpu: 100m
      memory: 100Mi
    storage:
      type: normal # none/normal/fast (default:none)
      limit: 100Mi
      name: ff22d2c1-02d8-42ec-91df-e452e69cbc7e # Auto Generate or sepecific
  env:
    - name: FOO
      value: bar
  envFrom:
    - configMapRef:
      name: sandbox-scheduler
status:
  phase: Running

镜像仓库地址约定

registry.your_website/runtime/sandbox-{.spec.type}:{.spec.version}

存储类型(Storage Type)

渲染的 k8s 对象

apiVersion: apps/v1
kind: Deployment
metadata:
  name: '{sandbox.metadata.name}'
spec:
  selector:
    matchLabels:
      app: sandbox
      type: '{sandbox.spec.type}'
      token: '{sandbox.metadata.token}'
      # {sandbox.metadata.labels}
  replicas: 1
  template:
    metadata:
      labels:
        app: sandbox
        type: '{sandbox.spec.type}'
        token: '{sandbox.metadata.token}'
        # {sandbox.metadata.labels}
    spec:
      volumes:
      - name: user-volume
        persistentVolumeClaim:
          claimName: '{sandbox.metadata.storage}'
      containers:
        - name: sandbox
          image: registry.your_website/runtime/sandbox-{sandbox.spec.type}:{sandbox.spec.version}
          imagePullPolicy: Always
          securityContext:
            allowPrivilegeEscalation: false
          env:
            - name: USERID
              value: '10001'
            - name: PVC_NAME
              value: '{sandbox.metadata.storage}'
              # {sandbox.spec.env}
          command:
            - /entrypoint.sh
          ports:
          - containerPort: 8888
            protocol: TCP
          volumeMounts:
          - name: user-volume
            mountPath: /home/{sandbox.metadata.storage}
            readOnly: false
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: sandbox
    type: '{sandbox.spec.type}'
    token: '{sandbox.metadata.token}'
    # {sandbox.metadata.labels}
  name: '{sandbox.metadata.name}'
spec:
  ports:
  - name: 8888-tcp
    port: 8888
    protocol: TCP
    targetPort: 8888
  selector:
    app: sandbox
    type: '{sandbox.spec.type}'
    token: '{sandbox.metadata.token}'
    # {sandbox.metadata.labels}
  type: ClusterIP

Kong API 对象

{
  "name": "{sandbox.metadata.name}",
  "upstream_url": "http://{sandbox.metadata.name}.{sandbox.metadata.group}.svc:8888",
  "uris": [
    "/{sandbox.metadata.group}/{sandbox.metadata.name}"
  ],
  "strip_uri":                false,
  "upstream_connect_timeout": 10 * 1000,   // 10s
  "upstream_send_timeout":    3600 * 1000, // 1h
  "upstream_read_timeout":    3600 * 1000, // 1h
}

数据库

sandbox_meta

key type comment
suid varchar(8) 沙盒的ID
storage_type varchar(8) 存储类型
storage varchar(32) 存储的名称
user_id int(5) 沙盒内用户的id
group varchar(16) 沙盒所在的分组(命名空间)
type varchar(16) 沙盒类型
version varchar(16) 沙盒版本