Skip to content
This repository was archived by the owner on Dec 20, 2020. It is now read-only.

Commit e09dad9

Browse files
committed
Added jenkins workflow
1 parent 9cfb6e1 commit e09dad9

15 files changed

+220
-14
lines changed

aws-k8s-pgdb-with-terraform/aws-rds/db.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ resource "aws_db_instance" "default" {
1818
name = "kubernetes101db"
1919
username = "${var.db_username}"
2020
password = "${var.db_password}"
21-
vpc_security_group_ids = ["sg-03dade87817c9c30f"] # aws_security_group_rule.kube-node-ingress-self
21+
vpc_security_group_ids = ["sg-e87817c9c30f"] # aws_security_group_rule.kube-node-ingress-self
2222
db_subnet_group_name = "${aws_db_subnet_group.dbsub.id}"
2323
}

jenkins/pipelines/Jenkinsfile-app-dev

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pipeline {
2+
agent any
3+
stages {
4+
stage('Build Docker image') {
5+
steps {
6+
sh 'echo "Build and push docker image...:"'
7+
sh '''#!/bin/bash
8+
GITREV=`git rev-parse --short HEAD`
9+
cat /opt/docker-hub-password.txt | docker login -u kubetotorial101 --password-stdin
10+
docker push kubetotorial101/pollproject:$GITREV
11+
12+
'''
13+
}
14+
}
15+
stage('dev environment deploy') {
16+
steps {
17+
sh 'echo "Deploy to the development K8s namespace..."'
18+
sh '''
19+
export PATH=$HOME/software:$PATH
20+
kubectl config use-context demo-dev
21+
GITREV=`git rev-parse --short HEAD`
22+
git checkout development
23+
sed -i -e \"s/image:.*/image: kubetotorial101\\/pollproject:$GITREV/g\" kubecode/deployment_django.yaml
24+
kubectl apply -f kubecode/deployment_django.yaml --record
25+
sleep 15
26+
kubectl describe deploy django-deployment
27+
kubectl describe deploy django-deployment | grep "MinimumReplicasAvailable" | grep "True"
28+
'''
29+
}
30+
}
31+
stage ('Unit testing') {
32+
steps {
33+
sh 'echo "Invoking and performing unit tests inside docker container"'
34+
}
35+
}
36+
stage ('Developer sign-off') {
37+
steps {
38+
sh 'echo "Unit testing passed, so pushing changes into devevlopment branch"'
39+
sh '''
40+
git commit -a -m "Unit tests passed, developer sign-off"
41+
git push origin development
42+
'''
43+
}
44+
}
45+
}
46+
post {
47+
always {
48+
cleanWs()
49+
}
50+
}
51+
}
52+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pipeline {
2+
agent any
3+
stages {
4+
stage('Production promotion') {
5+
steps {
6+
sh 'echo "Deploy to the production K8s namespace..."'
7+
sh '''
8+
export PATH=$HOME/software:$PATH
9+
kubectl config use-context demo-prod
10+
GITREV=`git rev-parse --short HEAD`
11+
echo "Deploying master branch ($GITREV) to production cluster."
12+
kubectl apply -f kubecode/deployment_django.yaml --record
13+
'''
14+
}
15+
}
16+
}
17+
post {
18+
always {
19+
cleanWs()
20+
}
21+
}
22+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pipeline {
2+
agent any
3+
stages {
4+
stage('Roll back') {
5+
steps {
6+
sh 'echo "undo rollout"'
7+
sh '''
8+
export PATH=$HOME/software:$PATH
9+
kubectl config use-context demo-prod
10+
echo "History BEFORE roll-back"
11+
kubectl rollout history deployment.v1.apps/django-deployment
12+
kubectl rollout undo deployment.v1.apps/django-deployment
13+
echo "History AFTER roll-back"
14+
kubectl rollout history deployment.v1.apps/django-deployment
15+
'''
16+
}
17+
}
18+
}
19+
}

jenkins/pipelines/Jenkinsfile-scale

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pipeline {
2+
agent any
3+
parameters {
4+
string(
5+
name: 'REPLICAS',
6+
defaultValue:"2",
7+
description: "Number of pods running the application server")
8+
}
9+
stages {
10+
stage('Collecting current status') {
11+
steps {
12+
sh 'echo "Current replica status"'
13+
sh '''
14+
export PATH=$HOME/software:$PATH
15+
kubectl config use-context demo-prod
16+
kubectl get pods --selector=app=django
17+
'''
18+
}
19+
}
20+
stage('Scale app (click to input # replicas') {
21+
steps {
22+
sh "echo REPLICAS: ${params.REPLICAS}"
23+
sh """#!/usr/bin/env bash
24+
export PATH=$HOME/software:$PATH
25+
kubectl config use-context demo-prod
26+
kubectl scale deployment.v1.apps/django-deployment --replicas=${params.REPLICAS}
27+
"""
28+
}
29+
}
30+
stage('Collecting new status') {
31+
steps {
32+
sh 'echo "Current replica status"'
33+
sh '''
34+
export PATH=$HOME/software:$PATH
35+
kubectl config use-context demo-prod
36+
kubectl get pods --selector=app=django
37+
'''
38+
}
39+
}
40+
}
41+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pipeline {
2+
agent any
3+
stages {
4+
stage('Create release candidate') {
5+
steps {
6+
sh 'echo "Creating release candidate branch from development."'
7+
sh '''
8+
GITREV=`git rev-parse --short HEAD`
9+
git branch release-candidate-$GITREV
10+
echo "Release candidate git hash is $GITREV"
11+
'''
12+
}
13+
}
14+
stage('Integration and Acceptance tests') {
15+
steps {
16+
sh 'echo "Running integration and acceptance tests"'
17+
sh 'echo "Running integration tests"'
18+
sh 'echo "Running acceptance tests"'
19+
sh 'echo "ALL tests passed"'
20+
}
21+
}
22+
stage('QA sign-off') {
23+
steps {
24+
sh 'echo "Integration and Acceptance tests passed, so merging into master branch"'
25+
sh '''
26+
GITREV=`git rev-parse --short HEAD`
27+
git fetch
28+
git checkout master
29+
git merge release-candidate-$GITREV
30+
git push origin master
31+
'''
32+
}
33+
}
34+
}
35+
post {
36+
always {
37+
cleanWs()
38+
}
39+
}
40+
}

kubecode/configmap_development.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: v1
33
kind: ConfigMap
44
metadata:
55
name: postgres-config
6-
namespace: development
6+
namespace: demo-dev
77
data:
88
POSTGRES_DB: "kubetutorial101"
99
POSTGRES_USER: "postgres"

kubecode/configmap_production.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ apiVersion: v1
33
kind: ConfigMap
44
metadata:
55
name: postgres-config
6-
namespace: production
6+
namespace: demo-prod
77
data:
8-
POSTGRES_DB: "kubernetes101db"
9-
POSTGRES_USER: "kubetutorial101"
8+
POSTGRES_DB: "kubetutorial101"
9+
POSTGRES_USER: "postgres"
1010
RUN_IN_PRODUCTION: "True"
1111
AWS_S3_REGION_NAME: "us-west-2"
1212

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"kind": "Namespace",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"name": "demo-dev",
6+
"labels": {
7+
"name": "demo-dev"
8+
}
9+
}
10+
}

