-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_mariadb_deployment.yaml
163 lines (161 loc) · 6.6 KB
/
2_mariadb_deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-
hosts: localhost
vars_files:
- external_vars.yaml
vars:
db_root_password: "{{ lookup('password', 'db.root.password') }}"
db_user_password: "{{ lookup('password', 'db.user.password') }}"
gcloud_creds_text: "{{ lookup('pipe', 'gcloud config config-helper --format=json') }}"
gcloud_credentials: "{{ gcloud_creds_text | json_query('@') }}"
tags: create
tasks:
-
name: Gather cluster facts
gcp_container_cluster_facts:
project: "{{ gcp_project_id }}"
auth_kind: serviceaccount
service_account_file: "{{ gcp_cred_file }}"
zone: "{{gcp_zone }}"
register: clusters
-
set_fact:
cluster: "{{ clusters['items'] | selectattr('name', 'match', cluster_name) | first }}"
-
name: Create a MariaDB deployment
k8s:
host: "https://{{ cluster.endpoint }}"
ca_cert: "{{ cluster.masterAuth.clusterCaCertificate }}"
#validate_certs: false
api_key: "{{ gcloud_credentials.credential.access_token }}"
namespace: "default"
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-server
spec:
replicas: 1
selector:
matchLabels:
app: mariadb
role: master
tier: backend
template:
metadata:
labels:
app: mariadb
role: master
tier: backend
spec:
containers:
-
# https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#container-v1-core
name: db
image: mariadb/server
env:
-
name: "MARIADB_ROOT_PASSWORD"
value: "{{ db_root_password }}"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
-
containerPort: 3306
volumeMounts:
-
mountPath: /var/lib/mysql
name: db-volume
volumes:
-
name: db-volume
gcePersistentDisk:
pdName: lb-demo-db
fsType: ext3
register: deployment
-
name: This is the deployment
debug:
var: deployment
-
name: Create a MariaDB service
k8s:
host: "https://{{ cluster.endpoint }}"
ca_cert: "{{ cluster.masterAuth.clusterCaCertificate }}"
#validate_certs: false
api_key: "{{ gcloud_credentials.credential.access_token }}"
namespace: "default"
definition:
apiVersion: v1
kind: Service
metadata:
name: mariadb-service
labels:
app: mariadb
role: master
tier: backend
spec:
type: LoadBalancer
#loadBalancerIP: "YOUR.IP.ADDRESS.HERE"
ports:
-
port: 3306
targetPort: 3306
selector:
app: mariadb
role: master
tier: backend
register: service
-
name: This is the service
debug:
var: service
-
name: Check out the App sources
git:
repo: "{{ app_repo }}"
dest: "{{ app_name }}"
force: true
-
name: Modify the login source in the DB creation script
replace:
path: "{{ app_name }}/sql/00_create.sql"
regexp: "(?<=@')[1-9][^']*"
replace: "%"
-
name: Modify the user password in the DB creation script
replace:
path: "{{ app_name }}/sql/00_create.sql"
regexp: "(?<=IDENTIFIED BY ')[^']*"
replace: "{{ db_user_password }}"
-
name: Wait for the service external IP
k8s_facts:
host: "https://{{ cluster.endpoint }}"
ca_cert: "{{ cluster.masterAuth.clusterCaCertificate }}"
#validate_certs: false
api_key: "{{ gcloud_credentials.credential.access_token }}"
kind: Service
field_selectors:
- metadata.name=mariadb-service
register: services
until: "services.resources[0].status.loadBalancer.ingress[0].ip is defined"
retries: 90
delay: 5
-
set_fact:
db_service_ip: "{{ services.resources[0].status.loadBalancer.ingress[0].ip }}"
-
name: This is the DB service IP
debug:
var: db_service_ip
-
name: Create the database structure
become: true
shell: |
mysql -u root --password='{{ db_root_password }}' -h {{ db_service_ip }} < 00_create.sql
mysql -u test --password='{{ db_user_password }}' -h {{ db_service_ip }} < 01_add_data.sql
args:
chdir: "{{ app_name }}/sql"
# vim: set sw=4 ts=4 et indk= :