generated from nginx/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.sh
845 lines (696 loc) · 25 KB
/
functions.sh
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#!/bin/sh
# Display colorized warning output for 'warnings'
function cwarn() {
COLOR='\033[01;33m' # bold yellow
RESET='\033[00;00m' # normal white
STATUS="[WARN]"
echo -e "${COLOR}${STATUS}${RESET}"
}
# Display colorized failure output for 'failures'
function cfail() {
COLOR='\033[01;31m' # bold red
RESET='\033[00;00m' # normal white
STATUS="[FAIL]"
echo -e "${COLOR}${STATUS}${RESET}"
}
# Display colorized passing output for 'passing'
function cpass() {
COLOR='\033[01;32m' # bold green
RESET='\033[00;00m' # normal white
STATUS="[DONE]"
echo -e "${COLOR}${STATUS}${RESET}"
}
# Display colorized passing output for 'not applicable'
function cnota() {
COLOR='\033[01;36m' # bold cyan
RESET='\033[00;00m' # normal white
STATUS="[ NA ]"
echo -e "${COLOR}${STATUS}${RESET}"
}
# Add padding to maake 80 characters on the line
function padding {
VAR1=$1
PAD=$((74-$VAR1))
for i in $(seq 1 $PAD); do echo -n ' '; done;
}
function azcli_test {
MESSAGE="Check for AZ CLI login"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
SIGNEDIN=`az ad signed-in-user show | jq -r .id`
if [ test -z "$SIGNEDIN" ]; then
cfail
else
cpass
fi
}
function setup(){
MESSAGE="Set variables for use in script"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
# This script creates clusters and switches k8s contexts.
# Removing this lock at the start makes things run smoother.
# If you changed the username for Owner you may need to edit this path.
rm -rf /Users/${MY_NAME}/.kube/config.lock
# Workaround on OS X for permissions for kubectl
export KUBECONFIG="~/.kube/config"
# Set some system variables for use in the script.
# you should be logged into azure cli before running this script
export MY_RESOURCEGROUP=${MY_NAME}-n4a-workshop
export MY_AZURE_PUBLIC_IP_NAME=n4a-publicIP # we chose this to standardize names for the workshop
export MY_PUBLICIP=$(curl -s ipinfo.io/ip)
export MY_SUBSCRIPTIONID=$(az account show --query id -o tsv)
if [[ -z "$MY_NAME" || -z "$MY_RESOURCEGROUP" || -z "$MY_PUBLICIP" || -z "$MY_SUBSCRIPTIONID" ]]; then
cfail
exit 1
else
cpass
fi
# Let's display some of these variables so we have it for later.
YELLOW='\033[01;33m'
ENDCOLOR='\033[00;00m'
echo -e "\t${YELLOW}MY_NAME:${ENDCOLOR} $MY_NAME"
echo -e "\t${YELLOW}MY_RESOURCEGROUP:${ENDCOLOR} $MY_RESOURCEGROUP"
echo -e "\t${YELLOW}MY_LOCATION:${ENDCOLOR} $MY_LOCATION"
echo -e "\t${YELLOW}MY_PUBLICIP:${ENDCOLOR} $MY_PUBLICIP"
echo -e "\t${YELLOW}MY_SUBSCRIPTIONID:${ENDCOLOR} $MY_SUBSCRIPTIONID"
}
# Clean up any files from previous run.
function cleanup(){
MESSAGE="Cleanup from previous run"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
if [[ -d "n4a-configs-staging" ]]; then
rm -r n4a-configs-staging
cpass
else
cnota
fi
}
function create_resource_group(){
MESSAGE="Creating Resource Group"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
CREATE_RESOURCE_GROUP=`az group create --name $MY_RESOURCEGROUP --location $MY_LOCATION --tags owner="$MY_NAME"`
if [[ -z "$CREATE_RESOURCE_GROUP" ]]; then
cfail
else
cpass
fi
}
function create_vnet(){
MESSAGE="Creating VNet"
LGTH=${#MESSAGE}
CREATE_VNET=`az network vnet create --resource-group $MY_RESOURCEGROUP --name n4a-vnet --address-prefixes 172.16.0.0/16`
if [[ -z "$CREATE_VNET" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_security_group(){
MESSAGE="Creating Network Security Group"
LGTH=${#MESSAGE}
CREATE_SECURITY_GROUP=`az network nsg create --resource-group $MY_RESOURCEGROUP --name n4a-nsg`
if [[ -z "$CREATE_SECURITY_GROUP" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_security_group_rules(){
MESSAGE="Creating NSG rules"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
CREATE_SECURITY_GROUP_RULES1=`az network nsg rule create --resource-group $MY_RESOURCEGROUP --nsg-name n4a-nsg --name HTTP --priority 320 --source-address-prefix $MY_PUBLICIP --source-port-range '*' --destination-address-prefix '*' --destination-port-range 80 --direction Inbound --access Allow --protocol Tcp --description "Allow HTTP traffic"`
CREATE_SECURITY_GROUP_RULES2=`az network nsg rule create --resource-group $MY_RESOURCEGROUP --nsg-name n4a-nsg --name HTTPS --priority 300 --source-address-prefix $MY_PUBLICIP --source-port-range '*' --destination-address-prefix '*' --destination-port-range 443 --direction Inbound --access Allow --protocol Tcp --description "Allow HTTPS traffic"`
CREATE_SECURITY_GROUP_RULES3=`az network nsg rule create --resource-group $MY_RESOURCEGROUP --nsg-name n4a-nsg --name HTTP_ALT --priority 310 --source-address-prefix $MY_PUBLICIP --source-port-range '*' --destination-address-prefix '*' --destination-port-range 8080 --direction Inbound --access Allow --protocol Tcp --description "Allow HTTPS traffic"`
if [[ -z "$CREATE_SECURITY_GROUP_RULES1" || -z "$CREATE_SECURITY_GROUP_RULES2" || -z "$CREATE_SECURITY_GROUP_RULES3" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_subnets(){
MESSAGE="Creating Subnets"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
CREATE_SUBNET=`az network vnet subnet create --resource-group $MY_RESOURCEGROUP --name n4a-subnet --vnet-name n4a-vnet --address-prefixes 172.16.1.0/24 --network-security-group n4a-nsg --delegations NGINX.NGINXPLUS/nginxDeployments`
CREATE_VM_SUBNET=`az network vnet subnet create --resource-group $MY_RESOURCEGROUP --name vm-subnet --vnet-name n4a-vnet --address-prefixes 172.16.2.0/24`
CREATE_AKS1_SUBNET=`az network vnet subnet create --resource-group $MY_RESOURCEGROUP --name aks1-subnet --vnet-name n4a-vnet --address-prefixes 172.16.10.0/23`
CREATE_AKS2_SUBNET=`az network vnet subnet create --resource-group $MY_RESOURCEGROUP --name aks2-subnet --vnet-name n4a-vnet --address-prefixes 172.16.20.0/23`
if [[ -z "$CREATE_SUBNET" || -z "$CREATE_VM_SUBNET" || -z "$CREATE_AKS1_SUBNET" || -z "$CREATE_AKS2_SUBNET" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_public_ip(){
MESSAGE="Creating Public IP"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
CREATE_PUBLIC_IP=`az network public-ip create --name $MY_AZURE_PUBLIC_IP_NAME --resource-group $MY_RESOURCEGROUP --allocation-method Static --sku Standard --location $MY_LOCATION --tags owner="$MY_NAME" --zone 1`
export MY_AZURE_PUBLIC_IP=$(az network public-ip show --resource-group $MY_RESOURCEGROUP --name $MY_AZURE_PUBLIC_IP_NAME --query ipAddress --output tsv)
if [[ -z "$CREATE_PUBLIC_IP" ]]; then
cfail
else
cpass
fi
}
function create_identity(){
MESSAGE="Creating Managed Identity"
LGTH=${#MESSAGE}
echo -ne $MESSAGE
padding $LGTH
CREATE_IDENTITY=`az identity create --resource-group $MY_RESOURCEGROUP --name n4a-useridentity`
if [[ -z "$CREATE_IDENTITY" ]]; then
cfail
else
cpass
fi
}
function create_n4a_deployment(){
MESSAGE="Creating N4A Deployment"
LGTH=${#MESSAGE}
CREATE_N4A_DEPLOYMENT=`az nginx deployment create --resource-group $MY_RESOURCEGROUP --name nginx4a --sku name="standardv2_Monthly" --network-profile front-end-ip-configuration="{public-ip-addresses:[{id:/subscriptions/$MY_SUBSCRIPTIONID/resourceGroups/$MY_RESOURCEGROUP/providers/Microsoft.Network/publicIPAddresses/n4a-publicIP}]}" network-interface-configuration="{subnet-id:/subscriptions/$MY_SUBSCRIPTIONID/resourceGroups/$MY_RESOURCEGROUP/providers/Microsoft.Network/virtualNetworks/n4a-vnet/subnets/n4a-subnet}" --identity="{type:'SystemAssigned, UserAssigned',userAssignedIdentities:{/subscriptions/$MY_SUBSCRIPTIONID/resourceGroups/$MY_RESOURCEGROUP/providers/Microsoft.ManagedIdentity/userAssignedIdentities/n4a-useridentity:{}}}"`
CREATE_N4A_DEPLOYMENT_DIAG=`az nginx deployment update --resource-group $MY_RESOURCEGROUP --name nginx4a --enable-diagnostics true`
export MY_N4A_ID=$(az nginx deployment show --resource-group $MY_RESOURCEGROUP --name nginx4a --query id --output tsv)
if [[ -z "$CREATE_N4A_DEPLOYMENT" || -z "$CREATE_N4A_DEPLOYMENT_DIAG" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_analytics(){
MESSAGE="Creating Log Analytics Monitor"
LGTH=${#MESSAGE}
CREATE_LOG_ANALYTICS=`az monitor log-analytics workspace create --resource-group $MY_RESOURCEGROUP --name n4a-loganalytics`
export MY_LOG_ANALYTICS_ID=`az monitor log-analytics workspace show --resource-group $MY_RESOURCEGROUP --name n4a-loganalytics --query id --output tsv`
CREATE_DIAG_SETTINGS=`az monitor diagnostic-settings create \
--name n4a-nginxlogs \
--resource $MY_N4A_ID \
--logs "[{category:NginxLogs,enabled:true,retention-policy:{enabled:false,days:0}}]" \
--resource-group $MY_RESOURCEGROUP \
--workspace $MY_LOG_ANALYTICS_ID`
if [[ -z "$CREATE_LOG_ANALYTICS" || -z "$CREATE_DIAG_SETTINGS" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_ubuntu_vm(){
MESSAGE="Creating the Ubuntu VM"
LGTH=${#MESSAGE}
ID=`az group show -n $MY_RESOURCEGROUP --query "id" -otsv`
CREATE_UBUNTU_VM=`az vm create \
--resource-group $MY_RESOURCEGROUP \
--name n4a-ubuntuvm \
--image Ubuntu2204 \
--admin-username azureuser \
--vnet-name n4a-vnet \
--subnet vm-subnet \
--assign-identity \
--scope $ID \
--role Owner \
--generate-ssh-keys \
--public-ip-sku Standard \
--custom-data labs/lab2/init.sh \
--security-type TrustedLaunch \
--enable-secure-boot true \
--enable-vtpm true`
export UBUNTU_VM_PUBLICIP=$(az vm show -d -g $MY_RESOURCEGROUP -n n4a-ubuntuvm --query publicIps -o tsv)
# az wants to have a role assignment to grant access, so let's do that here:
VMPRINCIPALID=`az vm show -g $MY_RESOURCEGROUP -n n4a-ubuntuvm --query "identity.principalId" -otsv`
RA=`az role assignment create --assignee $VMPRINCIPALID --role contributor --scope $ID`
if [[ -z "$CREATE_UBUNTU_VM" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function secure_port_22(){
MESSAGE="Securing port 22"
LGTH=${#MESSAGE}
SECURE_PORT_22=`az network nsg rule update \
--resource-group $MY_RESOURCEGROUP \
--nsg-name n4a-ubuntuvmNSG \
--name default-allow-ssh \
--destination-port-ranges 22 \
--source-address-prefix $MY_PUBLICIP`
if [[ -z "$SECURE_PORT_22" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_windows_vm(){
MESSAGE="Creating the Windows VM"
LGTH=${#MESSAGE}
# Set the image we want to use to build this VM
export MY_VM_IMAGE=cognosys:iis-on-windows-server-2016:iis-on-windows-server-2016:1.2019.1009
#export MY_VM_IMAGE=cognosys:iis-on-win-server-2022:iis-on-win-server-2022:0.0.1
CREATE_WINDOWS_VM=`az vm create \
--only-show-errors \
--resource-group $MY_RESOURCEGROUP \
--name n4a-windowsvm \
--image $MY_VM_IMAGE \
--vnet-name n4a-vnet \
--subnet vm-subnet \
--admin-username azureuser \
--admin-password "Nginxuser@123" \
--public-ip-sku Standard \
--security-type Standard`
export WINDOWS_VM_PUBLICIP=$(az vm show -d -g $MY_RESOURCEGROUP -n n4a-windowsvm --query publicIps -o tsv)
if [[ -z "$CREATE_WINDOWS_VM" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function secure_port_3389(){
MESSAGE="Secure port 3389"
LGTH=${#MESSAGE}
CREATE_SECURITY_GROUP_RULES4=`az network nsg rule create --resource-group $MY_RESOURCEGROUP --nsg-name n4a-windowsvmNSG --name RDP --priority 410 --source-address-prefix $MY_PUBLICIP --source-port-range '*' --destination-address-prefix '*' --destination-port-range 3389 --direction Inbound --access Allow --protocol Tcp --description "Allow RDP traffic" --only-show-errors`
if [[ -z "$CREATE_SECURITY_GROUP_RULES4" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
## Lab 3
function create_aks_cluster1(){
# Set some variables to use with this part of the lab setup
export MY_AKS1=n4a-aks1
export MY_AKS2=n4a-aks2
export MY_NAME=${MY_NAME:-$(whoami)}
export K8S_VERSION=1.29
export MY_SUBNET1=$(az network vnet subnet show -g $MY_RESOURCEGROUP -n aks1-subnet --vnet-name n4a-vnet --only-show-errors --query id -o tsv)
export MY_SUBNET2=$(az network vnet subnet show -g $MY_RESOURCEGROUP -n aks2-subnet --vnet-name n4a-vnet --only-show-errors --query id -o tsv)
# This requires that you place your JWT file in the labs/lab3 directory and name it nginx-repo.jwt
export JWT=$(cat labs/lab3/nginx-repo.jwt)
MESSAGE="Creating AKS Cluster 1"
LGTH=${#MESSAGE}
CREATE_AKS_CLUSTER1=`az aks create \
--only-show-errors \
--resource-group $MY_RESOURCEGROUP \
--name $MY_AKS1 \
--node-count 3 \
--node-vm-size Standard_B2s \
--kubernetes-version $K8S_VERSION \
--tags owner=$MY_NAME \
--vnet-subnet-id=$MY_SUBNET1 \
--enable-addons monitoring \
--generate-ssh-keys`
AKS1_CREDS=`az aks get-credentials --only-show-errors --resource-group $MY_RESOURCEGROUP --name n4a-aks1 --overwrite-existing`
if [[ -z "$CREATE_AKS_CLUSTER1" ]]; then
echo
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function clone_repo(){
if [ -d kubernetes-ingress/deployments ]; then
EXISTS=1
MESSAGE="Use Existing NGINX Ingress Controller Repo"
LGTH=${#MESSAGE}
else
CLONE=`git clone https://github.com/nginxinc/kubernetes-ingress.git --branch v3.3.2`
cd kubernetes-ingress/deployments
MESSAGE="Cloning NGINX Ingress Controller Repo"
LGTH=${#MESSAGE}
fi
if [[ -z "$CLONE" || -z "$EXISTS" ]]; then
echo -ne $MESSAGE
padding $LGTH
cpass
else
echo -ne $MESSAGE
padding $LGTH
cfail
fi
}
function create_nic_resources1(){
MESSAGE="Creating NGINX Ingress Controller Resources"
LGTH=${#MESSAGE}
CREATE_NIC_RESOURCES1=`kubectl apply -f kubernetes-ingress/deployments/common/ns-and-sa.yaml
kubectl apply -f kubernetes-ingress/deployments/rbac/rbac.yaml
kubectl apply -f kubernetes-ingress/examples/shared-examples/default-server-secret/default-server-secret.yaml
kubectl apply -f kubernetes-ingress/deployments/common/nginx-config.yaml
kubectl apply -f kubernetes-ingress/deployments/common/ingress-class.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_virtualservers.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_virtualserverroutes.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_transportservers.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_policies.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_globalconfigurations.yaml`
if [[ -z "$CREATE_NIC_RESOURCES1" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_jwt1(){
MESSAGE="Creating Secret for NGINX Plus JWT"
LGTH=${#MESSAGE}
CREATE_JWT1=`kubectl create secret docker-registry regcred \
--docker-server=private-registry.nginx.com \
--docker-username=$JWT \
--docker-password=none \
-n nginx-ingress`
kubectl get secret regcred -n nginx-ingress -o yaml
if [[ -z "$CREATE_JWT1" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function deploy_nic1(){
MESSAGE="Deploying NGINX Ingress Controller"
LGTH=${#MESSAGE}
DEPLOY_NIC1=`kubectl apply -f labs/lab3/nginx-plus-ingress.yaml`
#kubectl get pods -n nginx-ingress
export AKS1_NIC=$(kubectl get pods -n nginx-ingress -o jsonpath='{.items[0].metadata.name}')
if [[ -z "$DEPLOY_NIC1" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_aks_cluster2(){
MESSAGE="Creating AKS Cluster 2"
LGTH=${#MESSAGE}
CREATE_AKS_CLUSTER2=`az aks create \
--only-show-errors \
--resource-group $MY_RESOURCEGROUP \
--name $MY_AKS2 \
--node-count 4 \
--node-vm-size Standard_B2s \
--kubernetes-version $K8S_VERSION \
--tags owner=$MY_NAME \
--vnet-subnet-id=$MY_SUBNET2 \
--network-plugin azure \
--enable-addons monitoring \
--generate-ssh-keys`
AKS2_CREDS=`az aks get-credentials --resource-group $MY_RESOURCEGROUP --name n4a-aks2 --overwrite-existing --only-show-errors`
echo
# switch context for the next section
kubectl config use-context n4a-aks2
if [[ -z "$CREATE_AKS_CLUSTER2" ]]; then
echo
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_nic_resources2(){
MESSAGE="Creating NGINX Ingress Controller Resources"
LGTH=${#MESSAGE}
CREATE_NIC_RESOURCES2=`kubectl apply -f kubernetes-ingress/deployments/common/ns-and-sa.yaml
kubectl apply -f kubernetes-ingress/deployments/rbac/rbac.yaml
kubectl apply -f kubernetes-ingress/examples/shared-examples/default-server-secret/default-server-secret.yaml
kubectl apply -f kubernetes-ingress/deployments/common/nginx-config.yaml
kubectl apply -f kubernetes-ingress/deployments/common/ingress-class.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_virtualservers.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_virtualserverroutes.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_transportservers.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_policies.yaml
kubectl apply -f kubernetes-ingress/deployments/common/crds/k8s.nginx.org_globalconfigurations.yaml`
if [[ -z "$CREATE_NIC_RESOURCES2" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function create_jwt2(){
MESSAGE="Creating Secret for NGINX Plus JWT"
LGTH=${#MESSAGE}
CREATE_JWT2=`kubectl create secret docker-registry regcred --docker-server=private-registry.nginx.com --docker-username=$JWT --docker-password=none -n nginx-ingress`
kubectl get secret regcred -n nginx-ingress -o yaml
if [[ -z "$CREATE_JWT2" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function deploy_nic2(){
MESSAGE="Deploying NGINX Ingress Controller"
LGTH=${#MESSAGE}
DEPLOY_NIC2=`kubectl apply -f labs/lab3/nginx-plus-ingress.yaml`
#kubectl get pods -n nginx-ingress
export AKS2_NIC=$(kubectl get pods -n nginx-ingress -o jsonpath='{.items[0].metadata.name}')
if [[ -z "$DEPLOY_NIC2" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function kubectl_apply(){
MESSAGE="Applying Virtual Server and Dashboards"
LGTH=${#MESSAGE}
kubectl config use-context n4a-aks1 > /dev/null 2>&1 &
kubectl apply -f labs/lab3/dashboard-vs.yaml > /dev/null 2>&1 &
kubectl get svc,vs -n nginx-ingress > /dev/null 2>&1 &
kubectl apply -f labs/lab4/nodeport-static-redis.yaml > /dev/null 2>&1 &
kubectl config use-context n4a-aks2 > /dev/null 2>&1 &
kubectl apply -f labs/lab3/dashboard-vs.yaml > /dev/null 2>&1 &
kubectl get svc,vs -n nginx-ingress > /dev/null 2>&1 &
kubectl apply -f labs/lab4/nodeport-static-redis.yaml > /dev/null 2>&1 &
kubectl config use-context n4a-aks1 > /dev/null 2>&1 &
echo -ne $MESSAGE
padding $LGTH
cpass
}
function create_nsg_rule_aks(){
MESSAGE="Allow traffic to NIC Dashboards"
LGTH=${#MESSAGE}
CREATE_NSG_RULE_AKS=`az network nsg rule create \
--only-show-errors \
--resource-group $MY_RESOURCEGROUP \
--nsg-name n4a-nsg \
--name NIC_Dashboards \
--priority 330 \
--source-address-prefix $MY_PUBLICIP \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 9001-9002 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--description "Allow traffic to NIC Dashboards"`
if [[ -z "$CREATE_NSG_RULE_AKS" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
## lab 4
function deploy_apps(){
MESSAGE="Deploying Coffe/Tea App and Redis"
LGTH=${#MESSAGE}
kubectl config use-context n4a-aks1 > /dev/null 2>&1 &
kubectl apply -f labs/lab4/cafe.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/cafe-vs.yaml > /dev/null 2>&1 &
kubectl scale deployment coffee --replicas=2 > /dev/null 2>&1 &
kubectl scale deployment tea --replicas=2 > /dev/null 2>&1 &
kubectl config use-context n4a-aks2 > /dev/null 2>&1 &
kubectl apply -f labs/lab4/cafe.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/cafe-vs.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/redis-leader.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/redis-follower.yaml > /dev/null 2>&1 &
#kubectl get pods,svc -l app=redis
kubectl apply -f labs/lab4/global-configuration-redis.yaml > /dev/null 2>&1 &
kubectl describe gc nginx-configuration -n nginx-ingress > /dev/null 2>&1 &
kubectl apply -f labs/lab4/redis-leader-ts.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/redis-follower-ts.yaml > /dev/null 2>&1 &
kubectl apply -f labs/lab4/nodeport-static-redis.yaml > /dev/null 2>&1 &
rm -rf /Users/${MY_NAME}/.kube/config.lock
echo -ne $MESSAGE
padding $LGTH
cpass
}
#kubectl get svc -n nginx-ingress
function get_node_ids(){
MESSAGE="Get Node IDs and update configs"
LGTH=${#MESSAGE}
kubectl config use-context n4a-aks1
export AKS1_NODE_NUMBER=$(kubectl get nodes -o jsonpath="{.items[0].metadata.name}" | cut -d- -f 3)
kubectl config use-context n4a-aks2
export AKS2_NODE_NUMBER=$(kubectl get nodes -o jsonpath="{.items[0].metadata.name}" | cut -d- -f 3)
cp -r n4a-configs/ n4a-configs-staging/
sed -i '' 's/_AKS1_NODES_/'$AKS1_NODE_NUMBER'/g' n4a-configs-staging/etc/nginx/conf.d/aks1-upstreams.conf
sed -i '' 's/_AKS2_NODES_/'$AKS2_NODE_NUMBER'/g' n4a-configs-staging/etc/nginx/conf.d/aks2-upstreams.conf
sed -i '' 's/_AKS1_NODES_/'$AKS1_NODE_NUMBER'/g' n4a-configs-staging/etc/nginx/conf.d/nic1-dashboard-upstreams.conf
sed -i '' 's/_AKS2_NODES_/'$AKS2_NODE_NUMBER'/g' n4a-configs-staging/etc/nginx/conf.d/nic2-dashboard-upstreams.conf
echo -ne $MESSAGE
padding $LGTH
cpass
}
function create_archive(){
MESSAGE="Creating the archive"
LGTH=${#MESSAGE}
cd n4a-configs-staging
COPYFILE_DISABLE=1 tar --exclude='.DS_Store' -czf ../n4a-configs.tar.gz *
cd ..
rm -r n4a-configs-staging
echo -ne $MESSAGE
padding $LGTH
cpass
}
function upload_archive(){
MESSAGE="Prepare and upload archive package"
LGTH=${#MESSAGE}
export B64_N4A_CONFIG=$(base64 -i n4a-configs.tar.gz | tr -d '\n')
cat << EOF > package.json
{
"data": "$B64_N4A_CONFIG"
}
EOF
PACKAGE=`az nginx deployment configuration create \
--configuration-name default \
--deployment-name nginx4a \
--resource-group $MY_RESOURCEGROUP \
--root-file var/nginx.conf \
--package "@package.json"`
if [[ -z "$PACKAGE" ]]; then
echo -ne $MESSAGE
padding $LGTH
cfail
else
echo -ne $MESSAGE
padding $LGTH
cpass
fi
}
function update_hosts_file(){
MESSAGE="Updating hosts file with Azure Public IP"
LGTH=${#MESSAGE}
HOST="cafe.example.com bar.example.com dashboard.example.com grafana.example.com prometheus.example.com juiceshop.example.com redis.example.com"
sudo sed -i '' '/cafe.example.com/ s/.*/'"$MY_AZURE_PUBLIC_IP"'\t'"$HOST"'/g' /etc/hosts
echo -ne $MESSAGE
padding $LGTH
cpass
}
function delete(){
MESSAGE="Resource Group Deletion"
LGTH=${#MESSAGE}
# This will remove the resource group and everything in it. It will prompt for confirmation.
az group delete --name $MY_RESOURCEGROUP
echo -ne $MESSAGE
padding $LGTH
cpass
}
function display(){
# Display more variable values that we used in the script.
# Per instructions, copy output and paste back to the terminal so we have these moving forward.
export MY_N4A_ID=$(az nginx deployment show --resource-group $MY_RESOURCEGROUP --name nginx4a --query id --output tsv)
export MY_LOG_ANALYTICS_ID=`az monitor log-analytics workspace show --resource-group $MY_RESOURCEGROUP --name n4a-loganalytics --query id --output tsv`
kubectl config use-context n4a-aks1
export AKS1_NIC=$(kubectl get pods -n nginx-ingress -o jsonpath='{.items[0].metadata.name}')
kubectl config use-context n4a-aks2
export AKS2_NIC=$(kubectl get pods -n nginx-ingress -o jsonpath='{.items[0].metadata.name}')
export MY_AZURE_PUBLIC_IP=$(az network public-ip show --resource-group $MY_RESOURCEGROUP --name $MY_AZURE_PUBLIC_IP_NAME --query ipAddress --output tsv)
export UBUNTU_VM_PUBLICIP=$(az vm show -d -g $MY_RESOURCEGROUP -n n4a-ubuntuvm --query publicIps -o tsv)
export WINDOWS_VM_PUBLICIP=$(az vm show -d -g $MY_RESOURCEGROUP -n n4a-windowsvm --query publicIps -o tsv)
cat <<EOI
export MY_N4A_ID=$MY_N4A_ID
export MY_LOG_ANALYTICS_ID=$MY_LOG_ANALYTICS_ID
export AKS1_NIC=$AKS1_NIC
export AKS2_NIC=$AKS2_NIC
export MY_NAME=$MY_NAME
export MY_RESOURCEGROUP=$MY_RESOURCEGROUP
export MY_LOCATION=$MY_LOCATION
export MY_PUBLICIP=$MY_PUBLICIP
export MY_SUBSCRIPTIONID=$MY_SUBSCRIPTIONID
export MY_AZURE_PUBLIC_IP=$MY_AZURE_PUBLIC_IP
export UBUNTU_VM_PUBLICIP=$UBUNTU_VM_PUBLICIP
export WINDOWS_VM_PUBLICIP=$WINDOWS_VM_PUBLICIP
Copy the export statements and paste them into your terminal (so the variables exist outside the script.)
Access the Ubuntu VM with: ssh azureuser@$UBUNTU_VM_PUBLICIP
Access the Windows VM by using RDP to host: $WINDOWS_VM_PUBLICIP with user: azureuser and password: Nginxuser@123
EOI
}