| description | Tutorial on how to configure a Jenkins pipeline that deploys an simple hello-world application to a Kubernetes cluster. |
|---|
This tutorial contains a pipeline that deploys an simple hello-world application to a Kubernetes cluster.
You will need a running Kubernetes cluster, that supports services with LoadBalancer type, and a kubeconfig file that can be used to deploy application (a deployment and a service) into the cluster.
The pipeline provided by this tutorial can be added to any Jenkins instance you have administrator access and can run pipeline stages with docker agent. For example, Jenkins configuration from Jenkins with access to hosts Docker engine tutorial can be used.
We will use secret file to configure credentials for managing the target Kubernetes cluster. To create the secret file credential, open Global credentials from Jenkins credentials store from Manage Jenkins > Manage Credentials and click Add Credentials from the left side menu.
In the New credentials form:
- Select Secret file as the credential kind
- Upload your kubeconfig to the file input
- Configure ID for the credential. Jenkinsfile uses
kubeconfigas the ID. - (Optionally) add a description.
First, create a new pipeline via New Item button in the right side menu of the Jenkins dashboard. The name of the pipeline could be for example Animals and it should be an pipeline.
In the configure pipeline view, scroll to the bottom and under Pipeline sub-header select Pipeline script from SCM. SCM type should be Git and Repository URL the url of this repository: https://github.com/cicd-tutorials/cicd-tutorials.net.git. Ensure that branch specifier includes main branch of the repository and modify the Script Path to be docs/tutorials/jenkins/ansible-kubernetes/Jenkinsfile.
The pipeline deploys an example application to a Kubernetes cluster using Ansible playbook. The playbook selects a container image tag based on Jenkins build parameter.
---8<--- "docs/tutorials/jenkins/ansible-kubernetes/Jenkinsfile"After you have created the pipeline, try to execute it by clicking Build Now. The pipeline should have deployed the example application into the Kubernetes cluster with the default image tag (cow) defined in the deploy-to-kubernetes.yml Ansible playbook.
---8<--- "docs/tutorials/jenkins/ansible-kubernetes/deploy-to-kubernetes.yml"After the Ansible playbook has been executed, the pipeline runs wait-until-service-up.sh script. The script waits until the load-balancer created by the Kubernetes service has reached running state and parses the URL where the example application is running.
---8<--- "docs/tutorials/jenkins/ansible-kubernetes/wait-until-service-up.sh"You can find the URL of the created load-balancer from the console output of the build. Open the application with your browser or use curl to see the application response.
In addition, after the first execution Jenkins should have updated the project configuration to contain parameters defined in the pipeline and we can configure the image tag in Build with Parameters menu.