12.5. Monitoring virtual machine health
Use this procedure to create liveness and readiness probes to monitor virtual machine health.
12.5.1. About liveness and readiness probes
When a VirtualMachineInstance (VMI) fails, liveness probes stop the VMI. Controllers such as VirtualMachine then spawn other VMIs, restoring virtual machine responsiveness.
Readiness probes tell services and endpoints that the VirtualMachineInstance is ready to receive traffic from services. If readiness probes fail, the VirtualMachineInstance is removed from applicable endpoints until the probe recovers.
12.5.2. Define an HTTP liveness probe
This procedure provides an example configuration file for defining HTTP liveness probes.
Procedure
Customize a YAML configuration file to create an HTTP liveness probe, using the following code block as an example. In this example:
-
You configure a probe using
spec.livenessProbe.httpGet
, which queries port1500
of the virtual machine instance, after an initial delay of120
seconds. -
The virtual machine instance installs and runs a minimal HTTP server on port
1500
usingcloud-init
.
注意The
timeoutSeconds
value must be lower than theperiodSeconds
value. ThetimeoutSeconds
default value is1
. TheperiodSeconds
default value is10
.apiVersion: kubevirt.io/v1alpha3 kind: VirtualMachineInstance metadata: labels: special: vmi-fedora name: vmi-fedora spec: domain: devices: disks: - disk: bus: virtio name: containerdisk - disk: bus: virtio name: cloudinitdisk resources: requests: memory: 1024M livenessProbe: initialDelaySeconds: 120 periodSeconds: 20 httpGet: port: 1500 timeoutSeconds: 10 terminationGracePeriodSeconds: 0 volumes: - name: containerdisk registryDisk: image: kubevirt/fedora-cloud-registry-disk-demo - cloudInitNoCloud: userData: |- #cloud-config password: fedora chpasswd: { expire: False } bootcmd: - setenforce 0 - dnf install -y nmap-ncat - systemd-run --unit=httpserver nc -klp 1500 -e '/usr/bin/echo -e HTTP/1.1 200 OK\\n\\nHello World!' name: cloudinitdisk
-
You configure a probe using
Create the VirtualMachineInstance by running the following command:
$ oc create -f <file name>.yaml
12.5.3. Define a TCP liveness probe
This procedure provides an example configuration file for defining TCP liveness probes.
Procedure
Customize a YAML configuration file to create an TCP liveness probe, using this code block as an example. In this example:
-
You configure a probe using
spec.livenessProbe.tcpSocket
, which queries port1500
of the virtual machine instance, after an initial delay of120
seconds. -
The virtual machine instance installs and runs a minimal HTTP server on port
1500
usingcloud-init
.
注意The
timeoutSeconds
value must be lower than theperiodSeconds
value. ThetimeoutSeconds
default value is1
. TheperiodSeconds
default value is10
.apiVersion: kubevirt.io/v1alpha3 kind: VirtualMachineInstance metadata: labels: special: vmi-fedora name: vmi-fedora spec: domain: devices: disks: - disk: bus: virtio name: containerdisk - disk: bus: virtio name: cloudinitdisk resources: requests: memory: 1024M livenessProbe: initialDelaySeconds: 120 periodSeconds: 20 tcpSocket: port: 1500 timeoutSeconds: 10 terminationGracePeriodSeconds: 0 volumes: - name: containerdisk registryDisk: image: kubevirt/fedora-cloud-registry-disk-demo - cloudInitNoCloud: userData: |- #cloud-config password: fedora chpasswd: { expire: False } bootcmd: - setenforce 0 - dnf install -y nmap-ncat - systemd-run --unit=httpserver nc -klp 1500 -e '/usr/bin/echo -e HTTP/1.1 200 OK\\n\\nHello World!' name: cloudinitdisk
-
You configure a probe using
Create the VirtualMachineInstance by running the following command:
$ oc create -f <file name>.yaml
12.5.4. Define a readiness probe
This procedure provides an example configuration file for defining readiness probes.
Procedure
Customize a YAML configuration file to create a readiness probe. Readiness probes are configured in a similar manner to liveness probes. However, note the following differences in this example:
-
Readiness probes are saved using a different spec name. For example, you create a readiness probe as
spec.readinessProbe
instead of asspec.livenessProbe.<type-of-probe>
. -
When creating a readiness probe, you optionally set a
failureThreshold
and asuccessThreshold
to switch betweenready
andnon-ready
states, should the probe succeed or fail multiple times.
注意The
timeoutSeconds
value must be lower than theperiodSeconds
value. ThetimeoutSeconds
default value is1
. TheperiodSeconds
default value is10
.apiVersion: kubevirt.io/v1alpha3 kind: VirtualMachineInstance metadata: labels: special: vmi-fedora name: vmi-fedora spec: domain: devices: disks: - disk: bus: virtio name: containerdisk - disk: bus: virtio name: cloudinitdisk resources: requests: memory: 1024M readinessProbe: httpGet: port: 1500 initialDelaySeconds: 120 periodSeconds: 20 timeoutSeconds: 10 failureThreshold: 3 successThreshold: 3 terminationGracePeriodSeconds: 0 volumes: - name: containerdisk registryDisk: image: kubevirt/fedora-cloud-registry-disk-demo - cloudInitNoCloud: userData: |- #cloud-config password: fedora chpasswd: { expire: False } bootcmd: - setenforce 0 - dnf install -y nmap-ncat - systemd-run --unit=httpserver nc -klp 1500 -e '/usr/bin/echo -e HTTP/1.1 200 OK\\n\\nHello World!' name: cloudinitdisk
-
Readiness probes are saved using a different spec name. For example, you create a readiness probe as
Create the VirtualMachineInstance by running the following command:
$ oc create -f <file name>.yaml