Compare languages | Модуль service-with-healthchecks: примеры

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

Running two independent balancers on the same virtual machine

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

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.

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

Creating a virtual machine

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

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

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

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

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

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

Web service and SMTP load balancer manifests

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

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

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

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

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

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

Load balancers for working with a PostgreSQL cluster

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

Creating a StatefulSet for PostgreSQL

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

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.

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

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

Below is an example of a StatefulSet manifest:

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

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

Configuring ServiceWithHealthchecks load balancers

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

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

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()”