基于k8s的容器云CI/CD操作手册(三)在k8s中安装jenkin

docker pull jenkins/jenkins:lts
配置权限
vim rbac-jenkins.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
  namespace: kube-ops
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: jenkins
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
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins
    namespace: kube-ops

kubectl apply -f rbac-jenkins.yaml

安装jenkins
vim jenkins.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
  namespace: kube-ops
spec:
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccountName: jenkins              #在rbac-jenkins.yaml中定义该名字
      containers:
      - name: jenkins
        image: jenkins/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.NodeProvision
er.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: pvc-jenkins

kubectl apply -f jenkins.yaml
有关subPath: jenkins的说明:
若果pod使用的是手动创建的pvc,那么需要使用subPath参数,会在nfs服务器的/data/k8s内生成一个子目录jenkins
如果pod使用的是由StorageClass创建的pvc,那么不需要使用subPath参数,仍然会在nfs服务器的/data/k8s内随机生成一个子目录,如kube-ops-pvc-jenkins-pvc-6c81cdee-b128-11e9-86e0-000c29d8512b
jenkins的数据在nfs服务器的/data/k8s/jenkins内或者在/data/k8s/kube-ops-pvc-jenkins-pvc-6c81cdee-b128-11e9-86e0-000c29d8512b内

配置jenkins的svc
vim svc-jenkins.yaml

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: kube-ops
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins               
  type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: web
    nodePort: 30002
  - name: agent
    port: 50000
    targetPort: agent

kubectl apply -f svc-jenkins.yaml
http://192.168.1.243:30002
初始密码在nfs服务器的/data/k8s/jenkins子目录/内
kubectl get pod -n kube-ops |grep jenkins
kubectl logs -f jenkins-c8f98bbc4-vnxdc -n kube-ops
kubectl describe pod jenkins-c8f98bbc4-vnxdc -n kube-ops

如果报错
touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
在nfs服务器上修改目录权限
chown -R 1000 /data/k8s/jenkins子目录/
因为,jenkins镜像的Dockerfile文件中定义的是:
user=jenkins group=jenkins uid=1000 gid=1000

你可能感兴趣的:(基于k8s的容器云CI/CD操作手册(三)在k8s中安装jenkin)