# Deploy models on AWS EKS
URL: https://docs.datarobot.com/en/docs/classic-ui/integrations/aws/deploy-dr-models-on-aws.html

> Deploy and monitor DataRobot models on AWS Elastic Kubernetes Service (EKS).

# Deploy models on AWS EKS

With DataRobot MLOps, you can deploy DataRobot models into your own AWS Elastic Kubernetes Service (EKS) clusters and still have the advantages of using the [monitoring](https://docs.datarobot.com/en/docs/classic-ui/mlops/monitor/index.html) and [governance](https://docs.datarobot.com/en/docs/classic-ui/mlops/governance/index.html) capabilities of MLOps.

These exportable DataRobot models are known as [Portable Prediction Servers (PPSs)](https://docs.datarobot.com/en/docs/reference/glossary/index.html#portable-prediction-server-pps). The models are embedded into Docker containers, providing flexibility and portability, and making them suitable for container orchestration tools such as Kubernetes.

The following steps show how to deploy a DataRobot model on EKS.

## Before you start

> [!NOTE] Availability information
> To deploy a DataRobot model on EKS, you need to export a model package which requires the "Enable MMM model package export" flag. Contact your DataRobot representative for more information about enabling this feature.

Before deploying to Amazon EKS, you need to create an EKS cluster.  There are two approaches to spin up the cluster:

- Using theeksctltool (CLI for Amazon EKS). This is the simplest and fastest way to create an EKS cluster.
- Using AWS Management Console. This method provides more fine-grained tuning (for example, IAM role and VPC creation).

This topic shows how to install using the eksctl tool. See [Getting started with Amazon EKS – eksctl](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html) for detailed instructions.

If any of the tools are already installed and configured, you can skip the corresponding steps.

### Install and configure AWS and EKS

Follow the steps described below to install and configure Amazon Web Services CLI, EKS, and Kubernetes CLI.

1. Install the AWS CLI, version 2. aws --version
2. Configure your AWS CLI credentials.
3. Installeksctl. eksctl version
4. Install and configurekubectl(CLI for Kubernetes clusters). kubectl version --short --client

## Deploy models to EKS

Deploying DataRobot models on a Kubernetes infrastructure consists of three main activities:

- Preparing and pushing the Docker container with the MLOps package to the container registry
- Creating the external deployment in DataRobot
- Creating the Kubernetes cluster

### Configure and run the PPS Docker image

To complete the following steps, you need to first generate your model and [create an MLOps model package](https://docs.datarobot.com/en/docs/classic-ui/mlops/deployment/registry/reg-create.html).

DataRobot provides a UI to help you configure the Portable Prediction Server and create the Docker image. Follow these steps:

1. Configure the Portable Prediction Server (PPS) .
2. Obtain the PPS Docker image .
3. Load the image to Docker .
4. Download the model package .
5. Run your Docker image .
6. Monitor your model .
7. Create an external deployment . When you create the deployment, make note of the MLOps model ID and the MLOps deployment ID. You’re going to need these IDs when you deploy the MLOps package to Kubernetes.

### Push the Docker image to Amazon ECR

You need to upload the container image to Amazon Elastic Container Registry (ECR) so that your Amazon EKS cluster can download and run it.

1. Configure the Docker CLI tool to authenticate to Elastic Container Registry: awsecrget-login-password--regionus-east-1|dockerlogin--usernameXXX--password-stdin00000000000.xxx.ecr.us-east-1.amazonaws.com
2. Push the Docker image you just built to ECR: dockerpush00000000000.xxx.ecr.us-east-1.amazonaws.com/house-regression-model:latest

### Create an Amazon EKS cluster

Now that the Docker image is stored in ECR and the external deployment is created, you can spin up an Amazon EKS cluster. The EKS cluster needs VPC with either of the following:

- Two public subnets and two private subnets
- A VPC with three public subnets

The Amazon EKS requires subnets in at least two Availability Zones. A VPC with public and private subnets is recommended so that Kubernetes can create public load balancers in the public subnets to control traffic to the pods that run on nodes in private subnets.

To create the Amazon EKS cluster:

1. (Optional) Create or choose two public and two private subnets in your VPC. Make sure that “Auto-assign public IPv4 address” is enabled for the public subnets. NoteTheeksctltool creates all necessary subnets behind the scenes if you don’t provide the corresponding--vpc-private-subnetsand--vpc-public-subnetsparameters.
2. Create the cluster: eksctlcreatecluster\--namehouse-regression\--ssh-access\--ssh-public-keymy-public-key.pub\--managed NotesUsage of the--managedparameter enablesAmazon EKS-managed nodegroups. This feature automates the provisioning and lifecycle management of nodes (EC2 instances) for Amazon EKS Kubernetes clusters. You can provision optimized groups of nodes for their clusters. EKS will keep their nodes up-to-date with the latest Kubernetes and host OS versions. Theeksctltool makes it possible to choose the specific size and instance type family via command line flags or config files.Although--ssh-public-keyis optional, it is highly recommended that you specify it when you create your node group with a cluster. This option enables SSH access to the nodes in your managed node group. Enabling SSH access allows you to connect to your instances and gather diagnostic information if there are issues. You cannot enable remote access after the node group is created.Cluster provisioning usually takes between 10 and 15 minutes and results in the following:
3. When your cluster is ready, test that your kubectl configuration is correct: kubectlgetsvc

### Deploy the MLOps package to Kubernetes

To deploy the MLOps package to Kubernetes:

1. Create a Kubernetes namespace, for example: kubectlcreatenamespacehouse-regression-namespace
2. Save the following contents to ayamlfile on your local machine (in this case,house-regression-service.yaml), replacing the values for your project. Provide the values ofimage,DataRobot API token,model ID, anddeployment ID. (You should have saved the IDs when youcreated the external deployment in MLOps.) apiVersion:v1kind:Servicemetadata:name:house-regression-servicenamespace:house-regression-namespacelabels:app:house-regression-appspec:selector:app:house-regression-appports:-protocol:TCPport:80targetPort:8080---apiVersion:apps/v1kind:Deploymentmetadata:name:house-regression-deploymentnamespace:house-regression-namespacelabels:app:house-regression-appspec:replicas:3selector:matchLabels:app:house-regression-apptemplate:metadata:labels:app:house-regression-appspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:-matchExpressions:-key:beta.kubernetes.io/archoperator:Invalues:-amd64containers:-name:house-regression-modelimage:<your_aws_account_endpoint>.ecr.us-east-1.amazonaws.com/house-regression-model:latestenv:-name:PORTABLE_PREDICTION_API_WORKERS_NUMBERvalue:"2"-name:PORTABLE_PREDICTION_API_MONITORING_ACTIVEvalue:"True"-name:PORTABLE_PREDICTION_API_MONITORING_SETTINGSvalue:spooler_type=filesystem;directory=/tmp;max_files=50;file_max_size=10240000;model_id=<your mlops_model_id_obtained_at_step_5>;deployment_id=<your mlops_deployment_id_obtained_at_step_5>-name:MONITORING_AGENTvalue:"True"-name:MONITORING_AGENT_DATAROBOT_APP_URLvalue:https://app.datarobot.com/-name:MONITORING_AGENT_DATAROBOT_APP_TOKENvalue:<your_datarobot_api_token>ports:-containerPort:80
3. Create a Kubernetes service and deployment: kubectlapply-fhouse-regression-service.yaml
4. View all resources that exist in the namespace: kubectlgetall-nhouse-regression-namespace

### Set up horizontal pod autoscaling

The Kubernetes Horizontal Pod Autoscaler automatically scales the number of pods in a deployment, replication controller, or replica set based on that resource's CPU utilization. This can help your applications scale up to meet increased demand or scale back when resources are not needed, thus freeing up your nodes for other applications. When you set a target CPU utilization percentage, the Horizontal Pod Autoscaler scales your application up or back to try to meet that target.

1. Create a Horizontal Pod Autoscaler resource for the php-apache deployment. kubectlautoscaledeploymenthouse-regression-deployment-nhouse-regression-namespace--cpu-percent=80--min=1--max=5
2. View all resources that exist in the namespace. kubectlgetall-nhouse-regression-namespace Horizontal Pod Autoscaler appears in the resources list.

### Expose your model to the world (load balancing)

Amazon EKS supports the Network Load Balancer and the Classic Load Balancer for pods running on Amazon EC2 instance nodes through the Kubernetes service of type LoadBalancer.

1. Tag the public subnets in your VPC so that Kubernetes knows to use only those subnets for external load balancers instead of choosing a public subnet in each Availability Zone (in lexicographical order by subnet ID): kubernetes.io/role/elb = 1
2. Tag the private subnets in the following way so that Kubernetes knows it can use the subnets for internal load balancers. kubernetes.io/role/internal-elb = 1 ImportantIf you use an Amazon EKS AWS CloudFormation template to create your VPC after March 26, 2020, then the subnets created by the template are tagged when they're created as explainedhere.
3. Use the kubectlexposecommand to generate a Kubernetes service for the deployment. kubectlexposedeploymenthouse-regression-deployment-nhouse-regression-namespace--name=house-regression-external--type=LoadBalancer--port80--target-port8080
4. Run the following command to get the service details: kubectlgetservice-nhouse-regression-namespace
5. Copy theEXTERNAL_IPaddress.
6. Score your model using theEXTERNAL_IPaddress: curl-XPOSThttp://<EXTERNAL_IP>/predictions-H"Content-Type: text/csv"--data-binary@kaggle_house_test_dataset.csv
7. Check the service health ofthe external deployment you created. The scoring request is now included in the statistics.

### Clean up

1. Remove the sample service, deployment, pods, and namespace: kubectldeletenamespacehouse-regression-namespace
2. Delete the cluster: eksctldeletecluster--namehouse-regression

## Wrap-up

This walkthrough described how to deploy and monitor DataRobot models on the Amazon EKS platform via a Portable Prediction Server (PPS). A PPS is embedded into Docker containers alongside the MLOps agents, making it possible to acquire the principal IT (service health, number of requests, etc.) and ML (accuracy, data drift etc.) metrics in the cloud and monitor them on the centralized DataRobot MLOps dashboard.

Using DataRobot PPSs allows you to avoid vendor lock-in and easily migrate between cloud environments or deploy models across them simultaneously.

---
