From 28de1bd25c9bc83f867ca9278f7b0db6d2125fcb Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Thu, 13 Apr 2023 00:00:13 -0300 Subject: [PATCH 01/11] feat: incluindo o Dockerfile e Manifesto para fazer o deploy usando Kubernetes --- k8s/deployment.yaml | 84 +++++++++++++++++++++++++++++++++++++++++++++ src/Dockerfile | 7 ++++ 2 files changed, 91 insertions(+) create mode 100644 k8s/deployment.yaml create mode 100644 src/Dockerfile diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 000000000..90b499aa1 --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,84 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgre +spec: + selector: + matchLabels: + app: postgre + template: + metadata: + labels: + app: postgre + spec: + containers: + - name: postgre + image: postgres:15.0 + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: "kubenews" + - name: POSTGRES_USER + value: "kubenews" + - name: POSTGRES_PASSWORD + value: "Pg#123" + +--- + +apiVersion: v1 +kind: Service +metadata: + name: postgre +spec: + selector: + app: postgre + ports: + - port: 5432 + type: ClusterIP + +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: web +spec: + selector: + matchLabels: + app: web + template: + metadata: + labels: + app: web + spec: + containers: + - name: web + image: ilaraca/kube-news:v1 + ports: + - containerPort: 8080 + env: + - name: DB_DATABASE + value: "kubenews" + - name: DB_USERNAME + value: "kubenews" + - name: DB_PASSWORD + value: "Pg#123" + - name: DB_HOST + value: "postgre" + +--- + +apiVersion: v1 +kind: Service +metadata: + name: web +spec: + selector: + app: web + ports: + - port: 80 + # faz o redirecionamento da porta 8080 + targetPort: 8080 + nodePort: 30000 + type: NodePort diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 000000000..3bcd69739 --- /dev/null +++ b/src/Dockerfile @@ -0,0 +1,7 @@ +FROM node:18.11.0 +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +EXPOSE 8080 +CMD ["node", "server.js"] \ No newline at end of file From a942772b03f04624cbf8dc62070423ece9f2881a Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 14:46:22 -0300 Subject: [PATCH 02/11] feat: adicionando o Jenkins file e alterando o type do deployment para LoadBalancer --- Jenkinsfile | 16 ++++++++++++++++ k8s/deployment.yaml | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..fa5e43fcd --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,16 @@ +pipeline { + agent any + + stages { + + // Primeiro stage de build + stage ('Build Docker Image') { + steps: { + script { + // parametrizar - toda vez que executar a pipeline mudar a tag, incrementando + dockerapp = docker.build("ilaraca/kube-news:${env.BUILD_ID}", '-f ./src/Dockerfile ./src') + } + } + } + } +} \ No newline at end of file diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml index 90b499aa1..b97608879 100644 --- a/k8s/deployment.yaml +++ b/k8s/deployment.yaml @@ -80,5 +80,4 @@ spec: - port: 80 # faz o redirecionamento da porta 8080 targetPort: 8080 - nodePort: 30000 - type: NodePort + type: LoadBalancer From 57b3d3fb47701485ebd10f1f70a8e8cefd11d684 Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:11:47 -0300 Subject: [PATCH 03/11] fix: corrigindo o steps, tirando os : --- Jenkinsfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index fa5e43fcd..29e019a69 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,11 +3,9 @@ pipeline { stages { - // Primeiro stage de build stage ('Build Docker Image') { - steps: { + steps { script { - // parametrizar - toda vez que executar a pipeline mudar a tag, incrementando dockerapp = docker.build("ilaraca/kube-news:${env.BUILD_ID}", '-f ./src/Dockerfile ./src') } } From eb175318b2eeb684680449cc775670ed3afc6c22 Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:25:32 -0300 Subject: [PATCH 04/11] feat: adicionando o docker push --- Jenkinsfile | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 29e019a69..a0bcebdd1 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,5 +10,16 @@ pipeline { } } } + + stage ('Push Docker Image') { + steps { + script { + docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') { + dockerapp.push('latest') + dockerapp.push("${env.BUILD_ID}") + } + } + } + } } } \ No newline at end of file From 46cedff3fdb558443e31e89b9ee8627f09615ffa Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:39:38 -0300 Subject: [PATCH 05/11] feat: adicionando o kubectl --- Jenkinsfile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index a0bcebdd1..3a5d3f727 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -21,5 +21,15 @@ pipeline { } } } + + stage ('Deploy Kubernetes') { + steps { + withKubeconfig ([credentialsId: 'kubeconfig']) { + + sh 'kubectl apply -f ./k8s/deployment.yaml' + + } + } + } } } \ No newline at end of file From 6c33357dacef45d6c4d52feb9c7187c9f2cb93ca Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:42:18 -0300 Subject: [PATCH 06/11] fix: corrigindo o withKubeconfig para o withKubeConfig --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3a5d3f727..b5e4c6928 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,7 +24,7 @@ pipeline { stage ('Deploy Kubernetes') { steps { - withKubeconfig ([credentialsId: 'kubeconfig']) { + withKubeConfig ([credentialsId: 'kubeconfig']) { sh 'kubectl apply -f ./k8s/deployment.yaml' From 405c6001265ee05a21760c4150d03da6ebe8fc12 Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:51:33 -0300 Subject: [PATCH 07/11] feat: alterando no manifesto para a versao para dinamico e incluindo a nova tag no Jenkinsfile --- Jenkinsfile | 8 +++++++- k8s/deployment.yaml | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b5e4c6928..6343d4282 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,9 +23,15 @@ pipeline { } stage ('Deploy Kubernetes') { + + environment { + tag_version = "${env.BUILD_ID}" + } steps { withKubeConfig ([credentialsId: 'kubeconfig']) { - + + + sh 'sed -i "s/{{TAG}}/$tag_version/g ./k8s/deployment.yaml' sh 'kubectl apply -f ./k8s/deployment.yaml' } diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml index b97608879..192ef34d0 100644 --- a/k8s/deployment.yaml +++ b/k8s/deployment.yaml @@ -54,7 +54,7 @@ spec: spec: containers: - name: web - image: ilaraca/kube-news:v1 + image: ilaraca/kube-news:{{TAG}} ports: - containerPort: 8080 env: From 8f385989b4e5200d9be104c3d523c3e49d4bd8e2 Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 15:56:44 -0300 Subject: [PATCH 08/11] fix: corrigindo o comando de mudanca para variavel dinamica no withKubeConfig --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6343d4282..6788206ab 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -31,7 +31,7 @@ pipeline { withKubeConfig ([credentialsId: 'kubeconfig']) { - sh 'sed -i "s/{{TAG}}/$tag_version/g ./k8s/deployment.yaml' + sh 'sed -i "s/{{TAG}}/$tag_version/g" ./k8s/deployment.yaml' sh 'kubectl apply -f ./k8s/deployment.yaml' } From 49b36b666ffd51463dc00502ccebe4a18f027cae Mon Sep 17 00:00:00 2001 From: Ilara Almeida Date: Sun, 16 Apr 2023 16:07:20 -0300 Subject: [PATCH 09/11] test: modificando o componente front para teste com webhooks --- src/views/partial/nav-bar.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/partial/nav-bar.ejs b/src/views/partial/nav-bar.ejs index bed961c34..bd15cb28d 100644 --- a/src/views/partial/nav-bar.ejs +++ b/src/views/partial/nav-bar.ejs @@ -2,7 +2,7 @@