|
| 1 | +--- |
| 2 | +title: Application Database Access |
| 3 | +sidebar_position: 2 |
| 4 | +--- |
| 5 | +## Application Database Access |
| 6 | + |
| 7 | +:::note |
| 8 | +This step is only necessary if your application is connecting to a database. |
| 9 | +::: |
| 10 | + |
| 11 | +If your application needs database access, first obtain the database user credentials. Then create a Kubernetes secret containing those credentials. The secret is referenced in your application deployment. |
| 12 | + |
| 13 | +### Create the secret for the application |
| 14 | + |
| 15 | +Create a secret with database access information. This secret is used by the application configuration and is injected during deployment. |
| 16 | + |
| 17 | +For example if you have the following information: |
| 18 | + |
| 19 | +- name: `Your database name`. For example `helmdb` |
| 20 | +- username: `Your database user name`. For example `phonebook` |
| 21 | +- password: `Your database user password`. For example `Welcome-12345` |
| 22 | +- service: `Your servicename`. For example `helmdb_tp` |
| 23 | + |
| 24 | +Create a Kubernetes secret (in this example, `phonebook-db-secrets` in the `obaas-dev` namespace): |
| 25 | + |
| 26 | +```bash |
| 27 | +kubectl -n obaas-dev create secret generic phonebook-db-secrets \ |
| 28 | + --from-literal=name=helmdb \ |
| 29 | + --from-literal=username=phonebook \ |
| 30 | + --from-literal=password=Welcome-12345 \ |
| 31 | + --from-literal=service=helmdb_tp |
| 32 | +``` |
| 33 | + |
| 34 | +You can verify the values by running the following command (this is for the `username` value): |
| 35 | + |
| 36 | +```bash |
| 37 | +kubectl -n obaas-dev get secret phonebook-sb-secrets -o jsonpath='{.data.username}' | base64 -d |
| 38 | +``` |
| 39 | + |
| 40 | +### Create database user using `sqljob.yaml` k8s job |
| 41 | + |
| 42 | +Use this job to run SQL statements against the database with the credentials stored in the secret above. |
| 43 | + |
| 44 | +Set the namespace to where the `obaas-db-secret` secret resides (example uses `obaas-dev`): |
| 45 | + |
| 46 | +```yaml |
| 47 | +metadata: |
| 48 | + generateName: sqlcl-runner-job- |
| 49 | + namespace: obaas-dev |
| 50 | +``` |
| 51 | +
|
| 52 | +Update the `args:` section with your SQL: |
| 53 | + |
| 54 | +```yaml |
| 55 | +args: |
| 56 | + - | |
| 57 | + export TNS_ADMIN=/etc/oracle/wallet |
| 58 | +
|
| 59 | + # Create the SQL script inline |
| 60 | + cat > /tmp/run.sql << 'EOF' |
| 61 | + SET SERVEROUTPUT ON; |
| 62 | + WHENEVER SQLERROR EXIT SQL.SQLCODE; |
| 63 | +
|
| 64 | + <<SQL STATEMENTS>> |
| 65 | +
|
| 66 | + EXIT; |
| 67 | + EOF |
| 68 | +
|
| 69 | + # Execute the SQL script |
| 70 | + sql $(DB_USER)/$(DB_PASSWORD)@$(TNS_ALIAS) @/tmp/run.sql |
| 71 | +``` |
| 72 | + |
| 73 | +Update the `env:` section to reference the correct secret and keys (here using `obaas-db-secrets`): |
| 74 | + |
| 75 | +```yaml |
| 76 | +env: |
| 77 | +- name: DB_USER |
| 78 | + valueFrom: |
| 79 | + secretKeyRef: |
| 80 | + name: obaas-db-secret |
| 81 | + key: db.username |
| 82 | +- name: DB_PASSWORD |
| 83 | + valueFrom: |
| 84 | + secretKeyRef: |
| 85 | + name: obaas-db-secret |
| 86 | + key: db.password |
| 87 | +- name: TNS_ALIAS |
| 88 | + valueFrom: |
| 89 | + secretKeyRef: |
| 90 | + name: obaas-db-secret |
| 91 | + key: db.service |
| 92 | +``` |
| 93 | + |
| 94 | +Set the TNS Admin wallet secret name to match your OBaaS installation: |
| 95 | + |
| 96 | +```yaml |
| 97 | +volumes: |
| 98 | +- name: db-wallet-volume |
| 99 | + secret: |
| 100 | + secretName: obaas-adb-tns-admin-1 |
| 101 | +``` |
| 102 | + |
| 103 | +Execute `kubectl create -f sqljob.yaml` to create the kubernetes job. |
| 104 | + |
| 105 | +```log |
| 106 | +job.batch/sqlcl-runner-job-2vcrq created |
| 107 | +``` |
| 108 | + |
| 109 | +You can verify that the Job ran successfully by checking its logs. |
| 110 | + |
| 111 | +#### Example `sqljob.yaml` |
| 112 | + |
| 113 | +This is an example of a kubernetes job that creates a `phonebook` user and assigns roles to the user. |
| 114 | + |
| 115 | +```yaml |
| 116 | +# The Job to run the SQLcl container with embedded SQL |
| 117 | +apiVersion: batch/v1 |
| 118 | +kind: Job |
| 119 | +metadata: |
| 120 | + generateName: sqlcl-runner-job- |
| 121 | + namespace: obaas-dev |
| 122 | +spec: |
| 123 | + backoffLimit: 2 |
| 124 | + template: |
| 125 | + spec: |
| 126 | + restartPolicy: Never |
| 127 | + containers: |
| 128 | + - name: sqlcl-container |
| 129 | + image: container-registry.oracle.com/database/sqlcl:latest |
| 130 | + command: ["/bin/sh", "-c"] |
| 131 | + args: |
| 132 | + - | |
| 133 | + export TNS_ADMIN=/etc/oracle/wallet |
| 134 | + |
| 135 | + # Create the SQL script inline |
| 136 | + cat > /tmp/run.sql << 'EOF' |
| 137 | + SET SERVEROUTPUT ON; |
| 138 | + WHENEVER SQLERROR EXIT SQL.SQLCODE; |
| 139 | + |
| 140 | + create user if not exists phonebook identified by "Welcome-12345"; |
| 141 | + grant db_developer_role to phonebook; |
| 142 | + grant unlimited tablespace to phonebook; |
| 143 | + commit; |
| 144 | +
|
| 145 | + / |
| 146 | + EXIT; |
| 147 | + EOF |
| 148 | + |
| 149 | + # Execute the SQL script |
| 150 | + sql $(DB_USER)/$(DB_PASSWORD)@$(TNS_ALIAS) @/tmp/run.sql |
| 151 | + env: |
| 152 | + - name: DB_USER |
| 153 | + valueFrom: |
| 154 | + secretKeyRef: |
| 155 | + name: obaas-db-secret |
| 156 | + key: db.username |
| 157 | + - name: DB_PASSWORD |
| 158 | + valueFrom: |
| 159 | + secretKeyRef: |
| 160 | + name: obaas-db-secret |
| 161 | + key: db.password |
| 162 | + - name: TNS_ALIAS |
| 163 | + valueFrom: |
| 164 | + secretKeyRef: |
| 165 | + name: obaas-db-secret |
| 166 | + key: db.service |
| 167 | + volumeMounts: |
| 168 | + - name: db-wallet-volume |
| 169 | + mountPath: /etc/oracle/wallet |
| 170 | + readOnly: true |
| 171 | + volumes: |
| 172 | + - name: db-wallet-volume |
| 173 | + secret: |
| 174 | + secretName: obaas-adb-tns-admin-1 |
| 175 | +``` |
0 commit comments