Compare languages | Module service-with-healthchecks: examples

Running two independent balancers on the same virtual machine

Размещение двух независимых балансировщиков на одной виртуальной машине

Suppose that there are two applications running on a Linux virtual machine — an HTTP server (TCP 8080) and an SMTP server (TCP 2525). You need to set up two separate balancers for these services, a web balancer and an SMTP balancer.

На виртуальной машине с операционной системой Linux работают два приложения — HTTP-сервер (TCP 8080) и SMTP-сервер (TCP 2525). Необходимо настроить два отдельных балансировщика для этих сервисов — веб-балансировщик и SMTP-балансировщик.

Creating a virtual machine

Создание виртуальной машины

Create a my-vm virtual machine by following the examples in the DVP documentation.

Создайте виртуальную машину my-vm основываясь на примерах из документации DVP.

In the manifest example below, the vm: my-vm label is included so that the virtual machine can be bound to load balancers.

В примере манифеста ниже добавлен лейбл vm: my-vm для дальнейшей идентификации в балансировщиках.

yaml apiVersion: virtualization.deckhouse.io/v1alpha2 kind: VirtualMachine metadata: name: my-vm namespace: my-ns labels: vm: my-vm spec: virtualMachineClassName: host cpu: cores: 1 memory: size: 1Gi provisioning: type: UserData userData: | #cloud-config ssh_pwauth: True users:

  • name: cloud passwd: ‘$6$rounds=4096$saltsalt$fPmUsbjAuA7mnQNTajQM6ClhesyG0.yyQhvahas02ejfMAq1ykBo1RquzS0R6GgdIDlvS.kbUwDablGZKZcTP/’ shell: /bin/bash sudo: ALL=(ALL) NOPASSWD:ALL lock_passwd: False
    blockDeviceRefs:
  • kind: VirtualDisk name: linux-disk

yaml apiVersion: virtualization.deckhouse.io/v1alpha2 kind: VirtualMachine metadata: name: my-vm namespace: my-ns labels: vm: my-vm spec: virtualMachineClassName: host cpu: cores: 1 memory: size: 1Gi provisioning: type: UserData userData: | #cloud-config ssh_pwauth: True users:

  • name: cloud passwd: ‘$6$rounds=4096$saltsalt$fPmUsbjAuA7mnQNTajQM6ClhesyG0.yyQhvahas02ejfMAq1ykBo1RquzS0R6GgdIDlvS.kbUwDablGZKZcTP/’ shell: /bin/bash sudo: ALL=(ALL) NOPASSWD:ALL lock_passwd: False
    blockDeviceRefs:
  • kind: VirtualDisk name: linux-disk

Web service and SMTP load balancer manifests

Манифесты балансировщиков для веб-сервиса и SMTP

Below is an example of a manifest of a web service load balancer:

Пример манифеста веб-балансировщика:

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: web namespace: my-ns spec: ports:

  • port: 80 protocol: TCP targetPort: 8080 selector: vm: my-vm healthcheck: probes:
  • mode: HTTP http: targetPort: 8080 method: GET path: /healthz

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: web namespace: my-ns spec: ports:

  • port: 80 protocol: TCP targetPort: 8080 selector: vm: my-vm healthcheck: probes:
  • mode: HTTP http: targetPort: 8080 method: GET path: /healthz

Below is an example of a manifest of a SMTP load balancer:

Пример манифеста SMTP-балансировщика:

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: smtp namespace: my-ns spec: ports:

  • port: 25 protocol: TCP targetPort: 2525 selector: vm: my-vm healthcheck: probes:
  • mode: TCP tcp: targetPort: 2525

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: smtp namespace: my-ns spec: ports:

  • port: 25 protocol: TCP targetPort: 2525 selector: vm: my-vm healthcheck: probes:
  • mode: TCP tcp: targetPort: 2525

Load balancers for working with a PostgreSQL cluster

Балансировщики для работы с PostgreSQL-кластером

Creating a StatefulSet for PostgreSQL

Создание StatefulSet для PostgreSQL

In order for StatefulSet to operate properly, you will have to create a regular Service to generate the pod DNS names. This service will not be used for direct access to the database.

Для корректной работы StatefulSet потребуется создать стандартный сервис (Service) для формирования DNS-имени отдельных подов. Этот сервис не будет использоваться для прямого доступа к базе данных.

yaml apiVersion: v1 kind: Service metadata: name: postgres spec: selector: app: postgres ports:

  • protocol: TCP port: 5432 targetPort: 5432

yaml apiVersion: v1 kind: Service metadata: name: postgres spec: selector: app: postgres ports:

  • protocol: TCP port: 5432 targetPort: 5432

Below is an example of a StatefulSet manifest:

Пример манифеста StatefulSet:

yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: postgres name: my-ns spec: serviceName: postgres replicas: 3 selector: matchLabels: app: postgres template: metadata: labels: app: postgres spec: containers:

  • name: postgres image: postgres:13 ports:
  • containerPort: 5432 env:
  • name: POSTGRES_USER value: postgres
  • name: POSTGRES_PASSWORD value: example

yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: postgres name: my-ns spec: serviceName: postgres replicas: 3 selector: matchLabels: app: postgres template: metadata: labels: app: postgres spec: containers:

  • name: postgres image: postgres:13 ports:
  • containerPort: 5432 env:
  • name: POSTGRES_USER value: postgres
  • name: POSTGRES_PASSWORD value: example

Configuring ServiceWithHealthchecks load balancers

Конфигурация балансировщиков ServiceWithHealthchecks

Create a Secret to store credentials so that probes can access the database:

Создайте Secret для хранения учетных данных для доступа проб к базе данных:

shell kubectl -n my-ns create secret generic cred-secret –from-literal=user=postgres –from-literal=password=example cred-secret

shell kubectl -n my-ns create secret generic cred-secret –from-literal=user=postgres –from-literal=password=example cred-secret

Below is an example of a load balancer manifest for reading:

Пример манифеста балансировщика для чтения:

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: postgres-read spec: ports:

  • port: 5432 protocol: TCP targetPort: 5432 selector: app: postgres healthcheck: probes:
  • mode: PostgreSQL postgreSQL: targetPort: 5432 dbName: postgres authSecretName: cred-secret query: “SELECT 1”

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: postgres-read spec: ports:

  • port: 5432 protocol: TCP targetPort: 5432 selector: app: postgres healthcheck: probes:
  • mode: PostgreSQL postgreSQL: targetPort: 5432 dbName: postgres authSecretName: cred-secret query: “SELECT 1”

And here is an example of a load balancer manifest for writing:

Пример манифеста балансировщика для записи:

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: postgres-write spec: ports:

  • port: 5432 protocol: TCP targetPort: 5432 selector: app: postgres healthcheck: probes:
  • mode: PostgreSQL postgreSQL: targetPort: 5432 dbName: postgres authSecretName: cred-secret query: “SELECT NOT pg_is_in_recovery()”

yaml apiVersion: network.deckhouse.io/v1alpha1 kind: ServiceWithHealthchecks metadata: name: postgres-write spec: ports:

  • port: 5432 protocol: TCP targetPort: 5432 selector: app: postgres healthcheck: probes:
  • mode: PostgreSQL postgreSQL: targetPort: 5432 dbName: postgres authSecretName: cred-secret query: “SELECT NOT pg_is_in_recovery()”