Skip to content

Commit c8e42b2

Browse files
authored
Create max-to-ibm-cloud-tutorial.md
1 parent c216027 commit c8e42b2

File tree

1 file changed

+360
-0
lines changed

1 file changed

+360
-0
lines changed

docs/max-to-ibm-cloud-tutorial.md

+360
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
By Simon Plovyt @splovyt
2+
Published May 22, 2019
3+
4+
------------------------------------------------------------------------
5+
6+
## Learning objective
7+
8+
This tutorial explains how to use a Kubernetes system to deploy a cloud
9+
instance for your Model Asset Exchange (MAX) model. The Kubernetes
10+
service lets you easily scale the containerized model from supporting a
11+
low rate of queries to production-level queries. The free [IBM
12+
Cloud Kubernetes
13+
Service](https://web.archive.org/web/20210419215534/https://www.ibm.com/cloud/container-service) is
14+
limited to 1 worker, but this works fine for this application.
15+
Let’s get started!
16+
17+
## Prerequisites
18+
19+
If you are not familiar with the Model Asset Exchange,
20+
this [introductory
21+
article](https://web.archive.org/web/20210419215534/https://developer.ibm.com/articles/introduction-to-the-model-asset-exchange-on-ibm-developer/) provides
22+
a good overview.
23+
24+
Additionally, you need the following:
25+
26+
- An [IBM Cloud
27+
account](https://web.archive.org/web/20210419215534/http://ibm.biz/max-contents).
28+
You can sign up for a free account.
29+
- An available Kubernetes cluster on your IBM Cloud account. The free
30+
cluster type will suffice. Create a Kubernetes cluster from
31+
the [IBM Cloud
32+
catalog](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/kubernetes/catalog/cluster?cm_sp=ibmdev-_-developer-tutorials-_-cloudreg).
33+
- The IBM Cloud command-line interface (CLI) installed. After creating a
34+
Cloud account and a Kubernetes cluster, you must communicate with the
35+
cluster using your local machine. [Installation
36+
instructions](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/docs/cli/reference/ibmcloud?topic=cloud-cli-ibmcloud-cli#ibmcloud-cli) are
37+
available, or you can perform a web search for ‘IBM Cloud
38+
CLI.’
39+
40+
## Estimated time
41+
42+
With the prerequisites available, it should take you approximately 20
43+
minutes to complete this tutorial.
44+
45+
## Steps
46+
47+
This tutorial consists of the following steps:
48+
49+
1. Accessing the Kubernetes cluster
50+
2. Deploying the MAX model
51+
52+
A troubleshooting section can be found at the end of this tutorial.
53+
54+
### Step 1. Accessing the Kubernetes cluster
55+
56+
With an IBM Cloud account available, a Kubernetes cluster created, and
57+
the Cloud CLI installed
58+
(see [Prerequisites](https://web.archive.org/web/20210419215534/https://developer.ibm.com/tutorials/deploy-max-models-to-ibm-cloud-with-kubernetes/#prerequisites)),
59+
the actual deployment step itself is the easiest part. Begin by
60+
establishing the connection between your local machine and the
61+
Kubernetes cluster.
62+
63+
Use the CLI to log in to IBM Cloud on your local machine and specify
64+
what cluster you are targeting in the cloud. All of these instructions
65+
are also available under the **Access tab** on the online page
66+
of your Kubernetes cluster. Go to this **Access tab** to
67+
ensure that you are working with the most up-to-date instructions.
68+
69+
![IBM cloud
70+
access](https://web.archive.org/web/20210419215534im_/https://developer.ibm.com/developer/default/tutorials/deploy-max-models-to-ibm-cloud-with-kubernetes/images/ibm-cloud-access.png)
71+
72+
Now, execute the instructions.
73+
74+
1. Log in to the cluster from your local machine.
75+
76+
```
77+
ibmcloud login -a https://cloud.ibm.com
78+
```
79+
80+
81+
82+
2. Set the IBM Cloud region to the Kubernetes Service region that you
83+
selected when creating your cluster. The example cluster is in US
84+
South, but you can find information on
85+
other [regions](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/docs/containers?topic=containers-regions-and-zones).
86+
87+
```
88+
ibmcloud ks region-set us-south
89+
```
90+
91+
92+
93+
3. Get the command to set the environment variable and download the
94+
Kubernetes configuration files to the cluster.
95+
96+
> Note: Replace the last argument (`max-deployment-cluster`) with
97+
> the name you gave your Kubernetes cluster.
98+
99+
```
100+
ibmcloud ks cluster-config max-deployment-cluster
101+
```
102+
103+
104+
105+
4. Copy the output from the previous command and paste it in your
106+
terminal. The command starts with `export KUBECONFIG=...`. This
107+
command must be run because it sets the
108+
required `KUBECONFIG` environment variable.
109+
110+
5. Verify that you can connect to your cluster by listing your worker
111+
nodes. The `kubectl` commands are part of the Kubernetes
112+
CLI, which was installed in the same process as the IBM Cloud CLI.
113+
If the `kubectl` command returns `command not found`,
114+
you must install the Kubernetes CLI plug-in separately. More
115+
information on installing the plug-in separately can be found in the
116+
Troubleshooting section at the end of this tutorial.
117+
118+
```
119+
kubectl get nodes
120+
```
121+
122+
123+
124+
You are now ready for the final step of the deployment.
125+
126+
### Step 2. Deploying the MAX model
127+
128+
The actual deployment process is short. Now that the connection with the
129+
remote Kubernetes cluster has been set up, you only have to apply the
130+
model’s configuration file to the Kubernetes cluster to get the
131+
API up and running.
132+
133+
Let’s download the model’s repository, which contains the
134+
YAML configuration file.
135+
136+
1. Clone the MAX-Model repository from GitHub. The following code shows
137+
how to do this for the MAX-Object-Detector, but you can
138+
replace `MAX-Object-Detector` with whichever MAX Model you
139+
want to use.
140+
141+
```
142+
git clone https://github.com/IBM/MAX-Object-Detector.git
143+
```
144+
145+
146+
147+
2. Find the .yaml file in the downloaded directory.
148+
149+
```
150+
cd MAX-Object-Detector
151+
152+
ls *.yaml
153+
```
154+
155+
156+
157+
3. Apply the configuration file to your Kubernetes cluster. Again,
158+
replace the `max-object-detector.yaml` with the .yaml file
159+
corresponding to your model. The following command deploys the
160+
model.
161+
162+
```
163+
kubectl apply -f ./max-object-detector.yaml
164+
```
165+
166+
167+
168+
4. After the model is deployed (give it some time), find the public IP
169+
address and port that the API is served on.
170+
171+
- The public IP
172+
173+
```
174+
ibmcloud cs workers mycluster
175+
```
176+
177+
178+
179+
- The port
180+
181+
> Note: Replace `max-object-detector` with the service
182+
> name corresponding to your model.
183+
184+
```
185+
kubectl describe service max-object-detector | grep NodePort
186+
```
187+
188+
189+
190+
5. Navigate to the&nbsp;`http://<PUBLIC_IP>:<PORT>`&nbsp;address in
191+
your browser. You should find the API front end as shown in the
192+
following image.
193+
194+
![MAX object detector
195+
api](https://web.archive.org/web/20210419215534im_/https://developer.ibm.com/developer/default/tutorials/deploy-max-models-to-ibm-cloud-with-kubernetes/images/object-detector-api.png)
196+
197+
This means that the MAX model has been successfully deployed.
198+
Congratulations!
199+
200+
##### Some useful commands
201+
202+
Kubernetes CLI:
203+
204+
```
205+
# Show the log of all events
206+
kubectl get events
207+
208+
# Show all pods
209+
kubectl get pods
210+
211+
# Show all services
212+
kubectl get services
213+
214+
# Show all deployments
215+
kubectl get deployments
216+
217+
# Show the details of a service
218+
kubectl describe service SERVICE_NAME
219+
220+
# Show the details of a node
221+
kubectl describe node NODE_NAME
222+
223+
# Delete a service
224+
kubectl delete services SERVICE_NAME
225+
226+
# Delete a deployment
227+
kubectl delete deployment DEPLOYMENT_NAME
228+
229+
# Debugging a pod
230+
kubectl logs POD_NAME
231+
```
232+
233+
IBM Cloud CLI:
234+
235+
```
236+
# Show all clusters
237+
ibmcloud cs clusters
238+
239+
# Show the workers of a specific cluster
240+
ibmcloud cs workers CLUSTER_NAME
241+
```
242+
243+
244+
##### Further scaling
245+
246+
Scaling a Kubernetes-powered application beyond the 2vCPU and 4 GB RAM
247+
specifications of the free cluster is as easy as clicking a button.
248+
Adding worker nodes (computation units) or specifying advanced cluster
249+
parameters (for example, more CPU or RAM) can be done easily from the
250+
Cloud Dashboard. However, you must&nbsp;[upgrade to a standard
251+
cluster](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/docs/containers?topic=containers-cs_ov#cluster_types)&nbsp;to
252+
access these.
253+
254+
Additionally, with more worker nodes, a single node&rsquo;s public IP
255+
address does not suffice. Therefore, services
256+
like&nbsp;[Ingress](https://web.archive.org/web/20210419215534/https://kubernetes.io/docs/concepts/services-networking/ingress/)&nbsp;and&nbsp;[Loadbalancer](https://web.archive.org/web/20210419215534/https://kubernetes.io/docs/concepts/services-networking/ingress/)&nbsp;must
257+
be added as well. Exposing a public IP address on the deployment level
258+
instead of on the node level with the Loadbalancer can be set up in a
259+
few easy steps as demonstrated below.
260+
261+
1. Show the existing deployments and note the deployment name.
262+
263+
```
264+
kubectl get deployments
265+
```
266+
267+
268+
269+
2. With the deployment name, you can expose this deployment to a public
270+
IP address and port by using the following command.
271+
Replace&nbsp;`<DEPLOYMENT_NAME>`&nbsp;with the name of the
272+
deployment, and replace&nbsp;`<SERVICE_NAME>`&nbsp;with a custom
273+
name for your microservice. The&nbsp;`target-port`&nbsp;flag points
274+
to the port of the running container, which is set to 5000 by
275+
default for MAX models. The port that you want to expose on the
276+
public IP address can be set with the&nbsp;`port`&nbsp;flag. Here,
277+
it&rsquo;s specified as&nbsp;`80`.
278+
279+
```
280+
kubectl expose deployment <DEPLOYMENT_NAME> --port=80 --target-port=5000 --name=<SERVICE_NAME> --type=LoadBalancer --load-balancer-ip=''
281+
```
282+
283+
284+
285+
3. The public IP and port are now being configured as part of the
286+
service. When done (it can be a couple of minutes), the external IP
287+
address shows up in the output of the following command.
288+
289+
```
290+
kubectl get service --watch
291+
```
292+
293+
294+
295+
4. You can verify that your service is up and running by typing the IP
296+
address and port in the browser like&nbsp;`http://<IP>:<PORT>`.
297+
298+
More information can be found in the&nbsp;[Deploy a MAX model-serving
299+
microservice on IBM Cloud Kubernetes
300+
Service](https://web.archive.org/web/20210419215534/https://github.com/CODAIT/MAX-cloud-deployment-cheatsheets/tree/master/ibm-cloud)&nbsp;GitHub
301+
repository.
302+
303+
## Troubleshooting
304+
305+
An easy way to detect general issues with your Kubernetes cluster is to
306+
visit the Kubernetes Dashboard on IBM Cloud. Any critical or pending
307+
worker nodes are highlighted. In addition, you can run the following
308+
commands in the terminal.
309+
310+
- Check Kubernetes events
311+
312+
```
313+
kubectl get events
314+
```
315+
316+
317+
318+
- Get the logs for a pod
319+
320+
```
321+
kubectl logs POD_NAME
322+
```
323+
324+
325+
326+
##### Memory issues and killed containers
327+
328+
When trying to deploy a model with above-average memory requirements
329+
(for example, the MAX-Image-Resolution-Enhancer), you will run into
330+
a&nbsp;`TypeError: Failed to fetch`&nbsp;error message. Although the
331+
front end is still responsive, this error often means that the container
332+
was killed and restarted due to exceeding the memory limit after posting
333+
an input image. In this case, you must upgrade to a standard Kubernetes
334+
cluster to choose the specifications of the worker nodes. Increasing the
335+
resources of the node solves this issue.
336+
337+
##### Can&rsquo;t find the kubectl command
338+
339+
Most likely, this means that you are working with an older version of
340+
the IBM Cloud CLI for which the Kubernetes CLI was not included in the
341+
package. Verify that
342+
the&nbsp;`container-service/kubernetes-service`&nbsp;plug-in is not
343+
present by running the&nbsp;`ibmcloud plugin list`&nbsp;command. Run
344+
the&nbsp;`ibmcloud plugin install container-service`&nbsp;command to
345+
install the plug-in,
346+
or&nbsp;[reinstall](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/docs/cli?topic=cloud-cli-uninstall-ibmcloud-cli)&nbsp;the
347+
IBM Cloud CLI. You can find more information on the&nbsp;[official
348+
Kubernetes
349+
page](https://web.archive.org/web/20210419215534/https://kubernetes.io/docs/tasks/tools/install-kubectl/)&nbsp;or
350+
on the&nbsp;[IBM Cloud CLI
351+
page](https://web.archive.org/web/20210419215534/https://cloud.ibm.com/docs/containers?topic=containers-cs_cli_install#cs_cli_install_steps).
352+
353+
## Summary
354+
355+
This tutorial illustrates how to deploy a model container from the Model
356+
Asset Exchange to IBM Cloud using Kubernetes by following two easy
357+
steps: accessing the Kubernetes cluster and deploying the MAX model.
358+
359+
Collections[\#Open Source Data & AI
360+
Technologies](https://web.archive.org/web/20210419215534/https://developer.ibm.com/collections/codait/)

0 commit comments

Comments
 (0)