Skip to content

Commit

Permalink
WIP: Cherry-pick hostnetwork support (#774) to release-1.0 (#1202)
Browse files Browse the repository at this point in the history
* Support hostNetwork (#774)

* Support hostNetwork

* Use POD_NAME instead of HOSTNAME

* Update chart

* e2e test

* remove PodSpec

* Use HOSTNAME if POD_NAME is unset for backward compatibility. (#818)

* fix bug (#1032)

* remove tests

* add POD_NAME env conditionally
  • Loading branch information
cofyc committed Nov 21, 2019
1 parent 860eb54 commit 6b7bd19
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 56 deletions.
6 changes: 4 additions & 2 deletions charts/tidb-cluster/templates/scripts/_start_pd.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ then
tail -f /dev/null
fi

# Use HOSTNAME if POD_NAME is unset for backward compatibility.
POD_NAME=${POD_NAME:-$HOSTNAME}
# 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 +59,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
4 changes: 3 additions & 1 deletion charts/tidb-cluster/templates/scripts/_start_tikv.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ then
tail -f /dev/null
fi

# Use HOSTNAME if POD_NAME is unset for backward compatibility.
POD_NAME=${POD_NAME:-$HOSTNAME}
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
69 changes: 43 additions & 26 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,46 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster)
}
}

env := []corev1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "PEER_SERVICE_NAME",
Value: controller.PDPeerMemberName(tcName),
},
{
Name: "SERVICE_NAME",
Value: controller.PDMemberName(tcName),
},
{
Name: "SET_NAME",
Value: setName,
},
{
Name: "TZ",
Value: tc.Spec.Timezone,
},
}

dnsPolicy := corev1.DNSClusterFirst // same as k8s defaults
if tc.Spec.PD.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
env = append(env, corev1.EnvVar{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
})
}

pdSet := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: setName,
Expand All @@ -503,6 +543,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 All @@ -523,32 +565,7 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster)
},
VolumeMounts: volMounts,
Resources: util.ResourceRequirement(tc.Spec.PD.ContainerSpec),
Env: []corev1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "PEER_SERVICE_NAME",
Value: controller.PDPeerMemberName(tcName),
},
{
Name: "SERVICE_NAME",
Value: controller.PDMemberName(tcName),
},
{
Name: "SET_NAME",
Value: setName,
},
{
Name: "TZ",
Value: tc.Spec.Timezone,
},
},
Env: env,
},
},
RestartPolicy: corev1.RestartPolicyAlways,
Expand Down
7 changes: 7 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.TiDB.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,8 @@ func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbClust
Tolerations: tc.Spec.TiDB.Tolerations,
Volumes: vols,
SecurityContext: tc.Spec.TiDB.PodSecurityContext,
HostNetwork: tc.Spec.TiDB.HostNetwork,
DNSPolicy: dnsPolicy,
},
},
ServiceName: controller.TiDBPeerMemberName(tcName),
Expand Down
71 changes: 44 additions & 27 deletions pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/listers/apps/v1"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/kubernetes/pkg/kubelet/apis"
)
Expand Down Expand Up @@ -320,6 +320,46 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
storageClassName = controller.DefaultStorageClassName
}

env := []corev1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "CLUSTER_NAME",
Value: tcName,
},
{
Name: "HEADLESS_SERVICE_NAME",
Value: headlessSvcName,
},
{
Name: "CAPACITY",
Value: capacity,
},
{
Name: "TZ",
Value: tc.Spec.Timezone,
},
}

dnsPolicy := corev1.DNSClusterFirst // same as k8s defaults
if tc.Spec.TiKV.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
env = append(env, corev1.EnvVar{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
})
}

tikvset := &apps.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: setName,
Expand All @@ -339,6 +379,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.TiKV.HostNetwork,
DNSPolicy: dnsPolicy,
Containers: []corev1.Container{
{
Name: v1alpha1.TiKVMemberType.String(),
Expand All @@ -357,32 +399,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
},
VolumeMounts: volMounts,
Resources: util.ResourceRequirement(tc.Spec.TiKV.ContainerSpec),
Env: []corev1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "CLUSTER_NAME",
Value: tcName,
},
{
Name: "HEADLESS_SERVICE_NAME",
Value: headlessSvcName,
},
{
Name: "CAPACITY",
Value: capacity,
},
{
Name: "TZ",
Value: tc.Spec.Timezone,
},
},
Env: env,
},
},
RestartPolicy: corev1.RestartPolicyAlways,
Expand Down

0 comments on commit 6b7bd19

Please sign in to comment.