gitlab+rancher实现devops

  • ranchertool
    我们还需要一个基础镜像来执行rancher和k8s的命令,自己打一个docker推送到自己的docker仓库里就好了,我这有一个整理好的,放出来ranchertool
  • gitlab添加runner
    执行安装命令
docker run --rm -t -i -v /data/gitlabrunner/config:/etc/gitlab-runner gitlab/gitlab-runner register

这里需要输入gitlab地址和注册令牌,在管理中心->runner可以找到


image.png
  • 新建项目
    在gitlab中我们新建一个项目,我建了一个前端的项目


    image.png

    dockerfile自己写吧,没什么东西,主要看一下下面两个文件
    .gitlab-ci.yml

image: docker:dind
stages:
    - build
    - push
    - preview
    - production

build:
    stage: build
    only:
        - preview
        - master
    script:
        - docker build -t $CI_REGISTRY/library/gitlabcount:$CI_PIPELINE_ID .
    tags:  
        - md-tag
push:
    stage: push
    only:
        - preview
        - master
    script:
        - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
        - docker push $CI_REGISTRY/library/gitlabcount:$CI_PIPELINE_ID
    tags:  
        - md-tag

preview:
    stage: preview
    only:
        - preview
    image: $CI_REGISTRY/basic/rancher-tool
    script:
        - deploy_preview
    tags:  
        - md-tag
production:
    stage: production
    only:
        - master
    image: $CI_REGISTRY/basic/rancher-tool
    script:
        - deploy_production
    tags:
        - md-tag


.auto_devops: &auto_devops |

    function deploy_preview(){
        echo '部署preview'
        rancher login $RANCHER_API -t $RANCHER_TOKEN --skip-verify
        sed -i 's^${image_tag}^'"$CI_PIPELINE_ID^g" "./deployment.yaml"
        rancher kubectl --namespace=$NAMESPACE apply -f ./deployment.yaml
    }
    function deploy_production(){
        echo '部署production'
        rancher login $RANCHER_API -t $RANCHER_TOKEN --skip-verify
        sed -i 's^${image_tag}^'"$CI_PIPELINE_ID^g" "./deployment.yaml"
        rancher kubectl --namespace=$NAMESPACE apply -f ./deployment.yaml
    }

before_script:
    - *auto_devops

这里面有一些变量,他们在gitlab中配置,在刚才新建的项目中点设置,点CI/CD,在右侧的变量选项卡里新建这些变量


image.png

RANCHER_API和RANCHER_TOKEN在rancher中获取


image.png

image.png

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "3"
    field.cattle.io/creatorId: user-5xdgt
    field.cattle.io/publicEndpoints: '[{"addresses":["192.168.240.118"],"port":30509,"protocol":"TCP","serviceName":"default:gitcounter-nodeport","allNodes":true}]'
  creationTimestamp: "2020-07-02T01:49:13Z"
  generation: 4
  labels:
    cattle.io/creator: norman
    workload.user.cattle.io/workloadselector: deployment-default-gitcounter
  managedFields:
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:field.cattle.io/creatorId: {}
          f:field.cattle.io/publicEndpoints: {}
        f:labels:
          .: {}
          f:cattle.io/creator: {}
          f:workload.user.cattle.io/workloadselector: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector:
          f:matchLabels:
            .: {}
            f:workload.user.cattle.io/workloadselector: {}
        f:strategy:
          f:rollingUpdate:
            .: {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:annotations:
              .: {}
              f:cattle.io/timestamp: {}
              f:field.cattle.io/ports: {}
            f:labels:
              .: {}
              f:workload.user.cattle.io/workloadselector: {}
          f:spec:
            f:containers:
              k:{"name":"gitcounter"}:
                .: {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:ports:
                  .: {}
                  k:{"containerPort":80,"protocol":"TCP"}:
                    .: {}
                    f:containerPort: {}
                    f:name: {}
                    f:protocol: {}
                f:resources: {}
                f:securityContext:
                  .: {}
                  f:allowPrivilegeEscalation: {}
                  f:capabilities: {}
                  f:privileged: {}
                  f:readOnlyRootFilesystem: {}
                  f:runAsNonRoot: {}
                f:stdin: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
                f:tty: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    manager: Go-http-client
    operation: Update
    time: "2020-07-02T01:56:17Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          .: {}
          k:{"type":"Available"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    manager: kube-controller-manager
    operation: Update
    time: "2020-07-02T01:58:27Z"
  name: gitcounter
  namespace: default
  resourceVersion: "197684"
  selfLink: /apis/apps/v1/namespaces/default/deployments/gitcounter
  uid: 09c00890-65a0-429c-a16d-74604ea18093
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      workload.user.cattle.io/workloadselector: deployment-default-gitcounter
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        cattle.io/timestamp: "2020-07-02T01:56:17Z"
        field.cattle.io/ports: '[[{"containerPort":80,"dnsName":"gitcounter-nodeport","hostPort":0,"kind":"NodePort","name":"80tcp305091","protocol":"TCP","sourcePort":30509}]]'
      creationTimestamp: null
      labels:
        workload.user.cattle.io/workloadselector: deployment-default-gitcounter
    spec:
      containers:
      - image: docker.maidiyun.com/library/gitlabcount:${image_tag}
        imagePullPolicy: Always
        name: gitcounter
        ports:
        - containerPort: 80
          name: 80tcp305091
          protocol: TCP
        resources: {}
        securityContext:
          allowPrivilegeEscalation: false
          capabilities: {}
          privileged: false
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        stdin: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        tty: true
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 2
  conditions:
  - lastTransitionTime: "2020-07-02T01:58:27Z"
    lastUpdateTime: "2020-07-02T01:58:27Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-07-02T01:49:13Z"
    lastUpdateTime: "2020-07-02T01:58:27Z"
    message: ReplicaSet "gitcounter-6f47d79c6c" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 4
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

这个deployment是从rancher中复制出来的,你也可以自己写,嫌麻烦就去复制一个出来吧
我们首先在rancher中部署一个服务,随便部署一个就行


image.png

然后点击查看yaml


image.png

然后复制出来,删除里面的resourceVersion那一行,把image地址替换成我们刚才新建的那个项目的docker仓库地址,tag用变量替换一下,里面还有很多没用的东西可以删掉,自己看着删吧
  • 验证
    我新建的项目有三个分支dev、preview和master,当合并代码到preview或者master的时候会自动进行构建
    把代码合并到preview然后去gitlab里项目的CI/CD->流水线 看一下


    image.png

你可能感兴趣的:(gitlab+rancher实现devops)