Sunday, July 14, 2019

UBUNTU18+KVM+VAGRANT+KUBERNETES 11-1)CICD-JENKINS-ON-K8S-SPRING-MYSQL

 This post shows how to deploy spring-mysql examples using below sites's posts.


https://spring.io/guides/gs/accessing-data-mysql/
 https://wnapdlf.blogspot.com/2019/07/ubuntu18kvmvagrantkubernetes-11docker.html

 https://wnapdlf.blogspot.com/2019/07/ubuntu18kvmvagrantkubernetes-6.html
 and ETC.
git clone https://github.com/spring-guides/gs-accessing-data-mysql.git

 

 1. Create mariadb database,user,password. 

 2.Git add and init

 3. Add a repository

 4.Dockerfile, deliver.sh, Jenkinsfile contents.

 Conclusion)

 



 1. Create mariadb database,user,password.

 #I am controlling k8s cluster from local pc.
oyj@oyj-X555QG:~$ kb get po
NAME                                      READY   STATUS    RESTARTS   AGE
mariadb-master-0                          1/1     Running   31         9d
nfs-client-provisioner-78665db465-h98vr   1/1     Running   14         5d23h
oyj@oyj-X555QG:~$ kubectl exec -it mariadb-master-0 bash

root@mariadb-master-0:/# mysql -p
Enter password:

root@mariadb-master-0:/# mysql -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5414
Server version: 10.1.14-MariaDB-1~jessie mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database db_example;
Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> create user 'springuser'@'%' identified by 'Strongpass%';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> grant all on db_example.* to 'springuser'@'%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>  flush privileges;
Query OK, 0 rows affected (0.00 sec)



2.Git add and init

oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ git init

oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ git add .
oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ git commit -m "first spring-msyql"
[master (root-commit) a449986] first spring-msyql
 22 files changed, 1010 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .mvn/wrapper/maven-wrapper.jar
 create mode 100644 .mvn/wrapper/maven-wrapper.properties
 create mode 100644 Dockerfile
 create mode 100644 Jenkinsfile
 create mode 100644 build.gradle
 create mode 100644 deliver.sh
 create mode 100644 docker-compose.yml
 create mode 100644 gradle/wrapper/gradle-wrapper.jar
 create mode 100644 gradle/wrapper/gradle-wrapper.properties
 create mode 100755 gradlew
 create mode 100644 gradlew.bat
 create mode 100755 mvnw
 create mode 100755 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 spring-mysql-dp.yaml
 create mode 100644 spring-mysql-svc.yaml
 create mode 100644 src/main/java/hello/Application.java
 create mode 100644 src/main/java/hello/MainController.java
 create mode 100644 src/main/java/hello/User.java
 create mode 100644 src/main/java/hello/UserRepository.java
 create mode 100644 src/main/resources/application.properties

#Database access config
oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ cat src/main/resources/application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://mariadb-master:3306/db_example?serverTimezone=UTC
spring.datasource.username=springuser
spring.datasource.password=Strongpass%

 3. Add a repository




git push

oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ git remote add origin https://github.com/ohyoungjooung2/spring-mysql.git
@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ git push
Username for 'https://github.com': ohyoungjooung2
Password for 'https://ohyoungjooung2@github.com':



#Create jenkins new pipe line








4.Dockerfile, deliver.sh, Jenkinsfile contents.

oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ cat Dockerfile
FROM openjdk:8u111-jdk-alpine
VOLUME /tmp
ADD /target/gs-mysql-data-0.1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ cat deliver.sh -n
     1    #!/usr/bin/env bash
     2    echo 'TestingTesting'
     3    docker build . -t 10.1.0.7:3333/spring-mysql:1.0; docker images | grep spring-mysql
     4   
     5    #If docker build fails then exit!
     6    if [[ $? -eq 0 ]]
     7    then
     8        echo -e "\e[35m docker build success \e[00m"
     9    else
    10        echo -e "\e[33m docker build failed \e[00m"
    11        exit 1
    12    fi
    13   
    14   
    15    echo 'Pushing'
    16    docker push 10.1.0.7:3333/spring-mysql:1.0; echo $?; echo test
    17   
    18    echo "Deployment of spring-mysql"
    19    if [[ -e spring-mysql-dp.yaml ]]
    20    then
    21        scp -i $HOME/id_rsa -P 9999 spring-mysql-dp.yaml vagrant@172.17.0.1:/home/vagrant/
    22            ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl get deploy spring-mysql'
    23        if [[ $? -eq 0 ]]
    24        then
    25            echo "spring-mysql deploy already exists , apply!"
    26                    ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl apply -f spring-mysql-dp.yaml'
    27            else
    28            echo "spring-mysql deploy not exists , create!"
    29        ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl create -f spring-mysql-dp.yaml'
    30            fi
    31    else
    32        echo "spring-mysql-dp.yaml not exists"
    33        exit 1
    34    fi
    35   
    36    echo "Deployment of spring-mysql-svc"
    37    if [[ -e spring-mysql-svc.yaml ]]
    38    then
    39        scp -i $HOME/id_rsa -P 9999 spring-mysql-svc.yaml vagrant@172.17.0.1:/home/vagrant/
    40            ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl get svc spring-mysql'
    41        if [[ $? -eq 0 ]]
    42        then
    43            echo "spring-mysql svc already exists , apply!"
    44                    ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl apply -f spring-mysql-svc.yaml'
    45        else
    46   
    47            echo "spring-mysql svc not exists , apply!"
    48                ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 '/usr/bin/kubectl create -f spring-mysql-svc.yaml'
    49        fi
    50    else
    51        echo "spring-mysql-svc.yaml not exists"
    52        exit 1
    53    fi
    54   
    55    CHK(){
    56        if [[ $? -eq 0 ]]
    57            then
    58             echo "Deployment of spring-mysql Success!"
    59                     exit 0
    60            fi
    61     }
    62   
    63    #Default duration check time until service on.
    64    NOWTIME=$(date +%s)
    65    #5 MINUTES
    66    TILTIME=300
    67    TILTIMECOMPLETE=$(($NOWTIME+$TILTIME))
    68    while true
    69    do
    70     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.3:32339/demo/all'
    71     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.3:32339/demo/add?name=Tester&email=testermail@m.kkk'
    72     CHK
    73     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.4:32339/demo/all'
    74     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.3:32339/demo/add?name=Tester&email=testermail@m.kkk'
    75     CHK
    76     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.4:32339/demo/all'
    77     ssh -i $HOME/id_rsa -p 9999 vagrant@172.17.0.1 'curl http://10.1.0.3:32339/demo/add?name=Tester&email=testermail@m.kkk'
    78     CHK
    79     CURRENTIME=$(date +%s)
    80     DURATION_SEC=$(($CURRENTIME-$NOWTIME))
    81     echo "DURATION_SEC=$DURATION_SEC"
    82     echo
    83     #If current time unix epoch time is more than 5 minutes..since check service..then..stop..exit
    84     if [[ $CURRENTIME -ge $TILTIMECOMPLETE ]]
    85     then
    86         echo "Service wait time ends"
    87         exit 0
    88     fi
    89    done



oyj@oyj-X555QG:~/INSTALL/u18kvk8s/k8s/cicd/gs-accessing-data-mysql/complete$ cat Jenkinsfile
pipeline {
  agent any

  tools {
   maven 'maven 3.6'

  }
  

   options {
        skipStagesAfterUnstable()
    }
   stages {
        stage('Build') {

            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }

        stage('Test'){
            steps{
                sh 'mvn test'
            }
               }

    }

    post {
        always {
             sh 'chmod 755 ./deliver.sh'
             sh './deliver.sh'
          }
     }
}

5.Last result(Build and deploy)

