Uploading final .md files for PostgreSQL Code Challenge
This commit is contained in:
Родитель
d6ccd4672d
Коммит
0457d88692
|
@ -0,0 +1,31 @@
|
||||||
|
## Using Azure Database for PostgreSQL to Run a Python Application
|
||||||
|
### Overview
|
||||||
|
Azure Database for PostgreSQL is a PostgreSQL database service built on Microsoft's scalable cloud infrastructure for application developers.
|
||||||
|
|
||||||
|
Leverage your existing open-source PostgreSQL skills and tools and scale on-the-fly without downtime to efficiently deliver existing and new applications with reduced operational overhead.
|
||||||
|
|
||||||
|
Built-in features maximize performance, availability, and security. Azure Database for PostgreSQL empowers developers to focus on application innovation instead of database management tasks.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Scenario Overview
|
||||||
|
This hands-on lab will step you through the following:
|
||||||
|
|
||||||
|
1. Create an Azure storage account and initialize Azure Cloud Shell for Azure CLI.
|
||||||
|
1. Create an **Azure Database for PostgreSQL** instance.
|
||||||
|
1. Create an **Ubuntu Azure VM**.
|
||||||
|
2. Enable the firewall.
|
||||||
|
3. Create a database for an app written in Python and Django in Azure PostgreSQL database instance.
|
||||||
|
4. Login to ubuntu VM.
|
||||||
|
5. Download the app from GitHub: git clone https://github.com/vitorfs/bootcamp.git - cd bootcamp.
|
||||||
|
7. Change your connection string - vim .env - Paste this in the .env file (change database credentials to yours)
|
||||||
|
|
||||||
|
<span style="color:blue">DEBUG=True<br>SECRET_KEY='mys3cr3tk3y'<br>DATABASE_URL='postgres://pgsqluser@postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" />:P@ssword1@<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" />.database.windows.net:5432/bootcamp'<br>ALLOWED_HOSTS = "*"
|
||||||
|
8. Run migration and serve the app
|
||||||
|
|
||||||
|
Python manage.py migrate
|
||||||
|
Python manage.py runserver 0.0.0.0:8000
|
||||||
|
|
||||||
|
9. Open browser and go to http://[externalIpAddress]:8000 to interact with the app.
|
||||||
|
10. Play around with the app, and demonstrate that it works.
|
||||||
|
|
|
@ -0,0 +1,267 @@
|
||||||
|
##### Developing apps with Azure Database for PostgreSQL
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Start Lab
|
||||||
|
|
||||||
|
|
||||||
|
An **Azure Resource Group** is a logical container into which Azure resources are deployed and managed. A **Resource Group** and a **Storage Account** must exist before any virtual machines can be created.
|
||||||
|
In this example, a resource group named **rg123456** (where **123456** is the number in your username) is created in the southcentralus region. You will need to use this group name when creating your Database and Server.
|
||||||
|
|
||||||
|
Throughout this lab, we will use the **Azure Command Line Interface** or Azure CLI using the Cloud Shell feature in the Azure Portal.
|
||||||
|
|
||||||
|
**Sign in to the Azure portal**
|
||||||
|
|
||||||
|
1. [Click here to open the Azure portal](https://portal.azure.com) and maximize the browser window
|
||||||
|
1. In the **Email or phone** field, enter <span style="color:blue">**<inject key="AzureAdUserEmail" />**
|
||||||
|
1. In the **Password** field, enter <span style="color:blue">**<inject key="AzureAdUserPassword" />**
|
||||||
|
1. Click **Sign in**
|
||||||
|
1. You _may_ encounter a popup entitled **Welcome to Microsoft Azure** with buttons for **Start Tour** and **Maybe Later** - Choose **Maybe Later**
|
||||||
|
|
||||||
|
**Create a storage account and Initialize the Azure CLI**
|
||||||
|
|
||||||
|
1. To launch the **Azure Cloud Shell**, click the Cloud Shell button on the menu in the top menu bar of the Azure portal.
|
||||||
|
|
||||||
|
![Cloud Shell Button](imgs/cloud-shell-menu.png)
|
||||||
|
|
||||||
|
> The button launches an interactive shell that you can use to run all of the steps required to create and manage an Ubuntu Linux VM.
|
||||||
|
|
||||||
|
1. Once the shell launches, you will see a notification that you need to add storage.
|
||||||
|
![Create Storage](imgs/cli-storage.png)
|
||||||
|
Leave the Subscription unchanged, and select **"Show advanced settings"**
|
||||||
|
1. In the **File share** section, select **Create new** and give it a unique name.
|
||||||
|
1. Click **Create storage**
|
||||||
|
1. Once the storage gets created, your **Cloud Shell** will initialize and very shortly be ready to use.
|
||||||
|
![Shell is Ready](imgs/shell.png)
|
||||||
|
Notice the **Resource Group** name, the **Storage Account**, and the **File Share** you created are displayed in the CLI while it initializes.
|
||||||
|
|
||||||
|
#### Create an Azure PostGreSQL Database Server
|
||||||
|
|
||||||
|
1. A server contains a group of databases. Before we can create the **PostgreSQL Server**, we need to authenticate using **Azure Device Authentication**. Type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue">az login
|
||||||
|
|
||||||
|
> Note: Copy the code you are provided and use it for the next step.
|
||||||
|
|
||||||
|
1. Open a web browser and enter the following **URL**:
|
||||||
|
|
||||||
|
<span style="color:blue"> https://aka.ms/devicelogin
|
||||||
|
|
||||||
|
1. In the devicelogin page, type in the code and hit **Continue**. The next screen will display the **Temporary User** login.
|
||||||
|
|
||||||
|
1. Select the account and when login is confirmed, you can switch back to the **Azure Portal** and **Azure Cloud Shell**
|
||||||
|
|
||||||
|
1. Now that you are authenticated, you can create an **Azure Database for PostgreSQL server** using the **az postgres server create** command.
|
||||||
|
|
||||||
|
Type or copy and paste the following into the Azure command line:
|
||||||
|
|
||||||
|
<span style="color:blue"> az postgres server create --resource-group <inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --name postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --location southcentralus --admin-user pgsqluser --admin-password P@ssword1 --performance-tier Basic --compute-units 50 --version 9.6
|
||||||
|
|
||||||
|
> **Note:** Be sure to remember your **user name** and **password** as you will need to use it later for your connection information.
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
1. Create an Azure PostgreSQL server-level firewall rule with the **az postgres server firewall-rule create** command. A server-level firewall rule allows an external application, such as psql or PgAdmin to connect to your server through the Azure PostgreSQL service firewall.
|
||||||
|
|
||||||
|
<span style="color:blue"> az postgres server firewall-rule create --resource-group <inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --server postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --name AllowAllIps --start-ip-address 0.0.0.0 --end-ip-address 255.255.255.255
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
1. Now let's get the connection information for your new PostGreSQL Azure Database Server. To connect to your server, you need to provide host information and access credentials.
|
||||||
|
|
||||||
|
<span style="color:blue"> az postgres server show --resource-group <inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --name postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" />
|
||||||
|
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
The result is output to the screen in JSON format as shown in the example below. Make a note of the **administratorLogin** and **fullyQualifiedDomainName**.
|
||||||
|
> **Note:** Your **fullyQualifiedDomainName** will be **<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName"/>.postgres.database.azure.com**
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"administratorLogin": "pgsqluser",
|
||||||
|
"fullyQualifiedDomainName": "postgresqlrg123456.postgres.database.azure.com",
|
||||||
|
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforPostgreSQL/servers/mypgserver-20170401",
|
||||||
|
"location": "southcentralus",
|
||||||
|
"name": "postgresqlrg123456",
|
||||||
|
"resourceGroup": "rg123456",
|
||||||
|
"sku": {
|
||||||
|
"capacity": 50,
|
||||||
|
"family": null,
|
||||||
|
"name": "PGSQLS2M50",
|
||||||
|
"size": null,
|
||||||
|
"tier": "Basic"
|
||||||
|
},
|
||||||
|
"sslEnforcement": null,
|
||||||
|
"storageMb": 51200,
|
||||||
|
"tags": null,
|
||||||
|
"type": "Microsoft.DBforPostgreSQL/servers",
|
||||||
|
"userVisibleState": "Ready",
|
||||||
|
"version": "9.6"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
#### Create and connect to your PostgreSQL database using psql
|
||||||
|
1. Type the following **psql** cmmand in **Azure Cloud Shell** and hit **Enter:**
|
||||||
|
|
||||||
|
<span style="color:blue"> psql --host=postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName"/>.postgres.database.azure.com --port=5432 --username=pgsqluser@postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --dbname=postgres
|
||||||
|
|
||||||
|
1. You will be prompted for a password. Type **P@ssword1** at the prompt and hit **Enter**.
|
||||||
|
|
||||||
|
> Note: the **psql prompt** will not echo what you type.
|
||||||
|
|
||||||
|
1. Once you are connected to the server with **psql**, create a blank database for your Python app to use. At the prompt, type the following command and hit **Enter:**
|
||||||
|
|
||||||
|
<span style="color:blue"> create database bootcamp;
|
||||||
|
1. The Bootcamp database is now created. To connect to it and confirm it was created, type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> \c bootcamp;
|
||||||
|
1. Exit **psql** and return to the **Azure Cloud Shell** prompt. Type the following command and hit **Enter:**
|
||||||
|
|
||||||
|
<span style="color:blue"> \q
|
||||||
|
|
||||||
|
#### Create an Azure VM running Ubuntu Server
|
||||||
|
1. Create a virtual machine with the az vm create command. When creating a virtual machine, several options are available such as operating system image, disk sizing, and administrative credentials. In this example, a virtual machine is created with a name of myVM running Ubuntu Server.
|
||||||
|
|
||||||
|
<span style="color:blue"> az vm create --resource-group <inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --name myubuntu --vnet-name myvnet --image ubuntults --generate-ssh-keys
|
||||||
|
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
1. Once the VM has been created, the Azure CLI outputs information about the VM. Take note of the publicIpAddress, this address can be used to access the virtual machine..
|
||||||
|
|
||||||
|
{
|
||||||
|
"fqdns": "",
|
||||||
|
"id": "/subscriptions/d5b9d4b7-6fc1-0000-0000-000000000000/resourceGroups/myResourceGroupVM/providers/Microsoft.Compute/virtualMachines/myVM",
|
||||||
|
"location": "southcentralus",
|
||||||
|
"macAddress": "00-0D-3A-23-9A-49",
|
||||||
|
"powerState": "VM running",
|
||||||
|
"privateIpAddress": "10.0.0.4",
|
||||||
|
"publicIpAddress": "xxx.xxx.xxx.xxx",
|
||||||
|
"resourceGroup": "rg123456"
|
||||||
|
}
|
||||||
|
|
||||||
|
1. By default, only SSH connections are allowed into Linux virtual machines deployed in Azure. This VM is going to be a webserver, so you need to open a port from the Internet. Use the **az vm open-port** command to open the desired port.
|
||||||
|
|
||||||
|
<span style="color:blue"> az vm open-port --port 8000 --resource-group <inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --name myubuntu
|
||||||
|
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
1. Connect to the VM using SSH in the Azure CLI.
|
||||||
|
|
||||||
|
<span style="color:blue"> ssh [publicIpAddress]
|
||||||
|
|
||||||
|
> **A:** Replace publicIpAddress with the IP address of your server noted in the previous step (4).
|
||||||
|
|
||||||
|
1. Hit **Enter**
|
||||||
|
|
||||||
|
1. When prompted to continue, type <span style="color:blue">**yes** </span>and hit **Enter**
|
||||||
|
|
||||||
|
#### Configure the Bootcamp Application
|
||||||
|
1. Now you are at the shell of your new Ubuntu VM. Let's update the package index in Ubuntu, so that we have a recent list of the package repository
|
||||||
|
|
||||||
|
1. In your shell, type the following command and hit **Enter:**
|
||||||
|
|
||||||
|
<span style="color:blue"> sudo apt update
|
||||||
|
|
||||||
|
1. Now let's install the Python Package Index. Type the following command and hit **Enter:**
|
||||||
|
|
||||||
|
<span style="color:blue"> sudo apt install python-pip
|
||||||
|
|
||||||
|
> **Note:** When prompted to provide a yes/no, answer **yes**.
|
||||||
|
|
||||||
|
1. We are now ready to install our Bootcamp application that we will use with PostgreSQL. We will need to download a copy from GitHub.
|
||||||
|
|
||||||
|
1. To connect to GitHub and pull the code for Bootcamp, type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> git clone https://github.com/vitorfs/bootcamp.git
|
||||||
|
|
||||||
|
> **Note:** Feel free to browse to Github and have a look at the Bootcamp repo to learn about what the application does: https://github.com/vitorfs/bootcamp.git
|
||||||
|
|
||||||
|
1. In your shell, type the following and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> cd bootcamp
|
||||||
|
1. Now we will install the Django Framework requirements for the Python app. Type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> pip install -U -r requirements.txt
|
||||||
|
|
||||||
|
1. To list the files in the ../bootcamp directory, use the ls command.
|
||||||
|
|
||||||
|
> **Note:** You will need to run some of these commands as root. To do this, type "sudo" then the command you wish to run.
|
||||||
|
|
||||||
|
Type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> sudo ls -la
|
||||||
|
|
||||||
|
> **Note:** the manage.py script that was installed when we cloned the Git repository.
|
||||||
|
|
||||||
|
1. While still in the ../bootcamp directory, type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> vim .env
|
||||||
|
|
||||||
|
|
||||||
|
1. Enter the following lines to create your database connection environment file:
|
||||||
|
|
||||||
|
> **A:** Type <span style="color:blue">**i** </span>then hit **Enter**
|
||||||
|
>
|
||||||
|
>
|
||||||
|
> **B:** Copy and paste the following into the **vim editor**
|
||||||
|
>
|
||||||
|
|
||||||
|
<span style="color:blue">DEBUG=True<br>SECRET_KEY='mys3cr3tk3y'<br>DATABASE_URL='postgres://pgsqluser@postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" />:P@ssword1@postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" />.database.windows.net:5432/bootcamp'<br>ALLOWED_HOSTS = "*"
|
||||||
|
|
||||||
|
> **Note:** If you look at the credentials closely, you will notice we are using the new Azure Database for PostgreSQL service to connect to this app, and we have pre-populated the connection string.
|
||||||
|
|
||||||
|
1. On the Terminal, to come out of the editing mode-
|
||||||
|
|
||||||
|
> **A:** Press the **ESC** key
|
||||||
|
>
|
||||||
|
>
|
||||||
|
> **B:** Type <span style="color:blue">**:wr** </span>and hit **Enter**
|
||||||
|
>
|
||||||
|
>
|
||||||
|
> **C:** Type <span style="color:blue">**:q** </span>and hit **Enter**:
|
||||||
|
|
||||||
|
1. To run migration and serve the app, type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> python manage.py migrate
|
||||||
|
|
||||||
|
1. Let's bring the server up - Type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> python manage.py runserver 0.0.0.0:8000
|
||||||
|
|
||||||
|
1. Now, open a new web browser window, enter the following URL, and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> http://[publicIpAddress]:8000/
|
||||||
|
> **Note:** Use your Ubuntu server's external IP address from **Create an Azure VM running Ubuntu Server-Step 3**.
|
||||||
|
|
||||||
|
1. With the bootcamp app open in your browser, sign up for the bootcamp.
|
||||||
|
|
||||||
|
> **Note:** Enter a username; provide an email address; create a password; then create your account
|
||||||
|
|
||||||
|
> **Note:** The next screen you would see after successful creation of the account is this:
|
||||||
|
|
||||||
|
![Registered results](imgs/register.png)
|
||||||
|
|
||||||
|
1. Let's go to the database and query it directly. Return to **Azure Cloud Shell** where you have the SSH session and hit **Ctrl-C** to terminate the web server.
|
||||||
|
|
||||||
|
1. Next, let's terminate our SSH session to Ubuntu. Type the following command and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> exit
|
||||||
|
|
||||||
|
1. Now, let's connect to the **Azure PostgreSQL** database. Type the following and hit **Enter**:
|
||||||
|
|
||||||
|
<span style="color:blue"> psql --host=postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName"/>.postgres.database.azure.com --port=5432 --username=pgsqluser@postgresql<inject story-id="story://Content-Private/content/dfd/SP-OSS/postgresql/ossexperience1/story_a_postgresql" key="resourceGroupName" /> --dbname=bootcamp
|
||||||
|
|
||||||
|
1. If prompted for a password, type **P@ssword1** at the prompt and hit **Enter**.
|
||||||
|
|
||||||
|
> Note: the **psql shell** will not echo what you type.
|
||||||
|
|
||||||
|
1. Type in the following command and hit **Enter**
|
||||||
|
|
||||||
|
<span style="color:blue"> SELECT * FROM feeds_feed;
|
||||||
|
|
||||||
|
You should now see the username you registered in Bootcamp in this table.
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
### Conclusion
|
||||||
|
In a relatively short amount of time, we were able to git clone an app running on the Python Django framework, using a PostgreSQL database in the backend.
|
||||||
|
|
||||||
|
We changed the connection string to a provisioned **Azure Database for PostgreSQL**, and then connected the Bootcamp application in Azure.
|
||||||
|
|
||||||
|
When we ran the application and registered a username for ""Bootcamp, we were able to see that the username existed in our **Azure Database for PostgreSQL**.
|
||||||
|
|
||||||
|
With this simple installation, you can utilize many tools for PostgreSQL such as PostGIS, PGAdmin, pgScale, etc.
|
Двоичный файл не отображается.
Загрузка…
Ссылка в новой задаче