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.
Creating a virtual machine
Create a my-vm
virtual machine by following the examples in the DVP documentation.
In the manifest example below, the vm: my-vm
label is included so that the virtual machine can be bound to load balancers.
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
Below is an example of a manifest of a web service load balancer:
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:
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
Creating a StatefulSet for 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.
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:
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
Create a Secret to store credentials so that probes can access the database:
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:
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:
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()"