Skip to content

Instantly share code, notes, and snippets.

@carmark
Last active August 29, 2015 14:22
Show Gist options
  • Save carmark/dd333ac11084bd485d9a to your computer and use it in GitHub Desktop.
Save carmark/dd333ac11084bd485d9a to your computer and use it in GitHub Desktop.

The Yaml file support

Introduction

如下样例,是一个典型的Yaml格式的文件,存在于examples/ubuntu.yml

name: process-yaml-file-ubuntu
containers:
  - name:          ubuntu
    image:         ubuntu:latest
    workdir:       /
    command:
      - /bin/bash
resource:
    vcpu:          1
files:
volumes:
tty: true

要运行的话,就是给run或是create子命令加上-y选项,这样针对传入的文件(-p接收的)就可以按照Yaml格式转换。

root@ubuntu:/home/lei/src/hyper# ./hyper run --help
Usage:
  hyper run [OPTIONS] IMAGE [COMMAND] [ARG...]

create a pod, and launch a new VM to run the pod

Application Options:
  -p, --podfile=""       Create and Run a pod based on the pod file
  -k, --kubernetes=""    Create and Run a pod based on the kubernetes pod file
  -y, --yaml             Create a pod based on Yaml file
      --name=""          Assign a name to the container
      --attach           Attach the stdin, stdout and stderr to the container
      --workdir=""       Working directory inside the container
      --tty              Allocate a pseudo-TTY
      --cpu=1            CPU number for the VM
      --memory=128       Memory size (MB) for the VM
      --env=[]           Set environment variables
      --entrypoint=""    Overwrite the default ENTRYPOINT of the image
      --restart=""       Restart policy to apply when a container exits (never, onFailure, always)

Help Options:
  -h, --help             Show this help message

Test Cases

hyper run -y -p examples/ubuntu.yml

Kubernetes Compatability

Two new Status from Kubernetes

增加了两个新的状态,分别是failedsucceeded。这样我们的系统中就存在了四个状态:

  • pending
  • running
  • failed 如果pod内的所有container都运行完毕,有一个failed,那么pod的状态就为failed
  • succeeded 如果pod内的所有container都运行完毕,全部都成功,那么pod的状态就为succeeded

针对Kubernetes类型的pod,在得到VM退出的事件后,判定pod的状态(根据pod内所有container的状态),根据restartpolicy来决定pod的行为。

  • 如果pod状态为succeeded,在restartpolicy为always时,删除之前的pod,重新启动该pod,pod重新进入running状态;当restartpolicy不是always时,那么就是succeeded状态

  • 如果pod状态为failed,在restartpolicy不为never时,删除之前的pod,重新启动该pod;当restartpolicy是never时,pod的状态就是failed状态

当这两种状态出现时,就要区分pod的类型,如果是针对Kubernetes,那么就会删除这个pod(不论pod的状态是succeeded还是failed);如果不是,那么就保留这种状态。

Test cases

1 运行一个会成功的kubernetes类型的pod

examples/kubernetes_ubuntu_restart.pod 是一个测试restart policy的kubernetes pod模板。这个pod会成功的被运行,这段时间显示为running状态,等pod运行完毕,将会被删除。 hyper list将不会显示pod的信息。

{
  "apiVersion": "V1beta1",
  "kind": "Pod",
  "metadata":{
     "name":"test-k8s-restartPolicy",
     "labels":{
        "name":"ubuntu"
     }
  },
  "spec": {
      "version": "V1beta1",
      "id": "Ubuntu-Pod",
      "containers": [
        {
          "name": "Ubuntu-1",
          "image": "ubuntu",
          "args": ["sleep", "100"]
        }
      ],
      "restartPolicy": "Never"
  }
}

2 运行一个会失败的kubernetes类型的pod

修改examples/kubernetes_ubuntu_restart.pod中得args参数改为一个不合法的命令,那么container就会返回错误。这个pod会成功的被运行,这段时间显示为running状态,等pod运行完毕,pod状态为failed,并且将会被删除。 hyper list将不会显示pod的信息。

{
  "apiVersion": "V1beta1",
  "kind": "Pod",
  "metadata":{
     "name":"test-k8s-restartPolicy",
     "labels":{
        "name":"ubuntu"
     }
  },
  "spec": {
      "version": "V1beta1",
      "id": "Ubuntu-Pod",
      "containers": [
        {
          "name": "Ubuntu-1",
          "image": "ubuntu",
          "args": ["sleep 100"]
        }
      ],
      "restartPolicy": "Never"
  }
}

3 延长container命令运行时间,在命令运行时,给hyperdSIGHUP事件,再次重启

在#1中得测试用例运行阶段,给hyperd发送SIGHUB事件,并再次重启hyperd

重启后,该kubernetes会重新执行pod,状态为running,运行完成后会推出。

4 延长container命令运行时间,在命令运行时,给hyperdSIGTERM事件,再次重启

在#1中得测试用例运行阶段,给hyperd发送SIGHUB事件,并再次重启hyperd

重启后,该pod不会重新被运行。

5 针对一般的pod的restart policy,我没有测试,这个在init中完成,外部很难看到状态,所以pod将一直会是running状态。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment