Monitoring Python applications in Kubernetes environment
Before you monitor Python applications in Kubernetes, you must install the Unified Agent. Then, you update your application deployment to monitor the Python
applications.
Before you begin
- Check whether your service account has access to Kubernetes resources. For more information, see Authorizing the data collector to access Kubernetes resources.
- Ensure that Unified Agent is installed. For more information, see Installing and configuring the Unified Agent.
- Check whether you downloaded the Python data collector package from Passport Advantage and placed it with Python application Dockerfile in the same directory. For more information, see Downloading the Python data collector.
Procedure
-
Update the Dockerfile to add the following lines to install Python data collector for your Python application, and get write access to the root directory.
ADD ibm_python_datacollector.tgz root_of_application RUN chmod 777 root_of_application RUN pip install --no-index --find-links=root_of_application/python_dc ibm_python_dc
Where root_of_application is the Python application root directory of the context of the build (the Dockerfile).
-
You can use the Python data collector to monitor Python applications running on the web server uWSGI V1.9.0 or later versions. But some configurations are needed to make the Python data collector work well.
- If you start uWSGI without threads, the threads that are generated by your application and the Python data collector will never run and thus the Python data collector cannot work normally. You must enable uWSGI threads by adding the
--enable-threads
option in the uwsgi command. This option is applied automatically when you specify the --threads option to configure the number of threads. - If you run the uwsgi command with the --master option, Python scripts and modules are preloaded in the parent master process, and worker processes are forked from the parent master process. In addition, background threads that are created in the master process are killed in worker processes. To make the Python data collector work normally, you must add the --lazy-apps option in the uwsgi command to use the lazy loading mode.
Command example:
uwsgi --enable-threads --master --lazy-apps --processes 4 --http :8002 --wsgi-file=flask_hello.py --callable app
Note: You can also add the options in a
.ini
configuration file and run the uwsgi command with the.ini
file. For more information, see uWSGI documents. - If you start uWSGI without threads, the threads that are generated by your application and the Python data collector will never run and thus the Python data collector cannot work normally. You must enable uWSGI threads by adding the
-
Integrate the installed data collector in your Python application:
-
If your application is based on Django V1.10 or later versions, open
settings.py
of your Django application, and add the following content into first line of the MIDDLEWARE section in that file:'ibm_python_dc.kpg_dc_django.ResourceMiddleware',
-
If your application is based on Django V1.9 or older versions, add the following content into the first line of the MIDDLEWARE_CLASS section in that file:
'ibm_python_dc.kpg_dc_django.ResourceMiddleware',
-
If your application is based on Flask, add the Python data collector wsgi middleware in your Python application file, for example, if you run export FLASK_APP=run.py, then edit the
run.py
file and ensure that the Python data collector wsgi middleware are added in front of other middlewares. Example:from flask import Flask from flask_restful import Api from api.board import Article from api.auth import Login, Register, RefreshToken from middleware import Test from werkzeug.middleware.dispatcher import DispatcherMiddleware api.add_resource(Login, '/login') api.add_resource(Register, '/register') from ibm_python_dc.kpg_dc_wsgi import ResourceMiddleware app.wsgi_app = ResourceMiddleware(app.wsgi_app) app.wsgi_app = DispatcherMiddleware(serve_frontend, { '/test': test, '/admin': admin, }) ...... ......
-
- Build the new Docker image.
- Update the application yaml file to use the new Docker image.
- If your Unified Agent is not installed in the default namespace of
cp4mcm-cloud-native-monitoring
, you need to add the following lines to the yaml file:
Replace <NAMESPACE> with the namespace where you install Unified Agent.UA_LWDC_LISTENER_URL=http://lwdc.<NAMESPACE>:8848 UA_JAEGER_ENDPOINT=http://jaeger.<NAMESPACE>:14268/api/traces