1. Tổng quan
Jenkins là một Automation Server mã nguồn mở, đóng vai trò trung tâm trong các quy trình CI/CD. Jenkins vận hành theo mô hình Controller Agent:
- Jenkins Controller: quản lý cấu hình, giao diện web, lưu job, plugin và điều phối pipeline.
- Jenkins Agents: thực thi các tác vụ build/test/deploy.
2. Prerequisites
Phần mềm
- JDK 11 hoặc JDK 17 (khuyến nghị)
- JDK 21 (hiện ở chế độ preview, có thể chưa ổn định với mọi plugin)
Phần cứng
- Tối thiểu: 256MB RAM, 1GB disk
- Khuyến nghị: 4GB RAM+, 50GB disk+
(Pipeline nhiều stage, nên tăng 8–16GB RAM)
3. Cài đặt Jenkins bằng Docker
3.1 Docker Compose
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:lts-jdk17
container_name: jenkins-controller
restart: unless-stopped
privileged: true
user: root
ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
networks:
- ci-net
volumes:
jenkins_home:
networks:
ci-net:
Lưu ý quan trọng:
Mount docker.sock giúp Jenkins chạy Docker build trực tiếp trên host.
Tuy nhiên, điều này đồng nghĩa Jenkins có quyền root trên host.
Trong môi trường production nên cân nhắc Hardening hoặc dùng Kubernetes agent thay thế.
3.2 Khởi chạy
docker-compose up -d
docker exec jenkins-controller cat /var/jenkins_home/secrets/initialAdminPassword
4. Cài đặt trên Windows
4.1 Các bước
- Cài JDK 17 (Temurin / Amazon Corretto).
- Thiết lập biến môi trường
JAVA_HOME. - Tải Jenkins LTS bản Windows Installer.
- Cài đặt Jenkins như Windows Service.
Port mặc định: 8080
4.2 Lưu ý
- Mật khẩu ban đầu:
C:\Program Files\Jenkins\secrets\initialAdminPassword - Nếu bị lỗi access thì kiểm tra Windows Firewall.
5. Cài đặt Jenkins trên macOS (Homebrew)
5.1 Cài đặt
brew install jenkins-lts
brew services start jenkins-lts
5.2 Quản lý
- Dừng Jenkins:
brew services stop jenkins-lts - Mật khẩu:
cat ~/.jenkins/secrets/initialAdminPassword
6. Cài đặt trên Linux
6.1 Ubuntu / Debian
sudo apt update && sudo apt install fontconfig openjdk-17-jre -y
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install jenkins -y
sudo systemctl enable --now jenkins
6.2 RHEL / CentOS / Rocky
sudo dnf install java-17-openjdk -y
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo dnf install jenkins -y
sudo systemctl enable --now jenkins
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
7. Triển khai Jenkins trên Kubernetes
jenkins-k8s.yaml
apiVersion: v1
kind: Namespace
metadata:
name: devops
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-pvc
namespace: devops
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins
namespace: devops
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
namespace: devops
spec:
replicas: 1
selector:
matchLabels:
app: jenkins-server
template:
metadata:
labels:
app: jenkins-server
spec:
serviceAccountName: jenkins
containers:
- name: jenkins
image: jenkins/jenkins:lts
ports:
- containerPort: 8080
- containerPort: 50000
volumeMounts:
- name: jenkins-storage
mountPath: /var/jenkins_home
volumes:
- name: jenkins-storage
persistentVolumeClaim:
claimName: jenkins-pvc
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-svc
namespace: devops
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 32000
selector:
app: jenkins-server
8. Reverse Proxy Nginx
upstream jenkins_backend {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name jenkins.example.com;
location / {
proxy_pass http://jenkins_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600;
proxy_buffering off;
}
}
9. Distributed Agents
9.1 Linux Agent (SSH)
- Tạo SSH key
- Copy vào
~/.ssh/authorized_keystrên Agent - Jenkins => Manage Nodes => SSH Launch Method
9.2 Windows Agent (JNLP)
- Jenkins => chọn Random Agent Port
- Tải
agent.jar - Chạy:
java -jar agent.jar -jnlpUrl <url>
10. Post Installation
- Unlock Jenkins bằng mật khẩu ban đầu
- Install suggested plugins
- Tạo Admin User
- Xác nhận Jenkins URL
- Cấu hình backup định kỳ khuyên dùng plugin ThinBackup hoặc Velero nếu chạy K8S






