|
| 1 | +CI/CD workflow using Jenkins and Bitbucket |
| 2 | +------------------------------------------- |
| 3 | + |
| 4 | +Use your personal google account for this experiment. |
| 5 | + |
| 6 | +First, pull the latest code from CloudComputing git repo. |
| 7 | + |
| 8 | +cd CloudComputing |
| 9 | +git pull origin master |
| 10 | + |
| 11 | +Setup |
| 12 | +------ |
| 13 | +In this experiment, you will setup continuous integration, continuous delivery |
| 14 | +using Jenkins. You will configure your BitBucket account with a webhook |
| 15 | +to trigger a Jenkins job. The Jenkins job will deploy WordPress helm chart |
| 16 | +on your Kubernetes cluster in Google cloud (GKE). |
| 17 | + |
| 18 | +You will need to create your own GKE cluster (steps given below). |
| 19 | +You will install Jenkins in your cluster. |
| 20 | + |
| 21 | + |
| 22 | +1) For this assignment, create a new **public** Bitbucket repository named "assignment4" |
| 23 | + - Clone it on your machine |
| 24 | + - git clone https://<your-bitbucket-username>/assignment4.git |
| 25 | + |
| 26 | +2) Setup following files in your assignment4 folder: |
| 27 | + - cp -r CloudComputing/CICD/wp-chart assignment4/. |
| 28 | + - cp CloudComputing/CICD/containerized/jenkins_on_gke/build.sh assignment4/. |
| 29 | + - cp CloudComputing/CICD/containerized/jenkins_on_gke/deploy.sh assignment4/. |
| 30 | + - cd assignment4 |
| 31 | + - git add wp-chart |
| 32 | + - git add build.sh |
| 33 | + - git add deploy.sh |
| 34 | + - git commit |
| 35 | + - enter some commit message |
| 36 | + - git push origin master |
| 37 | + |
| 38 | + |
| 39 | +3) Install Google Cloud CLI on your machine |
| 40 | + - Follow the instructions from: https://cloud.google.com/sdk/docs/install |
| 41 | + |
| 42 | +4) Configure authentication for gcloud CLI. We need this step to create GKE cluster |
| 43 | + - gcloud auth login --> Follow the prompts (you will have to open browser window and paste the generated link, |
| 44 | + then paste the generated code in the verification field in your console.) |
| 45 | + - Create Project in Google Cloud Console --> Note down the Project ID. Remember that Project ID is different than the Project's Name. |
| 46 | + You will need Project ID in subsequent steps. |
| 47 | + - Set environment variables (Linux/MacOS use export command for this; for windows use set command) |
| 48 | + - export PROJECT_ID=<Project-ID-from-previous-step> |
| 49 | + - export CLOUDSDK_COMPUTE_ZONE=us-central1-b |
| 50 | + - gcloud config set project ${PROJECT_ID} |
| 51 | + |
| 52 | +5) Enable Kubernetes Engine API for your project |
| 53 | + - https://console.cloud.google.com/apis/library/browse?filter=category:compute&project=<your-project-name> |
| 54 | + |
| 55 | +6) Create GKE cluster that will run your WordPress Helm chart |
| 56 | + - ./create-gke-cluster.sh <project-id> <cluster-name> |
| 57 | + - NOTE: |
| 58 | + - create-gke-cluster.sh is available in CloudComputing/CICD/containerized |
| 59 | + - Capital letters are not allowed in cluster name. |
| 60 | + |
| 61 | +7) Once the cluster is created, you can open traffic to the ports on your cluster VM by following these steps: |
| 62 | + - Go to VPC Network -> Firewall -> Select the rule that has following name: |
| 63 | + gke-cluster_name-<string of letters+numbers>-all |
| 64 | + -> Hit Edit |
| 65 | + -> In the Source IP ranges, enter: 0.0.0.0/0 |
| 66 | + -> Hit Save |
| 67 | + |
| 68 | +8) Setup Jenkins |
| 69 | + 8.1) Install Helm |
| 70 | + 8.2) helm repo add bitnami https://charts.bitnami.com/bitnami |
| 71 | + 8.3) helm install jenkins jenkins-13.0.0.tgz --set image.registry=gcr.io --set image.repository=cloudark-kubeplus/updated-bitnami-jenkins --set image.tag="latest" --set jenkinsUser=<your-username> --set jenkinsPassword=<your-password> --set service.type=NodePort |
| 72 | + 8.4) kubectl get nodes -o=jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}' |
| 73 | + 8.5) kubectl get svc jenkins -o=jsonpath='{.spec.ports[?(@.name=="http")].nodePort}' |
| 74 | + 8.6) Grant jenkins ServiceAccount cluster-admin permissions: kubectl create -f jenkins-rbac.yaml |
| 75 | + 8.7) Jenkins URL: http://<IP-from-8.4>:<Port-from-8.5> |
| 76 | + 8.8) Install the Post build task Plugin and Bitbucket Plugin |
| 77 | + - Bitbucket plugin will be used to clone Bitbucket repo |
| 78 | + - Post build task plugin will be used to perform build and deploy tasks |
| 79 | + Jenkins -> Manage Jenkins -> Manage Plugins |
| 80 | +- In Available tab, search "Post build task" and select |
| 81 | +- In Available tab, search "Bitbucket" and select |
| 82 | + (Note: There are several Bitbucket plugins -- you want to choose the one whose |
| 83 | + name contains only the word "Bitbucket" and nothing else) |
| 84 | +- Choose Install without restarting option |
| 85 | + 8.9) Set up a Jenkins Job |
| 86 | + 8.9.1) Jenkins -> New Item -> Give name -> Select Freestyle project |
| 87 | + -> Source Code Management -> Git -> Give your assignment 4 Bitbucket Repository URL (using https protocol) |
| 88 | + -> Branches to build: */main |
| 89 | + 8.9.2) Build Triggers |
| 90 | + -> Choose "Build when a change is pushed to BitBucket" |
| 91 | + 8.9.3) Buid Section -> Add a build step -> Execute Shell script |
| 92 | + -> In the Execute shell Command section: |
| 93 | + - Add contents of build.sh |
| 94 | + 8.9.4) In Post-build Actions |
| 95 | + -> Add post-build action -> Post build task |
| 96 | + -> Log text "SUCCESS" -> Operation "-- OR --" |
| 97 | + -> In the Tasks -> Script section: |
| 98 | + - Add contents of deploy.sh |
| 99 | + - NOTE: (modify the deploy.sh steps to use your GCP Project ID and your cluster name. |
| 100 | + 8.9.5) Save |
| 101 | + 8.9.6) *** Manually trigger the Jenkins pipeline *** |
| 102 | + - Build with Parameters |
| 103 | + - Choose the json file that you downloaded in step 8. |
| 104 | + - Hit Build |
| 105 | + - This will upload the file to your Jenkins instance and will be available when |
| 106 | + you do the CI/CD experiment by triggering builds from BitBucket. |
| 107 | + |
| 108 | +9) Add a WebHook to your Bitbucket repository |
| 109 | + |
| 110 | + Go to your Bitbucket repository -> Settings -> Webhooks -> Add webhook |
| 111 | + - <Jenkins-url>/bitbucket-hook/ |
| 112 | + (Note: The trailing slash '/' is important. Don't forget that!!) |
| 113 | + - Make Status as "Active" |
| 114 | + - Check "Skip certificate verification" |
| 115 | + - Triggers -> Choose from a full list of triggers |
| 116 | + - Select following: |
| 117 | + - Repository->Push, Pull Request->Created, Updated, Comment created, Comment updated, Comment deleted |
| 118 | + Issue->Created, Issue->Updated, Issue->Comment Created |
| 119 | + |
| 120 | + |
| 121 | +CI/CD Experiment: |
| 122 | +----------------- |
| 123 | +1) Open the Jenkins URL in your browser and navigate to the job |
| 124 | + |
| 125 | +2) On your machine make changes to the wp-chart and push the code: |
| 126 | + - Modify something in the YAML file (say, make replicaCount=2) |
| 127 | + - git add |
| 128 | + - git commit |
| 129 | + - git push origin master |
| 130 | + |
| 131 | +3) If your CICD setup has been done properly, you should see a new build triggered (visible under "Build History") |
| 132 | + - Select the Build |
| 133 | + - Go to Console Output |
| 134 | + - Verify that the build and deploy steps have been executed |
| 135 | + |
| 136 | +4) On your Kubernetes cluster, check if the WordPress stack has been deployed or not |
| 137 | + - From your machine run |
| 138 | + - gcloud auth activate-service-account --key-file <JSON key file from step 8> |
| 139 | + - gcloud config set project <Your project ID> |
| 140 | + - gcloud container clusters get-credentials <Your cluster name> --zone=us-central1-b |
| 141 | + |
| 142 | + Then do: |
| 143 | + - kubectl get pods -A |
| 144 | + - Verify that the WordPress Pods are running |
| 145 | + - Get the ExternalIP of the VM of your cluster |
| 146 | + -> Google Cloud Console -> Compute Engine -> VM Instances |
| 147 | + - curl -v http://<External IP>:30005/ |
| 148 | + - Open in browser: http://<External IP>:30005 |
| 149 | + |
| 150 | +5) Repeat steps 2-3-4 with different changes |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +Clean up: |
| 155 | +--------- |
| 156 | +From Google Cloud Console: |
| 157 | +- Delete Kubernetes Cluster |
| 158 | + |
| 159 | + |
| 160 | +Troubleshooting: |
| 161 | +---------------- |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +Reference: |
| 166 | +----------- |
0 commit comments