在K8s中部署Jenkins

2023-11-05  本文已影响0人  sexy_cyber

devops所有工具都建议放在该命名空间下

apiVersion: v1
kind: Namespace
metadata:
  name: devops-tools
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: jenkins-admin
rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["*"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin
  namespace: devops-tools
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: jenkins-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins-admin
subjects:
- kind: ServiceAccount
  name: jenkins-admin
  namespace: devops-tools
df -h /data/jenkins

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lvol01  500G   91G  410G  19% /data

path: /data/jenkins 该配置为服务器的文件目录

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv-volume
  labels:
    type: local
spec:
  storageClassName: standard
  claimRef:
    name: jenkins-pv-claim
    namespace: devops-tools
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  local:
    path: /data/jenkins
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - spiders-control-plane
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv-claim
  namespace: devops-tools
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi

Kind部署的 k8s,需要执行该操作,因为Kind部署的k8s是运行在容器中的
非Kind部署的K8s,可以忽略该步骤

7.1 找到运行K8s的容器ID

docker ps

CONTAINER ID   IMAGE                                                         COMMAND                  CREATED        STATUS       PORTS                                                          NAMES
4317c7e4448a   uhub.service.ucloud.cn/drakespider/spider:dappradar6          "tail -f /dev/null"      3 weeks ago    Up 3 weeks                                                                  test
dbe0bb145add   kindest/node:v1.27.3                                          "/usr/local/bin/entr…"   7 weeks ago    Up 5 weeks   0.0.0.0:6443->6443/tcp, 0.0.0.0:30000-30218->30000-30218/tcp   spiders-control-plane

7.2 将服务器文件系统的目录挂载到容器中

docker exec -it dbe0bb145add mkdir -p /data/jenkins
docker exec -it dbe0bb145add mount --bind /data/jenkins /data/jenkins
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops-tools
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-server
  template:
    metadata:
      labels:
        app: jenkins-server
    spec:
      securityContext:
            fsGroup: 1000
            runAsUser: 1000
      serviceAccountName: jenkins-admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts
          resources:
            limits:
              memory: "2Gi"
              cpu: "1000m"
            requests:
              memory: "500Mi"
              cpu: "500m"
          ports:
            - name: httpport
              containerPort: 8080
            - name: jnlpport
              containerPort: 50000
          livenessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 90
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          volumeMounts:
            - name: jenkins-data
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
              claimName: jenkins-pv-claim

将容器端口暴露到命名空间

apiVersion: v1
kind: Service
metadata:
  name: jenkins-service
  namespace: devops-tools
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/path:   /
      prometheus.io/port:   '8080'
spec:
  selector:
    app: jenkins-server
  ports:
    - name: httpport
      port: 8080
      targetPort: 8080
    - name: jnlpport
      port: 50000
      targetPort: 50000

将该配置编辑到K8s nginx-config Config Maps 中

        server {
            listen 443 ssl;
            server_name jenkins.siquanzhuanxiang.top;
            location / {
                proxy_pass http://jenkins-service.devops-tools.svc.cluster.local:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                }
        }

您也可以参考Jenkins的官方文档来部署

上一篇下一篇

猜你喜欢

热点阅读