As Features and Benefits elaborates, Source-to-Image (S2I) is a toolkit and workflow for building reproducible container images from source code. S2I produces ready-to-run images by injecting source code into a container image and letting the container prepare that source code for execution. KubeSphere integrates S2I to enable automatically building images and publishing to Kubernetes without writing Dockerfile.
This tutorial will use S2I to import source code of a Java sample project into KubeSphere, build a docker image and push to a target registry, finally publish to Kubernetes and expose the service to outside.
project-regular
account with the role of operator, see Getting Started with Multi-tenant Management.20-30 minutes
Log in KubeSphere with the account project-regular
. Go to your project and create the secrets for DockerHub and GitHub. Please reference Creating Common-used Secrets.
Note you may not need to create GitHub Secret if your forked project below is open to public.
Log in GitHub and fork the GitHub repository devops-java-sample to your personal GitHub account.
3.1 Navigate to Application Workloads → Services, click Create Service.
3.2 Choose Java under Build a new service from source code repository, then name it s2i-demo
and click Next.
3.3. Now we need go to GitHub and copy the URL of the forked repository first.
3.4. Paste the URL in Code URL, enter <dockerhub_username>/<image_name>
into imageName, e.g. pengfeizhou/s2i-sample
in this demo. As for secret and Target image repository, you need to choose the secrets created in step 1, let's say dockerhub-id
and github-id
respectively.
Note: KubeSphere has built in common S2I templates for Java, Node.js and Python. It allows you to customize S2I template for other languages.
3.5. Click Next to Container Setting tab. In the Service Settings part, name the service http-port
for example. Container Port and Service Port are both 8080
.
3.6. Scroll down to Health Checker, check it and click Add Container ready check
, fill in the contents as follows:
8080
; it maps to the service port that we need to check.30
; number of seconds after the container has started before liveness or readiness probes are initiated.10
; number of seconds after the probe times out. Default is 1 second.Then click √
to save it when you are done, and click Next.
Click Next again to skip Mount Volumes. Check Internet Access, then choose NodePort to expose S2I service through <NodeIP>:<NodePort>
. Now click Create to start the S2I process.
Choose Image Builder, drill into the new generated S2I build.
You will be able to inspect the logs by expanding Job Records. Normally you can see it outputs "Build completed successfully" in the end.
So far this S2I build has created corresponding Job, Deployment and Service accordingly, We can verify each resource object as follows.
Go into S2I service detailed page, access this service through either Endpoints
, or <ClusterIP>:<Service Port>
, or <NodeIP>:<NodePort>
.
$ curl 10.233.90.126:8080
Really appreciate your star, that is the power of our life.
Tip: If you need to access to this service externally, make sure the traffic can pass through the NodePort. You may configure firewall and port forward according to your environment.
Since you set DockerHub as the target registry, you can log in to your personal DockerHub to check if the sample image has been pushed by the S2I job.
Congratulation! You have been familiar with S2I tool.