forked from dotnet-architecture/eShopOnContainers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·212 lines (180 loc) · 9.16 KB
/
deploy.sh
File metadata and controls
executable file
·212 lines (180 loc) · 9.16 KB
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
#!/usr/bin/env bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
# This script is comparable to the PowerShell script deploy.ps1 but to be used from a Mac bash environment.
# There are, however, the following few differences/limitations:
# It assumes docker/container registry login was already performed
# It assumes K8s was given access to the registry—does not create any K8s secrets
# It does not support explicit kubectl config file (relies on kubectl config use-context to point kubectl at the right cluster/namespace)
# It always deploys infrastructure bits (redis, SQL Server etc)
# The script was tested only with Azure Container Registry (not Docker Hub, although it is expected to work with Docker Hub too)
# Feel free to submit a PR in order to improve it.
usage()
{
cat <<END
deploy.sh: deploys eShopOnContainers application to Kubernetes cluster
Parameters:
-r | --registry <container registry>
Specifies container registry (ACR) to use (required), e.g. myregistry.azurecr.io
-t | --tag <docker image tag>
Default: newly created, date-based timestamp, with 1-minute resolution
-b | --build-solution
Force solution build before deployment (default: false)
--skip-image-build
Do not build images (default is to build all images)
--skip-image-push
Do not upload images to the container registry (just run the Kubernetes deployment portion)
Default is to push images to container registry
-h | --help
Displays this help text and exits the script
It is assumed that the Kubernetes AKS cluster has been granted access to ACR registry.
For more info see
https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
WARNING! THE SCRIPT WILL COMPLETELY DESTROY ALL DEPLOYMENTS AND SERVICES VISIBLE
FROM THE CURRENT CONFIGURATION CONTEXT.
It is recommended that you create a separate namespace and confguration context
for the eShopOnContainers application, to isolate it from other applications on the cluster.
For more information see https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
You can use eshop-namespace.yaml file (in the same directory) to create the namespace.
END
}
image_tag=$(date '+%Y%m%d%H%M')
build_solution=''
container_registry=''
build_images='yes'
push_images='yes'
while [[ $# -gt 0 ]]; do
case "$1" in
-r | --registry )
container_registry="$2"; shift 2 ;;
-t | --tag )
image_tag="$2"; shift 2 ;;
-b | --build-solution )
build_solution='yes'; shift ;;
--skip-image-build )
build_images=''; shift ;;
--skip-image-push )
push_images=''; shift ;;
-h | --help )
usage; exit 1 ;;
*)
echo "Unknown option $1"
usage; exit 2 ;;
esac
done
if [[ ! $container_registry ]]; then
echo 'Container registry must be specified (e.g. myregistry.azurecr.io)'
echo ''
usage
exit 3
fi
if [[ $build_solution ]]; then
echo "#################### Building eShopOnContainers solution ####################"
dotnet publish -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
fi
export TAG=$image_tag
if [[ $build_images ]]; then
echo "#################### Building eShopOnContainers Docker images ####################"
docker-compose -p .. -f ../docker-compose.yml build
# Remove temporary images
docker rmi $(docker images -qf "dangling=true")
fi
if [[ $push_images ]]; then
echo "#################### Pushing images to registry ####################"
services=(basket.api catalog.api identity.api ordering.api marketing.api payment.api locations.api webmvc webspa webstatus)
for service in "${services[@]}"
do
echo "Pushing image for service $service..."
docker tag "eshop/$service:$image_tag" "$container_registry/$service:$image_tag"
docker push "$container_registry/$service:$image_tag"
done
fi
echo "#################### Cleaning up old deployment ####################"
kubectl delete deployments --all
kubectl delete services --all
kubectl delete configmap config-files || true
kubectl delete configmap urls || true
kubectl delete configmap externalcfg || true
echo "#################### Deploying infrastructure components ####################"
kubectl create configmap config-files --from-file=nginx-conf=nginx.conf
kubectl label configmap config-files app=eshop
kubectl create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml
echo "#################### Creating application service definitions ####################"
kubectl create -f services.yaml -f frontend.yaml
echo "#################### Waiting for Azure to provision external IP ####################"
ip_regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
while true; do
printf "."
frontendUrl=$(kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}")
if [[ $frontendUrl =~ $ip_regex ]]; then
break
fi
sleep 5s
done
printf "\n"
externalDns=$frontendUrl
echo "Using $externalDns as the external DNS/IP of the K8s cluster"
echo "#################### Creating application configuration ####################"
# urls configmap
kubectl create configmap urls \
"--from-literal=BasketUrl=http://basket" \
"--from-literal=BasketHealthCheckUrl=http://basket/hc" \
"--from-literal=CatalogUrl=http://$externalDns/catalog-api" \
"--from-literal=CatalogHealthCheckUrl=http://catalog/hc" \
"--from-literal=PicBaseUrl=http://$externalDns/catalog-api/api/v1/catalog/items/[0]/pic/" \
"--from-literal=Marketing_PicBaseUrl=http://$externalDns/marketing-api/api/v1/campaigns/[0]/pic/" \
"--from-literal=IdentityUrl=http://$externalDns/identity" \
"--from-literal=IdentityHealthCheckUrl=http://identity/hc" \
"--from-literal=OrderingUrl=http://ordering" \
"--from-literal=OrderingHealthCheckUrl=http://ordering/hc" \
"--from-literal=MvcClientExternalUrl=http://$externalDns/webmvc" \
"--from-literal=WebMvcHealthCheckUrl=http://webmvc/hc" \
"--from-literal=MvcClientOrderingUrl=http://ordering" \
"--from-literal=MvcClientCatalogUrl=http://catalog" \
"--from-literal=MvcClientBasketUrl=http://basket" \
"--from-literal=MvcClientMarketingUrl=http://marketing" \
"--from-literal=MvcClientLocationsUrl=http://locations" \
"--from-literal=MarketingHealthCheckUrl=http://marketing/hc" \
"--from-literal=WebSpaHealthCheckUrl=http://webspa/hc" \
"--from-literal=SpaClientMarketingExternalUrl=http://$externalDns/marketing-api" \
"--from-literal=SpaClientOrderingExternalUrl=http://$externalDns/ordering-api" \
"--from-literal=SpaClientCatalogExternalUrl=http://$externalDns/catalog-api" \
"--from-literal=SpaClientBasketExternalUrl=http://$externalDns/basket-api" \
"--from-literal=SpaClientIdentityExternalUrl=http://$externalDns/identity" \
"--from-literal=SpaClientLocationsUrl=http://$externalDns/locations-api" \
"--from-literal=LocationsHealthCheckUrl=http://locations/hc" \
"--from-literal=SpaClientExternalUrl=http://$externalDns" \
"--from-literal=LocationApiClient=http://$externalDns/locations-api" \
"--from-literal=MarketingApiClient=http://$externalDns/marketing-api" \
"--from-literal=BasketApiClient=http://$externalDns/basket-api" \
"--from-literal=OrderingApiClient=http://$externalDns/ordering-api" \
"--from-literal=PaymentHealthCheckUrl=http://payment/hc"
kubectl label configmap urls app=eshop
# externalcfg configmap -- points to local infrastructure components (rabbitmq, SQL Server etc)
kubectl create -f conf_local.yml
# Create application pod deployments
kubectl create -f deployments.yaml
echo "#################### Deploying application pods ####################"
# update deployments with the correct image (with tag and/or registry)
kubectl set image deployments/basket "basket=$container_registry/basket.api:$image_tag"
kubectl set image deployments/catalog "catalog=$container_registry/catalog.api:$image_tag"
kubectl set image deployments/identity "identity=$container_registry/identity.api:$image_tag"
kubectl set image deployments/ordering "ordering=$container_registry/ordering.api:$image_tag"
kubectl set image deployments/marketing "marketing=$container_registry/marketing.api:$image_tag"
kubectl set image deployments/locations "locations=$container_registry/locations.api:$image_tag"
kubectl set image deployments/payment "payment=$container_registry/payment.api:$image_tag"
kubectl set image deployments/webmvc "webmvc=$container_registry/webmvc:$image_tag"
kubectl set image deployments/webstatus "webstatus=$container_registry/webstatus:$image_tag"
kubectl set image deployments/webspa "webspa=$container_registry/webspa:$image_tag"
kubectl rollout resume deployments/basket
kubectl rollout resume deployments/catalog
kubectl rollout resume deployments/identity
kubectl rollout resume deployments/ordering
kubectl rollout resume deployments/marketing
kubectl rollout resume deployments/locations
kubectl rollout resume deployments/payment
kubectl rollout resume deployments/webmvc
kubectl rollout resume deployments/webstatus
kubectl rollout resume deployments/webspa
echo "WebSPA is exposed at http://$externalDns, WebMVC at http://$externalDns/webmvc, WebStatus at http://$externalDns/webstatus"
echo "eShopOnContainers deployment is DONE"