Skip to content

Commit

Permalink
Merge pull request #39 from emqx/persist-pg-data-efs
Browse files Browse the repository at this point in the history
feat(pgsql): persist postgres data between clusters

to avoid losing monitoring data stored in postgres between cluster
runs, we store its data in an EFS volume
  • Loading branch information
thalesmg authored Dec 14, 2021
2 parents 04bdccd + 79281c8 commit 35d2f2e
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions cdk_emqx_cluster/cdk_emqx_cluster_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ def setup_monitoring(self, targets):
uid="65534", # nobody
gid="65534"),
file_system=self.shared_efs)
self.ap_pgsql_data = efs.AccessPoint(self, "shared-data-pgsql",
path='/pgsql_data',
create_acl=efs.Acl(
owner_uid="0",
owner_gid="0",
permissions="777"
),
posix_user=efs.PosixUser(
uid="0", # nobody
gid="0"),
file_system=self.shared_efs)

self.prom_data_vol = ecs.Volume(
name="prom_data",
Expand All @@ -336,14 +347,26 @@ def setup_monitoring(self, targets):
access_point_id=self.ap_prom_data.access_point_id),
)
)
self.pgsql_data_vol = ecs.Volume(
name="pgsql_data",
efs_volume_configuration=ecs.EfsVolumeConfiguration(
file_system_id=self.shared_efs.file_system_id,
transit_encryption='ENABLED',
authorization_config=ecs.AuthorizationConfig(
access_point_id=self.ap_pgsql_data.access_point_id),
)
)

cluster = ecs.Cluster(self, "Monitoring", vpc=vpc)
task = ecs.FargateTaskDefinition(self,
id='MonitorTask',
cpu=512,
memory_limit_mib=2048,
volumes=[self.prom_data_vol]
)
volumes=[
self.prom_data_vol,
self.pgsql_data_vol,
]
)
task.add_volume(name='prom_config')
c_config = task.add_container('config-prometheus',
image=ecs.ContainerImage.from_registry(
Expand Down Expand Up @@ -406,8 +429,24 @@ def setup_monitoring(self, targets):
stop_timeout=Duration.seconds(10), # It looks like postgres doesn't want to die sometimes
start_timeout=Duration.seconds(300),
environment={
'POSTGRES_PASSWORD': '123'}
'POSTGRES_PASSWORD': '123',
# must not use "/var/lib/postgresql/data", else it'll
# fail
'PGDATA': '/var/lib/postgresql/pgdata'
},
# uncomment for troubleshooting
# logging=ecs.LogDriver.aws_logs(stream_prefix="mon_postgres",
# log_retention=aws_logs.RetentionDays.ONE_DAY,
# ),
)
c_postgres.add_mount_points(
ecs.MountPoint(
read_only=False,
# must be the same as the PGDATA variable above
container_path='/var/lib/postgresql/pgdata',
source_volume=self.pgsql_data_vol.name,
),
)

c_grafana = task.add_container('grafana',
essential=True,
Expand Down

0 comments on commit 35d2f2e

Please sign in to comment.