如下样例,是一个典型的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
hyper run -y -p examples/ubuntu.yml
增加了两个新的状态,分别是failed
和succeeded
。这样我们的系统中就存在了四个状态:
- 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
);如果不是,那么就保留这种状态。
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"
}
}
修改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"
}
}
在#1中得测试用例运行阶段,给hyperd
发送SIGHUB
事件,并再次重启hyperd
。
重启后,该kubernetes会重新执行pod,状态为running,运行完成后会推出。
在#1中得测试用例运行阶段,给hyperd
发送SIGHUB
事件,并再次重启hyperd
。
重启后,该pod不会重新被运行。