Skip to content

Commit 8b4e164

Browse files
committed
iAdd host IP to etcd listen client URLs.
Allow kube-apiserver to use host IP to connect to etcd. Update etcd/migrate to allow additional client listening URLs.
1 parent d5ea0ed commit 8b4e164

File tree

7 files changed

+69
-11
lines changed

7 files changed

+69
-11
lines changed

cluster/gce/config-default.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,8 @@ export KONNECTIVITY_SERVICE_PROXY_PROTOCOL_MODE="${KUBE_KONNECTIVITY_SERVICE_PRO
524524

525525
# Optional: Enable Windows CSI-Proxy
526526
export ENABLE_CSI_PROXY="${ENABLE_CSI_PROXY:-true}"
527+
528+
# ETCD_LISTEN_ON_HOST_IP decides whether etcd servers should also listen on host IP,
529+
# in addition to listening to 127.0.0.1, and whether kube-apiserver should connect to etcd servers
530+
# through host IP.
531+
export ETCD_LISTEN_ON_HOST_IP="${ETCD_LISTEN_ON_HOST_IP:-false}"

cluster/gce/config-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,8 @@ export GCE_UPLOAD_KUBCONFIG_TO_MASTER_METADATA=true
571571

572572
# Optoinal: Enable Windows CSI-Proxy
573573
export ENABLE_CSI_PROXY="${ENABLE_CSI_PROXY:-true}"
574+
575+
# ETCD_LISTEN_ON_HOST_IP decides whether etcd servers should also listen on host IP,
576+
# in addition to listening to 127.0.0.1, and whether kube-apiserver should connect to etcd servers
577+
# through host IP.
578+
export ETCD_LISTEN_ON_HOST_IP="${ETCD_LISTEN_ON_HOST_IP:-false}"

cluster/gce/gci/apiserver_etcd_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type kubeAPIServeETCDEnv struct {
3737
StorageBackend string
3838
StorageMediaType string
3939
CompactionInterval string
40+
HostPrimaryIP string
41+
ETCDListenOnHostIP string
4042
}
4143

4244
func TestServerOverride(t *testing.T) {
@@ -52,7 +54,7 @@ func TestServerOverride(t *testing.T) {
5254
},
5355
},
5456
{
55-
desc: "ETCD-SERVERS and ETCD_SERVERS_OVERRIDES iare set",
57+
desc: "ETCD-SERVERS and ETCD_SERVERS_OVERRIDES are set",
5658
env: kubeAPIServeETCDEnv{
5759
ETCDServers: "ETCDServers",
5860
ETCDServersOverride: "ETCDServersOverrides",
@@ -61,6 +63,17 @@ func TestServerOverride(t *testing.T) {
6163
"--etcd-servers-overrides=ETCDServersOverrides",
6264
},
6365
},
66+
{
67+
desc: "HOST_PRIMARY_IP is set and etcd is set to listen to host IP",
68+
env: kubeAPIServeETCDEnv{
69+
HostPrimaryIP: "HostPrimaryIP",
70+
ETCDListenOnHostIP: "true",
71+
},
72+
want: []string{
73+
"--etcd-servers-overrides=/events#http://HostPrimaryIP:4002",
74+
"--etcd-servers=http://HostPrimaryIP:2379",
75+
},
76+
},
6477
}
6578