Started by user k
Obtained Jenkinsfile from git https://github.com/ohyoungjooung2/spring-mysql
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/spring-mysql
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 77bf41b6-5466-41cb-ac02-5c34193a75b2
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/ohyoungjooung2/spring-mysql # timeout=10
Fetching upstream changes from https://github.com/ohyoungjooung2/spring-mysql
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress https://github.com/ohyoungjooung2/spring-mysql +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision f0286156ad0abb2ca3993bcbaa3b287f6e1ab803 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f f0286156ad0abb2ca3993bcbaa3b287f6e1ab803
Commit message: "Add more delicate check process of svc and deploy of yaml"
 > git rev-list --no-walk fd065d6d7dd2f752f7afd0eae01f638ae36cc669 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ mvn -B -DskipTests clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< org.springframework:gs-mysql-data >------------------
[INFO] Building gs-mysql-data 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ gs-mysql-data ---
[INFO] Deleting /var/jenkins_home/workspace/spring-mysql/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ gs-mysql-data ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ gs-mysql-data ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to /var/jenkins_home/workspace/spring-mysql/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ gs-mysql-data ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/jenkins_home/workspace/spring-mysql/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ gs-mysql-data ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ gs-mysql-data ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ gs-mysql-data ---
[INFO] Building jar: /var/jenkins_home/workspace/spring-mysql/target/gs-mysql-data-0.1.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:repackage (repackage) @ gs-mysql-data ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.320 s
[INFO] Finished at: 2019-07-14T09:39:19Z
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ mvn test
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< org.springframework:gs-mysql-data >------------------
[INFO] Building gs-mysql-data 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ gs-mysql-data ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ gs-mysql-data ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ gs-mysql-data ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/jenkins_home/workspace/spring-mysql/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ gs-mysql-data ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ gs-mysql-data ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.037 s
[INFO] Finished at: 2019-07-14T09:39:27Z
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] sh
+ chmod 755 ./deliver.sh
[Pipeline] sh
+ ./deliver.sh
TestingTesting
Sending build context to Docker daemon   36.6MB

Step 1/4 : FROM openjdk:8u111-jdk-alpine
 ---> 3fd9dd82815c
Step 2/4 : VOLUME /tmp
 ---> Using cache
 ---> 5f00001cec33
Step 3/4 : ADD /target/gs-mysql-data-0.1.0.jar app.jar
 ---> 7abca838e101
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
 ---> Running in 096a6be68ee2
Removing intermediate container 096a6be68ee2
 ---> e0dec5b6df58
Successfully built e0dec5b6df58
Successfully tagged 10.1.0.7:3333/spring-mysql:1.0
10.1.0.7:3333/spring-mysql                     1.0                 e0dec5b6df58        1 second ago        181MB
10.1.0.7:3333/spring-mysql                     <none>              3c5521ea777f        29 minutes ago      181MB
10.1.0.7:3333/spring-mysql                     <none>              e42fc9ba448d        36 minutes ago      181MB
10.1.0.7:3333/spring-mysql                     <none>              5aba613ea309        44 minutes ago      181MB
 [35m docker build success  [00m
Pushing
The push refers to repository [10.1.0.7:3333/spring-mysql]
f550b2b9d93b: Preparing
a1e7033f082e: Preparing
78075328e0da: Preparing
9f8566ee5135: Preparing
a1e7033f082e: Layer already exists
9f8566ee5135: Layer already exists
78075328e0da: Layer already exists
f550b2b9d93b: Pushed
1.0: digest: sha256:e6146ea2a2784c51453ed0a01c147d17498d36dda8be453b4e799ec1b7e15535 size: 1159
0
test
Deployment of spring-mysql
Error from server (NotFound): deployments.extensions "spring-mysql" not found
spring-mysql deploy not exists , create!
deployment.extensions/spring-mysql created
Deployment of spring-mysql-svc
Error from server (NotFound): services "spring-mysql" not found
spring-mysql svc not exists , apply!
service/spring-mysql created
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100     2    0     2    0     0      1      0 --:--:--  0:00:01 --:--:--     1[]
100     2    0     2    0     0      1      0 --:--:--  0:00:01 --:--:--     1
Deployment of spring-mysql Success!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS


Conclusion)

In this post, I showed the example that is java maven + mariadb connection.
All sources are in below gitub link.
https://github.com/ohyoungjooung2/spring-mysql

Every source's license belongs to who created.

My shell script and etc follows MIT.

Thanks for reading

No comments:

Post a Comment