應對敏捷開發的需求,對CI(持續集成))/CD(持續交付)的提出了更高的標準,今天來討論下,如何基於開源組件(gitlab/jenkins/harbor/kubernetes)使用CI/CD,賦能團隊的開發、運維。node
組件名稱 | 版本 | 備註 |
---|---|---|
kubernetes | v1.15.3 | 10.0.0.182:6443 |
jenkins | 2.176.2 | 集羣內部署/ namespace: devops |
gitlab | 11.8 | 主機部署 |
harbor | v1.7.4 | docker-compose部署 |
注:git
注: 本文主要說明下jenkins的部署及配置,其餘組件若是你部署有問題,歡迎留言。github
說明:docker
nodeName ipaddress
, ipaddress 請確認其爲一個有效的ip./var/jenkins_home
是直接掛載到host_path, 若是你有條件,建議替換爲共享存儲。jenkins/jenkins:lts-alpine
推送至本身的內網鏡像倉庫。jenkins.dev.hanker.net
, 改成有效的域名地址,或是你也能夠經過NodePort
的形式聲明 service,就能夠直接經過ip:port
的形式訪問jenkins了。apiVersion: v1
kind: Namespace
metadata:
name: devops
# Deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: jenkins
namespace: devops
spec:
replicas: 1
revisionHistoryLimit: 3
template:
metadata:
labels:
app: jenkins
spec:
nodeName: 1.1.1.1
serviceAccountName: jenkins-admin
containers:
- image: jenkins/jenkins:lts-alpine
imagePullPolicy: IfNotPresent
name: jenkins
volumeMounts:
- name: jenkins-volume
mountPath: /var/jenkins_home
- name: jenkins-localtime
mountPath: /etc/localtime
env:
- name: JAVA_OPTS
value: '-Xms256m -Xmx1024m -Duser.timezone=Asia/Shanghai'
- name: TRY_UPGRADE_IF_NO_MARKER
value: 'true'
ports:
- name: http
containerPort: 8080
- name: agent
containerPort: 50000
resources:
requests:
cpu: 1000m
memory: 1Gi
limits:
cpu: 1200m
memory: 2Gi
volumes:
- name: jenkins-localtime
hostPath:
path: /etc/localtime
- name: jenkins-volume
hostPath:
path: /home/jenkins/jenkins_home
複製代碼
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-service
namespace: devops
spec:
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
- port: 50000
targetPort: 50000
name: agent
selector:
app: jenkins
複製代碼
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: jenkins
name: jenkins-admin
namespace: devops
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: jenkins-rbac
namespace: devops
rules:
- apiGroups: ["","extensions","app"]
resources: ["pods","pods/exec","deployments","replicasets"]
verbs: ["get","list","watch","create","update","patch","delete"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: jenkins-admin
namespace: devops
labels:
k8s-app: jenkins
subjects:
- kind: ServiceAccount
name: jenkins-admin
namespace: devops
roleRef:
kind: ClusterRole
name: jenkins-rbac
apiGroup: rbac.authorization.k8s.io
複製代碼
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: jenkins-ingress
namespace: devops
spec:
rules:
- host: jenkins.dev.hanker.net
http:
paths:
- backend:
serviceName: jenkins-service
servicePort: 8080
path: /
複製代碼
$ pwd
$ /home/jenkins_deploy
$ kubectl apply -f *.yaml
複製代碼
[root@node0 jenkins_deploy]# kubectl -n devops get deployment jenkins
NAME READY UP-TO-DATE AVAILABLE AGE
jenkins 1/1 1 1 51d
[root@node0 jenkins_deploy]#
複製代碼
注: 步驟1 聲明的域名 jenkins.dev.hanker.net
已經解析至ingress,故可直接訪問; 若是你也想經過自定義域名訪問jenkins,麻請解析至正確的ingress服務節點,便可。api
操做指引: 【Manage Jenkins】 -> 【Manage Plugins】緩存
你應該能夠經過相似的指令獲取jenkins-master的密碼bash
$ kubectl -n devops exec jenkins-pod-name cat /var/jenkins_home/secrets/initialAdminPassword
複製代碼
操做指引: 【Manage Jenkins】->【Configure System】網絡
圖中標註:app
圖中標註:運維