Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ip alias route on kubernetes-master during booting #89543

Merged
merged 1 commit into from Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions cluster/gce/gci/kube-master-internal-route.sh
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

METADATA_ENDPOINT="http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-master-internal-ip"
METADATA_HEADER="Metadata-Flavor: Google"
ip=$(curl -s --fail ${METADATA_ENDPOINT} -H "${METADATA_HEADER}")
if [ -n "$ip" ];
then
# Check if route is already set if not set it
if ! sudo ip route show table local | grep -q "$(echo "$ip" | cut -d'/' -f 1)";
then
sudo ip route add to local "${ip}/32" dev "$(ip route | grep default | awk '{print $5}')"
fi
fi
1 change: 1 addition & 0 deletions cluster/gce/gci/master-helper.sh
Expand Up @@ -157,6 +157,7 @@ function create-master-instance-internal() {
metadata="${metadata},gci-docker-version=${KUBE_TEMP}/gci-docker-version.txt"
metadata="${metadata},kube-master-certs=${KUBE_TEMP}/kube-master-certs.yaml"
metadata="${metadata},cluster-location=${KUBE_TEMP}/cluster-location.txt"
metadata="${metadata},kube-master-internal-route=${KUBE_ROOT}/cluster/gce/gci/kube-master-internal-route.sh"
metadata="${metadata},${MASTER_EXTRA_METADATA}"

local disk="name=${master_name}-pd"
Expand Down
19 changes: 19 additions & 0 deletions cluster/gce/gci/master.yaml
Expand Up @@ -23,6 +23,24 @@ write_files:
[Install]
WantedBy=kubernetes.target

- path: /etc/systemd/system/kube-master-internal-route.service
permissions: 0644
owner: root
content: |
[Unit]
Description=Configure kube internal route
After=kube-master-installation.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/usr/bin/curl --fail --retry 5 --retry-delay 3 --silent --show-error -H "X-Google-Metadata-Request: True" -o /home/kubernetes/bin/kube-master-internal-route.sh http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-master-internal-route
ExecStartPre=/bin/chmod 544 /home/kubernetes/bin/kube-master-internal-route.sh
ExecStart=/home/kubernetes/bin/kube-master-internal-route.sh

[Install]
WantedBy=kubernetes.target

- path: /etc/systemd/system/kube-master-configuration.service
permissions: 0644
owner: root
Expand Down Expand Up @@ -119,6 +137,7 @@ write_files:
runcmd:
- systemctl daemon-reload
- systemctl enable kube-master-installation.service
- systemctl enable kube-master-internal-route.service
- systemctl enable kube-master-configuration.service
- systemctl enable kube-container-runtime-monitor.service
- systemctl enable kubelet-monitor.service
Expand Down
4 changes: 3 additions & 1 deletion cluster/gce/util.sh
Expand Up @@ -2946,7 +2946,8 @@ function attach-internal-master-ip() {
echo "Setting ${name}'s aliases to '${aliases}' (added ${ip})"
# Attach ${ip} to ${name}
gcloud compute instances network-interfaces update "${name}" --project "${PROJECT}" --zone "${zone}" --aliases="${aliases}"
run-gcloud-command "${name}" "${zone}" 'sudo ip route add to local '${ip}'/32 dev $(ip route | grep default | awk '\''{print $5}'\'')' || true
gcloud compute instances add-metadata "${name}" --zone "${zone}" --metadata=kube-master-internal-ip="${ip}"
run-gcloud-command "${name}" "${zone}" 'sudo /bin/bash /home/kubernetes/bin/kube-master-internal-route.sh' || true
return $?
}

Expand All @@ -2964,6 +2965,7 @@ function detach-internal-master-ip() {
echo "Setting ${name}'s aliases to '${aliases}' (removed ${ip})"
# Detach ${MASTER_NAME}-internal-ip from ${name}
gcloud compute instances network-interfaces update "${name}" --project "${PROJECT}" --zone "${zone}" --aliases="${aliases}"
gcloud compute instances remove-metadata "${name}" --zone "${zone}" --keys=kube-master-internal-ip
run-gcloud-command "${name}" "${zone}" 'sudo ip route del to local '${ip}'/32 dev $(ip route | grep default | awk '\''{print $5}'\'')' || true
return $?
}
Expand Down