云原生 | Docker + GitLab + GitLab Runner 自动化部署
CI/CD
CI/CD 是持续集成 (CI-Continuous Integration) 和持 续交付(Continuous Delivery) 或持续部署(CD-Continuous Deployment) 的组合实践。CI/CD 实践构成了现代 DevOps 操作的支柱。
持续集成主要包含以下步骤:
- 自动化构建
Continuous Build - 自动化测试
Continuous Test, 验证软件功能 - 自动化集成
Continuous Intergration
GitLab
目前市面上实现自动化部署的开源方案一般有两个(Jenkins和GitLab)。从可扩展性而言Jenkins是更好的选择,不过自由也意味着很多东西都需要自己配置
Gitlab 是开源的 devops 平台,集成了 Gitlab CI/CD 功能,GitLab作为代码仓库管理系统,提供了可视化界面、版本控制、代码审核等功能,而GitLab Runner则是CI/CD的执行器,负责执行GitLab中配置的任务,可以自动从GitLab上获取任务并执行
- 开发者在
commit代码或者提交merge request会自动触发CI/CD流程 - 流程开始后,会主动读取项目根目录下的
.gitlab_ci.yml文件,获取构建镜像,构建步骤,构建命令等,并运行一个CI pipeline(一个pipeline通常分为三个阶段:build,test,deploy),即会执行一系列任务,如用eslint校验代码规范,单元测试等。 - 根据
.gitlab_ci.yml中配置的stage中的tags,选择对应的GitLab Runner,根据配置的image启动容器,并在该容器中执行stage中的构建命令
Gitlab CI/CD 中pipeline(流水线),stage(阶段),job(任务)之间的关系为:pipeline 包含了若干个 stage,stage 包含了多个 job,job 是流水线中最小的单位,这些任务是在 GitLab Runner 中运行。
GitLab还集成了sentry sdk,可通过连接sentry进行监控,还可以接入kubernets集群等
实践开始
GitLab环境安装配置
-
docker拉取GitLab镜像,运行## 拉取镜像 sudo docker pull docker.io/gitlab/gitlab-ce ## 创建目录 sudo mkdir -p /gitlab/data sudo mkdir -p /gitlab/logs sudo mkdir -p /gitlab/config ## 运行 sudo docker run --detach \ --publish 8443:443 --publish 8000:80 --publish 2222:22 \ --name gitlab \ --restart always \ --volume /gitlab/config:/etc/gitlab \ --volume /gitlab/logs:/var/log/gitlab \ --volume /gitlab/data:/var/opt/gitlab \ -log-driver=none \ docker.io/gitlab/gitlab-ce:latest速度过于慢了:sob:, 已经配置好了镜像源
-
进入镜像容器内修改
修改external_url: external_url 'http://IP:8000' # IP填写GitLab部署的服务器的IP
修改ssh端口: gitlab_rails['gitlab_shell_ssh_port'] = 2222
修改nginx监听的端口: nginx['listen_port'] = 80
-
在config下有一个文件存放了root用户的密码
-
修改
root密码 -
用
GitLab里面的模板创建个 项目
GitLab Runner环境安装配置
-
docker拉取gitlab/gitlab-runner镜像,运行docker pull gitlab/gitlab-runner docker run -d --name gitlab-runner --restart always \ -v /home/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest创建一个项目
拉取代码
git clone http://192.168.253.128:8000/root/hello.git # 将中间的那一串改成ip上传一个 Go 文件
package main import ( "fmt" "net/http" ) func indexHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello CI/CD") } func main() { http.HandleFunc("/", indexHandler) fmt.Println("listening on http://localhost:8009") http.ListenAndServe(":8009", nil) }之后复制这里的 Registration token
进入gitlab-runner容器中
docker exec -it gitlab-runner /bin/bash开始注册 runner
//进入容器后输入 gitlab-ci-multi-runner register执行之后可以看到注册的 Runner
自动化部署项目
首先在仓库中添加文件:.gitlab-ci.yml,内容如下
# ci demo stages: # List of stages for jobs, and their order of execution - build - test - deploy build-job: # This job runs in the build stage, which runs first. stage: build script: - echo "Compiling the code..." - echo "Compile complete." test-job: # This job also runs in the test stage. stage: test # It can run at the same time as unit-test-job (in parallel). script: - echo "Linting code... This will take about 10 seconds." - sleep 10 - echo "No lint issues found." deploy-job: # This job runs in the deploy stage. stage: deploy # It only runs when *both* jobs in the test stage complete successfully. environment: production script: - echo "Deploying application..." - echo "Application successfully deployed."提交到仓库,查看流水线可以看到