Giaosucan's blog - Chia sẻ kiến thức theo cách bá đạo

Ticker

20/recent/ticker-posts

Hướng dẫn cài đặt Jenkins trên Kubernetes cluster

Jenkins Scalability

Các anh em thiện lành hay cài Jenkins trên Ubuntu bằng cách down bộ cài Jenkins về rồi cài theo guideline là có thể launch Jenkins trong vòng một nốt nhạc. Tuy nhiên đối với hệ thống lớn, có tới hàng nghìn job chạy, cần phải nghiên cứu cách scaling Jenkins

Có nhiều cách để scale Jenkins, bài viết này hướng dẫn các scale Jenkins trên Kubernetes
Image result for jenkins scale on kubernetes

Chuẩn bị

Bạn cần Google account GCE, có thể xài free trial 1 năm được
Máy Linux Ubuntu hoặc centos

Cài đặt Jenkins lên Kubernetes cluster

Tạo 1 projects trên GCE, thực hiện command dưới để tạo 1 cluster tên jenkins-cd with 2 nodes. 

gcloud container clusters create jenkins-cd  --machine-type n1-standard-2 --num-nodes 2
 
Để tiết kiệm chi phí có thể resize cluster về 0 nếu ko xài đến
gcloud container clusters resize $CLUSTER_NAME --size=0
Click connect to để lấy command kết nối với cluster
Kiểm tra cluster đã chạy chưa
Trong bài viết này, tôi đã sử dụng helm. Helm giúp bạn quản lý các ứng dụng Kubernetes Download và cài đặt Helm binary:
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.9.1-linux-amd64.tar.gz
Unzip the file
tar zxfv helm-v2.9.1-linux-amd64.tar.gz cp linux-amd64/helm /usr/bin/
Add yourself as a cluster administrator in the cluster's RBAC so that you can give Jenkins permissions in the cluster:
kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin \ --user=$(gcloud config get-value account)
Grant Tiller, the server side of Helm, the cluster-admin role in your cluster:
kubectl create serviceaccount tiller --namespace kube-system kubectl create clusterrolebinding tiller-admin-binding --clusterrole=cluster-admin \ --serviceaccount=kube-system:tiller
  1. Initialize Helm để đảm bảo Tiller được cài đặt trong hệ thống

./helm init --service-account=tiller ./helm repo update
  1. Chạy command dưới để kiểm tra helm đã cài đặt OK chưa
helm versionhttps://perzoinc.atlassian.net/wiki/download/attachments/572949388/image2019-5-10_11-24-37.png?version=1&modificationDate=1557462281058&cacheVersion=1&api=v2
7. Dùng Helm để deploy Jenkins lên cluster
helm install -n cd stable/jenkins -f values.yaml --version 0.16.6 --wait
can get values.yaml here

git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes
continuous-deployment-on-kubernetes/jenkins/value.xml
8. Chạy command dưới để thay danh sách các pod đang chạy
kubectl get pods

Truy cập Jenkins từ bên ngoài

1. Mặc định Jenkins ko thể truy cập từ bên ngoài, bạn cần expose service
Get the deployments (cd - jenkins)
2. Expose the service load balancer

kubectl expose deployment cd-jenkins --type=LoadBalancer --name=my-service
my-service is load balancer

Bạn sẽ thấy my-service load balancer expose external ip như trên

Login with username, password provided by cmd
printf $(kubectl get secret cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo

Cấu hình kubernetes plugin in Jenkins



Việc đầu tiên là cái Kubernetes plugin vào Jenkins
 Chọn manage plugin → manage node, set the executors to zero 0 to để ngăn master node tự execute job
Chọn manage jenkin → configuration system, nhập thông tin như dưới
Kubernetes URL là endpoint của cluster, Jenkins URL là địa chỉ của Jenkins Master server.
|
At Kubernetes Pod template, nhập thông tin như ở dưới
Tạo một job kiểu free style, chỉ định Jenkins slave nào sẽ execute job
Sau khi execute job bạn sẽ thấy, Jenkins master chỉ định con slave để thực hiện job
Chạy command
kubectl get pods -w
Bạn sẽ thấy Jenkins slave được tự động launch và terminate sau khi run job

Đăng nhận xét

0 Nhận xét