k8s 部署 Jenkins
创建namespace
---
apiVersion: v1
kind: Namespace
metadata:
name: jenkins
创建PVC
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-pvc
namespace: jenkins
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Gi
storageClassName: nfs-csi
创建rbac
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins-master
namespace: jenkins
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: jenkins-master
rules:
- apiGroups: ["extensions", "apps"]
resources: ["deployments"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
- apiGroups: [""]
resources: ["services"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
- 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/v1
kind: ClusterRoleBinding
metadata:
name: jenkins-master
roleRef:
kind: ClusterRole
name: jenkins-master
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: jenkins-master
namespace: jenkins
创建deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
namespace: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
template:
metadata:
labels:
app: jenkins
spec:
serviceAccountName: jenkins-master
volumes:
- name: jenkins-store
persistentVolumeClaim:
claimName: jenkins-pvc
initContainers:
- name: fix-permissions
image: alpine
command: ["sh", "-c", "chown -R 1000:1000 /var/jenkins_home/"]
securityContext:
privileged: true
volumeMounts:
- name: jenkins-store
mountPath: /var/jenkins_home/
containers:
- name: jenkins
image: jenkins/jenkins:jdk17
volumeMounts:
- name: jenkins-store
mountPath: /var/jenkins_home/
imagePullPolicy: IfNotPresent
env:
- name: JAVA_OPTS
value: -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai -Djenkins.install.runSetupWizard=true
ports:
- containerPort: 8080
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
resources:
requests:
cpu: '2'
memory: 4G
limits:
cpu: '2'
memory: 4G
创建service
---
apiVersion: v1
kind: Service
metadata:
name: jenkins
namespace: jenkins
labels:
app: jenkins
spec:
selector:
app: jenkins
type: ClusterIP
ports:
- name: http
port: 8080
targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-jnlp
namespace: jenkins
labels:
app: jenkins
spec:
selector:
app: jenkins
ports:
- name: agent
port: 50000
targetPort: 50000
创建ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: jenkins
namespace: jenkins
spec:
ingressClassName: nginx
rules:
- host: cicd.yuankun.cc
http:
paths:
- backend:
service:
name: jenkins
port:
number: 8080
path: /
pathType: Prefix
- host: jenkins.yuankun.cc
http:
paths:
- backend:
service:
name: jenkins
port:
number: 8080
path: /
pathType: Prefix
以下内容需要使用istio服务网格才需创建
创建gateway
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: test-yuankun-cc-gateway
namespace: jenkins
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- test.yuankun.cc
port:
name: http
number: 80
protocol: HTTP
创建VirtualService
使用istio服务网格
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: jenkins-www
namespace: jenkins
spec:
hosts:
- "test.yuankun.cc"
gateways:
- test-yuankun-cc-gateway
http:
- match:
- uri:
prefix: /
# rewrite:
# uri: /
route:
- destination:
host: jenkins
port:
number: 8080
评论区