Github上有一个1.5k star的项目:
https://github.com/jenkinsci/kubernetes-plugin
上面提供了jenkins在kubernetes中容器化的部署方式,以及yaml文件,现在直接 git clone 下来。
[root@master ~]# cd /work/[root@master work]# git clone https://github.com/jenkinsci/kubernetes-plugin.git
克隆完之后进入到这个目录。
[root@master work]# cd kubernetes-plugin/src/main/kubernetes/[root@master kubernetes]# lsjenkins.yml service-account.yml
直接克隆下来的文件,可能在某些k8s版本上会报错,请根据报错提示修改;
腾讯云服务器新老用户优惠
下面的yaml在k8s v1.16.3版本上做过了测试。
可以看到有两个文件,service-account.yml 文件是创建 RBAC 授权相关的东西,这个不要动,主要看一下 jenkins.yml,需要修改一下,修改后的文件如下:
# jenkins---apiVersion: apps/v1kind: StatefulSetmetadata: name: jenkins labels: name: jenkinsspec: selector: matchLabels: name: jenkins serviceName: jenkins replicas: 1 updateStrategy: type: RollingUpdate template: metadata: name: jenkins labels: name: jenkins spec: terminationGracePeriodSeconds: 10 serviceAccountName: jenkins containers: - name: jenkins command: - /sbin/tini - /usr/local/bin/jenkins.sh - --prefix=/jenkins image: jenkins/jenkins:lts-alpine imagePullPolicy: Always ports: - containerPort: 8080 - containerPort: 50000 resources: limits: cpu: 1 memory: 1Gi requests: cpu: 0.5 memory: 500Mi env: - name: LIMITS_MEMORY valueFrom: resourceFieldRef: resource: limits.memory divisor: 1Mi - name: JAVA_OPTS # value: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -D--prefix=/jenkins volumeMounts: - name: jenkins-home mountPath: /var/jenkins_home livenessProbe: httpGet: path: /jenkins/login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 # ~2 minutes readinessProbe: httpGet: path: /jenkins/login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 securityContext: fsGroup: 1000 volumes: - hostPath: path: /data/jenkins name: jenkins-home---apiVersion: v1kind: Servicemetadata: name: jenkinsspec: selector: name: jenkins ports: - name: http port: 80 targetPort: 8080 protocol: TCP - name: agent port: 50000 protocol: TCP---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: jenkins annotations: nginx.ingress.kubernetes.io/ssl-redirect: "true" kubernetes.io/tls-acme: "true" # "413 Request Entity Too Large" uploading plugins, increase client_max_body_size nginx.ingress.kubernetes.io/proxy-body-size: 50m nginx.ingress.kubernetes.io/proxy-request-buffering: "off" # For nginx-ingress controller < 0.9.0.beta-18 ingress.kubernetes.io/ssl-redirect: "true" # "413 Request Entity Too Large" uploading plugins, increase client_max_body_size ingress.kubernetes.io/proxy-body-size: 50m ingress.kubernetes.io/proxy-request-buffering: "off"spec: rules: - http: paths: - path: /jenkins backend: serviceName: jenkins servicePort: 80 host: example.cn tls: - hosts: - example.cn secretName: litemall-admin-secret
注意到,上面yaml中主要修改ingress中的host为自己的域名,我的域名是在阿里备案过的域名(如果有国外的机器,不用备案也可以),而且申请了阿里的免费https证书,将证书配置到ingress-nginx中(通过创建一个secret,secret中包含了证书的信息);
还有需要修改ingress中的path为带前缀的:/jenkins
,因为对于入口网关:ingress-nginx
,有很多路径为/
的服务创建ingress
去暴露,为了区分,所以通过带前缀的方式。这样就需要在Statefulset的podTemplate中配置jenkins的参数--prefix=/jenkins
,这样Pod启动执行command:/sbin/tini -- /usr/local/bin/jenkins.sh --prefix=/jenkins
后,jenkins.sh
脚本中就会调用java -jar jenkins.war --prefix=/jenkins
将参数传入。
默认path
加了/jenkins
前缀后,同时对应的修改健康检查中的path
为/jenkins/login
;
另外,jenkins的数据目录需要做持久化,也就是/var/jenkins_home
,jenkins
所有的数据都是存在这个目录下面的。
如果没有公网域名,那就没必要创建Ingress,直接通过Service的NodePort方式去暴露,最后通过{k8s集群任一主机IP}:{nodePort端口}/jenkins
方式去访问。
最后顺便贴出Service Account
的配置,这里对github上的原有yaml没做修改:
# In GKE need to get RBAC permissions first with# kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin [--user=<user-name>|--group=<group-name>]---apiVersion: v1kind: ServiceAccountmetadata: name: jenkins---kind: RoleapiVersion: rbac.authorization.k8s.io/v1beta1metadata: name: jenkinsrules:- apiGroups: [""] resources: ["pods"] verbs: ["create","delete","get","list","patch","update","watch"]- apiGroups: [""] resources: ["pods/exec"] verbs: ["create","delete","get","list","patch","update","watch"]- apiGroups: [""] resources: ["pods/log"] verbs: ["get","list","watch"]- apiGroups: [""] resources: ["secrets"] verbs: ["get"]---apiVersion: rbac.authorization.k8s.io/v1beta1kind: RoleBindingmetadata: name: jenkinsroleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: jenkinssubjects:- kind: ServiceAccount name: jenkins
最后通过域名去访问:
在这里插入图片描述
阿里云服务器新老用户优惠
关注公众号,回复【docker】【k8s】【电子书】【ppt】,有更多资料供你学习!
更有5T技术资源大放送!包括但不限于:Golang,Linux,Python,Java,微服务,人工智能,前端,架构等等。由于资源链接失效,扫下面码后回复「2」,可免费获取!
扫码关注,精彩内容第一时间推给你
本文分享自微信公众号 - 我的小碗汤(mysmallsoup)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。