XenonStack

A Stack Innovator

Post Top Ad

Thursday 7 September 2017

Deploying Python Application on Docker & Kubernetes


Overview


In this post, We’ll share the process how you can Develop and Deploy Python Application using Docker and Kubernetes and adopt DevOps in existing Python Applications.

Prerequisites are mentioned below


To follow this guide you need

Kubernetes is an open source platform that automates container operations, and Minikube is best for testing kubernetes in a local environment.

Kubectl is command line interface to manage kubernetes cluster either remotely or locally. To configure kubectl in your machine follow this link.

Shared Persistent Storage is permanent storage that we attach to the kubernetes container. We will be using cephfs as a persistent data store for kubernetes container applications.

Application Source Code is source code that we want to run inside a kubernetes container.

Dockerfile contains all the actions that are performed to build python application.

The Registry is an online image store for container images.

Below mentioned options are few most popular registries.

2. AWS ECR

Dockerfile


The Below mentioned code is sample docker file for Python applications. In which we are using python 2.7 development environment.


FROM python:2.7
MAINTAINER XenonStack

# Creating Application Source Code Directory
RUN mkdir -p /usr/src/app

# Setting Home Directory for containers
WORKDIR /usr/src/app

# Installing python dependencies
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt

# Copying src code to Container
COPY . /usr/src/app

# Application Environment variables
ENV APP_ENV development

# Exposing Ports
EXPOSE 5035

# Setting Persistent data
VOLUME ["/app-data"]

# Running Python Application
CMD ["python", "wsgi.py"]

Building Python Docker Image

The Below mentioned command will build your application container image.

$ docker build -t <name of your python application>:<version of application> .

Publishing Container Image


To publish Python container image, we can use different private/public cloud repository like Docker HubAWS ECRGoogle Container RegistryPrivate Docker Registry.

  • Adding Container Registry to Docker Daemon
If you are using docker registry other than docker hub to store images, then we need to add that container registry to our local docker daemon and kubernetes Docker daemons.

You must have following things to follow next steps.

$ docker version
Client:
 Version:   17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:     Mon Mar 27 17:14:09 2017
 OS/Arch:   linux/amd64 (Ubuntu 16.04)


Now we need to Create a “daemon.json” in below-mentioned location

$ sudo nano /etc/docker/daemon.json


And add the following content to it.

{
 "insecure-registries": ["<name of your private registry>"]
}


Now Run the following commands to reload systemctl and restart docker daemon.

$ sudo systemctl daemon-reload
$ sudo service docker restart


To verify that your container registry is added to local docker daemon, use the below-mentioned steps.

$ docker info


In output of above, you get your container registry like this

Insecure Registries:
 <your container registry name>
 127.0.0.0/8

  • Pushing container Images to Registry

I'm using AWS ECR for publishing container images.

You must have an AWS account with Amazon ECR permissions. Create AWS ECR repository using a below-mentioned link.

After creation, you will get registry URL, username, and password from own AWS cloud.

Here is a shell script that will add your AWS credentials for Amazon ECR in your local system so that you can push images to AWS ECR.

#!/bin/bash
pip install --upgrade --user awscli

mkdir -p ~/.aws && chmod 755 ~/.aws

cat << EOF > ~/.aws/credentials
[default]
aws_access_key_id = XXXXXX
aws_secret_access_key = XXXXXX
EOF

cat << EOF > ~/.aws/config
[default]
output = json
region = XXXXX
EOF

chmod 600 ~/.aws/credentials

ecr-login=$(aws ecr get-login --region XXXXX)
$ecr-login

Now we need to retag python application image and push them to docker hub container registry.

To Retag application container image

$ docker tag <name of your application>:<version of your application> <aws ecr repository link>/<name of your application >:<version of your application>

Continue Reading The Full Article At - XenonStack.com/Blog

No comments:

Post a Comment