github.com/enmand/kubernetes@v1.2.0-alpha.0/docs/getting-started-guides/docker-multinode/master.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2015 The Kubernetes Authors All rights reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # A scripts to install k8s worker node.
    18  # Author @wizard_cxy @reouser
    19  
    20  set -e
    21  
    22  # Make sure docker daemon is running
    23  if ( ! ps -ef | grep "/usr/bin/docker" | grep -v 'grep' &> /dev/null ); then
    24      echo "Docker is not running on this machine!"
    25      exit 1
    26  fi
    27  
    28  # Make sure k8s version env is properly set
    29  if [ -z ${K8S_VERSION} ]; then
    30      K8S_VERSION="1.0.3"
    31      echo "K8S_VERSION is not set, using default: ${K8S_VERSION}"
    32  else
    33      echo "k8s version is set to: ${K8S_VERSION}"
    34  fi
    35  
    36  
    37  # Run as root
    38  if [ "$(id -u)" != "0" ]; then
    39      echo >&2 "Please run as root"
    40      exit 1
    41  fi
    42  
    43  # Check if a command is valid
    44  command_exists() {
    45      command -v "$@" > /dev/null 2>&1
    46  }
    47  
    48  lsb_dist=""
    49  
    50  # Detect the OS distro, we support ubuntu, debian, mint, centos, fedora dist
    51  detect_lsb() {
    52      case "$(uname -m)" in
    53      *64)
    54          ;;
    55      *)
    56          echo "Error: We currently only support 64-bit platforms."       
    57          exit 1
    58          ;;
    59      esac
    60  
    61      if command_exists lsb_release; then
    62          lsb_dist="$(lsb_release -si)"
    63      fi
    64      if [ -z ${lsb_dist} ] && [ -r /etc/lsb-release ]; then
    65          lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
    66      fi
    67      if [ -z ${lsb_dist} ] && [ -r /etc/debian_version ]; then
    68          lsb_dist='debian'
    69      fi
    70      if [ -z ${lsb_dist} ] && [ -r /etc/fedora-release ]; then
    71          lsb_dist='fedora'
    72      fi
    73      if [ -z ${lsb_dist} ] && [ -r /etc/os-release ]; then
    74          lsb_dist="$(. /etc/os-release && echo "$ID")"
    75      fi
    76  
    77      lsb_dist="$(echo ${lsb_dist} | tr '[:upper:]' '[:lower:]')"
    78  }
    79  
    80  
    81  # Start the bootstrap daemon
    82  bootstrap_daemon() {
    83      sudo -b docker -d -H unix:///var/run/docker-bootstrap.sock -p /var/run/docker-bootstrap.pid --iptables=false --ip-masq=false --bridge=none --graph=/var/lib/docker-bootstrap 2> /var/log/docker-bootstrap.log 1> /dev/null
    84      
    85      sleep 5
    86  }
    87  
    88  # Start k8s components in containers
    89  DOCKER_CONF=""
    90  
    91  start_k8s(){
    92      # Start etcd 
    93      docker -H unix:///var/run/docker-bootstrap.sock run --restart=always --net=host -d gcr.io/google_containers/etcd:2.0.12 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-addr=0.0.0.0:4001 --data-dir=/var/etcd/data
    94  
    95      sleep 5
    96      # Set flannel net config
    97      docker -H unix:///var/run/docker-bootstrap.sock run --net=host gcr.io/google_containers/etcd:2.0.12 etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16", "Backend": {"Type": "vxlan"}}'
    98  
    99      # iface may change to a private network interface, eth0 is for default
   100      flannelCID=$(docker -H unix:///var/run/docker-bootstrap.sock run --restart=always -d --net=host --privileged -v /dev/net:/dev/net quay.io/coreos/flannel:0.5.0 /opt/bin/flanneld -iface="eth0")
   101  
   102      sleep 8
   103  
   104      # Copy flannel env out and source it on the host
   105      docker -H unix:///var/run/docker-bootstrap.sock cp ${flannelCID}:/run/flannel/subnet.env .
   106      source subnet.env
   107  
   108      # Configure docker net settings, then restart it
   109      case "$lsb_dist" in
   110          fedora|centos|amzn)
   111              DOCKER_CONF="/etc/sysconfig/docker"
   112          ;;
   113          ubuntu|debian|linuxmint)
   114              DOCKER_CONF="/etc/default/docker"
   115          ;;
   116      esac
   117  
   118      # Append the docker opts
   119      echo "DOCKER_OPTS=\"\$DOCKER_OPTS --mtu=${FLANNEL_MTU} --bip=${FLANNEL_SUBNET}\"" | sudo tee -a ${DOCKER_CONF}
   120  
   121  
   122      # sleep a little bit
   123      ifconfig docker0 down
   124  
   125      case "$lsb_dist" in
   126          fedora|centos|amzn)
   127              yum install bridge-utils && brctl delbr docker0 && systemctl restart docker
   128          ;;
   129          ubuntu|debian|linuxmint)
   130              apt-get install bridge-utils && brctl delbr docker0 && service docker restart
   131          ;;
   132      esac
   133  
   134      # sleep a little bit
   135      sleep 5
   136  
   137      # Start kubelet & proxy, then start master components as pods
   138      docker run \
   139          --net=host \
   140          --privileged \
   141          --restart=always \
   142          -d \
   143          -v /sys:/sys:ro \
   144          -v /var/run:/var/run:rw \
   145          -v /:/rootfs:ro \
   146          -v /dev:/dev \
   147          -v /var/lib/docker/:/var/lib/docker:ro \
   148          -v /var/lib/kubelet/:/var/lib/kubelet:rw \
   149          gcr.io/google_containers/hyperkube:v${K8S_VERSION} \
   150          /hyperkube kubelet \
   151          --api-servers=http://localhost:8080 \
   152          --v=2 --address=0.0.0.0 --enable-server \
   153          --hostname-override=127.0.0.1 \
   154          --config=/etc/kubernetes/manifests-multi \
   155          --cluster-dns=10.0.0.10 \
   156          --cluster-domain=cluster.local
   157      
   158      docker run \
   159          -d \
   160          --net=host \
   161          --privileged \
   162          gcr.io/google_containers/hyperkube:v${K8S_VERSION} \
   163          /hyperkube proxy --master=http://127.0.0.1:8080 --v=2   
   164  }
   165  
   166  echo "Detecting your OS distro ..."
   167  detect_lsb
   168  
   169  echo "Starting bootstrap docker ..."
   170  bootstrap_daemon
   171  
   172  echo "Starting k8s ..."
   173  start_k8s
   174  
   175  echo "Master done!"