Kubernetes OpenShift for python - python

I am new to openshift, we are trying to deploy a python module in a pod which is accessed by other python code running in different pods. When i was deploying, the pod is running and immediately crash with status "Crash Loop Back Off".This python code is an independent module which does not have valid entrypoint. So how to deploy those type of python modules in openshift. Appreciate for any solutions

You don't. You deploy something that can run as a process, and as such, has a capability to contact with the external world in some way (ie. listen for requests, connect to message broker, send requests, read/write to db etc.) you do not package and deploy libraries that on their own are inoperable to the cluster.

Related

Is there any difference between the Kubernetes python client and regular Python?

I’m building an App that runs on K8s version 1.21 and the container already includes Python 3.92. Do I still need to install https://github.com/kubernetes-client/python if I want to interact with Kubernetes using Python or am I good?
Thanks,
Kubernetes Python client is the library that helps you interact with Kubernetes API.
So if you want to do anything with Kubernetes API from inside your Python program (e.g. query the currently running Pods), then you need to install Kubernetes python client.
However, if your application is just deployed in Kubernetes, but does not need interact with Kubernetes API, then you don't need it.
Would like more info on what are planning to do. If you going to just run Python programs then you don't need this library at all. This is for accessing Kubernetes (K8S) REST APIs.Also even for REST API you can do normal REST API calls and handle Requests/Responses or take help from this library for doing the heavy lifting. Whether python is running outside or inside a container or pod, you need the library for accessing REST APIs to do specific API functionalities not related to actual python to work.
Ref: https://kubernetes.io/docs/reference/using-api/client-libraries/

Parallelize a simple python script with kubernetes

I have a python script that looks like this:
def process_number(num):
# Do some processing using this number
print(num)
I want to spin up a kubernetes cluster and pass in a range of numbers, and have this script run in parallel across many machines. The number range can be hardcoded. I am unsure how to set up the dockerfile for this application, and how to deploy it to kubernetes, as most of the examples I can find are for web apps.
You need a base image that has python. You'll also need to define more of the script to ensure that this function runs over and over again, otherwise, the container will run and end very quickly which leads to crashLoopBackoff.
Before worrying about how to deploy this in kubernetes, you need to make sure you have a working script and that you can containerize it.
I recommend you containerize your application first and ensure it is running well before move to the kubernetes.
You can see some example of how containerize it in theses links:
Dockerize your Python Application
Containerize a Python App in 5 Minutes
After ensure your application in running as expect in a container, them you can move to he next step that is deploy it in kubernetes.
I strongly recommend you start with some kubernetes tutorials to understand how the components works and how you can deploy your application. You can use this links:
Local Development Environment for Kubernetes using Minikube
Getting Started With Kubernetes #1
Getting Started with Kubernetes #2
Kubernetes: An Introduction
kubernetes the hard way
k8s-intro-tutorials
If you don't have too much time, or you just need something that work for you case, take a look in Kubernetes Deployment, you can use your containerize application across multiples replicas.
To expose your service (internally and externally) you could use Kubernetes Service.
To test locally you can use the minikube installation on your machine
Hope that helps!

How can I call a python script from an asp.net Azure app service?

I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.
It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.
The two options I see are:
I might be able to install the right python version and libraries on the app service and call it from the .NET code. This seems like it might be deprecated: https://learn.microsoft.com/en-us/visualstudio/python/publishing-python-web-applications-to-azure-from-visual-studio?view=vs-2017
I can create a whole new and separate app service for just the python script and call it as a REST API on demand from the .NET app service. This seems like overkill, and introduces the problem of opening up a whole new service publicly, which I don't want to do. This also appears to have the limitation that Flask isn't mean for production, so hosting many calls at once is not really workable. https://learn.microsoft.com/en-us/visualstudio/python/publish-to-app-service-windows?view=vs-2017
What is the best way to call a python script on demand from .NET app service on Azure?
Per my experience, there are two ways to call a Python script in C# without IronPython.
Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.
The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.
Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.

Running One Instance of Google App Engine with frontend in nodejs and backend server in python

I'm getting my feet wet with GCP and GAE, also nodejs and python and networking (I know).
[+] What I have:
Basically I have some nodejs code that takes in some input and is supposed to then send that input to some python code that will do more stuff to it. My first idea was to deploy the nodejs code via GAE, then host the python code in a python server, then make post requests from the nodejs front-end to the python server backend.
[+] What I would like to be able to do:
just deploy both my nodejs code and my python code in the same project and instance of GAE so that the nodejs is the frontend that people see but so that the python server is also running in the same environment and can just communicate with the nodejs without sending anything online.
[+] What I have read
https://www.netguru.co/blog/use-node-js-backend
Google App Engine - Front and Backend Web Development
and countless other google searches for this type of setup but to no avail.
If anyone can point me in the right direction I would really appreciate it.
You can't have both python and nodejs running in the same instance, but they can run as separate services, each with their own instance(s) inside the same GAE app/project. See Service isolation and maybe Deploying different languages services to the same Application [Google App Engine]
Using post requests can work pretty well, but will likely take some effort to ensure no outside access.
Since you intend to use as frontend the nodejs service you're limited to using only the flexible environment for it, which limits the inter-service communication options - you can't use push queues (properly supported only in the standard environment) which IMHO would be a better/more secure solution than post requests.
Another secure communication option would be for the nodejs service to place the data into the datastore and have the python service pick it up from there - the datastore is shared by all instances/versions/services inside the same GAE app. Also more loosely coupled IMHO - each service can function (at least for a while) without the other being alive (not possible if using the post requests).
Maybe of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment
UPDATE:
Node.JS is currently available in the standard environment as well, so you can use those features, see:
Now, you can deploy your Node.js app to App Engine standard environment
Google App Engine Node.js Standard Environment Documentation

deploying python dynamic cgi application to bluemix?

I'm trying to deploy a python-based scientific application on IBM's bluemix platform. However, while I can launch the cgi services to host the web pages, the python application behind it doesn't currently run. The application currently runs on an apache server on rackspace, but I'm try trying launch a newer version for testing. I tried Heroku, with the same problem that the web pages will be served, but not the python application. But the heroku gist seems to be that Heroku can't serve cgi applications. And there was a suggestion that the Cloud Foundry platform would be able to do so. The application runs fine locally, so I'm trying to find the right tweak to deploy to Bluemix (or Heroku).
We have the requirements file, and the initial thought is that it's the procfile that needs tweaking. Currently it looks like:
web: python -m CGIHTTPServer $PORT
I've tried launching the application via the worker tag.
worker: python weblogo.py
worker: python setup.py
even trying to launch the internal files:
worker: python /weblogolib/_cgi.py
worker: python /weblogolib/__init__.py
Yet none of these methods worked on getting the application behind the web pages to work. Is there another method that we're unaware of?
The application is designed to be served locally with the command.
python ./weblogo --serve
Does this matter when deploying to a cloud platform?
Rewriting the application to Flask or Django isn't really an option now. Any guidance toward the getting the application launched would be much appreciated! Thank you in advance!

Categories

Resources