Skip to content

Commit

Permalink
*: add client.cnf for health check
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyass committed Jul 15, 2021
1 parent 5cf4171 commit ee94e41
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 110 deletions.
5 changes: 0 additions & 5 deletions cluster/container/init_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ func (c *initMysql) getVolumeMounts() []corev1.VolumeMount {
Name: utils.ConfVolumeName,
MountPath: utils.ConfVolumeMountPath,
},
{
Name: utils.ConfMapVolumeName,
MountPath: utils.MyCnfMountPath,
SubPath: "my.cnf",
},
{
Name: utils.DataVolumeName,
MountPath: utils.DataVolumeMountPath,
Expand Down
19 changes: 12 additions & 7 deletions cluster/container/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package container

import (
"fmt"

corev1 "k8s.io/api/core/v1"

"github.com/radondb/radondb-mysql-kubernetes/cluster"
Expand Down Expand Up @@ -86,7 +88,11 @@ func (c *mysql) getLivenessProbe() *corev1.Probe {
return &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"sh", "-c", "mysqladmin ping -uroot -p${MYSQL_ROOT_PASSWORD}"},
Command: []string{
"sh",
"-c",
fmt.Sprintf("mysqladmin --defaults-file=%s ping", utils.ConfClientPath),
},
},
},
InitialDelaySeconds: 30,
Expand All @@ -102,7 +108,11 @@ func (c *mysql) getReadinessProbe() *corev1.Probe {
return &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"sh", "-c", `mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "SELECT 1"`},
Command: []string{
"sh",
"-c",
fmt.Sprintf(`test $(mysql --defaults-file=%s -NB -e "SELECT 1") -eq 1`, utils.ConfClientPath),
},
},
},
InitialDelaySeconds: 10,
Expand All @@ -120,11 +130,6 @@ func (c *mysql) getVolumeMounts() []corev1.VolumeMount {
Name: utils.ConfVolumeName,
MountPath: utils.ConfVolumeMountPath,
},
{
Name: utils.ConfMapVolumeName,
MountPath: utils.MyCnfMountPath,
SubPath: "my.cnf",
},
{
Name: utils.DataVolumeName,
MountPath: utils.DataVolumeMountPath,
Expand Down
1 change: 1 addition & 0 deletions config/samples/mysql_v1alpha1_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ spec:

mysqlOpts:
rootPassword: ""
rootHost: "127.0.0.1"
user: qc_usr
password: Qing@123
database: qingcloud
Expand Down
116 changes: 116 additions & 0 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package sidecar

import (
"fmt"
"strconv"

"github.com/blang/semver"
Expand Down Expand Up @@ -141,3 +142,118 @@ func (cfg *Config) buildExtraConfig(filePath string) (*ini.File, error) {

return conf, nil
}

// buildXenonConf build a config file for xenon.
func (cfg *Config) buildXenonConf() []byte {
pingTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
heartbeatTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
requestTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount

version := "mysql80"
if cfg.MySQLVersion.Major == 5 {
if cfg.MySQLVersion.Minor == 6 {
version = "mysql56"
} else {
version = "mysql57"
}
}

var masterSysVars, slaveSysVars string
if cfg.InitTokuDB {
masterSysVars = "tokudb_fsync_log_period=default;sync_binlog=default;innodb_flush_log_at_trx_commit=default"
slaveSysVars = "tokudb_fsync_log_period=1000;sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
} else {
masterSysVars = "sync_binlog=default;innodb_flush_log_at_trx_commit=default"
slaveSysVars = "sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
}

hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace)

str := fmt.Sprintf(`{
"log": {
"level": "INFO"
},
"server": {
"endpoint": "%s:%d",
"peer-address": "%s:%d",
"enable-apis": true
},
"replication": {
"passwd": "%s",
"user": "%s"
},
"rpc": {
"request-timeout": %d
},
"mysql": {
"admit-defeat-ping-count": 3,
"admin": "root",
"ping-timeout": %d,
"passwd": "%s",
"host": "localhost",
"version": "%s",
"master-sysvars": "%s",
"slave-sysvars": "%s",
"port": 3306,
"monitor-disabled": true
},
"raft": {
"election-timeout": %d,
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "/var/lib/xenon/",
"leader-start-command": "/scripts/leader-start.sh",
"leader-stop-command": "/scripts/leader-stop.sh",
"semi-sync-degrade": true,
"purge-binlog-disabled": true,
"super-idle": false
}
}
`, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser, requestTimeout,
pingTimeout, cfg.RootPassword, version, masterSysVars, slaveSysVars, cfg.ElectionTimeout,
cfg.AdmitDefeatHearbeatCount, heartbeatTimeout)
return utils.StringToBytes(str)
}

// buildInitSql used to build init.sql. The file run after the mysql init.
func (cfg *Config) buildInitSql() []byte {
sql := fmt.Sprintf(`SET @@SESSION.SQL_LOG_BIN=0;
CREATE DATABASE IF NOT EXISTS %s;
DROP USER 'root'@'127.0.0.1';
GRANT ALL ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '%s';
DELETE FROM mysql.user WHERE user in ('%s', '%s', '%s', '%s');
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT SUPER, PROCESS, RELOAD, CREATE, SELECT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';
FLUSH PRIVILEGES;
`, cfg.Database, cfg.RootPassword, cfg.ReplicationUser, cfg.MetricsUser, cfg.OperatorUser, cfg.User,
cfg.ReplicationUser, cfg.ReplicationPassword, cfg.MetricsUser, cfg.MetricsPassword,
cfg.OperatorUser, cfg.OperatorPassword, cfg.Database, cfg.User, cfg.Password)

return utils.StringToBytes(sql)
}

// buildClientConfig used to build client.conf.
func (cfg *Config) buildClientConfig() (*ini.File, error) {
conf := ini.Empty()
sec := conf.Section("client")

if _, err := sec.NewKey("host", "127.0.0.1"); err != nil {
return nil, err
}

if _, err := sec.NewKey("port", fmt.Sprintf("%d", utils.MysqlPort)); err != nil {
return nil, err
}

if _, err := sec.NewKey("user", cfg.OperatorUser); err != nil {
return nil, err
}

if _, err := sec.NewKey("password", cfg.OperatorPassword); err != nil {
return nil, err
}

return conf, nil
}
117 changes: 21 additions & 96 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"strconv"

"github.com/spf13/cobra"

"github.com/radondb/radondb-mysql-kubernetes/utils"
)