6679
for _, tc := range testCases {

cluster/gce/gci/configure-helper.sh

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,8 +1686,8 @@ function start-kube-proxy {
16861686
# Replaces the variables in the etcd manifest file with the real values, and then
16871687
# copy the file to the manifest dir
16881688
# $1: value for variable 'suffix'
1689-
# $2: value for variable 'port'
1690-
# $3: value for variable 'server_port'
1689+
# $2: value for variable 'port', for listening to clients
1690+
# $3: value for variable 'server_port', for etcd peering
16911691
# $4: value for variable 'cpulimit'
16921692
# $5: pod name, which should be either etcd or etcd-events
16931693
function prepare-etcd-manifest {
@@ -1706,19 +1706,39 @@ function prepare-etcd-manifest {
17061706
if [[ -n "${INITIAL_ETCD_CLUSTER_STATE:-}" ]]; then
17071707
cluster_state="${INITIAL_ETCD_CLUSTER_STATE}"
17081708
fi
1709+
1710+
# Configure mTLS for etcd peers.
17091711
if [[ -n "${ETCD_CA_CERT:-}" && -n "${ETCD_PEER_KEY:-}" && -n "${ETCD_PEER_CERT:-}" ]]; then
17101712
etcd_creds=" --peer-trusted-ca-file /etc/srv/kubernetes/etcd-ca.crt --peer-cert-file /etc/srv/kubernetes/etcd-peer.crt --peer-key-file /etc/srv/kubernetes/etcd-peer.key -peer-client-cert-auth "
17111713
etcd_protocol="https"
17121714
fi
17131715

1714-
# mTLS should only be enabled for etcd server but not etcd-events. if $1 suffix is empty, it's etcd server.
1716+
# host_primary_ip is the primary internal IP of the host.
1717+
# Override host primary IP if specifically provided.
1718+
local host_primary_ip
1719+
host_primary_ip="${HOST_PRIMARY_IP:-$(hostname -i)}"
1720+
1721+
# Configure mTLS for clients (e.g. kube-apiserver).
1722+
# mTLS should only be enabled for etcd server but not etcd-events. If $1 suffix is empty, it's etcd server.
1723+
local etcd_listen_metrics_urls=""
17151724
if [[ -z "${suffix}" && -n "${ETCD_APISERVER_CA_KEY:-}" && -n "${ETCD_APISERVER_CA_CERT:-}" && -n "${ETCD_APISERVER_SERVER_KEY:-}" && -n "${ETCD_APISERVER_SERVER_CERT:-}" && -n "${ETCD_APISERVER_CLIENT_KEY:-}" && -n "${ETCD_APISERVER_CLIENT_CERT:-}" ]]; then
17161725
etcd_apiserver_creds=" --client-cert-auth --trusted-ca-file ${ETCD_APISERVER_CA_CERT_PATH} --cert-file ${ETCD_APISERVER_SERVER_CERT_PATH} --key-file ${ETCD_APISERVER_SERVER_KEY_PATH} "
17171726
etcd_apiserver_protocol="https"
17181727
etcd_livenessprobe_port="2382"
1719-
etcd_extra_args+=" --listen-metrics-urls=http://${ETCD_LISTEN_CLIENT_IP:-127.0.0.1}:${etcd_livenessprobe_port} "
1728+
etcd_listen_metrics_urls="http://${ETCD_LISTEN_CLIENT_IP:-127.0.0.1}:${etcd_livenessprobe_port}"
1729+
if [[ ${ETCD_LISTEN_ON_HOST_IP:-} == "true" ]]; then
1730+
etcd_listen_metrics_urls+=",http://${host_primary_ip}:${etcd_livenessprobe_port}"
1731+
fi
1732+
etcd_extra_args+=" --listen-metrics-urls=${etcd_listen_metrics_urls} "
1733+
fi
1734+
1735+
# If etcd is configured to listen on host IP, an additional client listening URL is added.
1736+
local etcd_listen_client_urls="${etcd_apiserver_protocol}://${ETCD_LISTEN_CLIENT_IP:-127.0.0.1}:$2"
1737+
if [[ ${ETCD_LISTEN_ON_HOST_IP:-} == "true" ]] ; then
1738+
etcd_listen_client_urls+=",${etcd_apiserver_protocol}://${host_primary_ip}:$2"
17201739
fi
17211740

1741+
# Generate etcd member URLs.
17221742
for host in $(echo "${INITIAL_ETCD_CLUSTER:-${host_name}}" | tr "," "\n"); do
17231743
etcd_host="etcd-${host}=${etcd_protocol}://${host}:$3"
17241744
if [[ -n "${etcd_cluster}" ]]; then
@@ -1738,6 +1758,7 @@ function prepare-etcd-manifest {
17381758
sed -i -e "s@{{ *etcd_cluster *}}@$etcd_cluster@g" "${temp_file}"
17391759
sed -i -e "s@{{ *liveness_probe_initial_delay *}}@${ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC:-15}@g" "${temp_file}"
17401760
sed -i -e "s@{{ *listen_client_ip *}}@${ETCD_LISTEN_CLIENT_IP:-127.0.0.1}@g" "${temp_file}"
1761+
sed -i -e "s@{{ *etcd_listen_client_urls *}}@${etcd_listen_client_urls:-}@g" "${temp_file}"
17411762
# Get default storage backend from manifest file.
17421763
local -r default_storage_backend=$( \
17431764
grep -o "{{ *pillar\.get('storage_backend', '\(.*\)') *}}" "${temp_file}" | \

cluster/gce/gci/configure-kubeapiserver.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@
1414
# limitations under the License.
1515

1616

17-
# Configures etcd related flags of kube-apiserver.
17+
# Configures etcd related parameters of kube-apiserver.
1818
function configure-etcd-params {
1919
local -n params_ref=$1
2020

21+
local host_ip="127.0.0.1"
22+
# If etcd is configured to listen on host IP,
23+
# host_ip is set to the primary internal IP of host VM.
24+
if [[ ${ETCD_LISTEN_ON_HOST_IP:-} == "true" ]] ; then
25+
host_ip="${HOST_PRIMARY_IP:-$(hostname -i)}"
26+
fi
27+
28+
# Configure the main etcd.
2129
if [[ -n "${ETCD_APISERVER_CA_KEY:-}" && -n "${ETCD_APISERVER_CA_CERT:-}" && -n "${ETCD_APISERVER_SERVER_KEY:-}" && -n "${ETCD_APISERVER_SERVER_CERT:-}" && -n "${ETCD_APISERVER_CLIENT_KEY:-}" && -n "${ETCD_APISERVER_CLIENT_CERT:-}" ]]; then
22-
params_ref+=" --etcd-servers=${ETCD_SERVERS:-https://127.0.0.1:2379}"
30+
params_ref+=" --etcd-servers=${ETCD_SERVERS:-https://${host_ip}:2379}"
2331
params_ref+=" --etcd-cafile=${ETCD_APISERVER_CA_CERT_PATH}"
2432
params_ref+=" --etcd-certfile=${ETCD_APISERVER_CLIENT_CERT_PATH}"
2533
params_ref+=" --etcd-keyfile=${ETCD_APISERVER_CLIENT_KEY_PATH}"
2634
elif [[ -z "${ETCD_APISERVER_CA_KEY:-}" && -z "${ETCD_APISERVER_CA_CERT:-}" && -z "${ETCD_APISERVER_SERVER_KEY:-}" && -z "${ETCD_APISERVER_SERVER_CERT:-}" && -z "${ETCD_APISERVER_CLIENT_KEY:-}" && -z "${ETCD_APISERVER_CLIENT_CERT:-}" ]]; then
27-
params_ref+=" --etcd-servers=${ETCD_SERVERS:-http://127.0.0.1:2379}"
35+
params_ref+=" --etcd-servers=${ETCD_SERVERS:-http://${host_ip}:2379}"
2836
echo "WARNING: ALL of ETCD_APISERVER_CA_KEY, ETCD_APISERVER_CA_CERT, ETCD_APISERVER_SERVER_KEY, ETCD_APISERVER_SERVER_CERT, ETCD_APISERVER_CLIENT_KEY and ETCD_APISERVER_CLIENT_CERT are missing, mTLS between etcd server and kube-apiserver is not enabled."
2937
else
3038
echo "ERROR: Some of ETCD_APISERVER_CA_KEY, ETCD_APISERVER_CA_CERT, ETCD_APISERVER_SERVER_KEY, ETCD_APISERVER_SERVER_CERT, ETCD_APISERVER_CLIENT_KEY and ETCD_APISERVER_CLIENT_CERT are missing, mTLS between etcd server and kube-apiserver cannot be enabled. Please provide all mTLS credential."
3139
exit 1
3240
fi
3341

42+
# Configure the event log etcd.
3443
if [[ -z "${ETCD_SERVERS:-}" ]]; then
35-
params_ref+=" --etcd-servers-overrides=${ETCD_SERVERS_OVERRIDES:-/events#http://127.0.0.1:4002}"
44+
params_ref+=" --etcd-servers-overrides=${ETCD_SERVERS_OVERRIDES:-/events#http://${host_ip}:4002}"
3645
elif [[ -n "${ETCD_SERVERS_OVERRIDES:-}" ]]; then
3746
params_ref+=" --etcd-servers-overrides=${ETCD_SERVERS_OVERRIDES:-}"
3847
fi

cluster/gce/gci/testdata/kube-apiserver/etcd.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ readonly ETCD_SERVERS_OVERRIDES={{.ETCDServersOverride}}
1313
readonly STORAGE_BACKEND={{.StorageBackend}}
1414
readonly STORAGE_MEDIA_TYPE={{.StorageMediaType}}
1515
readonly ETCD_COMPACTION_INTERVAL_SEC={{.CompactionInterval}}
16+
readonly HOST_PRIMARY_IP={{.HostPrimaryIP}}
17+
readonly ETCD_LISTEN_ON_HOST_IP={{.ETCDListenOnHostIP}}

cluster/gce/manifests/etcd.manifest

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"command": [
2727
"/bin/sh",
2828
"-c",
29-
"set -o errexit; if [ -e /usr/local/bin/migrate-if-needed.sh ]; then /usr/local/bin/migrate-if-needed.sh 1>>/var/log/etcd{{ suffix }}.log 2>&1; fi; exec /usr/local/bin/etcd --name etcd-{{ hostname }} --listen-peer-urls {{ etcd_protocol }}://{{ host_ip }}:{{ server_port }} --initial-advertise-peer-urls {{ etcd_protocol }}://{{ hostname }}:{{ server_port }} --advertise-client-urls {{ etcd_apiserver_protocol }}://127.0.0.1:{{ port }} --listen-client-urls {{ etcd_apiserver_protocol }}://{{ listen_client_ip }}:{{ port }} {{ quota_bytes }} --data-dir /var/etcd/data{{ suffix }} --initial-cluster-state {{ cluster_state }} --initial-cluster {{ etcd_cluster }} {{ etcd_creds }} {{ etcd_apiserver_creds }} {{ etcd_extra_args }} 1>>/var/log/etcd{{ suffix }}.log 2>&1"
29+
"set -o errexit; if [ -e /usr/local/bin/migrate-if-needed.sh ]; then /usr/local/bin/migrate-if-needed.sh 1>>/var/log/etcd{{ suffix }}.log 2>&1; fi; exec /usr/local/bin/etcd --name etcd-{{ hostname }} --listen-peer-urls {{ etcd_protocol }}://{{ host_ip }}:{{ server_port }} --initial-advertise-peer-urls {{ etcd_protocol }}://{{ hostname }}:{{ server_port }} --advertise-client-urls {{ etcd_apiserver_protocol }}://127.0.0.1:{{ port }} --listen-client-urls {{ etcd_listen_client_urls }} {{ quota_bytes }} --data-dir /var/etcd/data{{ suffix }} --initial-cluster-state {{ cluster_state }} --initial-cluster {{ etcd_cluster }} {{ etcd_creds }} {{ etcd_apiserver_creds }} {{ etcd_extra_args }} 1>>/var/log/etcd{{ suffix }}.log 2>&1"
3030
],
3131
"env": [
3232
{ "name": "TARGET_STORAGE",
@@ -58,8 +58,11 @@
5858
},
5959
{ "name": "ETCD_HOSTNAME",
6060
"value": "{{ hostname }}"
61+
},
62+
{ "name": "LISTEN_CLIENT_URLS",
63+
"value": "{{ etcd_listen_client_urls }}"
6164
}
62-
],
65+
],
6366
"livenessProbe": {
6467
"httpGet": {
6568
"host": "127.0.0.1",

0 commit comments

Comments
 (0)