Hello, Here we are using Ubuntu 20.04.3 LTS
OS as a base machine.
- Write Jenkinsfile for jenkins pipeline.
- Install Jenkins in minikube with the help of helm repo.
- Deploy WordPress and MySQL charts in minikube through Jenkins Pipeline.
- Install Kubernetes
- Install Helm
We are doing this deployment in a different namespace called demo
.
For creating a new namespace you can use this command.
kubectl create namespace demo
For all below steps we are using the same namespace
- Adding own jenkins slave image as an agent
this agent is common for all Jenkinsfiles
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: shell
image: jayagrawaldocker/jenkins:0.4
command:
- sleep
args:
- infinity
'''
defaultContainer 'shell'
}
}
here I am adding jayagrawaldocker/jenkins:0.4
as a slave image. In this image I am taking jenkins/inbound-agent:latest
as a base image and Install helm & kubernetes
is that base image. You can also create your own image with below Dockerfile or continue with mine.
Dockerfile
ARG version=latest
FROM jenkins/inbound-agent:$version
LABEL maintainer='Jay Agrawal'
ARG HELM_VERSION=3.5.2
USER root
RUN apt-get update && apt-get install curl
# install helm
RUN mkdir /tmp/helm \
RUN cd /tmp/helm \
RUN curl -s https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz | tar zxvf - \
RUN cp /tmp/helm/linux-amd64/helm /usr/local/bin/helm \
RUN chmod +x /usr/local/bin/helm \
RUN rm -rf /tmp/helm
# install Kubernetes CLI
# See https://storage.googleapis.com/kubernetes-release/release/stable.txt
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.19.0/bin/linux/amd64/kubectl \
&& chmod +x ./kubectl \
&& mv ./kubectl /usr/local/bin/kubectl
USER jenkins
- Stages in Jenkinfiles:
Here we are creating 2 jenkinsfile for WordPress & MySQL for seperate pipeline and 1 jenkinsfile is for deploying both WordPress & MySQL in single pipeline.
stages {
stage('version cheak') {
steps {
sh 'helm version'
sh 'kubectl version'
}
}
stage('helm install mysql'){
steps{
sh 'helm upgrade --install mysql sql -n demo'
}
}
stage('helm install wordpress'){
steps{
sh 'helm upgrade --install wp wordpress -n demo'
}
}
}
Given above stages is for Jenkinsfile for deploying both WordPress & MySQL in one pipline. Where as If you continue with only stage1 and stage2 and remove stage3 i.e. helm install wordpress
then you can use same file for Jenkinsfile1 and with stage1 and stage3 i.e. if you remove stage2 helm install mysql
then the same file is used as jenkinsfile2.
- You can refer to official documents for Jenkins installation
and skip
step 2: Install Jenkins in minikube Jenkins
or you can continue with us. - Get Repo Info
helm repo add jenkins https://charts.jenkins.io
helm repo update
- Install Chart
# Helm 3
helm install -n demo helm-jenkins2 jenkins/jenkins
- Wait for the container to start
kubectl get pods -namespace demo
Initial output
NAME READY STATUS RESTARTS AGE
helm-jenkins-0 0/2 Running 0 1m
It will take 5-10 mins according to your machine Configuration, repeat the 4th command, until the addition of all containers in the pool.
Final output
NAME READY STATUS RESTARTS AGE
helm-jenkins-0 2/2 Running 0 10m
- Generate a password for admin user
kubectl exec --namespace demo -it svc/helm-jenkins -c jenkins -- /bin/cat /run/secrets/chart-admin-password && echo
- For creating the link between minikube and local machine.
kubectl --namespace demo port-forward svc/helm-jenkins 8080:8080
- Go to the link http://127.0.0.1:8080 and log in with the username ->
admin
and password generated by the second-last command. (5th point)
- First go to the new item and create a new pipeline job with any name. (recommended name
demo
) - Then go in the last in the pipeline section there in Definition select
Pipeline script for SCM
. - In SCM select
git
and inRepository URL
past https://github.com/JayAgrawalgit/wordpress-mysql-deploy-by-jenkins.git or HTTPS link of this repo which is present in the top green code button. - In
Branches to build
enter*/main
. - In
Script Path
enterJenkinsfile
. Apply
andSave
.- Now Build the Project
- First go to the new item and create a new pipeline job with any name. (recommended name
MySQL-pipeline
) - Then go in the last in the pipeline section there in Definition select
Pipeline script for SCM
. - In SCM select
git
and inRepository URL
past https://github.com/JayAgrawalgit/wordpress-mysql-deploy-by-jenkins.git or HTTPS link of this repo which is present in the top green code button. - In
Branches to build
enter*/main
. - In
Script Path
enterJenkinsfile1
for MySQL. Apply
andSave
.
Note: As e need to perform individual job run for both MySQL and WordPress we need to take care of that especially
- For running the job individual In
Script Path
enterJenkinsfile2
for WordPress, rest of the procedure as above Apply
andSave
.- Now Build the Project name
MySQL-pipeline
and if It will runSUCCESS
then build the project with the nameWordPress-pipeline
.
failed to query with labels: secrets are forbidden: User "system:serviceaccount:demo:default" cannot list resource "secrets" in API group "" in the namespace "demo"
then run
Given command is
Strictly not recommended in production environment
, This is only a work around.
kubectl create clusterrolebinding permissive-binding --clusterrole=cluster-admin --user=admin --user=kubelet --group=system:serviceaccounts:demo
For more information kubernetes.io Jenkins.io