|
3 | 3 | set -o errexit |
4 | 4 | set -o pipefail |
5 | 5 |
|
6 | | -script_dir=$(dirname $(readlink -f $0)) |
| 6 | +help() |
| 7 | +{ |
| 8 | + echo "Usage: initialize-cluster.sh [OPTION]..." |
| 9 | + echo "Initialize cluster will install prerequisites and create a k3s cluster." |
| 10 | + echo |
| 11 | + echo "Options:" |
| 12 | + echo "-h Print this Help." |
| 13 | + echo "-s Skip the installation of prerequisites." |
| 14 | + echo "-y Automatically assume 'yes' to all questions." |
| 15 | + echo |
| 16 | +} |
| 17 | + |
| 18 | +install-prerequisites() |
| 19 | +{ |
| 20 | + echo |
| 21 | + echo "===========================" |
| 22 | + echo " Installing prerequisities" |
| 23 | + echo "===========================" |
| 24 | + echo |
7 | 25 |
|
8 | | -echo ========================= |
9 | | -echo Installing prerequisities |
10 | | -echo ========================= |
11 | | - |
12 | | -# install docker |
13 | | -SYSTEM_NAME=$(uname -r) |
14 | | -if ! [[ "$SYSTEM_NAME" == *"microsoft"* && "$SYSTEM_NAME" == *"WSL"* ]] |
15 | | -then |
16 | | - if ! which docker; |
17 | | - then |
18 | | - sudo apt-get update |
19 | | - sudo apt-get install -y docker.io |
| 26 | + # install docker |
| 27 | + SYSTEM_NAME=$(uname -r) |
| 28 | + if ! [[ "$SYSTEM_NAME" == *"microsoft"* && "$SYSTEM_NAME" == *"WSL"* ]]; then |
| 29 | + if ! which docker; |
| 30 | + then |
| 31 | + sudo apt-get update |
| 32 | + sudo apt-get install -y docker.io |
| 33 | + fi |
20 | 34 | fi |
21 | | -fi |
22 | 35 |
|
23 | | -# install k3d |
24 | | -if ! which k3d; |
25 | | -then |
26 | | - wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash |
27 | | -fi |
| 36 | + # install k3d |
| 37 | + if ! which k3d; then |
| 38 | + wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash |
| 39 | + fi |
28 | 40 |
|
29 | | -# install helm |
30 | | -if ! which helm; |
31 | | -then |
32 | | - curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash |
33 | | -fi |
| 41 | + # install helm |
| 42 | + if ! which helm; then |
| 43 | + curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash |
| 44 | + fi |
34 | 45 |
|
35 | | -# install step |
36 | | -if ! which step; |
37 | | -then |
38 | | - wget -q https://github.com/smallstep/cli/releases/download/v0.28.0/step-cli_amd64.deb -P /tmp |
39 | | - sudo dpkg -i /tmp/step-cli_amd64.deb |
40 | | -fi |
| 46 | + # install step |
| 47 | + if ! which step; then |
| 48 | + wget -q https://github.com/smallstep/cli/releases/download/v0.28.0/step-cli_amd64.deb -P /tmp |
| 49 | + sudo dpkg -i /tmp/step-cli_amd64.deb |
| 50 | + fi |
41 | 51 |
|
42 | | -# install az cli |
43 | | -if ! which az; |
44 | | -then |
45 | | - curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
46 | | - az aks install-cli |
47 | | -fi |
| 52 | + # install az cli |
| 53 | + if ! which az; then |
| 54 | + curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
| 55 | + az aks install-cli |
| 56 | + fi |
48 | 57 |
|
49 | | -# install/upgrade extensions |
50 | | -az extension add --upgrade --name azure-iot-ops |
51 | | -az extension add --upgrade --name connectedk8s |
| 58 | + # install/upgrade extensions |
| 59 | + az extension add --upgrade --name azure-iot-ops |
| 60 | + az extension add --upgrade --name connectedk8s |
52 | 61 |
|
53 | | -# install k9s |
54 | | -if ! which k9s; |
55 | | -then |
56 | | - wget -q https://github.com/derailed/k9s/releases/latest/download/k9s_linux_amd64.deb -P /tmp |
57 | | - sudo dpkg -i /tmp/k9s_linux_amd64.deb |
| 62 | + # install k9s |
| 63 | + if ! which k9s; then |
| 64 | + wget -q https://github.com/derailed/k9s/releases/latest/download/k9s_linux_amd64.deb -P /tmp |
| 65 | + sudo dpkg -i /tmp/k9s_linux_amd64.deb |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +create-cluster() { |
| 70 | + echo |
| 71 | + echo "=========================" |
| 72 | + echo " Creating cluster" |
| 73 | + echo "=========================" |
| 74 | + echo |
| 75 | + |
| 76 | + # Create k3d cluster and forwarded ports (MQTT/MQTTS) |
| 77 | + k3d cluster delete |
| 78 | + k3d cluster create \ |
| 79 | + -p '1883:1883@loadbalancer' \ |
| 80 | + -p '8883:8883@loadbalancer' \ |
| 81 | + -p '8884:8884@loadbalancer' \ |
| 82 | + --registry-create k3d-registry.localhost:127.0.0.1:5000 \ |
| 83 | + --wait |
| 84 | + |
| 85 | + # Set the default context / namespace to azure-iot-operations |
| 86 | + kubectl config set-context k3d-k3s-default --namespace=azure-iot-operations |
| 87 | + |
| 88 | + echo |
| 89 | + echo "=================================================================================================" |
| 90 | + echo " The k3d cluster has been created and the default context has been set to azure-iot-operations." |
| 91 | + echo |
| 92 | + echo " If you need non-root access to the cluster, run the following command:" |
| 93 | + echo |
| 94 | + echo " mkdir ~/.kube; sudo install -o $USER -g $USER -m 600 /root/.kube/config ~/.kube/config" |
| 95 | + echo "=================================================================================================" |
| 96 | +} |
| 97 | + |
| 98 | +# Get the options |
| 99 | +while getopts "hsy" option; do |
| 100 | + case $option in |
| 101 | + h) # display Help |
| 102 | + help |
| 103 | + exit;; |
| 104 | + s) # skip installation of prerequisites |
| 105 | + echo Skipping installation of prerequisites |
| 106 | + SKIP_INSTALL=true;; |
| 107 | + y) # assume yes to all questions |
| 108 | + AUTO_YES=true;; |
| 109 | + esac |
| 110 | +done |
| 111 | + |
| 112 | +script_dir=$(dirname $(readlink -f $0)) |
| 113 | + |
| 114 | +if [[ $SKIP_INSTALL != "true" ]]; then |
| 115 | + install-prerequisites |
| 116 | +fi |
| 117 | + |
| 118 | +if [[ $AUTO_YES != "true" ]]; then |
| 119 | + if [[ `k3d cluster list k3s-default` ]]; then |
| 120 | + echo |
| 121 | + read -p "An existing cluster was detected, are you sure you want to delete it? [y/N] " yn |
| 122 | + case $yn in |
| 123 | + [Yy]* ) ;; |
| 124 | + * ) exit 1;; |
| 125 | + esac |
| 126 | + fi |
58 | 127 | fi |
59 | 128 |
|
60 | | -echo ========================= |
61 | | -echo Creating cluster |
62 | | -echo ========================= |
63 | | - |
64 | | -# Create k3d cluster and forwarded ports (MQTT/MQTTS) |
65 | | -k3d cluster delete |
66 | | -k3d cluster create \ |
67 | | - -p '1883:1883@loadbalancer' \ |
68 | | - -p '8883:8883@loadbalancer' \ |
69 | | - -p '8884:8884@loadbalancer' \ |
70 | | - --registry-create k3d-registry.localhost:127.0.0.1:5000 \ |
71 | | - --wait |
72 | | - |
73 | | -# Set the default context / namespace to azure-iot-operations |
74 | | -kubectl config set-context k3d-k3s-default --namespace=azure-iot-operations |
75 | | - |
76 | | -echo |
77 | | -echo ================================================================================================= |
78 | | -echo The k3d cluster has been created and the default context has been set to azure-iot-operations. |
79 | | -echo If you need non-root access to the cluster, run the following command: |
80 | | -echo |
81 | | -echo "mkdir ~/.kube; sudo install -o $USER -g $USER -m 600 /root/.kube/config ~/.kube/config" |
82 | | -echo ================================================================================================= |
| 129 | +create-cluster |
0 commit comments