Build a Docker Image, Upload to Nexus Repository and Deploy on Kubernetes

kubernetes blogpost global WavenetPrasada Jayasinghe - Global Wavenet     

     Written by:’

      Prasad Jayasinghe,

     Tech Lead, Global Wavenet

 Prerequisites

· Bitbucket Account

· Container Repository Account (Nexus)

· Kubernetes Cluster

Commands

#git add
#git commit -m “Small Description”
#git push
CICD 2

Select the Pipelines tab.

CICD 3

CICD 4

Then it will allow us to select some predefined build templates. But don’t worry, your technology not there, still we can write build script to build the image.

CICD 5

Here we can write the custom build scripts.

CICD 6

I’m using Dockerfile to build the docker image from the committed code, then the build image push to Nexus repository. Then by using yaml file, I’m deploying the build image on  the Kubernetes cluster.

Dockerfile

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=/target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [“java”,”-jar”,”/app.jar”]

deployment.yml fiel

apiVersion: v1
kind: Service
metadata:
  name: coursework-app
spec:
  selector:
    app: coursework-app
  ports:
  - port: 8080
    targetPort: 8080
  type: LoadBalancer  
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coursework-app
spec:
  selector:
    matchLabels:
      app: coursework-app
  template:
    metadata:
      labels:
        app: coursework-app
    spec:
      imagePullSecrets:
        - name: <tag name>
      containers:
      - name: coursework-app
        image: <image url Eg: username:image_name:tag>
        ports:
        - containerPort: 8080

bitbucket-pipeline.yml

image: maven:3.6.3
pipelines:
  default:
    - step:
        name: Build and Push Wavenet Repository
        script:
          - mvn clean install
          - docker login test.domain.com --username $NEXUS_USERNAME --password $NEXUX_PASSWORD
          - docker build -t <username:image_name:tag> .
          - docker push <username:image_name:tag>
        services:
          - docker
    - step:
        name: Deploy to Kubernetes
        deployment: production
        script:
          - pipe: atlassian/google-gke-kubectl-run:1.3.1
            variables:
              KEY_FILE: $KUBE_CONFIG
              PROJECT: "wn-crs"
              COMPUTE_ZONE: "us-central1-c"
              CLUSTER_NAME: "cluster-1"
              KUBECTL_COMMAND: "apply"
              RESOURCE_PATH: "deployment.yml

This bitbucket-deployment.yml file mainly consist of 2 main sections:

1) Build

2) Deploy

Under this section, the code will get build and create the Docker image. Then image push to Nexus repository

- mvn clean install- docker login test.domain.com — username $NEXUS_USERNAME — password $NEXUX_PASSWORD- docker build -t <username:image_name:tag> .- docker push <username:image_name:tag>

Under this section by using the pushed image, the script will deploy on the Kubernetes cluster.

- step:
name: Deploy to Kubernetes
deployment: productionscript:
- pipe: atlassian/google-gke-kubectl-run:1.3.1variables:
KEY_FILE: $KUBE_CONFIG
PROJECT: “wn-crs”
COMPUTE_ZONE: “us-central1-c”
CLUSTER_NAME: “cluster-1”
KUBECTL_COMMAND: “apply”
RESOURCE_PATH: “deployment.yml

Also in this build script, we can use some variables to maintain the security eg : provide login details.

Select Repository Settings -> Under pipelines section we can have the Repository variables.

CICD 7

  Image for post

Environment Variable Details

NEXUS_USERNAME and NEXUS_PASSWORS

These two variables are contained login credentials for nexus repository. This is fully straight forward and we can user separate account user name and password.

KUBE_CONFIG

This variable contains login credentials for Kubernetes Cluster.

Generate KUBE_CONGIG value

First, you need to create the  service account in Google Cloud

Login to google cloud console -> go to IAM and Admin section -> Select Service account

CICD 8

Once you enter required details then it will ask you for Access permission.

There you can select “Kubernetes Engine Admin”

CICD 9

Then click on done. Once it is done, we have to generate the key file.

To do that click on 3 the dots under “Action” section

CICD 10

Then select the JSON and click the create link.

CICD 11

Then it will download a key file. To user this key file as KUBE_CONFIG variable we have convert it in to base64 format

Linux

base64 -w 0 < my_ssh_key

Mac

base64 < my_ssh_key

Generated Key file

CICD 12

Now we can user this key in KUBE_CONFIG variable.

Once every thing is done. Then we can run our build scripts.

Build Script is running

CICD 13

Bitbucket pipeline run without any issue

CICD 14

Results

Build image pushed to Repository

CICD 15

Pod is created on Kubernetes cluster:

CICD 16

End.

 

READ MORE

Select the tool to maximise Cloud strategyCloud ahasa webinar


More from our blog

See all posts