“App Dev: Deploying the Application into Kubernetes Engine – Python”
Daftar Isi
Pengantar
Google Kubernetes Engine menyediakan lingkungan terkelola untuk men-deploy, mengelola, dan menskalakan aplikasi dalam container Anda menggunakan infrastruktur Google. Lingkungan yang disediakan Kubernetes Engine terdiri dari beberapa mesin (khususnya, instance Compute Engine) yang dikelompokkan bersama untuk membentuk sebuah cluster.
Kubernetes menyediakan mekanisme untuk berinteraksi dengan klaster Anda. Anda menggunakan perintah dan sumber daya Kubernetes untuk menerapkan dan mengelola aplikasi Anda, melakukan tugas administrasi dan menetapkan kebijakan, serta memantau kesehatan beban kerja yang diterapkan.
Praktikum
Prepare the Quiz application
Clone source code in Cloud Shell
git clone https://github.com/GoogleCloudPlatform/training-data-analyst
ln -s ~/training-data-analyst/courses/developingapps/v1.2/python/kubernetesengine ~/kubernetesengine
Configure the Quiz application
cd ~/kubernetesengine/start
. prepare_environment.sh
Review the code
Navigate to training-data-analyst/courses/developingapps/v1.2/python/kubernetesengine/start
.
The folder structure for the Quiz application reflects how it will be deployed in Kubernetes Engine.
The web application is in a folder called frontend
.
The worker application code that subscribes to Cloud Pub/Sub and processes messages is in a folder called backend
.
There are configuration files for Docker (a Dockerfile
in the frontend
and backend
folder) and backend-deployment
and frontend-deployment
Kubernetes Engine .yaml
files.
Create and connect to a Kubernetes Engine Cluster
Create a Kubernetes Engine cluster
- In the Cloud Platform Console, click Navigation menu ()> Kubernetes Engine > Clusters.
- Click Create.
- Configure the
Standard
cluster. Set the following fields to the provided values, leave all others at the default value:
- Click Create. The cluster takes a few minutes to provision.
Connect to the cluster
- When the cluster is ready, click on the Actions icon and select Connect.
- In Connect to the cluster, click Run in Cloud Shell to populated Cloud shell with the command that resembles
gcloud container clusters get-credentials quiz-cluster --zone us-central1-b --project [Project-ID]
. Press ENTER to run the command in Cloud Shell. - Run the following command to list the pods in the cluster:
kubectl get pods
Build Docker images using Container Builder
Create the Dockerfile for the frontend and backend
- In the Cloud Shell code editor, open
frontend/Dockerfile
. - Now add a block of code that does the following:
- Enter the Dockerfile command to initialize the creation of a custom Docker image using Google’s Python App Engine image as the starting point.
- Writes the Dockerfile commands to activate a virtual environment.
- Writes the Dockerfile command to execute
pip install
as part of the build process. - Writes the Dockerfile command to add the contents of the current folder to the
/app
path in the container. - Completes the
Dockerfile
by entering the statement,gunicorn ...
, that executes when the container runs. Gunicorn (Green Unicorn) is an HTTP server that supports the Python Web Server Gateway Interface (WSGI) specification.
FROM gcr.io/google_appengine/python
RUN virtualenv -p python3.7 /env
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ADD . /app
CMD gunicorn -b 0.0.0.0:$PORT quiz:app
- Open the
backend/Dockerfile
file and copy and paste the following code:
FROM gcr.io/google_appengine/python
RUN virtualenv -p python3.7 /env
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ADD . /app
CMD python -m quiz.console.worker
Build Docker images with Container Builder
- In Cloud Shell, make sure you are in the
start
folder:
cd ~/kubernetesengine/start
- Run the following command to build the frontend Docker image:
gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-frontend ./frontend/
- Now run the following command to build the backend Docker image:
gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-backend ./backend/
- In the Cloud Platform console, from the Navigation menu menu, click Container Registry. You should see two pods:
quiz-frontend
andquiz-backend
. - Click quiz-frontend.
Create Kubernetes deployment and service resources
Create a Kubernetes deployment file
- In the Cloud Shell Code Editor, open the
frontend-deployment.yaml
file. - Replace the placeholders in the
frontend-deployment.yaml
file using the following values:
- Save the file.
- Replace the placeholders in the
backend-deployment.yaml
file using the following values:
- Save the file.
- Review the contents of the
frontend-service.yaml
file.
Execute the deployment and service Files
- In Cloud Shell, provision the quiz frontend deployment.
kubectl create -f ./frontend-deployment.yaml
- Provision the quiz backend Deployment.
kubectl create -f ./backend-deployment.yaml
- Provision the quiz frontend Service
kubectl create -f ./frontend-service.yaml
Penutup
Sahabat Blog Learning & Doing demikianlah penjelasan mengenai App Dev: Deploying the Application into Kubernetes Engine – Python. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.