Skip to content

Commit

Permalink
Support hostNetwork (pingcap#774)
Browse files Browse the repository at this point in the history
* Support hostNetwork

* Use POD_NAME instead of HOSTNAME

* Update chart

* e2e test

* remove PodSpec
  • Loading branch information
cofyc committed Nov 20, 2019
1 parent 860eb54 commit 6757338
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions charts/tidb-cluster/templates/scripts/_start_pd.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fi

# the general form of variable PEER_SERVICE_NAME is: "<clusterName>-pd-peer"
cluster_name=`echo ${PEER_SERVICE_NAME} | sed 's/-pd-peer//'`
domain="${HOSTNAME}.${PEER_SERVICE_NAME}.${NAMESPACE}.svc"
domain="${POD_NAME}.${PEER_SERVICE_NAME}.${NAMESPACE}.svc"
discovery_url="${cluster_name}-discovery.${NAMESPACE}.svc:10261"
encoded_domain_url=`echo ${domain}:2380 | base64 | tr "\n" " " | sed "s/ //g"`

Expand All @@ -57,7 +57,7 @@ while true; do
done

ARGS="--data-dir=/var/lib/pd \
--name=${HOSTNAME} \
--name=${POD_NAME} \
--peer-urls=http://0.0.0.0:2380 \
--advertise-peer-urls=http://${domain}:2380 \
--client-urls=http://0.0.0.0:2379 \
Expand Down
2 changes: 1 addition & 1 deletion charts/tidb-cluster/templates/scripts/_start_tikv.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ then
fi

ARGS="--pd=${CLUSTER_NAME}-pd:2379 \
--advertise-addr=${HOSTNAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc:20160 \
--advertise-addr=${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc:20160 \
--addr=0.0.0.0:20160 \
--status-addr=0.0.0.0:20180 \
--data-dir=/var/lib/tikv \
Expand Down
3 changes: 3 additions & 0 deletions charts/tidb-cluster/templates/tidb-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ spec:
podSecurityContext:
{{ toYaml .Values.pd.podSecurityContext | indent 6}}
{{- end }}
hostNetwork: {{ .Values.pd.hostNetwork }}
tikv:
replicas: {{ .Values.tikv.replicas }}
image: {{ .Values.tikv.image }}
Expand Down Expand Up @@ -76,6 +77,7 @@ spec:
{{ toYaml .Values.tikv.podSecurityContext | indent 6}}
{{- end }}
maxFailoverCount: {{ .Values.tikv.maxFailoverCount | default 3 }}
hostNetwork: {{ .Values.tikv.hostNetwork }}
tidb:
replicas: {{ .Values.tidb.replicas }}
image: {{ .Values.tidb.image }}
Expand All @@ -99,6 +101,7 @@ spec:
podSecurityContext:
{{ toYaml .Values.tidb.podSecurityContext | indent 6}}
{{- end }}
hostNetwork: {{ .Values.tidb.hostNetwork }}
binlogEnabled: {{ .Values.binlog.pump.create | default false }}
maxFailoverCount: {{ .Values.tidb.maxFailoverCount | default 3 }}
separateSlowLog: {{ .Values.tidb.separateSlowLog | default false }}
Expand Down
12 changes: 12 additions & 0 deletions charts/tidb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ pd:
# refer to https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
podSecurityContext: {}

# Use the host's network namespace if enabled.
# Default to false.
hostNetwork: false

tikv:
# Please refer to https://github.com/tikv/tikv/blob/master/etc/config-template.toml for the default
# tikv configurations (change to the tags of your tikv version),
Expand Down Expand Up @@ -242,6 +246,10 @@ tikv:
# refer to https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
podSecurityContext: {}

# Use the host's network namespace if enabled.
# Default to false.
hostNetwork: false

tidb:
# Please refer to https://github.com/pingcap/tidb/blob/master/config/config.toml.example for the default
# tidb configurations(change to the tags of your tidb version),
Expand Down Expand Up @@ -303,6 +311,10 @@ tidb:
# refer to https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
podSecurityContext: {}

# Use the host's network namespace if enabled.
# Default to false.
hostNetwork: false

maxFailoverCount: 3
service:
type: NodePort
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/pingcap.com/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type PodAttributesSpec struct {
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
PodSecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
}

// Service represent service type used in TidbCluster
Expand Down
15 changes: 15 additions & 0 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster)
}
}