kubecode/create_prod_namespace.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"kind": "Namespace",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"name": "demo-prod",
6+
"labels": {
7+
"name": "demo-prod"
8+
}
9+
}
10+
}

kubecode/deployment_django.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ spec:
1818
containers:
1919
- name: django
2020
# Notice the tag is also included in the image
21-
image: kubernetes101/django_image:09f1419
21+
image: kubetotorial101/pollproject:b832173
2222
ports:
2323
- containerPort: 8000
2424
# Minimum and maximum resources allocated to the container.
2525
resources:
2626
requests:
27-
memory: "64Mi"
28-
cpu: "200m"
27+
memory: "32Mi"
28+
cpu: "50m"
2929
limits:
3030
memory: "128Mi"
3131
cpu: "400m"

kubecode/deployment_postgres_development.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
labels:
66
app: postgres
77
# A namespace is a virtual k8s cluster
8-
namespace: development
8+
namespace: demo-dev
99
spec:
1010
# number of pods in the replicaset
1111
replicas: 1
@@ -27,10 +27,10 @@ spec:
2727
resources:
2828
requests:
2929
memory: "256Mi"
30-
cpu: "500m"
30+
cpu: "100m"
3131
limits:
3232
memory: "512Mi"
33-
cpu: "1000m"
33+
cpu: "300m"
3434
envFrom:
3535
- configMapRef:
3636
name: postgres-config

kubecode/ext_service_db_production.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apiVersion: v1
22
kind: Service
33
metadata:
44
name: pghost
5-
namespace: production
5+
namespace: demo-prod
66
spec:
77
type: ExternalName
8-
externalName: terraform-20181102201911881300000001.ctrkthnoul1u.us-west-2.rds.amazonaws.com
8+
externalName: terraform-20190219195934809500000001.ctrkthnoul1u.us-west-2.rds.amazonaws.com
99

kubecode/service_db_development.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
kind: Service
33
metadata:
44
name: pghost
5-
namespace: development
5+
namespace: demo-dev
66
spec:
77
selector:
88
app: postgres

kubecode/service_db_production.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: pghost
5+
namespace: demo-prod
6+
spec:
7+
selector:
8+
app: postgres
9+
ports:
10+
- protocol: TCP
11+
port: 5432
12+
targetPort: 5432

0 commit comments

Comments
 (0)