Configuraciones e instrucciones para desplegar aplicaciones en Kubernetes con Jenkins
Find a file
2025-10-08 14:32:57 +00:00
creaCredencialesRegistry.sh first commit 2025-08-21 12:37:42 +02:00
jenkins.yaml first commit 2025-08-21 12:37:42 +02:00
kubeconfig-imac first commit 2025-08-21 12:37:42 +02:00
namespace.yaml first commit 2025-08-21 12:37:42 +02:00
pvc-jenkins.yaml first commit 2025-08-21 12:37:42 +02:00
README.md Actualizar README.md 2025-10-08 14:32:57 +00:00

Despliegues a Kubernetes usando Jenkins CI/CD | Construyendo con Kaniko


¡ATENCIÓN! usar mejor este otro proyecto

Las ideas han salido de este Vídeo.

Comprobar y añadir repositorios Helm

helm repo list

el resultado en mi equipo es

NAME          	URL                                              
rancher-latest	https://releases.rancher.com/server-charts/latest
jetstack      	https://charts.jetstack.io                       
longhorn      	https://charts.longhorn.io 

relacionado todo con la instalación de Rancher.

helm repo add jenkinsci https://charts.jenkins.io
helm repo update

helm search repo jenkins

resultado:

NAME             	CHART VERSION	APP VERSION	DESCRIPTION                                       
jenkinsci/jenkins	5.8.83       	2.516.2    	Jenkins - Build great things at any scale! As t...

Obtener fichero de valores por defecto y adaptarlo

helm show values jenkinsci/jenkins > ./jenkins.yaml

Valores a cambiar.

sección campo valor
admin: username: "admin"
admin: password: "una-password"
serviceType: NodePort
nodePort: 32000
installLatestPlugins: true
installLatestSpecifiedPlugins: true
persistence: enabled: true
persistence: existingClaim: pvc-jenkins

Plugins a instalar

Para buscar la última versión de los plugins ir a Jenkins Plugins

En la sección

installPlugins:

añadir

- blueocean:1.27.21
- ansicolor:1.0.6

Instalar Jenkins

helm install jenkins jenkinsci/jenkins --values ./jenkins.yaml -n jenkins

Preparando proyecto

En el directorio raíz del proyecto tiene que haber un fichero llamado Jenkinsfile que contiene la descripción de lo que hay que hacer.

En el proyecto de prueba, el fichero es este

pipeline {
  options {
      ansiColor('xterm')
  }

  agent {
      kubernetes {
          yamlFile 'builder.yaml'
      }
  }

  stages {
    stage('Kaniko Build & Push Image') {
      steps {
        container('kaniko') {
          script {
            sh '''
            /kaniko/executor --dockerfile `pwd`/Dockerfile \
                             --context `pwd` \
                             --destination=registry.reymota.es/myweb:${BUILD_NUMBER}
            '''
          }
        }
      }
    }

    stage('Deploy App to Kubernetes') {     
      steps {
        container('kubectl') {
          withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
              sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" myweb.yaml'
              sh 'kubectl apply -f myweb.yaml'
          }
        }
      }
    }
  }
}

Creando secreto para el registry

el comando para crear el secreto es

kubectl create secret docker-registry regcred --docker-server=registry.reymota.es --docker-username=creylopez --docker-password=<Cambiame> --docker-email=creylopez@yahoo.es --namespace jenkins

Credenciales del cluster

Las credenciales del cluster hay que añadirlas a través del interfaz de Jenkins, en el apartado Administrar Jenkins->Credenciales. Hay que añadir una credencial de tipo Secret file y subir nuestro fichero de configuación. En el id hay qu eponer exactamente el nombre que tenemos en el fichero Jenkinsfile.