Version: v1.2

Web 服务

服务型组件是以容器为核心支撑对外访问服务的组件,其功能涵盖了主流微服务场景的需要,即在后端长时间运行、可水平扩展、且对外暴露服务端口的服务。

如何使用

为了便于你快速学习,请直接复制下面的 Shell 执行,应用会部署到集群中:

  1. cat <<EOF | vela up -f -
  2. # YAML 文件开始
  3. apiVersion: core.oam.dev/v1beta1
  4. kind: Application
  5. metadata:
  6. name: website
  7. spec:
  8. components:
  9. - name: frontend
  10. type: webservice
  11. properties:
  12. image: oamdev/testapp:v1
  13. cmd: ["node", "server.js"]
  14. port: 8080
  15. cpu: "0.1"
  16. env:
  17. - name: FOO
  18. value: bar
  19. # YAML 文件结束
  20. EOF

你也可以自行将 YAML 文件保存为 website.yaml,使用 vela up -f website.yaml 命令进行部署。

接下来,通过 vela status <应用 name> 查看应用的部署状态:

  1. $ vela status website
  2. About:
  3. Name: website
  4. Namespace: default
  5. Created at: 2022-01-11 21:04:59 +0800 CST
  6. Status: running
  7. Workflow:
  8. mode: DAG
  9. finished: true
  10. Suspend: false
  11. Terminated: false
  12. Steps
  13. - id:2y4rv8479h
  14. name:frontend
  15. type:apply-component
  16. phase:succeeded
  17. message:
  18. Services:
  19. - Name: frontend Env:
  20. Type: webservice
  21. healthy Ready:1/1

当我们看到 Workflow 的 finished 的字段为 true,并且 Status 为 running 时,即表示整个应用交付成功。

如果 Status 显示为 rendering,或者 healthy 为 false,则表示应用要么部署失败,要么还在部署中。

你也可以通过列表查看,使用如下命令:

  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. website frontend webservice running healthy 2021-08-28 18:26:47 +0800 CST

我们也看到 website APP 的 PHASE 为 running,同时 STATUS 为 healthy。

属性说明

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
cmdCommands to run in the container[]stringfalse
envDefine arguments by using environment variables[]envfalse
imageWhich image would you like to use for your servicestringtrue
portWhich port do you want customer traffic sent tointtrue80
imagePullPolicySpecify image pull policy for your servicestringfalse
cpuNumber of CPU units for the service, like 0.5 (0.5 CPU core), 1 (1 CPU core)stringfalse
memorySpecifies the attributes of the memory resource required for the container.stringfalse
volumesDeclare volumes and volumeMounts[]volumesfalse
livenessProbeInstructions for assessing whether the container is alive.livenessProbefalse
readinessProbeInstructions for assessing whether the container is in a suitable state to serve traffic.readinessProbefalse
imagePullSecretsSpecify image pull secrets for your service[]stringfalse

readinessProbe

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
execInstructions for assessing container health by executing a command. Either this attribute or theexecfalse
httpGet attribute or the tcpSocket attribute MUST be specified. This attribute is mutually exclusive
with both the httpGet attribute and the tcpSocket attribute.
httpGetInstructions for assessing container health by executing an HTTP GET request. Either this attributehttpGetfalse
or the exec attribute or the tcpSocket attribute MUST be specified. This attribute is mutually
exclusive with both the exec attribute and the tcpSocket attribute.
tcpSocketInstructions for assessing container health by probing a TCP socket. Either this attribute or thetcpSocketfalse
exec attribute or the httpGet attribute MUST be specified. This attribute is mutually exclusive with
both the exec attribute and the httpGet attribute.
initialDelaySecondsNumber of seconds after the container is started before the first probe is initiated.inttrue0
periodSecondsHow often, in seconds, to execute the probe.inttrue10
timeoutSecondsNumber of seconds after which the probe times out.inttrue1
successThresholdMinimum consecutive successes for the probe to be considered successful after having failed.inttrue1
failureThresholdNumber of consecutive failures required to determine the container is not alive (liveness probe) orinttrue3
not ready (readiness probe).

tcpSocket

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
portThe TCP socket within the container that should be probed to assess container health.inttrue

httpGet

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
pathThe endpoint, relative to the port, to which the HTTP GET request should be directed.stringtrue
portThe TCP socket within the container to which the HTTP GET request should be directed.inttrue
httpHeaders[]httpHeadersfalse
httpHeaders
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
valuestringtrue
exec
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
commandA command to be executed inside the container to assess its health. Each space delimited token of[]stringtrue
the command is a separate array element. Commands exiting 0 are considered to be successful probes,
whilst all other exit codes are considered failures.

livenessProbe

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
execInstructions for assessing container health by executing a command. Either this attribute or theexecfalse
httpGet attribute or the tcpSocket attribute MUST be specified. This attribute is mutually exclusive
with both the httpGet attribute and the tcpSocket attribute.
httpGetInstructions for assessing container health by executing an HTTP GET request. Either this attributehttpGetfalse
or the exec attribute or the tcpSocket attribute MUST be specified. This attribute is mutually
exclusive with both the exec attribute and the tcpSocket attribute.
tcpSocketInstructions for assessing container health by probing a TCP socket. Either this attribute or thetcpSocketfalse
exec attribute or the httpGet attribute MUST be specified. This attribute is mutually exclusive with
both the exec attribute and the httpGet attribute.
initialDelaySecondsNumber of seconds after the container is started before the first probe is initiated.inttrue0
periodSecondsHow often, in seconds, to execute the probe.inttrue10
timeoutSecondsNumber of seconds after which the probe times out.inttrue1
successThresholdMinimum consecutive successes for the probe to be considered successful after having failed.inttrue1
failureThresholdNumber of consecutive failures required to determine the container is not alive (liveness probe) orinttrue3
not ready (readiness probe).
tcpSocket
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
portThe TCP socket within the container that should be probed to assess container health.inttrue
httpGet
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
pathThe endpoint, relative to the port, to which the HTTP GET request should be directed.stringtrue
portThe TCP socket within the container to which the HTTP GET request should be directed.inttrue
httpHeaders[]httpHeadersfalse
httpHeaders
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
valuestringtrue
exec
NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
commandA command to be executed inside the container to assess its health. Each space delimited token of[]stringtrue
the command is a separate array element. Commands exiting 0 are considered to be successful probes,
whilst all other exit codes are considered failures.

volumes

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
namestringtrue
mountPathstringtrue
typeSpecify volume type, options: “pvc”,”configMap”,”secret”,”emptyDir”stringtrue

env

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
nameEnvironment variable namestringtrue
valueThe value of the environment variablestringfalse
valueFromSpecifies a source the value of this var should come fromvalueFromfalse

valueFrom

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
secretKeyRefSelects a key of a secret in the pod’s namespacesecretKeyReftrue

secretKeyRef

NAMEDESCRIPTIONTYPEREQUIREDDEFAULT
nameThe name of the secret in the pod’s namespace to select fromstringtrue
keyThe key of the secret to select from. Must be a valid secret keystringtrue