Skip to content

Commit ce70277

Browse files
can deploy manifests now from command line
1 parent 85cbd64 commit ce70277

File tree

9 files changed

+279
-0
lines changed

9 files changed

+279
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
param (
2+
[Parameter()]
3+
[string]$nameSuffix = "ek002",
4+
[Parameter()]
5+
[string]$deploymentName = "deploy-rg-aks-$nameSuffix",
6+
[Parameter()]
7+
[string]$resourceGroupName = "rg-aks-$nameSuffix",
8+
[Parameter()]
9+
[string]$location = "canadacentral",
10+
[Parameter()]
11+
[string]$templateFile = "main.bicep",
12+
[Parameter()]
13+
[string]$clusterName = "aks-cluster-$nameSuffix",
14+
[Parameter()]
15+
[string]$dnsPrefix = "$nameSuffix",
16+
[Parameter()]
17+
[string]$linuxAdminUsername = "azureuser",
18+
[Parameter()]
19+
[int]$agentCount = 1,
20+
[Parameter()]
21+
[string]$sshKeyPath = "$HOME\.ssh\aks-${nameSuffix}-id_rsa"
22+
)
23+
24+
# echo parameters
25+
Write-Host "deploymentName: $deploymentName"
26+
Write-Host "nameSuffix: $nameSuffix"
27+
Write-Host "resourceGroupName: $resourceGroupName"
28+
Write-Host "location: $location"
29+
Write-Host "templateFile: $templateFile"
30+
Write-Host "clusterName: $clusterName"
31+
Write-Host "dnsPrefix: $dnsPrefix"
32+
Write-Host "linuxAdminUsername: $linuxAdminUsername"
33+
Write-Host "agentCount: $agentCount"
34+
35+
Write-Output "Creating resource group $resourceGroupName in location $location"
36+
37+
# create resource group
38+
az group create --name $resourceGroupName `
39+
--location $location
40+
41+
# generate ssh key pair
42+
Write-Output "Generating ssh key pair at $sshKeyPath"
43+
if (-not (Test-Path $sshKeyPath)) {
44+
ssh-keygen -t rsa -b 2048 -f $sshKeyPath -q -N ""
45+
}
46+
else {
47+
Write-Output "ssh key pair already exists"
48+
}
49+
50+
# echo ssh public key
51+
Write-Output "Public key:"
52+
$sshPublicKey = Get-Content "$sshKeyPath.pub"
53+
Write-Output $sshPublicKey
54+
55+
Write-Output "Deploying AKS cluster $clusterName in resource group $resourceGroupName"
56+
57+
# deploy aks cluster
58+
az deployment group create --resource-group $resourceGroupName `
59+
--name $deploymentName `
60+
--template-file $templateFile `
61+
--parameters clusterName=$clusterName `
62+
--parameters dnsPrefix=$dnsPrefix `
63+
--parameters linuxAdminUsername=$linuxAdminUsername `
64+
--parameters agentCount=$agentCount `
65+
--parameters sshRSAPublicKey="`"$sshPublicKey`""
66+
67+
# output aks cluster fqdn from deployment output
68+
$fqdn = (az deployment group show `
69+
--name $deploymentName `
70+
--resource-group $resourceGroupName `
71+
--query "properties.outputs.fqdn.value" `
72+
--output tsv)
73+
74+
Write-Output "AKS cluster is deployed at $fqdn"
75+
76+
# give instructions to connect to the cluster
77+
Write-Output "To connect to the cluster, run the following command:"
78+
Write-Output "az aks get-credentials --resource-group $resourceGroupName --name $clusterName --overwrite-existing"
79+
80+
# # give instructions on how to ssh into the cluster
81+
# Write-Output "To ssh into the cluster, run the following command:"
82+
# Write-Output "ssh -i $sshKeyPath $linuxAdminUsername@$fqdn"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
param (
2+
[Parameter()]
3+
[string]$manifestTemplateFolder = "./manifests",
4+
[Parameter()]
5+
[string]$IMAGE = "devopsshield/devsecops-pygoat",
6+
[Parameter()]
7+
[string]$TAG = "latest",
8+
[Parameter()]
9+
[string]$HOSTURL = "pygoat-test.cad4devops.com"
10+
)
11+
# docker pull devopsshield/devsecops-pygoat:latest
12+
13+
# echo parameters
14+
Write-Host "manifestTemplateFolder: $manifestTemplateFolder"
15+
16+
Write-Output "Deploying k8s manifests in folder $manifestTemplateFolder"
17+
18+
# loop through each manifest file in the folder with extension template.yaml
19+
$manifestFiles = Get-ChildItem -Path $manifestTemplateFolder -Filter "*.template.yaml"
20+
foreach ($manifestFile in $manifestFiles) {
21+
Write-Output "Processing manifest file $manifestFile"
22+
$manifestFileContent = Get-Content $manifestFile.FullName
23+
# replace #{image}# with the value of the environment variable IMAGE
24+
$manifestFileContent = $manifestFileContent -replace "#\{image\}#", $IMAGE
25+
# replace #{tag}# with the value of the environment variable TAG
26+
$manifestFileContent = $manifestFileContent -replace "#\{tag\}#", $TAG
27+
# replace #{host}# with the value of the environment variable HOST
28+
$manifestFileContent = $manifestFileContent -replace "#\{host\}#", $HOSTURL
29+
# create a new file with the same name but without the .template extension
30+
$newManifestFile = $manifestFile.FullName -replace ".template", ""
31+
Write-Output "Writing processed manifest file $newManifestFile"
32+
Set-Content -Path $newManifestFile -Value $manifestFileContent
33+
# apply the manifest file
34+
kubectl apply -f $manifestFile.FullName
35+
}

infra/k8s-cluster/main.bicep

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@description('The name of the Managed Cluster resource.')
2+
param clusterName string //= 'aks101cluster'
3+
4+
@description('The location of the Managed Cluster resource.')
5+
param location string = resourceGroup().location
6+
7+
@description('Optional DNS prefix to use with hosted Kubernetes API server FQDN.')
8+
param dnsPrefix string
9+
10+
@description('Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize.')
11+
@minValue(0)
12+
@maxValue(1023)
13+
param osDiskSizeGB int = 0
14+
15+
@description('The number of nodes for the cluster.')
16+
@minValue(1)
17+
@maxValue(50)
18+
param agentCount int = 3
19+
20+
@description('The size of the Virtual Machine.')
21+
param agentVMSize string = 'standard_d2s_v3'
22+
23+
@description('User name for the Linux Virtual Machines.')
24+
param linuxAdminUsername string
25+
26+
@description('Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example \'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm\'')
27+
param sshRSAPublicKey string
28+
29+
resource aks 'Microsoft.ContainerService/managedClusters@2024-08-01' = {
30+
name: clusterName
31+
location: location
32+
identity: {
33+
type: 'SystemAssigned'
34+
}
35+
properties: {
36+
dnsPrefix: dnsPrefix
37+
agentPoolProfiles: [
38+
{
39+
name: 'agentpool'
40+
osDiskSizeGB: osDiskSizeGB
41+
count: agentCount
42+
vmSize: agentVMSize
43+
osType: 'Linux'
44+
mode: 'System'
45+
}
46+
]
47+
linuxProfile: {
48+
adminUsername: linuxAdminUsername
49+
ssh: {
50+
publicKeys: [
51+
{
52+
keyData: sshRSAPublicKey
53+
}
54+
]
55+
}
56+
}
57+
}
58+
}
59+
60+
output fqdn string = aks.properties.fqdn
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygoat-app
5+
labels:
6+
app: pygoat-app
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygoat-app
12+
template:
13+
metadata:
14+
labels:
15+
app: pygoat-app
16+
spec:
17+
containers:
18+
- image: #{image}#:#{tag}#
19+
name: pygoat-app
20+
ports:
21+
- containerPort: 8000
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygoat-app
5+
labels:
6+
app: pygoat-app
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygoat-app
12+
template:
13+
metadata:
14+
labels:
15+
app: pygoat-app
16+
spec:
17+
containers:
18+
- image: devopsshield/devsecops-pygoat:latest
19+
name: pygoat-app
20+
ports:
21+
- containerPort: 8000
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: pygoat-ingress
5+
spec:
6+
ingressClassName: nginx
7+
rules:
8+
- host: #{host}#
9+
http:
10+
paths:
11+
- backend:
12+
service:
13+
name: pygoat-svc
14+
port:
15+
number: 80
16+
path: /
17+
pathType: Prefix
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: pygoat-ingress
5+
spec:
6+
ingressClassName: nginx
7+
rules:
8+
- host: pygoat-test.cad4devops.com
9+
http:
10+
paths:
11+
- backend:
12+
service:
13+
name: pygoat-svc
14+
port:
15+
number: 80
16+
path: /
17+
pathType: Prefix
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: pygoat-svc
5+
labels:
6+
app: pygoat-svc
7+
spec:
8+
type: LoadBalancer # ClusterIP
9+
selector:
10+
app: pygoat-app
11+
ports:
12+
- port: 80
13+
targetPort: 8000
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: pygoat-svc
5+
labels:
6+
app: pygoat-svc
7+
spec:
8+
type: LoadBalancer # ClusterIP
9+
selector:
10+
app: pygoat-app
11+
ports:
12+
- port: 80
13+
targetPort: 8000

0 commit comments

Comments
 (0)