Repository: radondb/radondb-mysql-kubernetes
Pull request #393 (2022-02-28 03:40:09)

config: Add podAntiAffinity to default sample yaml. #371
### What type of PR is this?

/documentation
/enhancement

### Which issue(s) this PR fixes?

ref: #371 

### What this PR does?

Summary:

Add podAntiAffinity to default sample yaml. 

Guarantee to schedule nodes to different physical machines

### Special notes for your reviewer?


andyli029: Improve the issue #371
runkecheng: Using a new sample file may be better, if the default configuration of anti-affinity can be unable to deploy in the verification environment.
runkecheng: Using a new sample file may be better, if the default configuration of anti-affinity can be unable to deploy in the verification environment.
andyli029: > Using a new sample file may be better
ACK.

Patch:
diff --git a/config/samples/mysql_v1alpha1_mysqlcluster_podAntiAffinity.yaml b/config/samples/mysql_v1alpha1_mysqlcluster_podAntiAffinity.yaml
new file mode 100644
index 0000000..107c3b1
--- /dev/null
+++ b/config/samples/mysql_v1alpha1_mysqlcluster_podAntiAffinity.yaml
@@ -0,0 +1,97 @@
+apiVersion: mysql.radondb.com/v1alpha1
+kind: MysqlCluster
+metadata:
+  name: sample
+spec:
+  replicas: 3
+  mysqlVersion: "5.7"
+  
+  # the backupSecretName specify the secret file name which store S3 information,
+  # if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
+  # backupSecretName: 
+  
+  # if you want create mysqlcluster from S3, uncomment and fill the directory in S3 bucket below:
+  # restoreFrom: 
+  
+  mysqlOpts:
+    rootPassword: "RadonDB@123"
+    rootHost: localhost
+    user: radondb_usr
+    password: RadonDB@123
+    database: radondb
+    initTokuDB: true
+
+    # A simple map between string and string.
+    # Such as:
+    #    mysqlConf:
+    #      expire_logs_days: "7"
+    mysqlConf: {}
+
+    resources:
+      requests:
+        cpu: 100m
+        memory: 256Mi
+      limits:
+        cpu: 500m
+        memory: 1Gi
+
+  xenonOpts:
+    image: radondb/xenon:1.1.5-alpha
+    admitDefeatHearbeatCount: 5
+    electionTimeout: 10000
+
+    resources:
+      requests:
+        cpu: 50m
+        memory: 128Mi
+      limits:
+        cpu: 100m
+        memory: 256Mi
+
+  metricsOpts:
+    enabled: false
+    image: prom/mysqld-exporter:v0.12.1
+
+    resources:
+      requests:
+        cpu: 10m
+        memory: 32Mi
+      limits:
+        cpu: 100m
+        memory: 128Mi
+
+  podPolicy:
+    imagePullPolicy: IfNotPresent
+    sidecarImage: radondb/mysql-sidecar:latest
+    busyboxImage: busybox:1.32
+
+    slowLogTail: false
+    auditLogTail: false
+
+    labels: {}
+    annotations: {}
+    affinity: 
+      podAntiAffinity:
+        requiredDuringSchedulingIgnoredDuringExecution:
+        - labelSelector:
+            matchExpressions:
+            - key: mysql.radondb.com/cluster
+              operator: In
+              values:
+              - sample 
+          topologyKey: "kubernetes.io/hostname"
+    priorityClassName: ""
+    tolerations: []
+    schedulerName: ""
+    # extraResources defines quotas for containers other than mysql or xenon.
+    extraResources:
+      requests:
+        cpu: 10m
+        memory: 32Mi
+
+  persistence:
+    enabled: true
+    accessModes:
+    - ReadWriteOnce
+    #storageClass: ""
+    size: 20Gi
diff --git a/docs/en-us/DebugMode.md b/docs/en-us/DebugMode.md
deleted file mode 100644
index f042ab5..0000000
--- a/docs/en-us/DebugMode.md
+++ /dev/null
@@ -1,15 +0,0 @@
-# Debug Mode
-When you want avoid the restart-on-fail loop for mysql container, You should use Debug Mode.
-it just use create a empty file  `/var/lib/mysql/sleep-forever`
-for example:
-```bash
-kubectl exec -it sample-mysql-0 -c mysql -- sh -c 'touch /var/lib/mysql/sleep-forever'
-```
-it make pod sample-mysql-0's mysql container will never restart when mysqld is crashed.
-
-# Remove Debug Mode
-
-```bash
-kubectl exec -it sample-mysql-0 -c mysql -- sh -c 'rm /var/lib/mysql/sleep-forever'
-```
-restart the container
\ No newline at end of file
diff --git a/docs/zh-cn/deploy_radondb-mysql_operator_on_k8s.md b/docs/zh-cn/deploy_radondb-mysql_operator_on_k8s.md
index a23fc72..fc0880d 100644
--- a/docs/zh-cn/deploy_radondb-mysql_operator_on_k8s.md
+++ b/docs/zh-cn/deploy_radondb-mysql_operator_on_k8s.md
@@ -61,7 +61,7 @@ helm install demo radondb/mysql-operator
 
 ### 步骤 3: 部署 RadonDB MySQL 集群
 
