/
statefulset.go
406 lines (366 loc) · 12.4 KB
/
statefulset.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package statefulsets
import (
"fmt"
"os"
"strconv"
"strings"
apps "k8s.io/api/apps/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
agentopts "github.com/cuijxin/mysql-operator/cmd/mysql-agent/app/options"
operatoropts "github.com/cuijxin/mysql-operator/cmd/mysql-operator/app/options"
api "github.com/cuijxin/mysql-operator/pkg/apis/mysql/v1"
"github.com/cuijxin/mysql-operator/pkg/constants"
"github.com/cuijxin/mysql-operator/pkg/resources/secrets"
"github.com/cuijxin/mysql-operator/pkg/version"
)
const (
// MySQLServerName is the static name of all 'mysql(-server)' containers.
MySQLServerName = "mysql"
// MySQLAgentName is the static name of all 'mysql-agent' containers.
MySQLAgentName = "mysql-agent"
// MySQLAgentBasePath defines the volume mount path for the MySQL agent
MySQLAgentBasePath = "/var/lib/mysql-agent"
mySQLBackupVolumeName = "mysqlbackupvolume"
mySQLVolumeName = "mysqlvolume"
replicationGroupPort = 13306
)
func volumeMounts(cluster *api.MySQLCluster) []v1.VolumeMount {
var mounts []v1.VolumeMount
name := mySQLVolumeName
if cluster.Spec.VolumeClaimTemplate != nil {
name = cluster.Spec.VolumeClaimTemplate.Name
}
mounts = append(mounts, v1.VolumeMount{
Name: name,
MountPath: "/var/lib/mysql",
SubPath: "mysql",
})
backupName := mySQLBackupVolumeName
if cluster.Spec.BackupVolumeClaimTemplate != nil {
backupName = cluster.Spec.BackupVolumeClaimTemplate.Name
}
mounts = append(mounts, v1.VolumeMount{
Name: backupName,
MountPath: MySQLAgentBasePath,
SubPath: "mysql",
})
// A user may explicitly define a my.cnf configuration file for
// their MySQL cluster.
if cluster.RequiresConfigMount() {
mounts = append(mounts, v1.VolumeMount{
Name: cluster.Name,
MountPath: "/etc/my.cnf",
SubPath: "my.cnf",
})
}
return mounts
}
func clusterNameEnvVar(cluster *api.MySQLCluster) v1.EnvVar {
return v1.EnvVar{Name: "MYSQL_CLUSTER_NAME", Value: cluster.Name}
}
func namespaceEnvVar() v1.EnvVar {
return v1.EnvVar{
Name: "POD_NAMESPACE",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
}
}
func replicationGroupSeedsEnvVar(replicationGroupSeeds string) v1.EnvVar {
return v1.EnvVar{
Name: "REPLICATION_GROUP_SEEDS",
Value: replicationGroupSeeds,
}
}
func initDatabaseEnvVar(dbName string) v1.EnvVar {
return v1.EnvVar{
Name: "MYSQL_DATABASE",
Value: dbName,
}
}
func multiMasterEnvVar(enabled bool) v1.EnvVar {
return v1.EnvVar{
Name: "MYSQL_CLUSTER_MULTI_MASTER",
Value: strconv.FormatBool(enabled),
}
}
// Returns the MySQL_ROOT_PASSWORD environment variable
// If a user specifies a secret in the spec we use that
// else we create a secret with a random password
func mysqlRootPassword(cluster *api.MySQLCluster) v1.EnvVar {
var secretName string
if cluster.RequiresSecret() {
secretName = secrets.GetRootPasswordSecretName(cluster)
} else {
secretName = cluster.Spec.SecretRef.Name
}
return v1.EnvVar{
Name: "MYSQL_ROOT_PASSWORD",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: secretName,
},
Key: "password",
},
},
}
}
func serviceNameEnvVar(serviceName string) v1.EnvVar {
return v1.EnvVar{
Name: "MYSQL_CLUSTER_SERVICE_NAME",
Value: serviceName,
}
}
func getReplicationGroupSeeds(serviceName string, replicas int) string {
seeds := []string{}
for i := 0; i < replicas; i++ {
seeds = append(seeds, fmt.Sprintf("%s-%d:%d",
serviceName, i, replicationGroupPort))
}
return strings.Join(seeds, ",")
}
func getInitDBName(cluster *api.MySQLCluster) string {
var name string
if cluster.Spec.InitDBName != nil {
name = *cluster.Spec.InitDBName
}
return name
}
// Builds the MySQL operator container for a cluster.
// The 'mysqlImage' parameter is the image name of the mysql server to use with
// no version information.. e.g. 'mysql/mysql-server'
func mysqlServerContainer(cluster *api.MySQLCluster, mysqlServerImage string, rootPassword v1.EnvVar, serviceName string, replicas int) v1.Container {
replicationGroupSeeds := getReplicationGroupSeeds(serviceName, replicas)
dbName := getInitDBName(cluster)
fmt.Println("初始化数据名称: %s", dbName)
args := []string{
"--server_id=$(expr 1000 + $index)",
// basic process setup options
"--user=mysql",
"--datadir=/var/lib/mysql",
// storage engine options
"--default-storage-engine=innodb",
"--default-tmp-storage-engine=innodb",
"--internal-tmp-disk-storage-engine=innodb",
// character set, collation, and i18n options
"--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_520_ci",
// crash handling and debugging options
"--core-file",
"--default-password-lifetime=0",
// date and time handling options
"--default-time-zone=SYSTEM",
"--explicit-defaults-for-timestamp=ON",
// performance Schema options
"--performance-schema-consumer-events-transactions-current=ON",
"--performance-schema-consumer-events-transactions-history=ON",
// innoDB options
"--innodb-buffer-pool-size=128M",
"--innodb-buffer-pool-instances=4",
"--innodb-autoinc-lock-mode=2",
"--innodb-flush-method=O_DIRECT_NO_FSYNC",
"--innodb-open-files=128",
"--innodb-log-buffer-size=4M",
"--innodb-monitor-enable='%'",
"--innodb-print-all-deadlocks=ON",
"--innodb-undo-log-truncate=ON",
"--innodb-undo-tablespaces=2",
"--innodb-undo-logs=2",
// group replication pre-requisites & recommendations
"--binlog_checksum=NONE",
"--gtid_mode=ON",
"--enforce_gtid_consistency=ON",
"--log_bin",
"--binlog-format=ROW",
"--log-slave-updates=ON",
"--master-info-repository=TABLE",
"--relay-log-info-repository=TABLE",
"--slave-preserve-commit-order=ON",
"--disabled_storage_engines='MyISAM,BLACKHOLE,FEDERATED,ARCHIVE'",
"--transaction-isolation='READ-COMMITTED'",
// group replication specific options
"--transaction-write-set-extraction=XXHASH64",
"--loose-group-replication-ip-whitelist='0.0.0.0/0'",
}
entryPointArgs := strings.Join(args, " ")
cmd := fmt.Sprintf(
`# Note: We fiddle with the resolv.conf file in order to ensure that the mysql instances
# can refer to each other using just thier hostnames (e.g. mysql-N), thus do not need
# to qualify their names with the name of the (headless) service (e.g. mysql-N.mysql)
search=$(grep ^search /etc/resolv.conf)
echo "$search %s.${POD_NAMESPACE}.svc.cluster.local" >> /etc/resolv.conf
# Finds the replica index from the hostname, and uses this to define
# a unique server id for this instance.
index=$(cat /etc/hostname | grep -o '[^-]*$')
/entrypoint.sh %s`,
serviceName, entryPointArgs)
return v1.Container{
Name: MySQLServerName,
// TODO(apryde): Add BaseImage to cluster CRD.
Image: fmt.Sprintf("%s:%s", mysqlServerImage, cluster.Spec.Version),
Ports: []v1.ContainerPort{
v1.ContainerPort{
ContainerPort: 3306,
},
},
VolumeMounts: volumeMounts(cluster),
Command: []string{"/bin/bash", "-ecx", cmd},
Env: []v1.EnvVar{
clusterNameEnvVar(cluster),
namespaceEnvVar(),
serviceNameEnvVar(serviceName),
replicationGroupSeedsEnvVar(replicationGroupSeeds),
multiMasterEnvVar(cluster.Spec.MultiMaster),
initDatabaseEnvVar(dbName),
rootPassword,
v1.EnvVar{
Name: "MYSQL_ROOT_HOST",
Value: "%",
},
v1.EnvVar{
Name: "MYSQL_LOG_CONSOLE",
Value: "true",
},
},
}
}
func mysqlAgentContainer(cluster *api.MySQLCluster, mysqlAgentImage string, rootPassword v1.EnvVar, serviceName string, replicas int) v1.Container {
agentVersion := version.GetBuildVersion()
if version := os.Getenv("MYSQL_AGENT_VERSION"); version != "" {
agentVersion = version
}
replicationGroupSeeds := getReplicationGroupSeeds(serviceName, replicas)
return v1.Container{
Name: MySQLAgentName,
Image: fmt.Sprintf("%s:%s", mysqlAgentImage, agentVersion),
Args: []string{"--v=4"},
VolumeMounts: volumeMounts(cluster),
Env: []v1.EnvVar{
clusterNameEnvVar(cluster),
namespaceEnvVar(),
serviceNameEnvVar(serviceName),
replicationGroupSeedsEnvVar(replicationGroupSeeds),
multiMasterEnvVar(cluster.Spec.MultiMaster),
rootPassword,
},
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/live",
Port: intstr.FromInt(int(agentopts.DefaultMySQLAgentHeathcheckPort)),
},
},
},
ReadinessProbe: &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/ready",
Port: intstr.FromInt(int(agentopts.DefaultMySQLAgentHeathcheckPort)),
},
},
},
}
}
// NewForCluster creates a new StatefulSet for the given MySQLCluster.
func NewForCluster(cluster *api.MySQLCluster, images operatoropts.Images, serviceName string) *apps.StatefulSet {
rootPassword := mysqlRootPassword(cluster)
replicas := int(cluster.Spec.Replicas)
// If a PV isn't specified just use a EmptyDir volume
var podVolumes = []v1.Volume{}
if cluster.Spec.VolumeClaimTemplate == nil {
podVolumes = append(podVolumes, v1.Volume{Name: mySQLVolumeName,
VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{Medium: ""}}})
}
// If a Backup PV isn't specified just use a EmptyDir volume
if cluster.Spec.BackupVolumeClaimTemplate == nil {
podVolumes = append(podVolumes, v1.Volume{Name: mySQLBackupVolumeName,
VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{Medium: ""}}})
}
if cluster.RequiresConfigMount() {
podVolumes = append(podVolumes, v1.Volume{
Name: cluster.Name,
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: cluster.Spec.ConfigRef.Name,
},
},
},
})
}
containers := []v1.Container{
mysqlServerContainer(cluster, images.MySQLServerImage, rootPassword, serviceName, replicas),
mysqlAgentContainer(cluster, images.MySQLAgentImage, rootPassword, serviceName, replicas)}
podLabels := map[string]string{
constants.MySQLClusterLabel: cluster.Name,
}
if cluster.Spec.MultiMaster {
podLabels[constants.LabelMySQLClusterRole] = constants.MySQLClusterRolePrimary
}
ss := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: cluster.Name,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cluster, schema.GroupVersionKind{
Group: api.SchemeGroupVersion.Group,
Version: api.SchemeGroupVersion.Version,
Kind: api.MySQLClusterCRDResourceKind,
}),
},
Labels: map[string]string{
constants.MySQLClusterLabel: cluster.Name,
constants.MySQLOperatorVersionLabel: version.GetBuildVersion(),
},
},
Spec: apps.StatefulSetSpec{
Replicas: &cluster.Spec.Replicas,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: podLabels,
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "8080",
},
},
Spec: v1.PodSpec{
// FIXME: LIMITED TO DEFAULT NAMESPACE. Need to dynamically
// create service accounts and (cluster role bindings?)
// for each namespace.
ServiceAccountName: "mysql-agent",
NodeSelector: cluster.Spec.NodeSelector,
Affinity: cluster.Spec.Affinity,
Containers: containers,
Volumes: podVolumes,
},
},
ServiceName: serviceName,
},
}
if cluster.Spec.VolumeClaimTemplate != nil {
ss.Spec.VolumeClaimTemplates = append(ss.Spec.VolumeClaimTemplates, *cluster.Spec.VolumeClaimTemplate)
}
if cluster.Spec.BackupVolumeClaimTemplate != nil {
ss.Spec.VolumeClaimTemplates = append(ss.Spec.VolumeClaimTemplates, *cluster.Spec.BackupVolumeClaimTemplate)
}
return ss
}