美文网首页
docker-for-desktop中kubernetes的ci

docker-for-desktop中kubernetes的ci

作者: 红色的砖 | 来源:发表于2019-08-07 13:37 被阅读0次
    1. 在Mac上安装Docker并启用kubernetes
    image.png
    1. 使用kubernetes方式运行jenkins,执行kubectl命令创建jenkins

    jenkins-volume-claim.yaml

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: jenkins
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
    

    jenkins-deployment.yaml

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: jenkins
      labels:
        k8s-app: jenkins
    spec:
      replicas: 1
      selector:
        matchLabels:
          k8s-app: jenkins
      template:
        metadata:
          labels:
            k8s-app: jenkins
        spec:
          containers:
          - name: jenkins
            image: jenkins/jenkins:lts
            imagePullPolicy: IfNotPresent
            volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home
            ports:
            - containerPort: 8080
              name: web
            - containerPort: 50000
              name: agent
          volumes:
            - name: jenkins-home
              persistentVolumeClaim:
                claimName: jenkins
    
    

    jenkins-service.yaml

    kind: Service
    apiVersion: v1
    metadata:
      labels:
        k8s-app: jenkins
      name: jenkins
    spec:
      type: NodePort
      ports:
        - port: 8080
          name: web
          targetPort: 8080
        - port: 50000
          name: agent
          targetPort: 50000
      selector:
        k8s-app: jenkins
    
    kubectl create -f jenkins-volume-claim.yaml
    kubectl create -f jenkins-service.yaml
    kubectl create -f jenkins-deployment.yaml
    
    1. 查看jenkins暴露的端口号并进入jenkins ui界面
    kubectl get service
    
    image.png

    使用localhost:32381进入jenkins ui界面

    1. 安装kubernetes插件
    image.png
    1. 在Configure System下配置kubernetes cloud
    image.png
    image.png
    Kubernetes URL : https://kubernetes.default.svc.cluster.local
    Jenkins URL为jenkins服务对应的service name和port端口的组合,可以进入jenkins pod中执行一下命令测试是否可行
    kubectl get pods 
    
    image.png
    kubectl exec -it jenkins-675965b8f-6ds66 bash
    curl -I http://jenkins:8080/tcpSlaveAgentListener/
    
    image.png
    1. 配置Pod Template(即jenkinsFile)
      Pod Template即可通过代码配置也可通过界面配置。
    def label = "mypod-${UUID.randomUUID().toString()}"
    podTemplate(label: label, cloud: 'kubernetes', containers: [
            containerTemplate(
                    name: 'kubectl', //执行kubeclt命令的容器
                    image: 'roffe/kubectl',
                    ttyEnabled: true,
                    command: 'cat'),
            containerTemplate(  //执行docker命令的容器
                    name: 'docker',
                    image: 'docker',
                    command: 'cat',
                    ttyEnabled: true),
            containerTemplate(  //执行gradle命令的容器
                    name: 'jnlp',
                    image: 'jicki/jenkins-jnlp',
                    alwaysPullImage: true,
                    args: '${computer.jnlpmac} ${computer.name}')],
            volumes: [hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
                      hostPathVolume(hostPath: '/tmp/jenkins/.gradle', mountPath: '/home/gradle/.gradle')]) {
        node(label) {
            properties([parameters([string(defaultValue: 'master', name: 'BRANCH_SPECIFIER'),
                                    string(defaultValue: 'Dev', name: 'DEPLOY_ENV')])])
    
            stage('clone') {
                checkout scm   //注意在执行gradle命令前得先执行命令clone代码,否则会出现gradle task不可用的情况
                env.gitCommitHash = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
            }
            stage('build jar and publish it to nexus') {
                container('jnlp') {
                    stage('code quality check') {
                        sh 'chmod +x gradlew'
                        sh './gradlew checkstyleMain'
                        sh './gradlew checkstyleTest'
                        sh './gradlew pmdMain'
                    }
                    stage('test') {
                        sh './gradlew test'
                    }
                    stage('build jar') {
                        sh './gradlew bootJar'
                        stash includes: 'build/libs/*.jar', name: 'buildJar'
                    }
                    stage('Publish Artifacts') {
                        withCredentials([usernamePassword(credentialsId: 'fe7045c5-c7e0-4faa-b96d-ac206bf791d0',
                                passwordVariable: 'NEXUS_MAVEN_PASSWORD',
                                usernameVariable: 'NEXUS_MAVEN_USERNAME')]) {
                            sh "./gradlew publish -PgitCommitHash=${gitCommitHash} -PnexusMavenUsername=${NEXUS_MAVEN_USERNAME} -PnexusMavenPassword=${NEXUS_MAVEN_PASSWORD}"
                        }
                    }
                }
            }
            stage('build image and push it to dockerhub') {
                container('docker') {
                    withCredentials([usernamePassword(credentialsId: '96dcf601-9b90-45fc-a625-047d02db8658',
                            passwordVariable: 'DOCKER_HUB_PASSWORD',
                            usernameVariable: 'DOCKER_HUB_USERNAME')]) {
                        unstash 'buildJar'
                        sh "docker build -t kubernetes-demo:${gitCommitHash} ."
                        sh "docker tag kubernetes-demo:${gitCommitHash} 18271959943/kubernetes-demo:${gitCommitHash}"
                        sh "docker login -u=${DOCKER_HUB_USERNAME} -p=${DOCKER_HUB_PASSWORD}"
                        sh "docker push 18271959943/kubernetes-demo:${gitCommitHash}"
                    }
                }
            }
            stage('deploy application') {
                container('kubectl') {
                    stage('deploy mysql') {
                        sh 'kubectl apply -f kubernetes-demo-deployment/mysql-volume-claim.yaml'
                        sh 'kubectl apply -f kubernetes-demo-deployment/mysql-secrets.yaml'
                        sh 'kubectl apply -f kubernetes-demo-deployment/mysql-config-map.yaml'
                        sh 'kubectl apply -f kubernetes-demo-deployment/mysql-deployment.yaml'
                    }
                    stage('deploy kubernetes-demo') {
                        sh 'kubectl apply -f kubernetes-demo-deployment/kubernetes-demo-deployment.yaml'
                    }
                }
            }
        }
    }
    

    在jenkins中运行job时最好先将containerTemplate 中的镜像pull到本地,否则job得执行时间会很长,因为job执行过程中会pull镜像

    相关文章

      网友评论

          本文标题:docker-for-desktop中kubernetes的ci

          本文链接:https://www.haomeiwen.com/subject/jqkydctx.html