-执行以下指令，以默认参数为 CRD `mysqlclusters.mysql.radondb.com` 创建一个实例，即创建 RadonDB MySQL 集群。您可参见[配置参数](./config_para.md)说明，自定义集群部署参数。
+执行以下指令，以默认参数为 CRD `mysqlclusters.mysql.radondb.com` 创建一个实例，即创建 RadonDB MySQL 集群。您可参见[配置参数](../config_para.md)说明，自定义集群部署参数。
 
 ```kubectl
 kubectl apply -f https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1alpha1_mysqlcluster.yaml
@@ -123,28 +123,8 @@ service/sample-mysql             ClusterIP   None            <none>        3306/
 
 以下为客户端与数据库在同一 Kubernetes 集群内，访问 RadonDB MySQL 的方式。
 
-### `clusterIP` 方式
-
-RadonDB MySQL 的高可用读写 IP 指向 leader 服务的 `clusterIP`，高可用只读 IP 指向 follower 服务的 `clusterIP`。
-
-```shell
-mysql -h <clusterIP> -P <mysql_Port> -u <user_name> -p
-```
-
-以下示例用户名为 `radondb_usr`， leader 服务的 clusterIP 为 `10.10.128.136` ，连接示例如下：
-
-```shell
-mysql -h 10.10.128.136 -P 3306 -u radondb_usr -p
-```
-
 ### `service_name` 方式
 
-Kubernetes 集群的 Pod 之间支持通过 `service_name` 方式访问 RadonDB MySQL。
-
-> **说明**
-> 
-> `service_name` 方式不适用于从 Kubernetes 集群的物理机访问数据库 Pod。
-
 * 连接 leader 服务(RadonDB MySQL 主节点)
 
     ```shell
@@ -169,6 +149,20 @@ Kubernetes 集群的 Pod 之间支持通过 `service_name` 方式访问 RadonDB
     mysql -h sample-follower.default -u radondb_usr -p  
     ```
 
+### `clusterIP` 方式
+
+RadonDB MySQL 的高可用读写 IP 指向 leader 服务的 `clusterIP`，高可用只读 IP 指向 follower 服务的 `clusterIP`。
+
+```shell
+mysql -h <clusterIP> -P <mysql_Port> -u <user_name> -p
+```
+
+以下示例用户名为 `radondb_usr`， leader 服务的 clusterIP 为 `10.10.128.136` ，连接示例如下：
+
+```shell
+mysql -h 10.10.128.136 -P 3306 -u radondb_usr -p
+```
+
 ## 卸载
 
 ### 卸载 Operator
diff --git a/docs/zh-cn/deploy_radondb-mysql_operator_on_rancher.md b/docs/zh-cn/deploy_radondb-mysql_operator_on_rancher.md
index 6e91b54..0071387 100644
--- a/docs/zh-cn/deploy_radondb-mysql_operator_on_rancher.md
+++ b/docs/zh-cn/deploy_radondb-mysql_operator_on_rancher.md
@@ -108,7 +108,7 @@ RadonDB MySQL 支持在 Rancher 上安装部署和管理，自动执行与运行
 
 1. 下载 [RadonDB MySQL 集群配置样例](/config/samples/mysql_v1alpha1_mysqlcluster.yaml)，修改 YAML 文件中配置参数值。
 
-   详细配置参数说明，请参见[配置参数](./config_para.md)。
+   详细配置参数说明，请参见[配置参数](../config_para.md)。
 
 2. 在 Rancher 集群管理页面，点击右上角 YAML 导入图标。在弹出的窗口中，导入修改后的 YAML 文件。
 
@@ -155,48 +155,42 @@ RadonDB MySQL 支持在 Rancher 上安装部署和管理，自动执行与运行
 
 以下为客户端与数据库在同一 Rancher 集群内，访问 RadonDB MySQL 的方式。
 
-### `clusterIP` 方式
-
-RadonDB MySQL 的高可用读写 IP 指向 leader 服务的 `clusterIP`，高可用只读 IP 指向 follower 服务的 `clusterIP`。
-
-```shell
-$ mysql -h <clusterIP> -P <mysql_Port> -u <user_name> -p
-```
-
-以下示例用户名为 `radondb_usr`， leader 服务的 clusterIP 为 `10.10.128.136` ，连接示例如下：
-
-```shell
-$ mysql -h 10.10.128.136 -P 3306 -u radondb_usr -p
-```
-
 ### `service_name` 方式
 
-Rancher 集群的 Pod 之间支持通过 `service_name` 方式访问 RadonDB MySQL。
-
-> **说明**
-> 
-> `service_name` 方式不适用于从 Rancher 集群的物理机访问数据库 Pod。
-
 * 连接 leader 服务(RadonDB MySQL 主节点)
 
     ```shell
