Source to Image: Publish your app without Dockerfile
Source to Image (S2I) is a tool that allows a programmer to input source codes and transfer the codes into runnable program to Docker image. This tool enables programers to build images even when they don’t know about Dockerfile. By inputing source codes into the source code compiler, Builder image, Source to Image can transfer the codes into Docker images.
Objective
The example below is an official Java demonstration for Hello World. It will show you how to use Source to Image on KubeSphere to build image, push the images to mirroring repositories and finally deploy them to clusters.
Prerequisites
- The example below takes the GitHub codes repository and DockerHub repositories for example. Make sure you have created GitHub and DockerHub accounts.
- Build workspace, projects and
project-regular
accounts for common users at first. If not, please refer to Quick Start Guide of Multi-tenant Management. - Use
project-admin
to inviteproject-regular
, regular users, to join in the project. Refer to Quick Start Guide of Multi-tenant Management-inviting members to grant theoperator
role.
Estimated Time
20-30 minutes (The time varies according to the Internet speed and other factors.)
Hands-on Lab
Create Secrets
Create the secrets for DockerHub repository and GitHub codes repository as dockerhub-id and github-id. Please refer to Creating Common Secrets
Fork Project
Login GitHub. Fork the GitHub repository devops-java-sample to your personal GitHUb.
Create a Deployment
Step 1: Fill in basic information
1.1 Click Deployment and enter.
1.2 Click Create.
- Name: (Necessary) Name the deployment as
s2i-test
; - Nickname: It can be customized.
- Description: Simply decirbe the deployment information. This is customizable.
Step 2: Container Group Template Settings
2.1. Click Next and then click Add Container.
2.2. Then choose Build a new container image from code
.
2.3. Fork the Repository Sample to your personal GItHUb. Then copy your personal repository’s git address.
2.4. Refer to the following information and fill in.
Note: KubeSphere has built in common s2i templates for Java, Node.js and Python. If you need to customize other languages or use the s2i template, please refer to Customized s2i template.
- Code URL: Paste the git address fromt he last step (Git, HTTP and HTTPS are fine. You can also specify the related path in the source code terminal);
- Secret: Inject the secret
github-id
into the S2I Pod; - Mirroring Template: Choose
kubespheredev/java-8-centos7
as the Builder image. - Code relative path: Use
/
by default. - Image Name: It can be customized. The user names here are
dockerhub_username>/hello
anddockerhub_username
. - Tag: The mirroring tag can be
latest
by default. - Target image repository: Choose
dockerhub-id
created in the previous step.
2.4. Scroll down to Container Specification
. It is suggested to set CPU and RAM’s biggest storages as 500M and 1000Mi.
2.5. Scroll down to Service Settings
. The port number is 8080:
2.6. Then click Save.
The duplicate number is 1. The click Next.
Step 3: Create s2i Deployment
No need to set storage volume. Click Next. Save the tag by default. Then choose Create. Complete the s2i deployment.
Complete the creation
The green check below indicates that the mirroring had been created by s2i.
Check the container group. Function well.
Verify The Operation Results
If the deployment was successful through s2i, you will see the setted image in Dockerhub. The name and tage have been setted in the Container Group Template Setting. If you want to check the deployment results, you can configerate as follows.
Step 1: Create a Service
Choose Network & Services → Services on the left menu, then click on the Create button.
Step 2: Fill in the basic information
This step is similar to deployment creation, e.g. Name : s2i-test-service
. The rest are customized as below.
Step 3: Service Settings
3.1. Choose the first item Virtual IP: Access the service through the internal IP of the cluster
.
3.2. Then click on the Specify Workload
and choose springboot-s2i
deployment as below.
3.3. Click save. Refer to the port information as follows.
- Port name: s2i-port
- TCP defaulted, Port Number:
8080
and the target port is8080
. - Finish the settings then click Next.
Step 5: Tag setting
Setting by default then click Next.
Step 6: Setting External Network Access
Select the access method as NodePort
.
For now, the service check has been created. As below, the Virtual IP is 10.233.40.2, the service port is 8080 and the Nodeport is 30454.
Step 7: Verify Visits
If you visit the HelloWorld service from the internal network, you can login cluster nodes through SSH or use Cluster admin to login KubeSphere. Then putin the following command in web kubectl:
# curl {$Virtual IP}:{$Port} or curl {$Node IP}:{$NodePort}
$ curl 10.233.60.196:8080
Hello,World!
Tip: If you need to access to this service from external network, you may need to bind to the public network EIP and set port forwarding and firewall rules. Forward the Internal Network Port 30454 to the source port 30454 in the forwarding rule. Then open this port in the firewall to ensure the external network traffic can pass this port. For operations on the QingCloud platform, you can refer to Cloud Platform Setting Port Forward and Firewall.
Verify the Service and Image Push
Since we set the target mirroring repository as DockerHub, you can login your personal DockerHub to check the image pushed by Source to Image. If you find hello:latest
, the image has been successfully pushed to DockerHub