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Container Registry and Github to set up a new streamlit app on azure.
|
2 |
+
To create a Streamlit application using Terraform, Github Actions, Azure Container Registry, and Github, you will need to follow these steps:
|
3 |
+
|
4 |
+
Create a new Streamlit application.
|
5 |
+
Push the application to a Github repository.
|
6 |
+
Configure the Azure Container Registry.
|
7 |
+
Set up a Github Actions workflow to build and push the Docker image to the Azure Container Registry.
|
8 |
+
Use Terraform to create an Azure Kubernetes Service (AKS) cluster and deploy the Streamlit app on it.
|
9 |
+
Here's an example implementation of these steps:
|
10 |
+
|
11 |
+
Create a new Streamlit app:
|
12 |
+
python
|
13 |
+
Copy code
|
14 |
+
# app.py
|
15 |
+
import streamlit as st
|
16 |
+
|
17 |
+
st.title("Hello, Streamlit!")
|
18 |
+
st.write("This is a simple Streamlit app deployed on Azure.")
|
19 |
+
Create a Dockerfile to containerize the app:
|
20 |
+
docker
|
21 |
+
Copy code
|
22 |
+
# Dockerfile
|
23 |
+
FROM python:3.8-slim
|
24 |
+
|
25 |
+
WORKDIR /app
|
26 |
+
|
27 |
+
COPY requirements.txt .
|
28 |
+
RUN pip install -r requirements.txt
|
29 |
+
|
30 |
+
COPY . .
|
31 |
+
|
32 |
+
CMD ["streamlit", "run", "app.py"]
|
33 |
+
Add a requirements.txt file with the necessary dependencies:
|
34 |
+
makefile
|
35 |
+
Copy code
|
36 |
+
streamlit==1.1.0
|
37 |
+
Push the application to a new Github repository.
|
38 |
+
|
39 |
+
Configure the Azure Container Registry:
|
40 |
+
|
41 |
+
Log in to the Azure Portal.
|
42 |
+
Create a new Azure Container Registry.
|
43 |
+
Copy the registry's login server name for later use.
|
44 |
+
Set up a Github Actions workflow to build and push the Docker image to the Azure Container Registry:
|
45 |
+
yaml
|
46 |
+
Copy code
|
47 |
+
# .github/workflows/main.yml
|
48 |
+
name: CI/CD
|
49 |
+
|
50 |
+
on:
|
51 |
+
push:
|
52 |
+
branches:
|
53 |
+
- main
|
54 |
+
|
55 |
+
jobs:
|
56 |
+
build-and-push:
|
57 |
+
runs-on: ubuntu-latest
|
58 |
+
|
59 |
+
steps:
|
60 |
+
- name: Checkout code
|
61 |
+
uses: actions/checkout@v2
|
62 |
+
|
63 |
+
- name: Set up QEMU
|
64 |
+
uses: docker/setup-qemu-action@v1
|
65 |
+
|
66 |
+
- name: Set up Docker Buildx
|
67 |
+
uses: docker/setup-buildx-action@v1
|
68 |
+
|
69 |
+
- name: Login to Azure Container Registry
|
70 |
+
run: echo ${{ secrets.ACR_PASSWORD }} | docker login ${{ secrets.ACR_REGISTRY }} -u ${{ secrets.ACR_USERNAME }} --password-stdin
|
71 |
+
|
72 |
+
- name: Build and push
|
73 |
+
uses: docker/build-push-action@v2
|
74 |
+
with:
|
75 |
+
context: .
|
76 |
+
push: true
|
77 |
+
tags: ${{ secrets.ACR_REGISTRY }}/streamlit-app:latest
|
78 |
+
Add the necessary secrets to your Github repository:
|
79 |
+
ACR_REGISTRY: The login server name of your Azure Container Registry.
|
80 |
+
ACR_USERNAME: The username for your Azure Container Registry.
|
81 |
+
ACR_PASSWORD: The password for your Azure Container Registry.
|
82 |
+
Create Terraform configuration files to set up the Azure Kubernetes Service (AKS) cluster and deploy the Streamlit app:
|
83 |
+
main.tf: This file will contain the main Terraform configuration.
|
84 |
+
variables.tf: This file will contain the Terraform variables.
|
85 |
+
outputs.tf: This file will contain the Terraform outputs.
|
86 |
+
hcl
|
87 |
+
Copy code
|
88 |
+
# main.tf
|
89 |
+
provider "azurerm" {
|
90 |
+
features {}
|
91 |
+
}
|
92 |
+
|
93 |
+
resource "azurerm_resource_group" "aks" {
|
94 |
+
name = "streamlit-aks-rg"
|
95 |
+
location = "East US"
|
96 |
+
}
|
97 |
+
|
98 |
+
resource "azurerm_kubernetes_cluster" "aks" {
|
99 |
+
name = "streamlit-aks"
|
100 |
+
location = azurerm_resource_group.aks.location
|
101 |
+
resource_group_name = azurerm_resource_group.aks.name
|
102 |
+
dns_prefix = "streamlit-aks"
|
103 |
+
|
104 |
+
default_node_pool {
|
105 |
+
name = "default"
|
106 |
+
node_count =
|
107 |
+
|
108 |
+
3
|
109 |
+
vm_size = "Standard_DS2_v2"
|
110 |
+
}
|
111 |
+
|
112 |
+
identity {
|
113 |
+
type = "SystemAssigned"
|
114 |
+
}
|
115 |
+
}
|
116 |
+
|
117 |
+
resource "azurerm_kubernetes_cluster_node_pool" "streamlit" {
|
118 |
+
name = "streamlit"
|
119 |
+
kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
|
120 |
+
vm_size = "Standard_DS2_v2"
|
121 |
+
node_count = 1
|
122 |
+
}
|
123 |
+
|
124 |
+
provider "kubernetes" {
|
125 |
+
host = azurerm_kubernetes_cluster.aks.kube_config.0.host
|
126 |
+
client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
|
127 |
+
client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
|
128 |
+
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
|
129 |
+
}
|
130 |
+
|
131 |
+
resource "kubernetes_namespace" "streamlit" {
|
132 |
+
metadata {
|
133 |
+
name = "streamlit"
|
134 |
+
}
|
135 |
+
}
|
136 |
+
|
137 |
+
resource "kubernetes_deployment" "streamlit" {
|
138 |
+
metadata {
|
139 |
+
name = "streamlit"
|
140 |
+
namespace = kubernetes_namespace.streamlit.metadata.0.name
|
141 |
+
}
|
142 |
+
|
143 |
+
spec {
|
144 |
+
replicas = 1
|
145 |
+
|
146 |
+
selector {
|
147 |
+
match_labels = {
|
148 |
+
app = "streamlit"
|
149 |
+
}
|
150 |
+
}
|
151 |
+
|
152 |
+
template {
|
153 |
+
metadata {
|
154 |
+
labels = {
|
155 |
+
app = "streamlit"
|
156 |
+
}
|
157 |
+
}
|
158 |
+
|
159 |
+
spec {
|
160 |
+
container {
|
161 |
+
name = "streamlit"
|
162 |
+
image = "${var.acr_login_server}/streamlit-app:latest"
|
163 |
+
|
164 |
+
resources {
|
165 |
+
limits {
|
166 |
+
cpu = "1"
|
167 |
+
memory = "1Gi"
|
168 |
+
}
|
169 |
+
|
170 |
+
requests {
|
171 |
+
cpu = "500m"
|
172 |
+
memory = "500Mi"
|
173 |
+
}
|
174 |
+
}
|
175 |
+
|
176 |
+
port {
|
177 |
+
container_port = 8501
|
178 |
+
}
|
179 |
+
}
|
180 |
+
}
|
181 |
+
}
|
182 |
+
}
|
183 |
+
}
|
184 |
+
|
185 |
+
resource "kubernetes_service" "streamlit" {
|
186 |
+
metadata {
|
187 |
+
name = "streamlit"
|
188 |
+
namespace = kubernetes_namespace.streamlit.metadata.0.name
|
189 |
+
}
|
190 |
+
|
191 |
+
spec {
|
192 |
+
selector = {
|
193 |
+
app = "streamlit"
|
194 |
+
}
|
195 |
+
|
196 |
+
bash
|
197 |
+
Copy code
|
198 |
+
port {
|
199 |
+
port = 80
|
200 |
+
target_port = 8501
|
201 |
+
}
|
202 |
+
|
203 |
+
type = "LoadBalancer"
|
204 |
+
}
|
205 |
+
}
|
206 |
+
|
207 |
+
bash
|
208 |
+
Copy code
|
209 |
+
|
210 |
+
```hcl
|
211 |
+
# variables.tf
|
212 |
+
variable "acr_login_server" {
|
213 |
+
description = "The Azure Container Registry login server"
|
214 |
+
type = string
|
215 |
+
}
|
216 |
+
hcl
|
217 |
+
Copy code
|
218 |
+
# outputs.tf
|
219 |
+
output "streamlit_url" {
|
220 |
+
value = "http://${kubernetes_service.streamlit.status.0.load_balancer.0.ingress.0.ip}"
|
221 |
+
description = "Streamlit app public URL"
|
222 |
+
}
|
223 |
+
Initialize Terraform and apply the configuration:
|
224 |
+
sh
|
225 |
+
Copy code
|
226 |
+
$ terraform init
|
227 |
+
$ terraform apply -var "acr_login_server=<your_acr_login_server>"
|
228 |
+
Replace <your_acr_login_server> with the login server name of your Azure Container Registry.
|
229 |
+
|
230 |
+
Access the Streamlit app using the public URL displayed in the Terraform output.
|
231 |
+
Please note that this is an example implementation and you may need to adjust the configurations according to your specific requirements and environment.
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
|
236 |
+
|
237 |
+
|