Skip to content

External Service

1. Register a service pointing to your local development environment

During testing stage, it is cumbersome to debug the codes using a deployment.

It's preferred everything is in your local environment, while the k8s sends the resource directly to your local web server.

YAMLs

apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
ports:
    - protocol: TCP
    port: 80
    targetPort: 8000
apiVersion: v1
kind: Endpoints
metadata:
name: external-service
subsets:
- addresses:
    - ip: 10.5.11.123
    ports:
    - port: 8000

Apply the yaml:

$ kubectl apply -f external-endpoint.yaml
endpoints/external-service created
# 🧑🏻‍🎄🧑🏻‍🎄$ kubectl apply -f external-service.yaml
service/external-service created

2. Write a testing web server

Here we use fastapi to write the server and start the server at the local workstation.

fastapi

from fastapi import FastAPI

app = FastAPI()

@app.get("/hello")
async def hello():
    return "Hello, world!"

Run the server:

$ uvicorn main:app --reload --host=0.0.0.0 --port=8000
INFO:     Will watch for changes in these directories: ['/home/jwang/Documents/Test/fastapi']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [4236] using watchgod
INFO:     Started server process [4238]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

3. Test the server by calling k8s service cluster IP

Get the cluster IP of the service by running the commands in the k8s node

$ kubectl -n kserve get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
external-service ClusterIP   10.43.122.110   <none>        80/TCP     9s

Test the server:

$ curl http://10.43.122.110/hello
"Hello, world!"