dnsPolicy := corev1.DNSClusterFirst // same as k8s defaults
if tc.Spec.PD.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}

pdSet := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: setName,
Expand All @@ -503,6 +508,8 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster)
SchedulerName: tc.Spec.SchedulerName,
Affinity: tc.Spec.PD.Affinity,
NodeSelector: tc.Spec.PD.NodeSelector,
HostNetwork: tc.Spec.PD.HostNetwork,
DNSPolicy: dnsPolicy,
Containers: []corev1.Container{
{
Name: v1alpha1.PDMemberType.String(),
Expand Down Expand Up @@ -532,6 +539,14 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster)
},
},
},
{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: "PEER_SERVICE_NAME",
Value: controller.PDPeerMemberName(tcName),
Expand Down
6 changes: 6 additions & 0 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbClust
},
})

dnsPolicy := corev1.DNSClusterFirst // same as k8s defaults
if tc.Spec.PD.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}

tidbLabel := label.New().Instance(instanceName).TiDB()
podAnnotations := CombineAnnotations(controller.AnnProm(10080), tc.Spec.TiDB.Annotations)
tidbSet := &apps.StatefulSet{
Expand All @@ -355,6 +360,7 @@ func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbClust
Tolerations: tc.Spec.TiDB.Tolerations,
Volumes: vols,
SecurityContext: tc.Spec.TiDB.PodSecurityContext,
HostNetwork: tc.Spec.PD.HostNetwork,
},
},
ServiceName: controller.TiDBPeerMemberName(tcName),
Expand Down
15 changes: 15 additions & 0 deletions pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
storageClassName = controller.DefaultStorageClassName
}

dnsPolicy := corev1.DNSClusterFirst // same as k8s defaults
if tc.Spec.PD.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}

tikvset := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: setName,
Expand All @@ -339,6 +344,8 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
SchedulerName: tc.Spec.SchedulerName,
Affinity: tc.Spec.TiKV.Affinity,
NodeSelector: tc.Spec.TiKV.NodeSelector,
HostNetwork: tc.Spec.PD.HostNetwork,
DNSPolicy: dnsPolicy,
Containers: []corev1.Container{
{
Name: v1alpha1.TiKVMemberType.String(),
Expand Down Expand Up @@ -366,6 +373,14 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
},
},
},
{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: "CLUSTER_NAME",
Value: tcName,
Expand Down
11 changes: 11 additions & 0 deletions tests/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func (tc *TidbClusterConfig) ScaleTiDB(replicas uint) *TidbClusterConfig {
return tc
}

func (tc *TidbClusterConfig) RunInHost(flag bool) *TidbClusterConfig {
val := "false"
if flag {
val = "true"
}
tc.set("pd.hostNetwork", val)
tc.set("tikv.hostNetwork", val)
tc.set("tidb.hostNetwork", val)
return tc
}

func (tc *TidbClusterConfig) UpgradePD(image string) *TidbClusterConfig {
tc.PDImage = image
return tc
Expand Down
10 changes: 10 additions & 0 deletions tests/cmd/e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func main() {
UpdateTiDBTokenLimit(cfg.TiDBTokenLimit)
oa.UpgradeTidbClusterOrDie(cluster1)
oa.CheckTidbClusterStatusOrDie(cluster1)

// switch to host network
cluster1.RunInHost(true)
oa.UpgradeTidbClusterOrDie(cluster1)
oa.CheckTidbClusterStatusOrDie(cluster1)

// switch to pod network
cluster1.RunInHost(false)
oa.UpgradeTidbClusterOrDie(cluster1)
oa.CheckTidbClusterStatusOrDie(cluster1)
}
fn2 := func(wg *sync.WaitGroup) {
defer wg.Done()
Expand Down

0 comments on commit 6757338

Please sign in to comment.