1.安装gitlab runner

1.添加GitLab的官方存储库:

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

2.安装最新版本的GitLab Runner

sudo yum install gitlab-runner

3.验证安装

gitlab-runner -version

2.注册gitlab runner

gitlan-runner register

在这里插入图片描述

根据提示输入

url(登陆GitLab->Settings->CI/CD->Runners展开,有url信息,防坑,可能需要把http改成https,否则提示401未授权),

token(登陆GitLab->Settings->CI/CD->Runners展开,复制token即可),

description(自定义),

tags(很关键,后面写.gitlab-ci.yml配置文件时需要指定对应的tags)

3.ecs环境配置

备注:ecs上可能没有安装docker,需要自己安装

1.docker安装步骤地址如下

http://www.runoob.com/docker/centos-docker-install.html

2.安装jdk环境变量

vim /etc/profile

JAVA_HOME=/yinjian/java/jdk-11.0.2/
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH

3.安装maven环境变量

vim /etc/profile

MAVEN_HOME=/yinjian/maven/apache-maven-3.6.0/
export MAVEN_HOME
export PATH=$PATH:$MAVEN_HOME/bin

(1)期间遇到一个问题!!!

修改/etc/profile出错导致所有命令都command not found的解决办法

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin
因为基本所有的shell命令都包含在上面几个目录中, 
执行完后,就可以执行其他命令,赶紧把/etc/profile的错误改正过来~~~ 
然后再
*#立即生效*
source /etc/profile

(2)maven里settings.xml需要

配置镜像服务器

<mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
    <mirrorOf>central</mirrorOf>          
</mirror>

指定编译的jdk版本,否则会有问题

<profile>  
    <id>jdk-11</id>  
    <activation>  
        <activeByDefault>true</activeByDefault>  
        <jdk>11</jdk>  
    </activation>  
    <properties>  
        <maven.compiler.source>11</maven.compiler.source>  
        <maven.compiler.target>11</maven.compiler.target>  
        <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>  
    </properties>   
</profile>

4.项目添加.gitlab-ci.yml文件发布至gitlab测试

variables:
  REPOSITORY: "registry.cn-hangzhou.aliyuncs.com/workbei-project/workbei-ec-engine"
  TAG: 1.4.2
  DOCKER_USERNAME: xxx
  DOCKER_PASSWORD: xxx
stages:
  - package
  - build-and-push
  - deploy
#maven打成war包
maven-package:
  stage: package
  tags:
    - workbei-ec-engine
  only:
    - dev
  script:
    - mvn package -B -DskipTests
  artifacts:
    paths:
      - ec-web-main/target/workbeiurl.war
#docker构建镜像and上传镜像
docker-build-and-push:
  stage: build-and-push
  tags:
    - workbei-ec-engine
  only:
    - dev
  script:
    - sudo docker login --username=$DOCKER_USERNAME --password=$DOCKER_PASSWORD registry.cn-hangzhou.aliyuncs.com
    - sudo docker build -t workbei-ec-engine:$TAG .
    - sudo docker tag workbei-ec-engine:$TAG $REPOSITORY:$TAG
    - sudo docker push $REPOSITORY:$TAG
#部署到K8S集群并更新
k8s-deploy:
  stage: deploy
  tags:
    - workbei-ec-engine
  only:
    - dev
  script:
    - echo "k8s-deploy-start"

坑:

sudo docker login权限不够
是因为默认构建的时候是gitlab-runner 这个用户 去执行这个命令的
所以要为gitlab-runner 用户提权
chmod +x /etc/sudoers
vim /etc/sudoers 
root那块添加如下内容
gitlab-runner   ALL=(ALL)       NOPASSWD:ALL

如果顺利,现在已经成功push到远程仓库了,下一步是k8s集群拉取更新镜像!

5.kubectl安装

解压
tar -zxvf kubernetes-client-linux-amd64.tar.gz
进入目录
cd kubernetes/client/bin
把kubectl赋权可执行命令
chmod +x ./kubectl
把kubectl拷贝到环境变量中去
sudo mv ./kubectl /usr/local/bin/kubectl

阿里云->容器服务->选择集群->内网访问即可

1551169256065

成功后输入命令查看最后整合效果

kubectl config view

6.完善.gitlab-ci.yml配置文件添加deploy步骤

k8s-deploy:
  stage: deploy
  tags:
    - workbei-ec-engine
  only:
    - dev
  script:
    - sudo kubectl set image deployment workbei-ec-engine-deploy workbei-ec-engine=registry-vpc.cn-hangzhou.aliyuncs.com/workbei-project/workbei-ec-engine:$TAG --namespace=dingtalk-auth

更新镜像,等待,查看结果即可

7.最终版本

当时考虑多个job之间需要协调一个war包,选择artifacts上传到gitlab,但后面发现会特别耗时!最终选择把package和build and push放到一个job里。

variables:
  REPOSITORY: registry-vpc.cn-hangzhou.aliyuncs.com/workbei-project/workbei-ec-engine
  TAG: 1.4.3
  DOCKER_USERNAME: dingclouduser@1403209023012938
  DOCKER_PASSWORD: Create640321
stages:
  - package-build-push
  - deploy
#maven打成war包
maven-package:
  stage: package-build-push
  tags:
    - workbei-ec-engine
  only:
    - dev
  script:
    - mvn package -B -DskipTests
    - sudo docker login --username=$DOCKER_USERNAME --password=$DOCKER_PASSWORD registry-vpc.cn-hangzhou.aliyuncs.com
    - sudo docker build -t workbei-ec-engine:$TAG .
    - sudo docker tag workbei-ec-engine:$TAG $REPOSITORY:$TAG
    - sudo docker push $REPOSITORY:$TAG
  #artifacts:
    #expire_in: 60 mins
    #paths:
      #- ec-web-main/target/workbeiurl.war
#部署到K8S集群并更新
k8s-deploy:
  stage: deploy
  tags:
    - workbei-ec-engine
  only:
    - dev
  #dependencies: []
  script:
    - /usr/local/bin/kubectl config use-context 211484640430774345-c5b80bf3faeea4ce3a7ca65809e5601b9
    - /usr/local/bin/kubectl set image deployment workbei-ec-engine-deploy workbei-ec-engine=$REPOSITORY:$TAG --namespace=dingtalk-auth

学习Linux命令

Linux which命令用于查找文件。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。

which java

切换用户

su gitlab-runner

复制文件

cp /root/.kube/config /home/gitlab-runner/.kube/config

修改文件夹或者文件拥有者和拥有组

chown gitlab-runner config
chgrp gitlab-runner config

查看文件(包含隐藏文件)

ll -a
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