summaryrefslogtreecommitdiffstats
path: root/tools/kubernetes/k8s-cluster.sh
blob: 1ef17e20fe6280afe41a8b52b6c1f90aefecabaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
# Copyright 2017 AT&T Intellectual Property, Inc
#
# 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.
#
#. What this is: script to setup a kubernetes cluster with calico as sni
#. Prerequisites:
#. - Ubuntu xenial server for master and agent nodes
#. - key-based auth setup for ssh/scp between master and agent nodes
#. - 192.168.0.0/16 should not be used on your server network interface subnets
#. Usage:
#. $ git clone https://gerrit.opnfv.org/gerrit/models ~/models
#. $ cd ~/models/tools/kubernetes
#. $ bash k8s-cluster.sh master
#. $ bash k8s-cluster.sh agents "<nodes>"
#.     nodes: space-separated list of ceph node IPs
#. $ bash k8s-cluster.sh ceph "<nodes>" <cluster-net> <public-net> <ceph-mode> [ceph_dev]
#.     nodes: space-separated list of ceph node IPs
#.     cluster-net: CIDR of ceph cluster network e.g. 10.0.0.1/24
#.     public-net: CIDR of public network
#.     ceph-mode: "helm" or "baremetal"
#.     ceph_dev: disk to use for ceph. ***MUST NOT BE USED FOR ANY OTHER PURPOSE***
#.               if not provided, ceph data will be stored on osd nodes in /ceph
#. $ bash k8s-cluster.sh helm
#.     Setup helm as app kubernetes orchestration tool
#. $ bash k8s-cluster.sh demo
#.     Install helm charts for mediawiki and dokuwiki
#. $ bash k8s-cluster.sh all "<nodes>" <cluster-net> <public-net> <ceph-mode> [ceph_dev]
#.     Runs all the steps above
#.
#. When deployment is complete, the k8s API will be available at the master
#. node, e.g. via: curl -k https://<master-ip>:6443/api/v1
#.
#. Status: work in progress, incomplete
#

function setup_prereqs() {
  echo "${FUNCNAME[0]}: Create prerequisite setup script"
  cat <<'EOG' >/tmp/prereqs.sh
#!/bin/bash
# Basic server pre-reqs
sudo apt-get -y remove kubectl kubelet kubeadm
sudo apt-get update
sudo apt-get upgrade -y
# Set hostname on agent nodes
if [[ "$1" == "agent" ]]; then
  echo $(ip route get 8.8.8.8 | awk '{print $NF; exit}') $HOSTNAME | sudo tee -a /etc/hosts
fi
# Install docker 1.12 (default for xenial is 1.12.6)
sudo apt-get install -y docker.io
sudo service docker start
export KUBE_VERSION=1.7.5
# per https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
# Install kubelet, kubeadm, kubectl per https://kubernetes.io/docs/setup/independent/install-kubeadm/
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
# Next command is to workaround bug resulting in "PersistentVolumeClaim is not bound" for pod startup (remain in Pending)
# TODO: reverify if this is still an issue in the final working script
sudo apt-get -y install ceph ceph-common
sudo apt-get -y install --allow-downgrades kubectl=${KUBE_VERSION}-00 kubelet=${KUBE_VERSION}-00 kubeadm=${KUBE_VERSION}-00
# Needed for ceph setup etc
sudo apt-get -y install jq
EOG
}

function setup_k8s_master() {
  echo "${FUNCNAME[0]}: Setting up kubernetes master"
  setup_prereqs

  # Install master
  bash /tmp/prereqs.sh master
  # per https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
  # If the following command fails, run "kubeadm reset" before trying again
  # --pod-network-cidr=192.168.0.0/16 is required for calico; this should not conflict with your server network interface subnets
  sudo kubeadm init --pod-network-cidr=192.168.0.0/16 >>/tmp/kubeadm.out
  cat /tmp/kubeadm.out
  export k8s_joincmd=$(grep "kubeadm join" /tmp/kubeadm.out)
  echo "${FUNCNAME[0]}: Cluster join command for manual use if needed: $k8s_joincmd"

  # Start cluster
  echo "${FUNCNAME[0]}: Start the cluster"
  mkdir -p $HOME/.kube
  sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  # Deploy pod network
  echo "${FUNCNAME[0]}: Deploy calico as CNI"
  sudo kubectl apply -f http://docs.projectcalico.org/v2.4/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml
}

