README.md 3.6 KB

jenkins 部署

准备

  • 创建namespace
kubectl create ns devops
  • 准备pvc,用于jenkins插件等持久化的存储
# 查看是否有storageclass
kubectl get sc

# 如果已经有sc,
# 修改pvc.yaml中的storageClassName
# 默认写的是nas-storage
kubectl apply -f pvc.yaml -n devops

部署

  • 修改clusterRoleBing 中的namespace
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
 name: jenkins
roleRef:
 apiGroup: rbac.authorization.k8s.io
 kind: ClusterRole
 name: jenkins
subjects:
 - kind: ServiceAccount
   name: jenkins
   namespace: devops
  • 如果需要ingress,修改对应的域名
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jenkins-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: jenkins.sxkj.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: jenkins
            port:
              number: 30080
  • 部署jenkins应用
kubectl apply -f deploy.yaml -n devops
  • 验证,访问ip:30080,ip为pod调度到k8s某一个节点的ip,或者直接访问对应的域名: 如,jenkins.sxkj.com

配置jenkins

  • 管理插件中下载插件

    • git
    • Pipeline
    • git parameter
    • publish over ssh
    • Localization: Chinese (Simplified)
    • kubernetes
    • Blue Ocean
    • Role-based Authorization Strategy
  • 节点管理 -> Configure Cloud

  • 添加凭据

Jenkinsfile demo

def label = "slave-${UUID.randomUUID().toString()}"

podTemplate(cloud: 'kubernetes', namespace:'devops',label: label, serviceAccount: 'jenkins',containers: [
  containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'kubectl', image: 'cnych/kubectl', command: 'cat', ttyEnabled: true)
],volumes: [
  hostPathVolume(mountPath: '/home/jenkins/.kube', hostPath: '/root/.kube'),
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
  hostPathVolume(mountPath: '/etc/docker/daemon.json', hostPath: '/etc/docker/daemon.json')
], yaml: """
spec:
  nodeSelector:
    kubernetes.io/hostname: h249
  hostalias:
  - ip: 192.168.199.31
    hostnames:
    - "SXKJ"
"""
) {
    node(label) {
        stage('构建 Docker 镜像') {
          git url: ' http://gogs.soaringnova.com/sxwl_DL/meta_be.git', branch: 'master'
          container('docker') {
            echo "构建 Docker 镜像阶段"
            retry(2) { sh "docker build  -t SXKJ:32775/meta-app:latest --output type=docker ." }
            echo "build success"
          }
        }
        stage('Docker Push 镜像') {
            container('docker') {
              retry(2) { sh "docker push SXKJ:32775/meta-app:latest" }
              echo "Push success"
            }
        }
        stage('运行 Kubectl') {
          container('kubectl') {
            echo "重启 pod"
            sh "kubectl rollout restart deployments/app-be  -n meta-demo"
          }
        }
    }
}

参考文档