Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines using YAML files, making it easy to version control, review and automate your pipelines. In this article, we will use Tekton pipelines to deploy Python FastAPI microservice as a KNative service.
Refer to this article Going Serverless with KNative for an introduction to KNative.
Tekton Pipeline Components
- Pipeline: A pipeline consists of a series of tasks that are executed in order. Each task can have its own inputs and outputs.
- Task: A task is a single unit of work within a pipeline. A task can be a script, a container image, or a Kubernetes resource.
- TaskRun: A TaskRun is an instance of a task that has been executed within a pipeline.
- Resource: A resource is an input or output of a task in a pipeline. It can be a git repository, an artifact, or anything that is needed to complete a task.
- PipelineRun: A PipelineRun is an instance of a pipeline that has been executed.
How to install Tekton on Docker desktop
Installing Tekton on Docker Desktop involves a few steps. Tekton is an open-source framework that provides a declarative way to define pipelines for building, testing, and deploying software. Docker Desktop is a desktop application that provides a streamlined way to run Docker on your computer.
Here are the steps to install Tekton on Docker Desktop:-
Step 1: Install Docker Desktop
First, you need to install Docker Desktop on your computer. You can download Docker Desktop from the Docker website and follow the installation instructions for your operating system. -
Step 2: Enable Kubernetes in Docker Desktop
Next, you need to enable Kubernetes in Docker Desktop. You can do this by opening the Docker Desktop settings and navigating to the Kubernetes tab. Then, click the "Enable Kubernetes" checkbox and wait for Kubernetes to be installed. -
Step 3: Install Tekton on Kubernetes
Once Kubernetes is enabled, you can install Tekton on Kubernetes using the following command:kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml
This command will download the latest Tekton release YAML file and apply it to your Kubernetes cluster. -
Step 4: Verify the installation
Finally, you can verify that Tekton is installed correctly by running the following command:kubectl get pods --namespace tekton-pipelines
This command will show you a list of the Tekton pods running in the tekton-pipelines namespace. If you see the tekton-pipelines-controller pod and the tekton-pipelines-webhook pod with a status of "Running", then Tekton is installed and running correctly.
Write a simple FastAPI microservice and dockerize it
Here's a simple FastAPI microservice and the steps to dockerize it:-
Step 1: Install FastAPI
First, you need to install FastAPI using pip:pip install fastapi uvicorn
-
Step 2: Create the microservice
Next, you can create a simple FastAPI microservice. Create a new file called main.py and add the following code:from fastapi import FastAPI import uvicorn app = FastAPI() @app.get("/hello") async def root(): return {"message": "Hello, World!"} @app.get("/health") async def root(): return "ok" if __name__ == '__main__': uvicorn.run('main:app', host='0.0.0.0', port=9089, reload=True)
This code defines a FastAPI application with endpoints that returns a simple message. -
Step 3: Run the microservice
You can now run the microservice using the following command:uvicorn main:app --reload
This command starts a development server that listens on http://localhost:9089 and reloads the server automatically when you make changes to the code. -
Step 4: Create a Dockerfile
Now you can create a Dockerfile to containerize your microservice. Create a new file called Dockerfile and add the following code:FROM python:3.9-slim COPY ./app /app RUN pip3 install -r /app/requirements.txt WORKDIR /app EXPOSE 9089 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9089"]
This code defines a Docker image and copies the contents of the app directory to the /app directory in the image. -
Step 5: Build the Docker image
You can now build the Docker image using the following command:docker build -t myapp:latest -f Dockerfile .
This command builds the Docker image and tags it with the name mymicroservice. -
Step 6: Run the Docker container
Finally, you can run the Docker container using the following command:docker run -p 9089:9089 myapp:latest
This command runs the Docker container and maps port 9089 in the container to port 9089 on your local machine. You can now access your microservice by opening a web browser and navigating to http://localhost:9089/hello. -
Step 1: Expose Tekton Dashboard Service
Use the following command to expose the Tekton Dashboard service:kubectl port-forward -n tekton-pipelines service/tekton-dashboard 9097:9097
This command will forward the Tekton Dashboard service to your localhost at port 9097. -
Step 2: Access the Tekton Dashboard
Now you can access the Tekton Dashboard by opening a web browser and navigating to http://localhost:9097. This should display the Tekton Dashboard login page. -
Step 3: Log in to the Tekton Dashboard
To log in to the Tekton Dashboard, use the following credentials:
Username: admin
Password: The password is generated during the Tekton installation process.
You can retrieve the password by running the following command:kubectl get secret tekton-dashboard-auth -n tekton-pipelines -o jsonpath='{.data.TEKTON_DASHBOARD_BASIC_AUTH_PASSWORD}' | base64 --decode
Once you have logged in, you should see the Tekton Dashboard interface, where you can manage and monitor your pipelines and pipeline runs. -
Step 1: Define the Tekton pipeline that will build and deploy your FastAPI microservice. You can use the following YAML file as a starting point. This pipeline pulls the source code from GIT repo, build/ push the docker image and also deploys the service on KNative.
apiVersion: tekton.dev/v1beta1 kind: Pipeline metadata: name: fastapi-pipeline spec: workspaces: - name: my-source tasks: - name: clone-repo taskRef: kind: Task name: git-cli params: - name: GIT_SCRIPT value: | rm -rf $(workspaces.source.path)/python-samples git init git remote remove origin git remote add origin https://github.com/stackstalk/python-samples.git git pull origin main workspaces: - name: source workspace: my-source - name: build taskRef: kind: Task name: buildah runAfter: - clone-repo params: - name: DOCKERFILE value: ./Dockerfile - name: CONTEXT value: $(workspaces.source.path)/fastapi-hello - name: IMAGE value: registry.docker.com/userid/myapp:latest workspaces: - name: source workspace: my-source - name: deploy taskRef: kind: Task name: kn runAfter: - build params: - name: ARGS value: - "service" - "create" - "hello" - "--force" - "--image=registry.docker.com/userid/myapp:latest" - "--probe-liveness=http:localhost:9089:health" - "--probe-readiness=http:localhost:9089:health"
In this pipeline, we leverage git-cli, buildah and kn (KNative CLI) tasks which are reusable artifacts from Tekton Hub. Refer to the following links on how to install.- https://hub.tekton.dev/tekton/task/git-cli
- https://hub.tekton.dev/tekton/task/buildah
- https://hub.tekton.dev/tekton/task/kn
Now you need to apply the Tekton pipeline to your Kubernetes cluster using the following command. Make sure to replace mypipeline.yaml with the name of the YAML file that contains your Tekton pipeline definition. Also replace userid with the correct docker registry user id.
kubectl apply -f mypipeline.yaml
Now the pipeline is deployed.
-
Step 2: Create the PV, PVC, KNative deployer service account and docker registry credentials. You can leverage the following samples.
- To create PV and PVC refer my-source.yaml
- To create KNative deploer service account refer kn-deployer-account.yaml
- To create build bot account refer build-bot.yaml
-
To create the docker registry credentials use the following command.
kubectl create secret docker-registry --dry-run=client docker-credentials \ --docker-server=registry.docker.com \ --docker-username=username \ --docker-password=password \ --docker-email=email -o yaml > docker-credentials.yaml
-
Step 3: Next create a pipeline run which invokes the defined pipeline. You can use the following YAML as starting point.
apiVersion: tekton.dev/v1beta1 kind: PipelineRun metadata: name: fastapi-pipeline-run spec: taskRunSpecs: - pipelineTaskName: build taskServiceAccountName: build-bot - pipelineTaskName: deploy taskServiceAccountName: kn-deployer-account pipelineRef: name: fastapi-pipeline workspaces: - name: my-source persistentVolumeClaim: claimName: my-source subPath: my-subdir
Now you need to apply the Tekton pipeline run to your Kubernetes cluster using the following command. Make sure to replace mypipeline-run.yaml with the name of the YAML file that contains your Tekton pipeline definition.
kubectl apply -f mypipeline-run.yaml
Now the pipeline run is deployed and it should start to run the pipeline. You can see the pipeline status in the Tekon dashboard.
-
Step 4: Once the pipeline execution is complete you should see the KNative service deployed. Use the kn cli to verify this.
NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.127.0.0.1.nip.io hello-00001 2d3h 3 OK / 3 True
-
Step 5: You can invoke the fastapi service with curl as below. It is required to port-forwarding to access the service outside the cluster.
curl --location --request GET 'http://localhost:9089/hello' \ --header 'Host: hello.default.127.0.0.1.nip.io'
That's it! You have now created a simple FastAPI microservice and dockerized it. Source code of the Python service is here
0 comments:
Post a Comment