10

ArgoCD二:与Gitlab-CI集成的完整示例

 3 years ago
source link: https://www.bladewan.com/2021/01/17/argo_2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ArgoCD二:与Gitlab-CI集成的完整示例

此篇文章演示一个完整的GitOps工作流程,使用Gitlab-CI+ArgoCD来实现,其中Gitlab-CI主要用于实现代码编译、镜像构建、修改部署的yaml文件。ArgoCD用于将修改后的部署yaml文件推送到各个集群中。

软件 版本 Gitlab 13.7.0 ArgoCD 1.8.1 Kubernetes 1.17.4

1、将应用代码和构建Docker镜像的Dockerfile文件放置到Gitlab对应项目中

2、在Gitlab中创建用于专门用于存放部署yaml的项目

3、配置Gitlab-CI用于代码编译镜像构建和业务yaml文件修改

4、配置ArgoCD检测存放部署yaml的项目,有更新后自动部署到对应环境中

Gitlab配置

以一个Go-server项目为例

代码文件
server.go文件

package main
import (
"fmt"
"log"
"net/http"
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world")
func main() {
http.HandleFunc("/", hello)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)

Dockerfile文件

FROM golang:alpine
WORKDIR /go
ADD server /go
CMD ["./server"]

在gitlab中创建proc-yaml的项目,用于集中存放到Kubernetes集群中的业务的yaml文件
在proc-yaml项目中创建go-server项目的文件夹,专门存放go-server项目的yaml文件

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: go-demo
labels:
app: go-demo
spec:
replicas: 1
selector:
matchLabels:
app: go-demo
template:
metadata:
labels:
app: go-demo
spec:
containers:
- name: go-demo
image: 172.16.1.31/library/go-server-demo:63
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: go-demo
spec:
type: NodePort
selector:
app: go-demo
ports:
- port: 8080
targetPort: 8080
nodePort: 30007

Gitlab-CI配置

Gitlab-CI配置和基本使用可以参考本博客的Gitlab-CI时用章节
这里体现的主要是和之前的差异性部分
Gitlab-CI环境变量配置

主要添加连接镜像仓库和代码仓库的凭证

创建.gitlab-ci.yml文件,用于配置gitlab-ci

stages:
- package
- docker_build
- modify_yaml
build_job:
image: golang:alpine
stage: package
tags:
- k8s-runner
script:
- go build server.go
artifacts:
paths:
- /builds/root/go-server
docker_build_job:
image: docker:19.03.0
services:
- name: docker:19.03.0-dind
command: ["--insecure-registry=0.0.0.0/0","--registry-mirror=https://vqgjby9l.mirror.aliyuncs.com"]
variables:
DOCKER_HOST: tcp://127.0.0.1:2375
DOCKER_TLS_CERTDIR: ""
stage: docker_build
tags:
- k8s-runner
script:
- docker info
- docker login -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD 172.16.1.31
- docker build -t 172.16.1.31/library/go-server-demo:$CI_PIPELINE_ID .
- docker push 172.16.1.31/library/go-server-demo:$CI_PIPELINE_ID
modify_yaml:
image: wanshaoyuan/git-client:v1.0
stage: modify_yaml
tags:
- k8s-runner
script:
- git clone http://$GIT_USERNAME:[email protected]/root/proc-yaml.git
- git config --global user.email "[email protected]"
- git config --global user.name "root"
- git remote set-url origin http://$GIT_USERNAME:[email protected]/root/proc-yaml.git
- sed -i "s/go-server-demo:.*/go-server-demo:$CI_PIPELINE_ID/g" proc-yaml/go-server/deployment.yaml
- cd proc-yaml/
- git add *
- git commit -m "update go-server"
- git push origin master

主要包含3个阶段,代码编译阶段、镜像构建阶端、部署yaml文件修改阶段

代码编译阶段:启动一个编译容器,根据代码编译出对应的制品,这里为了能让下个镜像构建阶段直接使用编译好的制品,需要配置artifacts将目录共享给下个阶段。

镜像构建阶段:使用DinD的方式构建Docker镜像,然后镜像tag以我们Pipeline的ID为tag,这里好处在于可以根据对应的Pipeline的迅速定位到对应的镜像版本

部署yaml修改阶段: 通过git命令将对部署yaml项目的repo clone下来然后通过sed进行修改并commit回去,通过此步ArgoCD检测到变化后会拉取新的yaml进行部署

ArgoCD配置

在ArgoCD中配置对接pro-yaml的repo认证

创建App

选择自动同步模式

PATH这配置我们go-server这个项目的路径,就会检测这里面的yaml文件,并自动更新到集群中

最后创建完成

修改server.go的Hello world为Hello Argo

等待git-ci执行

检查proc-yaml项目是否更新

可以看见对应的image的tag修改成了,我们实际上Pipeline的ID

等待片刻后ArgoCD检测到yaml变化,自动部署,此时Kubernetes中应用进行了自动更新

curl 192.168.0.6:30007
Hello argo

通过GitlabCI与ArgoCD结合确实非常方便的将我们应用进行部署到Kubernetes中,并且后续也非常容易进行回滚,在之前通过Gitlab-CI中集成kubectl镜像,进行部署容易造成kubeconfig文件的泄漏,也不方便做权限的管理和应用状态控制,但结合ArgoCD后一切都美好了起来。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK