Source to Image: Publish an App without a Dockerfile
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 automatically build images and publish them to Kubernetes without any Dockerfile.
This tutorial demonstrates how to use S2I to import source code of a Java sample project into KubeSphere by creating a Service. Based on the source code, the KubeSphere Image Builder will create a Docker image, push it to a target repository and publish it to Kubernetes.
- You need to enable the KubeSphere DevOps System as S2I is integrated into it.
- You need to create a GitHub account and a Docker Hub account. GitLab and Harbor are also supported. This tutorial uses a GitHub repository to provide the source code for building and pushes an image to Docker Hub.
- You need to create a workspace, a project and a user (
project-regular). The user must be invited to the project with the role of
operator. For more information, see Create Workspaces, Projects, Users and Roles.
- Set a CI dedicated node for building images. This is not mandatory but recommended for the development and production environment as it caches dependencies and reduces build time. For more information, see Set a CI Node for Dependency Caching.
Use Source-to-Image (S2I)
Step 1: Fork the example repository
Log in to GitHub and fork the GitHub repository devops-maven-sample to your personal GitHub account.
Step 2: Create Secrets
Log in to KubeSphere as
project-regular. Go to your project and create a Secret for Docker Hub and GitHub respectively. For more information, see Create the Most Common Secrets.
Step 3: Create a Service
In the same project, navigate to Services under Application Workloads and click Create.
Choose Java under Create Service from Source Code, name it
s2i-demoand click Next.
NoteKubeSphere has integrated common S2I templates such as Java, Node.js and Python. If you want to use other languages or customize your S2I templates, see Customize S2I Templates.
On the Build Settings page, provide the following information accordingly and click Next.
Service Type: Select Stateless Service for this example. For more information about different Services, see Service Type.
Build Environment: Select kubesphere/java-8-centos7:v2.1.0.
Code Repository URL: The source code repository address (currently support Git). You can specify the code branch and the relative path in the source code terminal. The URL supports HTTP and HTTPS. Paste the forked repository URL (your own repository address) into this field.
Code Repository Branch: The branch that is used for image building. Enter
masterfor this tutorial. You can enter
dependencyfor a cache test.
Code Repository Key: You do not need to provide any Secret for a public repository. Select the GitHub Secret if you want to use a private repository.
Image Name: Customize an image name. As this tutorial will push an image to Docker Hub, enter
dockerhub_usernameis your Docker ID and make sure it has the permission to push and pull images.
Image Tag: The image tag. Enter
Target Image Registry: Select the Docker Hub Secret as the image is pushed to Docker Hub.
Advanced Settings: You can define the code relative path. Use the default
/for this field.
On the Pod Settings page, scroll down to Port Settings to set the access policy for the container. Select HTTP for Protocol, customize the name (for example,
http-1), and enter
8080for both Container Port and Service Port.
Scroll down to Health Check and select it. Set Readiness Check by filling out the following parameters. Click √ when you finish setting the probe and then click Next to continue.
HTTP Request: Select HTTP as the protocol, enter
/as the path (root path in this tutorial), and enter
8080as the port exposed.
Initial Delay (s): The number of seconds after the container has started before the liveness probe is initiated. Enter
30for this field.
Timeout (s): The number of seconds after which the probe times out. Enter
10for this field.
For other fields, use the default value directly. For more information about how to configure probes and set other parameters on the Container Settings page, see Pod Settings.
On the Storage Settings page, you can add a volume for the container. For more information, see Volumes. Click Next to continue.
On the Advanced Settings page, select External Access and select NodePort as the access method. Click Create to finish the whole process.
Click Image Builders from the navigation bar and you can see that the example image is being built.
Step 4: Check results
Wait for a while and you can see the status of the image builder has reached Successful.
Click this image builder to go to its details page. Under Job Records, click on the right of a record to see building logs. You can see
Build completed successfullyat the end of the log if everything runs normally.
Go back to the Services, Deployments, and Jobs page, and you can see the corresponding Service, Deployment, and Job of the image have been all created successfully.
In your Docker Hub repository, you can see that KubeSphere has pushed the image to the repository with the expected tag.
Step 5: Access the S2I Service
On the Services page, click the S2I Service to go to its details page.
To access the Service, you can either use the endpoint with the
curlcommand or visit
<Node IP>:<NodePort>. For example:
$ curl 10.10.131.44:8080 Really appreciate your star, that is the power of our life.
NoteIf you want to access the Service outside the cluster, you may need to open the port in your security groups and configure port forwarding rules depending on your deployment environment.
Was this page Helpful?
Receive the latest news, articles and updates from KubeSphere
Thanks for the feedback. If you have a specific question about how to use KubeSphere, ask it on Slack. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.