The provided content outlines a comprehensive tutorial on building a health entity labeling service using Azure Kubernetes Service (AKS), Seldon Core, and Azure Cognitive Services, along with other technologies like cdktf, s2i, and Istio.
Abstract
The tutorial guides readers through deploying a health search entity inference service within the Azure cloud ecosystem. It details the use of cdktf for infrastructure as code in Python, the deployment of a machine learning model using Seldon Core, and the integration of Azure Cognitive Services for health data analysis. The guide also covers the installation of necessary components such as AKS, Istio, and Seldon Core within the Kubernetes environment, and the deployment of the Seldon health service using source-to-image (s2i) for containerization. The article emphasizes the scalability, security, and ease of configuration of the final ML service, which is capable of labeling medical terms and their relationships in text data.
Opinions
The author suggests that the combination of Azure services and open-source tools like Seldon Core provides a robust platform for deploying machine learning models.
The use of cdktf is presented as an efficient method for defining and deploying Azure cloud infrastructure using Python.
The integration of Azure Cognitive Services is highlighted as a key component for processing health data and extracting entities and relationships.
The article implies that Istio is a valuable service mesh for securing, connecting, and monitoring microservices in a Kubernetes environment.
The author expresses that the described approach to deploying an ML service is scalable, secure, and easily configurable, suggesting a favorable opinion towards the architecture's effectiveness.
The recommendation for ZAI.chat as a cost-effective alternative to ChatGPT Plus (GPT-4) indicates the author's endorsement of this AI service for similar performance and functions.
Building a Health Entity labelling service using Azure Kubernetes Service, Seldon Core and Azure Cognitive
In this tutorial we will build an inference service entirely in Kubernetes in the Azure ecosystem
I this article we will deploy a health search entity inference service in Azure. We will deploy infrastructure with cdktf and will write a seldon core model that will interact with Azure Cognitive Services APIs to analyse health data. We will also use helm to install seldon core in AKS and various other dependencies. We assume that you have already installed kubectl, helm and the azure CLI in your environment and you are able to run most of the command shown here in a linux based terminal.
Step 1: writing the azure infrastructure
To build and deploy the infrastructure we will need access to an azure account (free trial enrolment is available here) and cdktf installed in our environemnt. We can use brew for this purpose:
brew install cdktf
Ok, so now we can start coding. The first thing we need to do in cdktf is to create the main class that will hold all the azure components, we will call it MLAzureStack:
As we can see we need to inherit from TerraformStack and also to create an AzurermProvider that will help with communicating with the azure cloud APIs in order to build the desired infrastructure.
Next we will create a resource group. This is a logic container that will hold all the infrastructure we deploy. It’s a nice way to group all your resources together.
We use a vars instance of a class StackVariables that will hold all the custom values for our infrastructure. Please visit the github repo for full details about this.
In the next part we create the Kubernetes cluster. We will create a basic cluster with 2 nodes and D2_V4 VMs that will be enough to host the Seldon Core and Istio installations.
We also need to create the Cognitive Account responsible of the Text Analytics API that will run the Health Entity Service that will be used to label new data.
We will have to create a docker container with our code and this container will have to be hosted in Azure. For this purpose we will create a container registry.
And the final piece of infra we need is creating a Key vault. Basically we have sensitive information that we want to store securely. For this purpose we create a KeyVault.
In the KeyVault we can define the permission we want to give for new secrets and keys. We also create 2 Vault Secrets that will hold the Cognitive access key and endpoint. These values are needed to access the Health Entity Service API.
We are now ready to deploy the Azure stack. We need to run the cdktf deploy command from the terminal:
cdktf deploy
This will initialise the terraform provider and deploy the declared resources:
Image by Author
If we navigate to the azure portal we should see the all the resources created in the resource group:
Image by Author
Step 2: writing the Seldon core model
In this section we will write the Seldon model that will receive new requests and will send back labeled health data if it encounters that.
We define a health_entity class that will have an __init__ method and the predict method that will be basically responsible to handle requests.
In the constructor we connect to the KeyVault and we get our secrets that we defined previusly. We use these parameters to create a TextAnalyticsClient which will be responsible with handling the ML requests.
In the predict method we pass our data request to the healthcare azure client. We get back from the API some entities and some defined relationships between them. We create a JSON response will all the information and we return it to the user.
In the folder that we define the health_entity.py we also have to create another file .s2i/environment that will contain all the environment variables used by Seldon in the docker image.
We can see we will create a standard model and we will serve REST requests. GRPC is also supported.
Step 3: installing Seldon core in AKS
To be able to deploy the Seldon model in AKS we need to install a couple of things in the cluster. We will need the azure cli, helm and kubectl to run some installation scripts.
To connect kubectl to the Azure cluster we need to add the needed credentials in the kube config file:
We can test if this works, checking the namespaces that are available in the cluster using kubectl get ns :
Image by Author
The next component that seldon core needs is istio. First we need to download it using:
curl -L https://istio.io/downloadIstio | sh -
Move into the istio package directory and add it to your path, in order to be able to use the istio comand line utility istioctl:
cd istio-1.11.4
export PATH=$PWD/bin:$PATH
Now we can install istio on the cluster:
istioctl install --setprofile=demo -y
The namespace label istio-injection=enabled instructs Istio to automatically inject proxies alongside anything we deploy in that namespace. We’ll set it up for our seldon namespace that will contain our inference model:
In order to let Seldon Core use the Istio mesh we need to create an Istio Gateway that will help to root traffic into the mesh:
kubectl apply -f - << END
apiVersion:networking.istio.io/v1alpha3kind:Gatewaymetadata:name:seldon-gatewaynamespace:istio-systemspec:selector:istio:ingressgatewayservers:-port:number:80name:httpprotocol:HTTPhosts:-"*"
END
If we want to check if all the Istio components are installed correctly we can run kubectl -n istio-system get all and we should have something simmilar:
Image by Author
Before we install Seldon we need to create a namespace to host it:
Once the image is in the azure container we can deploy the seldon model using kubectl. The kubernetes manifest contains the image that we just deployed:
kubectl apply -f - << END
apiVersion:machinelearning.seldon.io/v1kind:SeldonDeploymentmetadata:name:seldon-healthnamespace:seldonspec:name:seldon-healthpredictors:-componentSpecs:-spec:containers:-name: classifierimage:seldoncontainerregistry.azurecr.io/seldon-health:0.1graph:name:classifiername:defaultreplicas:1
END
And we can verify id the deployment is successful using kubectl -n seldon get pods:
Image by Author
Step 5: testing the health service
We can test the health service in two ways. First we will test the code locally using the seldon-core-microservice CLI and we will also test the deployment on AKS.
We need to install the seldon core CLI with pip:
pip install seldon-core
Then in a separate terminal we can run a local seldon core micro service server:
cd model/ && seldon-core-microservice --service-type MODEL health_entity
And you should be able to see a gunicorn server started listening on port 9000:
Image by Author
On a separate terminal we can run a curl command to check our service:
curl -X POST http://localhost:9000/api/v1.0/predictions -H 'Content-Type: application/json' -d '{ "data": { "ndarray": ["Patient needs to take 50 mg of ibuprofen every day."] } }'
As we can see we’ve sent some medical info in the curl request. We should get back a JSON with labeled information:
As we can see we get back the medical terms labeled for example ibuprofen is recognised as an entity of type MedicationName and we can also see relations between different terms such as 50 mg (Dosage) and ibuprofen (Medication) and the relationship type is DosageOfMedication.
We can also sun a similar curl command in AKS. We need to get the cluster ip and port for our deployed seldon health service. For this we need to run a couple of kubectl commands:
exportINGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
exportINGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
exportINGRESS_URL=$INGRESS_HOST:$INGRESS_PORT
echo $INGRESS_URL
Once we have the INGRESS_URL we can replace it in our curl command:
curl -X POST http://$INGRESS_URL/api/v1.0/predictions -H 'Content-Type: application/json' -d '{ "data": { "ndarray": ["Patient needs to take 50 mg of ibuprofen every day."] } }'
and we should have a similar response as the local request.
Well we finally reached the end of the tutorial. It was a complex one, but we’ve managed to create a Kubernetes ML solution end to end in Azure. We’ve seen how we can create complex infrastructure in python using CDKTF, how we can install various frameworks in Kubernetes using helm and kubectl and also how we can create and deploy an ML RESTful service using Seldon Core, that is very scalable, secure and easily configurable.