$kubectl create namespace jenkins
本例使用NFS,在NFS中添加相关记录
apiVersion: v1
kind: PersistentVolume
metadata:
name: jekins-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Delete
nfs:
server: 192.168.56.4
path: /data/nfs/jekins
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: jenkins-pvc
namespace: jenkins
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins-sa
namespace: jenkins
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: jenkins-cluster-role
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/v1beta1
kind: ClusterRoleBinding
metadata:
name: jenkins-cluster-role-binding
namespace: jenkins
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
# 角色的名字
name: jenkins-cluster-role
subjects:
- kind: ServiceAccount
# 账户的名字
name: jenkins-sa
namespace: jenkins
$kubectl describe clusterrole jenkins-cluster-role
Name: jenkins-cluster-role
Labels: <none>
Annotations: <none>
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
pods/exec [] [] [create delete get list patch update watch]
pods [] [] [create delete get list patch update watch]
services [] [] [create delete get list watch patch update]
deployments.apps [] [] [create delete get list watch patch update]
deployments.extensions [] [] [create delete get list watch patch update]
pods/log [] [] [get list watch]
secrets [] [] [get]
可以看到已经创建成功
kubectl describe clusterrolebinding jenkins-cluster-role
Name: jenkins-cluster-role-binding
Labels: <none>
Annotations: <none>
Role:
Kind: ClusterRole
Name: jenkins-cluster-role
Subjects:
Kind Name Namespace
---- ---- ---------
ServiceAccount jenkins-sa jenkins
可以看到“角色”与“账户”已经绑定
名称 | 值 | 作用 | 备注 |
---|---|---|---|
namespace | jenkins | 命名空间 | 为了安全进行空间隔离 |
pv,pvc | jekins-pv | 存储 | Jenkins集成过程需要 |
ServiceAccount | jenkins-sa | 操作账户 | |
ClusterRole | jenkins-cluster-role | 集群角色 | Jenkins自动部署 需要操作集群所以需要此项 |
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins-deploy
namespace: jenkins
spec:
selector:
matchLabels:
app: jenkins-pod
template:
metadata:
labels:
app: jenkins-pod
spec:
terminationGracePeriodSeconds: 10
serviceAccount: jenkins-sa
imagePullSecrets:
- name: jenkins-secret
containers:
- name: jenkins
image: registry.i.smokelee.com/devops/jenkins:lts
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
readinessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
volumeMounts:
- name: jenkinshome
subPath: jenkins
mountPath: /var/jenkins_home
env:
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
resource: limits.memory
divisor: 1Mi
- name: JAVA_OPTS
value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai
securityContext:
fsGroup: 1000
volumes:
- name: jenkinshome
persistentVolumeClaim:
claimName: jenkins-pvc
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-svc
namespace: jenkins
labels:
app: jenkins-svc
spec:
selector:
app: jenkins-pod
ports:
- name: web
port: 8080
targetPort: web
- name: agent
port: 50000
targetPort: agent