Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 0f26537

Browse files
committed
2 parents 9057635 + 19d2d87 commit 0f26537

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

k8s/deploy.sh

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/usr/bin/env bash
2+
3+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
4+
set -euo pipefail
5+
6+
# This script is comparable to the PowerShell script deploy.ps1 but to be used from a Mac bash environment.
7+
# There are, however, the following few differences/limitations:
8+
9+
# It assumes docker/container registry login was already performed
10+
# It assumes K8s was given access to the registry—does not create any K8s secrets
11+
# It does not support explicit kubectl config file (relies on kubectl config use-context to point kubectl at the right cluster/namespace)
12+
# It always deploys infrastructure bits (redis, SQL Server etc)
13+
# The script was tested only with Azure Container Registry (not Docker Hub, although it is expected to work with Docker Hub too)
14+
15+
# Feel free to submit a PR in order to improve it.
16+
17+
usage()
18+
{
19+
cat <<END
20+
deploy.sh: deploys eShopOnContainers application to Kubernetes cluster
21+
Parameters:
22+
-r | --registry <container registry>
23+
Specifies container registry (ACR) to use (required), e.g. myregistry.azurecr.io
24+
-t | --tag <docker image tag>
25+
Default: newly created, date-based timestamp, with 1-minute resolution
26+
-b | --build-solution
27+
Force solution build before deployment (default: false)
28+
--skip-image-build
29+
Do not build images (default is to build all images)
30+
--skip-image-push
31+
Do not upload images to the container registry (just run the Kubernetes deployment portion)
32+
Default is to push images to container registry
33+
-h | --help
34+
Displays this help text and exits the script
35+
36+
It is assumed that the Kubernetes AKS cluster has been granted access to ACR registry.
37+
For more info see
38+
https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
39+
40+
WARNING! THE SCRIPT WILL COMPLETELY DESTROY ALL DEPLOYMENTS AND SERVICES VISIBLE
41+
FROM THE CURRENT CONFIGURATION CONTEXT.
42+
It is recommended that you create a separate namespace and confguration context
43+
for the eShopOnContainers application, to isolate it from other applications on the cluster.
44+
For more information see https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
45+
You can use eshop-namespace.yaml file (in the same directory) to create the namespace.
46+
47+
END
48+
}
49+
50+
image_tag=$(date '+%Y%m%d%H%M')
51+
build_solution=''
52+
container_registry=''
53+
build_images='yes'
54+
push_images='yes'
55+
56+
while [[ $# -gt 0 ]]; do
57+
case "$1" in
58+
-r | --registry )
59+
container_registry="$2"; shift 2 ;;
60+
-t | --tag )
61+
image_tag="$2"; shift 2 ;;
62+
-b | --build-solution )
63+
build_solution='yes'; shift ;;
64+
--skip-image-build )
65+
build_images=''; shift ;;
66+
--skip-image-push )
67+
push_images=''; shift ;;
68+
-h | --help )
69+
usage; exit 1 ;;
70+
*)
71+
echo "Unknown option $1"
72+
usage; exit 2 ;;
73+
esac
74+
done
75+
76+
if [[ ! $container_registry ]]; then
77+
echo 'Container registry must be specified (e.g. myregistry.azurecr.io)'
78+
echo ''
79+
usage
80+
exit 3
81+
fi
82+
83+
if [[ $build_solution ]]; then
84+
echo "#################### Building eShopOnContainers solution ####################"
85+
dotnet publish -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
86+
fi
87+
88+
export TAG=$image_tag
89+
90+
if [[ $build_images ]]; then
91+
echo "#################### Building eShopOnContainers Docker images ####################"
92+
docker-compose -p .. -f ../docker-compose.yml build
93+
94+
# Remove temporary images
95+
docker rmi $(docker images -qf "dangling=true")
96+
fi
97+
98+
if [[ $push_images ]]; then
99+
echo "#################### Pushing images to registry ####################"
100+
services=(basket.api catalog.api identity.api ordering.api marketing.api payment.api locations.api webmvc webspa webstatus)
101+
102+
for service in "${services[@]}"
103+
do
104+
echo "Pushing image for service $service..."
105+
docker tag "eshop/$service:$image_tag" "$container_registry/$service:$image_tag"
106+
docker push "$container_registry/$service:$image_tag"
107+
done
108+
fi
109+
110+
echo "#################### Cleaning up old deployment ####################"
111+
kubectl delete deployments --all
112+
kubectl delete services --all
113+
kubectl delete configmap config-files || true
114+
kubectl delete configmap urls || true
115+
kubectl delete configmap externalcfg || true
116+
117+
echo "#################### Deploying infrastructure components ####################"
118+
kubectl create configmap config-files --from-file=nginx-conf=nginx.conf
119+
kubectl label configmap config-files app=eshop
120+
kubectl create -f sql-data.yaml -f basket-data.yaml -f keystore-data.yaml -f rabbitmq.yaml -f nosql-data.yaml
121+
122+
echo "#################### Creating application service definitions ####################"
123+
kubectl create -f services.yaml -f frontend.yaml
124+
125+
echo "#################### Waiting for Azure to provision external IP ####################"
126+
127+
ip_regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
128+
while true; do
129+
printf "."
130+
frontendUrl=$(kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}")
131+
if [[ $frontendUrl =~ $ip_regex ]]; then
132+
break
133+
fi
134+
sleep 5s
135+
done
136+
137+
printf "\n"
138+
externalDns=$frontendUrl
139+
echo "Using $externalDns as the external DNS/IP of the K8s cluster"
140+
141+
echo "#################### Creating application configuration ####################"
142+
143+
# urls configmap
144+
kubectl create configmap urls \
145+
"--from-literal=BasketUrl=http://basket" \
146+
"--from-literal=BasketHealthCheckUrl=http://basket/hc" \
147+
"--from-literal=CatalogUrl=http://$externalDns/catalog-api" \
148+
"--from-literal=CatalogHealthCheckUrl=http://catalog/hc" \
149+
"--from-literal=PicBaseUrl=http://$externalDns/catalog-api/api/v1/catalog/items/[0]/pic/" \
150+
"--from-literal=Marketing_PicBaseUrl=http://$externalDns/marketing-api/api/v1/campaigns/[0]/pic/" \
151+
"--from-literal=IdentityUrl=http://$externalDns/identity" \
152+
"--from-literal=IdentityHealthCheckUrl=http://identity/hc" \
153+
"--from-literal=OrderingUrl=http://ordering" \
154+
"--from-literal=OrderingHealthCheckUrl=http://ordering/hc" \
155+
"--from-literal=MvcClientExternalUrl=http://$externalDns/webmvc" \
156+
"--from-literal=WebMvcHealthCheckUrl=http://webmvc/hc" \
157+
"--from-literal=MvcClientOrderingUrl=http://ordering" \
158+
"--from-literal=MvcClientCatalogUrl=http://catalog" \
159+
"--from-literal=MvcClientBasketUrl=http://basket" \
160+
"--from-literal=MvcClientMarketingUrl=http://marketing" \
161+
"--from-literal=MvcClientLocationsUrl=http://locations" \
162+
"--from-literal=MarketingHealthCheckUrl=http://marketing/hc" \
163+
"--from-literal=WebSpaHealthCheckUrl=http://webspa/hc" \
164+
"--from-literal=SpaClientMarketingExternalUrl=http://$externalDns/marketing-api" \
165+
"--from-literal=SpaClientOrderingExternalUrl=http://$externalDns/ordering-api" \
166+
"--from-literal=SpaClientCatalogExternalUrl=http://$externalDns/catalog-api" \
167+
"--from-literal=SpaClientBasketExternalUrl=http://$externalDns/basket-api" \
168+
"--from-literal=SpaClientIdentityExternalUrl=http://$externalDns/identity" \
169+
"--from-literal=SpaClientLocationsUrl=http://$externalDns/locations-api" \
170+
"--from-literal=LocationsHealthCheckUrl=http://locations/hc" \
171+
"--from-literal=SpaClientExternalUrl=http://$externalDns" \
172+
"--from-literal=LocationApiClient=http://$externalDns/locations-api" \
173+
"--from-literal=MarketingApiClient=http://$externalDns/marketing-api" \
174+
"--from-literal=BasketApiClient=http://$externalDns/basket-api" \
175+
"--from-literal=OrderingApiClient=http://$externalDns/ordering-api" \
176+
"--from-literal=PaymentHealthCheckUrl=http://payment/hc"
177+
178+
kubectl label configmap urls app=eshop
179+
180+
# externalcfg configmap -- points to local infrastructure components (rabbitmq, SQL Server etc)
181+
kubectl create -f conf_local.yml
182+
183+
# Create application pod deployments
184+
kubectl create -f deployments.yaml
185+
186+
echo "#################### Deploying application pods ####################"
187+
188+
# update deployments with the correct image (with tag and/or registry)
189+
kubectl set image deployments/basket "basket=$container_registry/basket.api:$image_tag"
190+
kubectl set image deployments/catalog "catalog=$container_registry/catalog.api:$image_tag"
191+
kubectl set image deployments/identity "identity=$container_registry/identity.api:$image_tag"
192+
kubectl set image deployments/ordering "ordering=$container_registry/ordering.api:$image_tag"
193+
kubectl set image deployments/marketing "marketing=$container_registry/marketing.api:$image_tag"
194+
kubectl set image deployments/locations "locations=$container_registry/locations.api:$image_tag"
195+
kubectl set image deployments/payment "payment=$container_registry/payment.api:$image_tag"
196+
kubectl set image deployments/webmvc "webmvc=$container_registry/webmvc:$image_tag"
197+
kubectl set image deployments/webstatus "webstatus=$container_registry/webstatus:$image_tag"
198+
kubectl set image deployments/webspa "webspa=$container_registry/webspa:$image_tag"
199+
200+
kubectl rollout resume deployments/basket
201+
kubectl rollout resume deployments/catalog
202+
kubectl rollout resume deployments/identity
203+
kubectl rollout resume deployments/ordering
204+
kubectl rollout resume deployments/marketing
205+
kubectl rollout resume deployments/locations
206+
kubectl rollout resume deployments/payment
207+
kubectl rollout resume deployments/webmvc
208+
kubectl rollout resume deployments/webstatus
209+
kubectl rollout resume deployments/webspa
210+
211+
echo "WebSPA is exposed at http://$externalDns, WebMVC at http://$externalDns/webmvc, WebStatus at http://$externalDns/webstatus"
212+
echo "eShopOnContainers deployment is DONE"

k8s/eshop-namespace.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: eshop
5+

0 commit comments

Comments
 (0)