-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlwebservice.py
115 lines (107 loc) · 3.54 KB
/
mysqlwebservice.py
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
from constructs import Construct
from cdk8s import Names
from imports import k8s
class MysqlWebService(Construct):
def __init__(
self,
scope: Construct,
id: str,
*,
image: str,
replicas=1,
port=3306,
container_port=3306,
**kwargs
):
super().__init__(scope, id)
label = {"app": Names.to_label_value(self)}
config_map = k8s.KubeConfigMap(
self,
'mysql-config-map',
data={
'MYSQL_DATABASE': 'nextclouddb',
'MYSQL_USER': 'nextclouddb-user',
'MYSQL_PASSWORD': 'nextclouddb-secret',
'MYSQL_ROOT_PASSWORD': 'mysqldb-root-secret',
}
)
k8s.KubePersistentVolumeClaim(
self,
'pvc',
metadata=k8s.ObjectMeta(name='mysql-persistent-storage'),
spec=k8s.PersistentVolumeClaimSpec(
access_modes=['ReadWriteOnce'],
resources=k8s.ResourceRequirements(
requests={
'storage': k8s.Quantity.from_string("10Gi"),
},
),
storage_class_name="rawfile-btrfs"
),
)
container = k8s.Container(
name='mysql',
image=image,
env_from=[
k8s.EnvFromSource(
config_map_ref=k8s.ConfigMapEnvSource(
name=config_map.name,
),
),
],
ports=[
k8s.ContainerPort(container_port=3306),
],
resources=k8s.ResourceRequirements(
limits={
"cpu": k8s.Quantity.from_string("200m"),
"memory": k8s.Quantity.from_string("550Mi"),
},
requests={
"cpu": k8s.Quantity.from_string("100m"),
"memory": k8s.Quantity.from_string("200Mi"),
},
),
volume_mounts=[
k8s.VolumeMount(name='mysql-data', mount_path='/var/lib/mysql'),
],
)
k8s.KubeStatefulSet(
self,
'mysql',
metadata=k8s.ObjectMeta(name='mysql'),
spec=k8s.StatefulSetSpec(
service_name='mysql',
replicas=replicas,
selector=k8s.LabelSelector(match_labels=label),
template=k8s.PodTemplateSpec(
metadata=k8s.ObjectMeta(labels=label),
spec=k8s.PodSpec(
containers=[container],
volumes=[
k8s.Volume(
name="mysql-data",
persistent_volume_claim=k8s.PersistentVolumeClaimVolumeSource(
claim_name="mysql-persistent-storage"
),
)
],
),
),
),
)
k8s.KubeService(
self,
"service",
metadata=k8s.ObjectMeta(name="mysql"),
spec=k8s.ServiceSpec(
type="ClusterIP",
ports=[
k8s.ServicePort(
port=port,
target_port=k8s.IntOrString.from_number(container_port),
)
],
selector=label,
),
)