|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2021 The Karmada Authors. |
| 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 | + |
| 18 | +## This script deploy karmada control plane to a cluster using helm chart, and then deploy several member clusters. |
| 19 | +## Some member clusters are joined in Push mode, using karmadactl join command. |
| 20 | +## Other member clusters are joined in Pull mode, using helm chart of karmada-agent. |
| 21 | + |
| 22 | +# NEED_CREATE_KIND_CLUSTER customize whether you need to create clusters by kind, if you have clusters already, please unset this option |
| 23 | +NEED_CREATE_KIND_CLUSTER=true |
| 24 | + |
| 25 | +# IMAGE_FROM customize whether you need fetch images in advance, optional value are as follows: |
| 26 | +# pull-in-advance: use 'docker pull' to fetch needed images to local node in advance (in case of trouble in automatically pulling images) |
| 27 | +# make: build karmada images from source code with latest tag and pull images (other than karmada) in advance ( in case of trying latest code) |
| 28 | +# empty or any other value: ignored, just pull image by k8s-runtime when needing |
| 29 | +IMAGE_FROM="pull-in-advance" |
| 30 | + |
| 31 | +# LOAD_IMAGE_IN_ADVANCE if you fetch images in advance and you are using KinD clusters, may be you'd like to load it to KinD in advance too |
| 32 | +# if not, please unset this option |
| 33 | +LOAD_IMAGE_IN_ADVANCE=true |
| 34 | + |
| 35 | +# KARMADA_HOST_NAME customize your cluster name and context name of karmada-host cluster, if you already has a cluster, replace it by your real name |
| 36 | +KARMADA_HOST_NAME="karmada-host" |
| 37 | +# KARMADA_HOST_KUBECONFIG customize your kubeconfig of karmada-host cluster, if you already has a cluster, replace it by your real kubeconfig path |
| 38 | +KARMADA_HOST_KUBECONFIG="${HOME}/.kube/karmada-host.config" |
| 39 | + |
| 40 | +# PUSH_MODE_MEMBERS a map which customizing your push mode member clusters |
| 41 | +# the key of the map is the cluster name / context name of your member clusters |
| 42 | +# the value of the map is the corresponding kubeconfig path of the member cluster |
| 43 | +# if you already has member clusters, replace the key with real name and replace the value with real kubeconfig path |
| 44 | +# you can add any number push mode clusters as you expect, just append this map as following examples |
| 45 | +declare -A PUSH_MODE_MEMBERS |
| 46 | +PUSH_MODE_MEMBERS["member1"]="${HOME}/.kube/members.config" |
| 47 | +PUSH_MODE_MEMBERS["member3"]="${HOME}/.kube/members.config" |
| 48 | + |
| 49 | +# PULL_MODE_MEMBERS a map which customizing your pull mode member clusters |
| 50 | +# the key of the map is the cluster name / context name of your member clusters |
| 51 | +# the value of the map is the corresponding kubeconfig path of the member cluster |
| 52 | +# if you already has member clusters, replace the key with real name and replace the value with real kubeconfig path |
| 53 | +# you can add any number pull mode clusters as you expect, just append this map as following examples |
| 54 | +declare -A PULL_MODE_MEMBERS |
| 55 | +PULL_MODE_MEMBERS["member2"]="${HOME}/.kube/members.config" |
| 56 | + |
| 57 | +echo "########## start installing karmada control plane ##########" |
| 58 | + |
| 59 | +# 1. create KinD cluster if you set NEED_CREATE_KIND_CLUSTER |
| 60 | +if ${NEED_CREATE_KIND_CLUSTER}; then |
| 61 | + # 1.1 create karmada-host cluster |
| 62 | + kind delete clusters karmada-host |
| 63 | + rm -rf ~/.karmada "${KARMADA_HOST_KUBECONFIG}" |
| 64 | + hack/create-cluster.sh ${KARMADA_HOST_NAME} "${KARMADA_HOST_KUBECONFIG}" |
| 65 | +fi |
| 66 | + |
| 67 | +# 2. fetch images in advance is you set IMAGE_FROM |
| 68 | +if [ "${IMAGE_FROM}" == "pull-in-advance" ]; then |
| 69 | + ## 2.1 use 'docker pull' to fetch target images to local node in advance |
| 70 | + imgs=$(cat charts/karmada/values.yaml | grep -C 1 'repository:' | sed 's/*karmadaImageVersion/latest/g' | awk -F ':' '{print $2}' | sed 's/\"//g' | xargs -n3 | awk '{print $1"/"$2":"$3}') |
| 71 | + for img in ${imgs}; do |
| 72 | + docker pull "${img}" |
| 73 | + done |
| 74 | +elif [ "${IMAGE_FROM}" == "make" ]; then |
| 75 | + ## 2.2 build karmada images from source code with latest tag and pull images (other than karmada) in advance |
| 76 | + imgs=$(cat charts/karmada/values.yaml | grep -v 'karmada' | grep -C 1 'repository: ' | sed 's/*karmadaImageVersion/latest/g' | awk -F ':' '{print $2}' | sed 's/\"//g' | xargs -n3 | awk '{print $1"/"$2":"$3}') |
| 77 | + for img in ${imgs}; do |
| 78 | + docker pull "${img}" |
| 79 | + done |
| 80 | + |
| 81 | + export VERSION="latest" |
| 82 | + export REGISTRY="docker.io/karmada" |
| 83 | + make images GOOS="linux" . |
| 84 | +fi |
| 85 | + |
| 86 | +# 3. load images into KinD karmada-host cluster in advance if you set LOAD_IMAGE_IN_ADVANCE |
| 87 | +if ${LOAD_IMAGE_IN_ADVANCE}; then |
| 88 | + imgs=$(cat charts/karmada/values.yaml | grep -C 1 'repository:' | sed 's/*karmadaImageVersion/latest/g' | awk -F ':' '{print $2}' | sed 's/\"//g' | xargs -n3 | awk '{print $1"/"$2":"$3}') |
| 89 | + for img in ${imgs}; do |
| 90 | + kind load docker-image "${img}" --name ${KARMADA_HOST_NAME} |
| 91 | + done |
| 92 | +fi |
| 93 | + |
| 94 | +# 4. this script try to deploy karmada-apiserver by host-network |
| 95 | +# so, it needs to get host-network ip (node ip) from kube-apiserver, and then add this ip to values.yaml as SANs of certificate |
| 96 | +export KUBECONFIG=${KARMADA_HOST_KUBECONFIG} |
| 97 | +HOST_IP=$(kubectl get ep kubernetes -o jsonpath='{.subsets[0].addresses[0].ip}') |
| 98 | +sed -i'' -e "/localhost/p; s/localhost/${HOST_IP}/" charts/karmada/values.yaml |
| 99 | + |
| 100 | +# 5. install karmada at karmada-host cluster by helm |
| 101 | +helm install karmada -n karmada-system \ |
| 102 | + --kubeconfig "${KARMADA_HOST_KUBECONFIG}" \ |
| 103 | + --create-namespace \ |
| 104 | + --dependency-update \ |
| 105 | + --set apiServer.hostNetwork=true \ |
| 106 | + ./charts/karmada |
| 107 | + |
| 108 | +# 6. export kubeconfig of karmada-apiserver to local path |
| 109 | +KARMADA_APISERVER_KUBECONFIG="${HOME}/.kube/karmada-apiserver.config" |
| 110 | +kubectl get secret -n karmada-system karmada-kubeconfig -o jsonpath={.data.kubeconfig} | base64 -d > "${KARMADA_APISERVER_KUBECONFIG}" |
| 111 | +KARMADA_APISERVER_ADDR=$(kubectl get ep karmada-apiserver -n karmada-system | tail -n 1 | awk '{print $2}') |
| 112 | +sed -i'' -e "s/karmada-apiserver.karmada-system.svc.*:5443/${KARMADA_APISERVER_ADDR}/g" "${KARMADA_APISERVER_KUBECONFIG}" |
| 113 | + |
| 114 | +echo "########## end installing karmada control plane success ##########" |
| 115 | + |
| 116 | + |
| 117 | +echo "########## start deploying member clusters ##########" |
| 118 | + |
| 119 | +# 1. create KinD cluster if you set NEED_CREATE_KIND_CLUSTER |
| 120 | +if ${NEED_CREATE_KIND_CLUSTER}; then |
| 121 | + ## 1.1. create push mode member clusters by KinD |
| 122 | + for clustername in "${!PUSH_MODE_MEMBERS[@]}"; do |
| 123 | + kind delete clusters "${clustername}" |
| 124 | + hack/create-cluster.sh "${clustername}" "${PUSH_MODE_MEMBERS[$clustername]}" |
| 125 | + done |
| 126 | + |
| 127 | + ## 1.2. create pull mode member clusters by KinD |
| 128 | + for clustername in "${!PULL_MODE_MEMBERS[@]}"; do |
| 129 | + kind delete clusters "${clustername}" |
| 130 | + hack/create-cluster.sh "${clustername}" "${PULL_MODE_MEMBERS[$clustername]}" |
| 131 | + done |
| 132 | +fi |
| 133 | + |
| 134 | +# 2. load karmada-agent image into pull mode member clusters in advance if you set LOAD_IMAGE_IN_ADVANCE |
| 135 | +if ${LOAD_IMAGE_IN_ADVANCE}; then |
| 136 | + agentImage=$(cat charts/karmada/values.yaml | grep -C 1 'repository: karmada/karmada-agent' | sed 's/*karmadaImageVersion/latest/g' | awk -F ':' '{print $2}' | sed 's/\"//g' | xargs -n3 | awk '{print $1"/"$2":"$3}') |
| 137 | + for clustername in "${!PULL_MODE_MEMBERS[@]}"; do |
| 138 | + kind load docker-image "${agentImage}" --name "${clustername}" |
| 139 | + done |
| 140 | +fi |
| 141 | + |
| 142 | +# 3. download karmadactl if not exist |
| 143 | +if ! which karmadactl >/dev/null 2>&1; then |
| 144 | + GO111MODULE=on go install "github.com/karmada-io/karmada/cmd/karmadactl" |
| 145 | + GOPATH=$(go env GOPATH | awk -F ':' '{print $1}') |
| 146 | + alias karmadactl='${GOPATH}/bin/karmadactl' |
| 147 | +fi |
| 148 | + |
| 149 | +# 4. join push mode member clusters by 'karmadactl join' command |
| 150 | +for clustername in "${!PUSH_MODE_MEMBERS[@]}"; do |
| 151 | + karmadactl join "${clustername}" --kubeconfig "${KARMADA_APISERVER_KUBECONFIG}" --karmada-context karmada-apiserver --cluster-kubeconfig "${PUSH_MODE_MEMBERS[$clustername]}" --cluster-context "${clustername}" |
| 152 | +done |
| 153 | + |
| 154 | +# 5. when you deploy karmada-agent by helm chart, you should manually fill in the cert of karmada-apiserver at values.yaml |
| 155 | +# so, it needs to get cert from karmada-apiserver.config for agent |
| 156 | +CA_CRT=$(cat "${KARMADA_APISERVER_KUBECONFIG}" | grep certificate-authority-data | awk -F ': ' '{print $2}' | base64 -d) |
| 157 | +AGENT_CRT=$(cat "${KARMADA_APISERVER_KUBECONFIG}" | grep client-certificate-data | awk -F ': ' '{print $2}' | base64 -d) |
| 158 | +AGENT_KEY=$(cat "${KARMADA_APISERVER_KUBECONFIG}" | grep client-key-data | awk -F ': ' '{print $2}' | base64 -d) |
| 159 | + |
| 160 | +# 6. join pull mode member clusters by helm chart |
| 161 | +for clustername in "${!PULL_MODE_MEMBERS[@]}"; do |
| 162 | + helm install karmada-agent -n karmada-system \ |
| 163 | + --kubeconfig "${PULL_MODE_MEMBERS[$clustername]}" \ |
| 164 | + --kube-context "${clustername}" \ |
| 165 | + --create-namespace \ |
| 166 | + --dependency-update \ |
| 167 | + --set installMode=agent,agent.clusterName="${clustername}",agent.kubeconfig.server=https://"${KARMADA_APISERVER_ADDR}",agent.kubeconfig.caCrt="${CA_CRT}",agent.kubeconfig.crt="${AGENT_CRT}",agent.kubeconfig.key="${AGENT_KEY}" \ |
| 168 | + ./charts/karmada |
| 169 | +done |
| 170 | + |
| 171 | +echo "########## end deploying member clusters success ##########" |
| 172 | + |
| 173 | +# merge karmada-host.config and karmada-apiserver.config into ${KARMADA_MERGE_KUBECONFIG}, keep the same with other installation method |
| 174 | +KARMADA_MERGE_KUBECONFIG="${HOME}/.kube/karmada.config" |
| 175 | +export KUBECONFIG="${KARMADA_HOST_KUBECONFIG}":"${KARMADA_APISERVER_KUBECONFIG}" |
| 176 | +kubectl config view --flatten > "${KARMADA_MERGE_KUBECONFIG}" |
| 177 | + |
| 178 | +# verify: wait for member cluster ready and then print member clusters |
| 179 | +MEMBERS_NUMBER=$(( ${#PUSH_MODE_MEMBERS[*]} + ${#PULL_MODE_MEMBERS[*]} )) |
| 180 | +while [[ "$(kubectl --context karmada-apiserver get clusters -o wide | grep -c "True")" -ne ${MEMBERS_NUMBER} ]]; do |
| 181 | + echo "waiting for member clusters ready..."; sleep 2; |
| 182 | +done |
| 183 | +kubectl --context karmada-apiserver get cluster -o wide |
0 commit comments