diff --git a/charts/tidb-cluster/templates/scripts/_start_tidb.sh.tpl b/charts/tidb-cluster/templates/scripts/_start_tidb.sh.tpl old mode 100644 new mode 100755 index ee79a7594e..f68fb7ee17 --- a/charts/tidb-cluster/templates/scripts/_start_tidb.sh.tpl +++ b/charts/tidb-cluster/templates/scripts/_start_tidb.sh.tpl @@ -26,7 +26,12 @@ then tail -f /dev/null fi +# Use HOSTNAME if POD_NAME is unset for backward compatibility. +POD_NAME=${POD_NAME:-$HOSTNAME} ARGS="--store=tikv \ +{{- if .Values.tidb.enableAdvertiseAddress | default false }} +--advertise-address=${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc \ +{{- end }} --host=0.0.0.0 \ --path=${CLUSTER_NAME}-pd:2379 \ --config=/etc/tidb/tidb.toml diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index ac779ac161..1cf1a0ab8b 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -399,6 +399,10 @@ tidb: # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true + + # Add --advertise-address to TiDB's startup parameters + enableAdvertiseAddress: false + slowLogTailer: image: busybox:1.26.2 resources: diff --git a/manifests/crd.yaml b/manifests/crd.yaml index 2dfd896c72..49a83c1836 100644 --- a/manifests/crd.yaml +++ b/manifests/crd.yaml @@ -3782,6 +3782,10 @@ spec: cluster-level updateStrategy if present Optional: Defaults to cluster-level setting' type: string + enableAdvertiseAddress: + description: 'Add --advertise-address to TiDB''s startup parameters + Optional: Defaults to false' + type: boolean hostNetwork: description: 'Whether Hostnetwork of the component is enabled. Override the cluster-level setting if present Optional: Defaults to cluster-level diff --git a/pkg/apis/pingcap/v1alpha1/openapi_generated.go b/pkg/apis/pingcap/v1alpha1/openapi_generated.go index 9c80b21198..c13249bf6c 100644 --- a/pkg/apis/pingcap/v1alpha1/openapi_generated.go +++ b/pkg/apis/pingcap/v1alpha1/openapi_generated.go @@ -3595,6 +3595,13 @@ func schema_pkg_apis_pingcap_v1alpha1_TiDBSpec(ref common.ReferenceCallback) com Format: "", }, }, + "enableAdvertiseAddress": { + SchemaProps: spec.SchemaProps{ + Description: "Add --advertise-address to TiDB's startup parameters Optional: Defaults to false", + Type: []string{"boolean"}, + Format: "", + }, + }, "maxFailoverCount": { SchemaProps: spec.SchemaProps{ Description: "MaxFailoverCount limit the max replicas could be added in failover, 0 means unlimited Optional: Defaults to 0", diff --git a/pkg/apis/pingcap/v1alpha1/tidbcluster.go b/pkg/apis/pingcap/v1alpha1/tidbcluster.go index 67d8cf2e23..cb87fc3432 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbcluster.go +++ b/pkg/apis/pingcap/v1alpha1/tidbcluster.go @@ -22,13 +22,14 @@ import ( const ( // defaultHelperImage is default image of helper - defaultHelperImage = "busybox:1.26.2" - defaultTimeZone = "UTC" - defaultEnableTLSCluster = false - defaultEnableTLSClient = false - defaultExposeStatus = true - defaultSeparateSlowLog = true - defaultEnablePVReclaim = false + defaultHelperImage = "busybox:1.26.2" + defaultTimeZone = "UTC" + defaultEnableTLSCluster = false + defaultEnableTLSClient = false + defaultExposeStatus = true + defaultSeparateSlowLog = true + defaultEnablePVReclaim = false + defaultEnableTiDBAdvertiseAddress = false ) var ( @@ -342,6 +343,13 @@ func (tidb *TiDBSpec) IsTLSClientEnabled() bool { return tidb.TLSClient != nil && tidb.TLSClient.Enabled } +func (tidb *TiDBSpec) IsAdvertiseAddressEnabled() bool { + if tidb.EnableAdvertiseAddress == nil { + return defaultEnableTiDBAdvertiseAddress + } + return *tidb.EnableAdvertiseAddress +} + func (tidb *TiDBSpec) IsUserGeneratedCertificate() bool { return tidb.IsTLSClientEnabled() && tidb.TLSClient.SecretName != "" } diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index ea2882b003..549fcc7c20 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -296,6 +296,11 @@ type TiDBSpec struct { // +optional BinlogEnabled *bool `json:"binlogEnabled,omitempty"` + // Add --advertise-address to TiDB's startup parameters + // Optional: Defaults to false + // +optional + EnableAdvertiseAddress *bool `json:"enableAdvertiseAddress,omitempty"` + // MaxFailoverCount limit the max replicas could be added in failover, 0 means unlimited // Optional: Defaults to 0 // +kubebuilder:validation:Minimum=0 diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index ecb5c5ec15..63eb953525 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -2521,6 +2521,11 @@ func (in *TiDBSpec) DeepCopyInto(out *TiDBSpec) { *out = new(bool) **out = **in } + if in.EnableAdvertiseAddress != nil { + in, out := &in.EnableAdvertiseAddress, &out.EnableAdvertiseAddress + *out = new(bool) + **out = **in + } if in.MaxFailoverCount != nil { in, out := &in.MaxFailoverCount, &out.MaxFailoverCount *out = new(int32) diff --git a/pkg/manager/member/template.go b/pkg/manager/member/template.go index f0e5a4c90a..fbc2758240 100644 --- a/pkg/manager/member/template.go +++ b/pkg/manager/member/template.go @@ -47,7 +47,12 @@ then tail -f /dev/null fi +# Use HOSTNAME if POD_NAME is unset for backward compatibility. +POD_NAME=${POD_NAME:-$HOSTNAME} ARGS="--store=tikv \ +{{- if .EnableAdvertiseAddress }} +--advertise-address=${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc \ +{{- end }} --host=0.0.0.0 \ --path=${CLUSTER_NAME}-pd:2379 \ --config=/etc/tidb/tidb.toml @@ -74,10 +79,11 @@ exec /tidb-server ${ARGS} `)) type TidbStartScriptModel struct { - ClusterName string - EnablePlugin bool - PluginDirectory string - PluginList string + ClusterName string + EnableAdvertiseAddress bool + EnablePlugin bool + PluginDirectory string + PluginList string } func RenderTiDBStartScript(model *TidbStartScriptModel) (string, error) { diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index f4a2b144b9..eb113c213c 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -456,10 +456,11 @@ func getTiDBConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) { plugins := tc.Spec.TiDB.Plugins startScript, err := RenderTiDBStartScript(&TidbStartScriptModel{ - ClusterName: tc.Name, - EnablePlugin: len(plugins) > 0, - PluginDirectory: "/plugins", - PluginList: strings.Join(plugins, ","), + ClusterName: tc.Name, + EnableAdvertiseAddress: tc.Spec.TiDB.IsAdvertiseAddressEnabled(), + EnablePlugin: len(plugins) > 0, + PluginDirectory: "/plugins", + PluginList: strings.Join(plugins, ","), }) if err != nil { return nil, err @@ -580,6 +581,7 @@ func getNewTiDBHeadlessServiceForTidbCluster(tc *v1alpha1.TidbCluster) *corev1.S func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) *apps.StatefulSet { ns := tc.GetNamespace() tcName := tc.GetName() + headlessSvcName := controller.TiDBPeerMemberName(tcName) baseTiDBSpec := tc.BaseTiDBSpec() instanceName := tc.GetInstanceName() tidbConfigMap := controller.MemberConfigMapName(tc, v1alpha1.TiDBMemberType) @@ -730,10 +732,37 @@ func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) }, } + if tc.Spec.TiDB.IsAdvertiseAddressEnabled() { + advertiseEnvs := []corev1.EnvVar{ + { + Name: "POD_NAME", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.name", + }, + }, + }, + { + Name: "NAMESPACE", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.namespace", + }, + }, + }, + { + Name: "HEADLESS_SERVICE_NAME", + Value: headlessSvcName, + }, + } + envs = append(envs, advertiseEnvs...) + } + scheme := corev1.URISchemeHTTP if tc.IsTLSClusterEnabled() { scheme = corev1.URISchemeHTTPS } + containers = append(containers, corev1.Container{ Name: v1alpha1.TiDBMemberType.String(), Image: tc.TiDBImage(),