沙盒系统
架构基于 Kubernetes
设计,通过 CRD(CustomResourceDefinition
) 实现沙盒的抽象对象,简化沙盒的管理流程,统一通过 k8s 的管理 API 来实现沙盒的“增删改查”
Kubernetes
控制器,用于监听 Sandbox
对象的变化Sandbox
对象的变化来控制相应的 k8s 组件,包括Deployment
、Service
Kong
网关增加或者删除对应沙盒的路由Sandbox Controller
的 Web 接口Storage Policy Controller
申请存储空间,确定沙盒所在的命名空间Sandbox
对象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}
GlusterFS
Ceph
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
{ "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 }
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) | 沙盒版本 |