flannel.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ETCD_SERVERS=${1:-"http://8.8.8.18:2379"}
  16. FLANNEL_NET=${2:-"172.16.0.0/16"}
  17. cat <<EOF >/opt/kubernetes/cfg/flannel
  18. FLANNEL_ETCD="-etcd-endpoints=${ETCD_SERVERS}"
  19. FLANNEL_ETCD_KEY="-etcd-prefix=/coreos.com/network"
  20. EOF
  21. cat <<EOF >/usr/lib/systemd/system/flannel.service
  22. [Unit]
  23. Description=Flanneld overlay address etcd agent
  24. After=network.target
  25. Before=docker.service
  26. [Service]
  27. EnvironmentFile=-/opt/kubernetes/cfg/flannel
  28. ExecStartPre=/opt/kubernetes/bin/remove-docker0.sh
  29. ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \${FLANNEL_ETCD} \${FLANNEL_ETCD_KEY}
  30. ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -d /run/flannel/docker
  31. Type=notify
  32. [Install]
  33. WantedBy=multi-user.target
  34. RequiredBy=docker.service
  35. EOF
  36. # Store FLANNEL_NET to etcd.
  37. attempt=0
  38. while true; do
  39. /opt/kubernetes/bin/etcdctl --no-sync -C ${ETCD_SERVERS} \
  40. get /coreos.com/network/config >/dev/null 2>&1
  41. if [[ "$?" == 0 ]]; then
  42. break
  43. else
  44. if (( attempt > 600 )); then
  45. echo "timeout for waiting network config" > ~/kube/err.log
  46. exit 2
  47. fi
  48. /opt/kubernetes/bin/etcdctl --no-sync -C ${ETCD_SERVERS} \
  49. mk /coreos.com/network/config "{\"Network\":\"${FLANNEL_NET}\"}" >/dev/null 2>&1
  50. attempt=$((attempt+1))
  51. sleep 3
  52. fi
  53. done
  54. wait
  55. systemctl daemon-reload