Deploy to GKE Cluster from Jenkins

1_tGI3umgdq-9fd_7gh2f5rg.png

In this article we would revisit to see:

- Pre-requisites to configure on the kubernetes cluster deployed on GKE to connect from Jenkins

- Configuration/Plugins Required on the Jenkins

- How we can execute the simple manifest file ( Kind: Deployment/ Pod)

Step 1:

1.1. Setup the necessary environment variables

                export PROJECT=$(gcloud info --format='value(config.project)')
export CLUSTER=<YOUR_CLUSTER_NAME>
export ZONE=<YOUR_PROJECTS_ZONE>
export SA=<YOUR_GCP_SA_NAME>
export SA_EMAIL=${SA}@${PROJECT}.iam.gserviceaccount.com
            

1.2. Create a service account using the Google Cloud SDK , this is the service account that is used when you login into your cluster on the GCP. Alternatively you can also create a new SA different from your default login as provide necessary roles.

                gcloud iam service-accounts create $SA
            

1.3. Creation of Custom IAM Role with minimum permissions

                gcloud iam roles create gke_deployer --project $PROJECT --file IAMRole.yaml
            

Refer to https://github.com/santoshkothapalli/s3/blob/main/IAMRole.yaml

1.4. Assign the Role created in 1.3 to the service account

                gcloud projects add-iam-policy-binding $PROJECT \
--member serviceAccount:$SA_EMAIL \
--role projects/$PROJECT/roles/gke_deployer
            

1.5. Download the key , this is required to be uploaded in the Jenkins Google Kubernetes Plugin to connect to the GKE Cluster

                gcloud iam service-accounts keys create ~/jenkins-gke-key.json --iam-account $SA_EMAIL
            

Step2 : There are various plugins provided to connect to Kubernetes clusters , we will leverage the Google Kubernetes Engine Plugin

2.1 Installation of Plugin

Navigate to Jenkins Home Page and then to Manage Jenkins -> Manage Plugins -> Available

Search for Google Kubernetes Engine Plugin and install the plugin.

2.2 Configuration of Plugin

Once the Plugin is installed navigate to Manage Jenkins -> Manage Credentials -> Store (Name would depend upon Jenkins configuration)

Add the Credential with the name , note the name as this will be used in your pipeline , upload the key downloaded in step 1.5 . Refer to below screen shot

Sample Pipeline script which is self explanatory is available at below path which would create a pod with nginx image on your cluster.

https://github.com/santoshkothapalli/s3/blob/main/env/Jenkinsfile_gke1


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Stats
12

Influence

843

Total Hits

1

Posts

Discussed tools