Setting up Jenkins on Kubernetes Engine
How to set up Jenkins on Google Kubernetes Engine to help orchestrate your software delivery pipeline.
Objectives
- Creating a Kubernetes cluster with Kubernetes Engine.
- Creating a Jenkins deployment and services.
- Connecting to Jenkins.
$ history
1 gcloud config set compute/zone us-east1-d
2 git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes.git
3 cd continuous-deployment-on-kubernetes/
4 ls -l
5 gcloud container clusters create jenkins-cd --num-nodes 2 --scopes "https://www.googleapis.com/auth/projecthosting,cloud-platform" 6 gcloud container clusters get-credentials jenkins-cd
7 kubectl cluster-info
8 helm repo add jenkins https://charts.jenkins.io
9 helm repo update
10 helm upgrade --install -f jenkins/values.yaml myjenkins jenkins/jenkins
11 kubectl get pods
12 kubectl get replicasets
13 kubectl get replicasets -A
14 echo http://127.0.0.1:8080
15 kubectl --namespace default port-forward svc/myjenkins 8080:8080 >> /dev/null &
16 kubectl get svc
17 kubectl exec --namespace default -it svc/myjenkins -c jenkins -- /bin/cat /run/secrets/additional/chart-admin-password && echo
Set the default Compute Engine zone to us-east1-d:
gcloud config set compute/zone us-east1-d
Clone the sample code:
git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes.git
cd continuous-deployment-on-kubernetes
Creating a Kubernetes cluster
Now you'll use the Kubernetes Engine to create and manage your Kubernetes cluster.
Next, provision a Kubernetes cluster using Kubernetes Engine. This step can take several minutes to complete:
gcloud container clusters create jenkins-cd \
--num-nodes 2 \
--scopes "https://www.googleapis.com/auth/projecthosting,cloud-platform"
The extra scopes enable Jenkins to access Cloud Source Repositories and Google Container Registry.
Confirm that your cluster is running:
gcloud container clusters list
student_04_a0019c906262@cloudshell:~ (qwiklabs-gcp-02-8a889eaabc18)$ gcloud container clusters list
NAME: jenkins-cd
LOCATION: us-east1-d
MASTER_VERSION: 1.27.2-gke.1200
MASTER_IP: 34.139.224.11
MACHINE_TYPE: e2-medium
NODE_VERSION: 1.27.2-gke.1200
NUM_NODES:
STATUS: PROVISIONING
student_04_a0019c906262@cloudshell:~ (qwiklabs-gcp-02-8a889eaabc18)$
Get the credentials for your cluster. Kubernetes Engine uses these credentials to access your newly provisioned cluster.
gcloud container clusters get-credentials jenkins-cd
Confirm that you can connect to your cluster:
kubectl cluster-info
Copied!
Example output: If the cluster is running, the URLs of where your Kubernetes components are accessible display:
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ gcloud container clusters get-credentials jenkins-cd
Fetching cluster endpoint and auth data.
kubeconfig entry generated for jenkins-cd.
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl cluster-info
Kubernetes control plane is running at https://34.139.224.11
GLBCDefaultBackend is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
KubeDNS is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
Task 2. Configure Helm
In this lab, you will use Helm to install Jenkins from the Charts repository. Helm is a package manager that makes it easy to configure and deploy Kubernetes applications. Your Cloud Shell will already have a recent, stable version of Helm pre-installed.
If curious, you can run helm version in Cloud Shell to check which version you are using and also ensure that Helm is installed.
Add Helm's jenkins chart repository:
helm repo add jenkins https://charts.jenkins.io
Copied!
Update the repo to ensure you get the latest list of charts:
helm repo update
Copied!
Task 3. Configure and install Jenkins
You will use a custom values file to add the Google Cloud specific plugin necessary to use service account credentials to reach your Cloud Source Repository.
Use the Helm CLI to deploy the chart with your configuration set:
helm upgrade --install -f jenkins/values.yaml myjenkins jenkins/jenkins
Copied!
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.
Configure and Install Jenkins
Once that command completes ensure the Jenkins pod goes to the Running state and the container is in the READY state. This may take about 2 minutes:
kubectl get pods
Copied!
Example output:
NAME READY STATUS RESTARTS AGE
myjenkins-0 2/2 Running 0 1m
Run the following command to setup port forwarding to the Jenkins UI from the Cloud Shell:
echo http://127.0.0.1:8080
kubectl --namespace default port-forward svc/myjenkins 8080:8080 >> /dev/null &
Copied!
Now, check that the Jenkins Service was created properly:
kubectl get svc
Copied!
Example output:
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myjenkins 10.35.249.67 8080/TCP 3h
myjenkins-agent 10.35.248.1 50000/TCP 3h
kubernetes 10.35.240.1 443/TCP 9h
We are using the Kubernetes Plugin so that our builder nodes will be automatically launched as necessary when the Jenkins master requests them. Upon completion of their work, they will automatically be turned down and their resources added back to the clusters resource pool.
Notice that this service exposes ports 8080 and 50000 for any pods that match the selector. This will expose the Jenkins web UI and builder/agent registration ports within the Kubernetes cluster.
Additionally, the jenkins-ui service is exposed using a ClusterIP so that it is not accessible from outside the cluster.
Task 4. Connect to Jenkins
The Jenkins chart will automatically create an admin password for you. To retrieve it, run:
kubectl exec --namespace default -it svc/myjenkins -c jenkins -- /bin/cat /run/secrets/additional/chart-admin-password && echo
Copied!
To get to the Jenkins user interface, click on the Web Preview button in cloud shell, then click Preview on port 8080:
Expanded Web preview dropdown menu with Preview on port 8080 option highlighted
You should now be able to log in with the username admin and your auto-generated password.
You may also be automatically logged in as well.
You now have Jenkins set up in your Kubernetes cluster!
C:\Users\Ketan.Patel\Desktop\2023-08-01 21_28_04-Dashboard [Jenkins].png
student_04_a0019c906262@cloudshell:~ (qwiklabs-gcp-02-8a889eaabc18)$ git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes.git
Cloning into 'continuous-deployment-on-kubernetes'...
remote: Enumerating objects: 971, done.
remote: Counting objects: 100% (147/147), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 971 (delta 133), reused 122 (delta 122), pack-reused 824
Receiving objects: 100% (971/971), 1.91 MiB | 25.03 MiB/s, done.
Resolving deltas: 100% (485/485), done.
student_04_a0019c906262@cloudshell:~ (qwiklabs-gcp-02-8a889eaabc18)$ cd continuous-deployment-on-kubernetes/
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ ls -l
total 64
-rw-r--r-- 1 student_04_a0019c906262 student_04_a0019c906262 1642 Aug 2 04:10 CONTRIBUTING.md
drwxr-xr-x 3 student_04_a0019c906262 student_04_a0019c906262 4096 Aug 2 04:10 docs
drwxr-xr-x 2 student_04_a0019c906262 student_04_a0019c906262 4096 Aug 2 04:10 jenkins
-rw-r--r-- 1 student_04_a0019c906262 student_04_a0019c906262 11324 Aug 2 04:10 LICENSE
-rw-r--r-- 1 student_04_a0019c906262 student_04_a0019c906262 30618 Aug 2 04:10 README.md
drwxr-xr-x 4 student_04_a0019c906262 student_04_a0019c906262 4096 Aug 2 04:10 sample-app
drwxr-xr-x 5 student_04_a0019c906262 student_04_a0019c906262 4096 Aug 2 04:10 tests
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ gcloud container clusters create jenkins-cd \
--num-nodes 2 \
--scopes "https://www.googleapis.com/auth/projecthosting,cloud-platform"
Default change: VPC-native is the default mode during cluster creation for versions greater than 1.21.0-gke.1500. To create advanced routes based clusters, please pass the `--no-enable-ip-alias` flag
Default change: During creation of nodepools or autoscaling configuration changes for cluster versions greater than 1.24.1-gke.800 a default location policy is applied. For Spot and PVM it defaults to ANY, and for all other VM kinds a BALANCED policy is used. To change the default values use the `--location-policy` flag.
Note: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
Creating cluster jenkins-cd in us-east1-d... Cluster is being health-checked (master is healthy)...working
Creating cluster jenkins-cd in us-east1-d... Cluster is being health-checked (master is healthy)...working.
Creating cluster jenkins-cd in us-east1-d... Cluster is being health-checked (master is healthy)...done.
Created [https://container.googleapis.com/v1/projects/qwiklabs-gcp-02-8a889eaabc18/zones/us-east1-d/clusters/jenkins-cd].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-east1-d/jenkins-cd?project=qwiklabs-gcp-02-8a889eaabc18
kubeconfig entry generated for jenkins-cd.
NAME: jenkins-cd
LOCATION: us-east1-d
MASTER_VERSION: 1.27.2-gke.1200
MASTER_IP: 34.139.224.11
MACHINE_TYPE: e2-medium
NODE_VERSION: 1.27.2-gke.1200
NUM_NODES: 2
STATUS: RUNNING
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ gcloud container clusters get-credentials jenkins-cd
Fetching cluster endpoint and auth data.
kubeconfig entry generated for jenkins-cd.
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl cluster-info
Kubernetes control plane is running at https://34.139.224.11
GLBCDefaultBackend is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
KubeDNS is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://34.139.224.11/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ helm repo add jenkins https://charts.jenkins.io^C
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ helm repo add jenkins https://charts.jenkins.io
"jenkins" has been added to your repositories
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "jenkins" chart repository
Update Complete. ⎈Happy Helming!⎈
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ helm upgrade --install -f jenkins/values.yaml myjenkins jenkins/jenkins
Release "myjenkins" does not exist. Installing it now.
NAME: myjenkins
LAST DEPLOYED: Wed Aug 2 04:21:37 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get your 'admin' user password by running:
kubectl exec --namespace default -it svc/myjenkins -c jenkins -- /bin/cat /run/secrets/additional/chart-admin-password && echo
2. Get the Jenkins URL to visit by running these commands in the same shell:
echo http://127.0.0.1:8080
kubectl --namespace default port-forward svc/myjenkins 8080:8080
3. Login with the password from step 1 and the username: admin
4. Configure security realm and authorization strategy
5. Use Jenkins Configuration as Code by specifying configScripts in your values.yaml file, see documentation: http://127.0.0.1:8080/configuration-as-code and examples: https://github.com/jenkinsci/configuration-as-code-plugin/tree/master/demos
For more information on running Jenkins on Kubernetes, visit:
https://cloud.google.com/solutions/jenkins-on-container-engine
For more information about Jenkins Configuration as Code, visit:
https://jenkins.io/projects/jcasc/
NOTE: Consider using a custom image with pre-installed plugins
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
myjenkins-0 2/2 Running 0 3m3s
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl get replicasets
No resources found in default namespace.
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl get replicasets -A
NAMESPACE NAME DESIRED CURRENT READY AGE
gmp-system gmp-operator-6f6c647c7f 1 1 1 10m
gmp-system rule-evaluator-5d6c9f4645 0 0 0 10m
gmp-system rule-evaluator-7f5dc755cb 1 1 1 9m1s
kube-system event-exporter-gke-7bf6c99dcb 1 1 1 10m
kube-system konnectivity-agent-autoscaler-5d9dbcc6d8 1 1 1 10m
kube-system kube-dns-865c4fb86d 2 2 2 10m
kube-system kube-dns-autoscaler-84b8db4dc7 1 1 1 10m
)$ student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ echo http://127.0.0.1:8080kubectl --namespace default port-forward svc/myjenkins 8080:8080 >> /dev/null &
http://127.0.0.1:8080
[1] 1248
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 11m
myjenkins ClusterIP 10.100.2.210 <none> 8080/TCP 4m4s
myjenkins-agent ClusterIP 10.100.8.55 <none> 50000/TCP 4m4s
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ kubectl exec --namespace default -it svc/myjenkins -c jenkins -- /bin/cat /run/secrets/additional/chart-admin-password && echo
f5yATnNHYSC9AsMroOTce6
To get to the Jenkins user interface, click on the Web Preview button in cloud shell, then click Preview on port 8080:
student_04_a0019c906262@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-8a889eaabc18)$ history 1 gcloud config set compute/zone us-east1-d
2 git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes.git
3 cd continuous-deployment-on-kubernetes/
4 ls -l
5 gcloud container clusters create jenkins-cd --num-nodes 2 --scopes "https://www.googleapis.com/auth/projecthosting,cloud-platform" 6 gcloud container clusters get-credentials jenkins-cd
7 kubectl cluster-info
8 helm repo add jenkins https://charts.jenkins.io
9 helm repo update
10 helm upgrade --install -f jenkins/values.yaml myjenkins jenkins/jenkins
11 kubectl get pods
12 kubectl get replicasets
13 kubectl get replicasets -A
14 echo http://127.0.0.1:8080
15 kubectl --namespace default port-forward svc/myjenkins 8080:8080 >> /dev/null &
16 kubectl get svc
17 kubectl exec --namespace default -it svc/myjenkins -c jenkins -- /bin/cat /run/secrets/additional/chart-admin-password && echo
No comments:
Post a Comment