Skip to content

Commit 65f1d48

Browse files
jsilvelagbartolini
authored andcommitted
chore: review
Signed-off-by: Jaime Silvela <[email protected]>
1 parent cdd00f4 commit 65f1d48

File tree

1 file changed

+30
-28
lines changed
  • content/blog/migrations-with-atlas-and-cloudnativepg

1 file changed

+30
-28
lines changed

content/blog/migrations-with-atlas-and-cloudnativepg/index.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@ summary: Doing schema migrations on CloudNativePG clusters using the Atlas opera
1515

1616
One of the most important practices when developing code that relies on
1717
databases is to use *database migration tools* for change management.
18-
It's something you *will* learn, even if it has to be the hard way. I hope
19-
you didn't have to learn it the hard way though!
18+
It's something you *will* learn, even if it has to be the hard way.
2019
(Another thing I see too many newcomers learning the hard way is to take backups
21-
often, and to test those backups with some regularity.)
20+
often, and to test those backups regularly.)
2221

2322
In the post [*Developing webapps with CloudNativePG*]({{% ref "/blog/developing-webapps-with-cloudnative-pg" %}}),
24-
we mentioned [Liquibase](https://www.liquibase.com), which is one of the best
25-
known database migration tools.
23+
we mentioned [Liquibase](https://www.liquibase.com), which is one of the most
24+
popular database migration tools.
2625

2726
Traditional database migration tools assume a connection is available to the
28-
database one wants to perform migrations on. In the context of Kubernetes,
29-
and of Postgresql clusters built using CloudNativePG,
30-
we would need to expose the database service beyond the Kubernetes cluster,
27+
target database. In the context of Kubernetes,
28+
and of Postgresql clusters built using CloudNativePG, to use such a tool
29+
we would need to expose the database *service* outside the Kubernetes cluster,
3130
for example via
3231
[port forwarding](https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/),
3332
an [ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/),
3433
or similar solutions.
3534

3635
The [Atlas database migration tool](https://atlasgo.io) includes a Kubernetes
37-
operator that allows us to manage database migrations in a Kubernetes-native
38-
way.
36+
operator that lets us manage database migrations within Kubernetes.
3937
Let's see an example of how to do that:
4038

4139
## Step 0: install CloudNativePG and create a Postgres cluster
@@ -46,8 +44,8 @@ If you don't yet have this, you can follow the
4644
[CloudNativePG quickstart](https://cloudnative-pg.io/documentation/current/quickstart/).
4745

4846
Whether you follow the quickstart or you already had a CloudNativePG/Postgres
49-
cluster up and running, let's assume your CloudNativePG cluster is called
50-
`cluster-example`.
47+
cluster up and running, we'll assume for the rest of this post that your
48+
CloudNativePG cluster is called `cluster-example`.
5149

5250
## Step 1: install the Atlas operator
5351

@@ -58,7 +56,7 @@ helm install atlas-operator oci://ghcr.io/ariga/charts/atlas-operator
5856
```
5957

6058
Note that you may need an access token to retrieve the image from
61-
the GHCR registry (please see the [documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry)
59+
the GHCR registry (please see the [GHCR documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry)
6260
for details).
6361

6462
After Atlas is installed, you will notice new CRDs in your Kubernetes
@@ -76,13 +74,13 @@ We will use the *Atlas Schema* CRD to manage migrations. You may want
7674
to open the [Atlas operator quickstart](https://atlasgo.io/integrations/kubernetes/quickstart)
7775
for reference, though we're not going to follow it exactly.
7876

79-
To apply a migration, we connect to our target database, for which
80-
we need credentials. The Atlas operator quickstart uses the `urlFrom`
77+
To apply a migration, we need credentials to access our target database.
78+
The Atlas operator quickstart uses the `urlFrom`
8179
stanza, but with CloudNativePG there is a more convenient way.
8280

8381
From Step 0, we assumed we have a CloudNativePG cluster called
8482
`cluster-example`.
85-
CloudNativePG, by default, creates a database called `app` on the cluster, and
83+
CloudNativePG, by default, creates a database called `app` on clusters, and
8684
a user `app` whose credentials are held in a Secret called
8785
`cluster-example-app`:
8886

@@ -91,18 +89,20 @@ a user `app` whose credentials are held in a Secret called
9189
cluster-example-app kubernetes.io/basic-auth 9 18h
9290
```
9391

94-
You may inspect the contents of the secret running `kubectl get secrets cluster-example-app -o yaml`,
92+
You can inspect the contents of the secret running `kubectl get secrets cluster-example-app -o yaml`,
9593
and you will find that it contains a key called `password`, holding of course
9694
the password for the `app` user (base64 encoded).
9795
In addition to the `cluster-example-app` Secret, the CloudNativePG operator
9896
creates Services for Postgres. In particular, we will want to use the ReadWrite
9997
service called `cluster-example-rw` for the migrations.
10098

10199
We're going to use the [`credentials` object](https://atlasgo.io/integrations/kubernetes/declarative#credentials-object)
102-
from the AtlasSchema CRD referencing
100+
from the AtlasSchema CRD to reference
103101
the password and the service. Following along the Atlas Operator Quickstart, we
104102
create a migration defining a table called `t1`. Save the following to a file
105103
named `atlas-schema.yaml`.
104+
Notice how CloudNativePG automatically produced the values we need to put
105+
in the `passwordFrom` and `host` fields:
106106

107107
``` yaml
108108
apiVersion: db.atlasgo.io/v1alpha1
@@ -139,7 +139,8 @@ NAME READY REASON
139139
atlasschema-pg True Applied
140140
```
141141

142-
To see the results, let's get a `psql` session open on one of our instances:
142+
To verify the effects on the database, let's get a `psql` session open on one
143+
of our instances:
143144

144145
``` console
145146
> kubectl exec -ti cluster-example-1 -- psql app
@@ -162,7 +163,7 @@ app=# \d t1
162163
```
163164

164165
As suggested in the Atlas Quickstart, we can modify the schema in the
165-
`atlas-schema.yaml` file, and then re-apply:
166+
`atlas-schema.yaml` file, and then re-apply with
166167
`kubectl apply -f atlas-schema.yaml`, and the Atlas operator will again
167168
reconcile the database to the desired state.
168169

@@ -175,23 +176,24 @@ transactions, perhaps maps using PostGIS, etc.
175176
Application developers will regularly need to add functionality, and often
176177
that will involve creating new tables, schemas, procedures, indexes, perhaps
177178
doing some INSERT statements to populate static data, etc.
178-
The developers will be using a development database, possibly locally.
179+
The developers will be using a development database, probably hosted in their
180+
dev machines.
179181
There might be another database for the purpose of automated testing, and
180182
of course there's the production database, where the changes will need to be
181-
applied in the end. These different databases have different data in them,
183+
applied in the end. These different databases will have different data in them,
182184
and different loads.
183-
Add time and other developers, and you have a lot of databases on your hands,
184-
and a big chance for "it works on my machine" snafus.
185+
Add time and other developers, and you have a big chance for snafus of the type
186+
"but it worked on my machine!"
185187

186188
Database migration tools manage this, bringing DevOps to this area of data.
187189
Atlas, by working as a Kubernetes operator, makes the dev/prod transition even
188190
smoother.
189191

190-
Using CloudNativePG, with its credentials secrets and services created out of
192+
Using CloudNativePG, with its services and credentials secrets created out of
191193
the box, together with Atlas, will enable the developers to create migrations
192-
in their local Kubernetes clusters, update the YAML
193-
files for Atlas in version control, and apply the same files in the testing
194-
cluster, and then in the production cluster, with no changes.
194+
in their development Kubernetes installations, update the YAML
195+
files for Atlas in version control to share with the other devs, and eventually
196+
apply to the production database smoothly.
195197

196198
---
197199

0 commit comments

Comments
 (0)