// NewInitCommand return a pointer to cobra.Command.
Expand Down Expand Up @@ -75,9 +73,24 @@ func runInitCommand(cfg *Config) error {
}
}

if err = os.Mkdir(configPath, os.FileMode(0755)); err != nil {
// copy appropriate my.cnf from config-map to config mount.
if err = copyFile(path.Join(configMapPath, "my.cnf"), path.Join(configPath, "my.cnf")); err != nil {
return fmt.Errorf("failed to copy my.cnf: %s", err)
}

// build client.conf.
clientConfig, err := cfg.buildClientConfig()
if err != nil {
return fmt.Errorf("failed to build client.conf: %s", err)
}
// save client.conf to /etc/mysql.
if err := clientConfig.SaveTo(path.Join(clientConfPath)); err != nil {
return fmt.Errorf("failed to save client.conf: %s", err)
}

if err = os.Mkdir(extraConfPath, os.FileMode(0755)); err != nil {
if !os.IsExist(err) {
return fmt.Errorf("error mkdir %s: %s", configPath, err)
return fmt.Errorf("error mkdir %s: %s", extraConfPath, err)
}
}

Expand All @@ -87,8 +100,8 @@ func runInitCommand(cfg *Config) error {
}

// build init.sql.
initSqlPath := path.Join(configPath, "init.sql")
if err = ioutil.WriteFile(initSqlPath, buildInitSql(cfg), 0644); err != nil {
initSqlPath := path.Join(extraConfPath, "init.sql")
if err = ioutil.WriteFile(initSqlPath, cfg.buildInitSql(), 0644); err != nil {
return fmt.Errorf("failed to write init.sql: %s", err)
}

Expand All @@ -98,7 +111,7 @@ func runInitCommand(cfg *Config) error {
return fmt.Errorf("failed to build extra.cnf: %s", err)
}
// save extra.cnf to conf.d.
if err := extraConfig.SaveTo(path.Join(configPath, "extra.cnf")); err != nil {
if err := extraConfig.SaveTo(path.Join(extraConfPath, "extra.cnf")); err != nil {
return fmt.Errorf("failed to save extra.cnf: %s", err)
}

Expand Down Expand Up @@ -132,7 +145,7 @@ func runInitCommand(cfg *Config) error {

// build xenon.json.
xenonFilePath := path.Join(xenonPath, "xenon.json")
if err = ioutil.WriteFile(xenonFilePath, buildXenonConf(cfg), 0644); err != nil {
if err = ioutil.WriteFile(xenonFilePath, cfg.buildXenonConf(), 0644); err != nil {
return fmt.Errorf("failed to write xenon.json: %s", err)
}

Expand All @@ -153,91 +166,3 @@ func checkIfPathExists(path string) (bool, error) {
err = f.Close()
return true, err
}

// buildXenonConf build a config file for xenon.
func buildXenonConf(cfg *Config) []byte {
pingTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
heartbeatTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
requestTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount

version := "mysql80"
if cfg.MySQLVersion.Major == 5 {
if cfg.MySQLVersion.Minor == 6 {
version = "mysql56"
} else {
version = "mysql57"
}
}

var masterSysVars, slaveSysVars string
if cfg.InitTokuDB {
masterSysVars = "tokudb_fsync_log_period=default;sync_binlog=default;innodb_flush_log_at_trx_commit=default"
slaveSysVars = "tokudb_fsync_log_period=1000;sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
} else {
masterSysVars = "sync_binlog=default;innodb_flush_log_at_trx_commit=default"
slaveSysVars = "sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
}

hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace)

str := fmt.Sprintf(`{
"log": {
"level": "INFO"
},
"server": {
"endpoint": "%s:%d",
"peer-address": "%s:%d",
"enable-apis": true
},
"replication": {
"passwd": "%s",
"user": "%s"
},
"rpc": {
"request-timeout": %d
},
"mysql": {
"admit-defeat-ping-count": 3,
"admin": "root",
"ping-timeout": %d,
"passwd": "%s",
"host": "localhost",
"version": "%s",
"master-sysvars": "%s",
"slave-sysvars": "%s",
"port": 3306,
"monitor-disabled": true
},
"raft": {
"election-timeout": %d,
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "/var/lib/xenon/",
"leader-start-command": "/scripts/leader-start.sh",
"leader-stop-command": "/scripts/leader-stop.sh",
"semi-sync-degrade": true,
"purge-binlog-disabled": true,
"super-idle": false
}
}
`, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser, requestTimeout,
pingTimeout, cfg.RootPassword, version, masterSysVars, slaveSysVars, cfg.ElectionTimeout,
cfg.AdmitDefeatHearbeatCount, heartbeatTimeout)
return utils.StringToBytes(str)
}

// buildInitSql used to build init.sql. The file run after the mysql init.
func buildInitSql(cfg *Config) []byte {
sql := fmt.Sprintf(`SET @@SESSION.SQL_LOG_BIN=0;
CREATE DATABASE IF NOT EXISTS %s;
DELETE FROM mysql.user WHERE user in ('%s', '%s', '%s', '%s');
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT SUPER, PROCESS, RELOAD, CREATE, SELECT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';
FLUSH PRIVILEGES;
`, cfg.Database, cfg.ReplicationUser, cfg.MetricsUser, cfg.OperatorUser, cfg.User, cfg.ReplicationUser, cfg.ReplicationPassword,
cfg.MetricsUser, cfg.MetricsPassword, cfg.OperatorUser, cfg.OperatorPassword, cfg.Database, cfg.User, cfg.Password)

return utils.StringToBytes(sql)
}
6 changes: 6 additions & 0 deletions sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ var (
// configPath is the mysql configs path.
configPath = utils.ConfVolumeMountPath

// clientConfPath is the client.cnf path.
clientConfPath = utils.ConfClientPath

// extraConfPath is the mysql extra configs path.
extraConfPath = utils.ConfVolumeMountPath + "/conf.d"

// configMapPath is the mounted configmap.
configMapPath = utils.ConfMapVolumeMountPath

Expand Down
7 changes: 5 additions & 2 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ const (
InitFileVolumeName = "init-mysql"

// volumes mount path.
MyCnfMountPath = "/etc/mysql/my.cnf"
ConfVolumeMountPath = "/etc/mysql/conf.d"
ConfVolumeMountPath = "/etc/mysql"
ConfMapVolumeMountPath = "/mnt/config-map"
LogsVolumeMountPath = "/var/log/mysql"
DataVolumeMountPath = "/var/lib/mysql"
SysVolumeMountPath = "/host-sys"
ScriptsVolumeMountPath = "/scripts"
XenonVolumeMountPath = "/etc/xenon"
InitFileVolumeMountPath = "/docker-entrypoint-initdb.d"

// The path to the client MySQL client configuration.
// The file used to liveness and readiness check.
ConfClientPath = "/etc/mysql/client.conf"
)

// ResourceName is the type for aliasing resources that will be created.
Expand Down

0 comments on commit ee94e41

Please sign in to comment.