function setup_k8s_agents() {
  agents="$1"
  export k8s_joincmd=$(grep "kubeadm join" /tmp/kubeadm.out)
  echo "${FUNCNAME[0]}: Installing agents at $1 with joincmd: $k8s_joincmd"

  setup_prereqs

  kubedns=$(kubectl get pods --all-namespaces | grep kube-dns | awk '{print $4}')
  while [[ "$kubedns" != "Running" ]]; do
    echo "${FUNCNAME[0]}: kube-dns status is $kubedns. Waiting 60 seconds for it to be 'Running'"
    sleep 60
    kubedns=$(kubectl get pods --all-namespaces | grep kube-dns | awk '{print $4}')
  done
  echo "${FUNCNAME[0]}: kube-dns status is $kubedns"

  for agent in $agents; do
    echo "${FUNCNAME[0]}: Install agent at $agent"
    scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/prereqs.sh ubuntu@$agent:/tmp/prereqs.sh
    ssh -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@$agent bash /tmp/prereqs.sh agent
    # Workaround for "[preflight] Some fatal errors occurred: /var/lib/kubelet is not empty" per https://github.com/kubernetes/kubeadm/issues/1
    ssh -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@$agent sudo kubeadm reset
    ssh -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@$agent sudo $k8s_joincmd
  done

  echo "${FUNCNAME[0]}: Cluster is ready when all nodes in the output of 'kubectl get nodes' show as 'Ready'."
}

function wait_for_service() {
  echo "${FUNCNAME[0]}: Waiting for service $1 to be available"
  pod=$(kubectl get pods --namespace default | awk "/$1/ { print \$1 }")
  echo "${FUNCNAME[0]}: Service $1 is at pod $pod"
  ready=$(kubectl get pods --namespace default -o jsonpath='{.status.containerStatuses[0].ready}' $pod)
  while [[ "$ready" != "true" ]]; do
    echo "${FUNCNAME[0]}: $1 container is not yet ready... waiting 10 seconds"
    sleep 10
    # TODO: figure out why transient pods sometimes mess up this logic, thus need to re-get the pods
    pod=$(kubectl get pods --namespace default | awk "/$1/ { print \$1 }")
    ready=$(kubectl get pods --namespace default -o jsonpath='{.status.containerStatuses[0].ready}' $pod)
  done
  echo "${FUNCNAME[0]}: pod $pod container status is $ready"
  host_ip=$(kubectl get pods --namespace default -o jsonpath='{.status.hostIP}' $pod)
  port=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services $1)
  echo "${FUNCNAME[0]}: pod $pod container is at host $host_ip and port $port"
  while ! curl http://$host_ip:$port ; do
    echo "${FUNCNAME[0]}: $1 service is not yet responding... waiting 10 seconds"
    sleep 10
  done
  echo "${FUNCNAME[0]}: $1 is available at http://$host_ip:$port"
}