-    mysql -h <leader_service_name>.<namespace> -u <user_name> -p
+    $ mysql -h <leader_service_name>.<namespace> -u <user_name> -p
     ```
 
    用户名为 `radondb_usr`，release 名为 `sample`，RadonDB MySQL 命名空间为 `default` ，连接示例如下：
 
     ```shell
-    mysql -h sample-leader.default -u radondb_usr -p
+    $ mysql -h sample-leader.default -u radondb_usr -p
     ```
 
 * 连接 follower 服务(RadonDB MySQL 从节点)
 
     ```shell
-    mysql -h <follower_service_name>.<namespace> -u <user_name> -p
+    $ mysql -h <follower_service_name>.<namespace> -u <user_name> -p
     ```
 
    用户名为 `radondb_usr`，release 名为 `sample`，RadonDB MySQL 命名空间为 `default` ，连接示例如下：
 
     ```shell
-    mysql -h sample-follower.default -u radondb_usr -p  
+    $ mysql -h sample-follower.default -u radondb_usr -p  
     ```
+
+### `clusterIP` 方式
+
+RadonDB MySQL 的高可用读写 IP 指向 leader 服务的 `clusterIP`，高可用只读 IP 指向 follower 服务的 `clusterIP`。
+
+```shell
+$ mysql -h <clusterIP> -P <mysql_Port> -u <user_name> -p
+```
+
+以下示例用户名为 `radondb_usr`， leader 服务的 clusterIP 为 `10.10.128.136` ，连接示例如下：
+
+```shell
+$ mysql -h 10.10.128.136 -P 3306 -u radondb_usr -p
+```
diff --git a/hack/xenon/Dockerfile b/hack/xenon/Dockerfile
index 53f1d41..f5131c2 100644
--- a/hack/xenon/Dockerfile
+++ b/hack/xenon/Dockerfile
@@ -4,7 +4,7 @@
 
 FROM golang:1.16 as builder
 
-ARG XENON_BRANCH=master
+ARG XENON_BRANCH=feature_adapt_k8s
 RUN set -ex; \
     mkdir -p /go/src/github.com/radondb; \
     cd /go/src/github.com/radondb; \
diff --git a/mysqlcluster/container/mysql.go b/mysqlcluster/container/mysql.go
index d01f9f2..f0f0a8d 100644
--- a/mysqlcluster/container/mysql.go
+++ b/mysqlcluster/container/mysql.go
@@ -46,11 +46,7 @@ func (c *mysql) getImage() string {
 
 // getCommand get the container command.
 func (c *mysql) getCommand() []string {
-	return []string{
-		"sh",
-		"-c",
-		"while  [ -f '/var/lib/mysql/sleep-forever' ] ;do sleep 2 ; done; /docker-entrypoint.sh mysqld",
-	}
+	return nil
 }
 
 // getEnvVars get the container env.
@@ -92,15 +88,7 @@ func (c *mysql) getLivenessProbe() *corev1.Probe {
 	return &corev1.Probe{
 		Handler: corev1.Handler{
 			Exec: &corev1.ExecAction{
-
-				/* /var/lib/mysql/sleep-forever is used to prevent mysql's container from exiting.
-				kubectl exec -it sample-mysql-0 -c mysql -- sh -c 'touch /var/lib/mysql/sleep-forever'
-				*/
-				Command: []string{
-					"sh",
-					"-c",
-					"if [ -f '/var/lib/mysql/sleep-forever' ] ;then exit 0 ; fi; pgrep mysqld",
-				},
+				Command: []string{"pgrep", "mysqld"},
 			},
 		},
 		InitialDelaySeconds: 30,
@@ -119,7 +107,7 @@ func (c *mysql) getReadinessProbe() *corev1.Probe {
 				Command: []string{
 					"sh",
 					"-c",
-					fmt.Sprintf(`if [ -f '/var/lib/mysql/sleep-forever' ] ;then exit 0 ; fi; test $(mysql --defaults-file=%s -NB -e "SELECT 1") -eq 1`, utils.ConfClientPath),
+					fmt.Sprintf(`test $(mysql --defaults-file=%s -NB -e "SELECT 1") -eq 1`, utils.ConfClientPath),
 				},
 			},
 		},
diff --git a/mysqlcluster/container/xenon.go b/mysqlcluster/container/xenon.go
index eab0b1d..0c8c3fd 100644
--- a/mysqlcluster/container/xenon.go
+++ b/mysqlcluster/container/xenon.go
@@ -76,11 +76,7 @@ func (c *xenon) getLivenessProbe() *corev1.Probe {
 	return &corev1.Probe{
 		Handler: corev1.Handler{
 			Exec: &corev1.ExecAction{
-				Command: []string{
-					"sh",
-					"-c",
-					"pgrep xenon && xenoncli xenon ping",
-				},
+				Command: []string{"pgrep", "xenon"},
 			},
 		},
 		InitialDelaySeconds: 30,
diff --git a/mysqlcluster/syncer/statefulset.go b/mysqlcluster/syncer/statefulset.go
index 15bef66..b8b88a1 100644
--- a/mysqlcluster/syncer/statefulset.go
+++ b/mysqlcluster/syncer/statefulset.go
@@ -323,7 +323,7 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
 			return fmt.Errorf("can't start/continue 'update': pod[%s] is unhealthy", pod.Name)
 		}
 		// Skip if pod is leader.
-		if pod.ObjectMeta.Labels["role"] == string(utils.Leader) && leaderPod.Name == "" {
+		if pod.ObjectMeta.Labels["role"] == "leader" && leaderPod.Name == "" {
 			leaderPod = pod
 			continue
 		}
@@ -355,7 +355,7 @@ func (s *StatefulSetSyncer) mutate() error {
 	for k, v := range s.Spec.PodPolicy.Labels {
 		s.sfs.Spec.Template.ObjectMeta.Labels[k] = v
 	}
-	s.sfs.Spec.Template.ObjectMeta.Labels["role"] = string(utils.Candidate)
+	s.sfs.Spec.Template.ObjectMeta.Labels["role"] = "candidate"
 	s.sfs.Spec.Template.ObjectMeta.Labels["healthy"] = "no"
 
 	s.sfs.Spec.Template.Annotations = s.Spec.PodPolicy.Annotations


Test patch:
diff --git a/mysqlcluster/container/mysql_test.go b/mysqlcluster/container/mysql_test.go
index e502021..8648dcb 100644
--- a/mysqlcluster/container/mysql_test.go
+++ b/mysqlcluster/container/mysql_test.go
@@ -58,8 +58,7 @@ func TestGetMysqlImage(t *testing.T) {
 }
 
 func TestGetMysqlCommand(t *testing.T) {
-	assert.Equal(t, mysqlCase.Command,
-		[]string{"sh", "-c", "while  [ -f '/var/lib/mysql/sleep-forever' ] ;do sleep 2 ; done; /docker-entrypoint.sh mysqld"})
+	assert.Nil(t, mysqlCase.Command)
 }
 
 func TestGetMysqlEnvVar(t *testing.T) {
@@ -110,7 +109,7 @@ func TestGetMysqlLivenessProbe(t *testing.T) {
 	livenessProbe := &corev1.Probe{
 		Handler: corev1.Handler{
 			Exec: &corev1.ExecAction{
-				Command: []string{"sh", "-c", "if [ -f '/var/lib/mysql/sleep-forever' ] ;then exit 0 ; fi; pgrep mysqld"},
+				Command: []string{"pgrep", "mysqld"},
 			},
 		},
 		InitialDelaySeconds: 30,
@@ -126,7 +125,7 @@ func TestGetMysqlReadinessProbe(t *testing.T) {
 	readinessProbe := &corev1.Probe{
 		Handler: corev1.Handler{
 			Exec: &corev1.ExecAction{
-				Command: []string{"sh", "-c", `if [ -f '/var/lib/mysql/sleep-forever' ] ;then exit 0 ; fi; test $(mysql --defaults-file=/etc/mysql/client.conf -NB -e "SELECT 1") -eq 1`},
+				Command: []string{"sh", "-c", `test $(mysql --defaults-file=/etc/mysql/client.conf -NB -e "SELECT 1") -eq 1`},
 			},
 		},
 		InitialDelaySeconds: 10,
diff --git a/mysqlcluster/container/xenon_test.go b/mysqlcluster/container/xenon_test.go
index 322d7e2..6f5c081 100644
--- a/mysqlcluster/container/xenon_test.go
+++ b/mysqlcluster/container/xenon_test.go
@@ -96,11 +96,7 @@ func TestGetXenonLivenessProbe(t *testing.T) {
 	livenessProbe := &corev1.Probe{
 		Handler: corev1.Handler{
 			Exec: &corev1.ExecAction{
-				Command: []string{
-					"sh",
-					"-c",
-					"pgrep xenon && xenoncli xenon ping",
-				},
+				Command: []string{"pgrep", "xenon"},
 			},
 		},
 		InitialDelaySeconds: 30,