function demo_chart() {
  cd ~
  rm -rf charts
  git clone https://github.com/kubernetes/charts.git
  cd charts/stable
  case "$1" in
    mediawiki)
      # NOT YET WORKING
      # mariadb: Readiness probe failed: mysqladmin: connect to server at 'localhost' failed
      mkdir ./mediawiki/charts
      cp -r ./mariadb ./mediawiki/charts
      # LoadBalancer is N/A for baremetal (public cloud only) - use NodePort
      sed -i -- 's/LoadBalancer/NodePort/g' ./mediawiki/values.yaml
      # Select the storageClass created in the ceph setup step
      sed -i -- 's/# storageClass:/storageClass: "general"/g' ./mediawiki/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./mediawiki/charts/mariadb/values.yaml
      helm install --name mw -f ./mediawiki/values.yaml ./mediawiki
      wait_for_service mw-mediawiki
      ;;
    dokuwiki)
      sed -i -- 's/# storageClass:/storageClass: "general"/g' ./dokuwiki/values.yaml
      sed -i -- 's/LoadBalancer/NodePort/g' ./dokuwiki/values.yaml
      helm install --name dw -f ./dokuwiki/values.yaml ./dokuwiki
      wait_for_service dw-dokuwiki
      ;;
    wordpress)
      # NOT YET WORKING
      # mariadb: Readiness probe failed: mysqladmin: connect to server at 'localhost' failed
      mkdir ./wordpress/charts
      cp -r ./mariadb ./wordpress/charts
      sed -i -- 's/LoadBalancer/NodePort/g' ./wordpress/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./wordpress/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./wordpress/charts/mariadb/values.yaml
      helm install --name wp -f ./wordpress/values.yaml ./wordpress
      wait_for_service wp-wordpress
      ;;
    redmine)
      # NOT YET WORKING
      # mariadb: Readiness probe failed: mysqladmin: connect to server at 'localhost' failed
      mkdir ./redmine/charts
      cp -r ./mariadb ./redmine/charts
      cp -r ./postgresql ./redmine/charts
      sed -i -- 's/LoadBalancer/NodePort/g' ./redmine/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./redmine/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./redmine/charts/mariadb/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./redmine/charts/postgresql/values.yaml
      helm install --name rdm -f ./redmine/values.yaml ./redmine
      wait_for_service rdm-redmine
      ;;
    owncloud)
      # NOT YET WORKING: needs resolvable hostname for service
      mkdir ./owncloud/charts
      cp -r ./mariadb ./owncloud/charts
      sed -i -- 's/LoadBalancer/NodePort/g' ./owncloud/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./owncloud/values.yaml
      sed -i -- 's/# storageClass: "-"/storageClass: "general"/g' ./owncloud/charts/mariadb/values.yaml
      helm install --name oc -f ./owncloud/values.yaml ./owncloud
      wait_for_service oc-owncloud
      ;;
    *)
      echo "${FUNCNAME[0]}: demo not implemented for $1"
  esac
# extra useful commands
# kubectl describe pvc
# kubectl get pvc
# kubectl describe pods
# kubectl get pods --namespace default
# kubectl get pods --all-namespaces
# kubectl get svc --namespace default dw-dokuwiki
# kubectl describe svc --namespace default dw-dokuwiki
# kubectl describe pods --namespace default dw-dokuwiki
}

function setup_helm() {
  echo "${FUNCNAME[0]}: Setup helm"
  # Install Helm
  cd ~
  curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh
  chmod 700 get_helm.sh
  ./get_helm.sh
  helm init
  nohup helm serve &
  helm repo update
  # TODO: Workaround for bug https://github.com/kubernetes/helm/issues/2224
  # For testing use only!
  kubectl create clusterrolebinding permissive-binding --clusterrole=cluster-admin --user=admin --user=kubelet --group=system:serviceaccounts;
  # TODO: workaround for tiller FailedScheduling (No nodes are available that match all of the following predicates:: PodToleratesNodeTaints (1).)
  # kubectl taint nodes $HOSTNAME node-role.kubernetes.io/master:NoSchedule-
  # Wait till tiller is running
  tiller_deploy=$(kubectl get pods --all-namespaces | grep tiller-deploy | awk '{print $4}')
  while [[ "$tiller_deploy" != "Running" ]]; do
    echo "${FUNCNAME[0]}: tiller-deploy status is $tiller_deploy. Waiting 60 seconds for it to be 'Running'"
    sleep 60
    tiller_deploy=$(kubectl get pods --all-namespaces | grep tiller-deploy | awk '{print $4}')
  done
  echo "${FUNCNAME[0]}: tiller-deploy status is $tiller_deploy"

  # Install services via helm charts from https://kubeapps.com/charts
  # e.g. helm install stable/dokuwiki
}

function setup_ceph() {
  if [[ "$4" == "helm" ]]; then
    source ./ceph-helm.sh "$1" $2 $3 $5
  else
    source ./ceph-baremetal.sh "$1" $2 $3 $5
  fi
}

export WORK_DIR=$(pwd)
case "$1" in
  master)
    setup_k8s_master
    ;;
  agents)
    setup_k8s_agents "$2"
    ;;
  ceph)
    setup_ceph "$2" $3 $4 $5 $6
    ;;
  helm)
    setup_helm
    ;;
  demo)
    demo_chart $2
    ;;
  all)
    setup_k8s_master
    setup_k8s_agents "$2"
    setup_helm
    setup_ceph "$2" $3 $4 $5 $6
    demo_chart dokuwiki
    ;;
  clean)
    # TODO
    ;;
  *)
    if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then grep '#. ' $0; fi
esac