美文网首页
argocd入门

argocd入门

作者: wwq2020 | 来源:发表于2022-04-11 18:36 被阅读0次

准备

准备一个k8s
准备一个gitlab仓库,配置webhook,指向myip:32001,在分支test上有代码和Dockerfile,如监听push event

安装argo

kubectl create ns argocd
helm repo add argo https://argproj.github.io/argo-helm
helm install argocd argo/argo-cd -n argocd
helm install argoevents argo/argo-events -n argocd
kubectl -n argocd -f https://raw.githubusercontent.com/argoproj/argo-events/master/examples/rbac/sensor-rbac.yaml
kubectl -n argocd -f https://raw.githubusercontent.com/argoproj/argo-workflows/master/manifests/quick-start-postgres.yaml

配置

修改argocd下default serviceaccount权限

kubectl create rolebinding default-admin --clusterrole=admin --serviceaccount=argocd:default -n argocd

新建文件eventbus.yaml,内容如下

apiVersion: argoproj.io/v1alpha1
kind: EventBus
metadata:
  name: default
  namespace: argocd
spec:
  nats:
    native: {}

创建eventbus

kubectl apply -f  eventbus.yaml

新建文件eventsource.yaml,内容如下

apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: myapp-webhook
  namespace: argocd
spec:
  webhook:
    test:
      port: "12000"
      endpoint: /test
      method: POST

创建eventsource.yaml

kubectl -n argocd -f eventsource.yaml

新建文件sensor.yaml,内容如下

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: myapp-test-webhook
  namespace: argocd
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: myapp-test-webhook
      eventSourceName: myapp-webhook
      eventName: test
  triggers:
    - template:
        name: webhook-workflow-trigger
        k8s:
          operation: create
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: myapp-test-webhook-
              spec:
                arguments:
                  parameters:
                  - name: image
                    value: myip:5000/myapp
                  - name: myip
                    value: myip
                entrypoint: main
                templates:
                - name: main
                  dag:
                    tasks:
                      - name: git
                        template: git
                      - name: build
                        depends: "git"
                        arguments:
                          artifacts:
                            - name: code
                              from: "{{tasks.git.outputs.artifacts.code}}"
                        template: build
                      - name: deploy
                        depends: "build"
                        template: deploy
                        arguments:
                          artifacts:
                            - name: code
                              from: "{{tasks.git.outputs.artifacts.code}}"
                - name: git
                  inputs:
                    parameters:
                      - name: mess
                  outputs:
                    artifacts:
                      - name: code
                        path: /src/
                  script:
                    image: alpine/git:v2.26.2
                    workingDir: "/src/"
                    command: [git]
                    args: ["clone","--single-branch","--branch","test","http://user:token@myapp.git","project"]
                - name: build
                  inputs:
                    parameters:
                      - name: namespace
                        value: dsudev
                    artifacts:
                      - name: code
                        path: /src/
                  script:
                    image: docker:20.10.14-alpine3.15
                    workingDir: "/src/project"
                    command: [sh]
                    source: |-
                      tag=`git rev-parse --short HEAD`
                      docker -H {{myip}}:2379 build {{workflow.parameters.image}}:${tag} -f Dockerfile .
                      docker -H {{myip}}:2379 push {{workflow.parameters.image}}:${tag}
                - name: deploy
                  inputs:
                    artifacts:
                      - name: code
                        path: /src/
                  script:
                    image: bitnami/kubectl
                    workingDir: "/src/project"
                    command: [sh]
                    source: |-
                      tag=`git rev-parse --short HEAD`
                      kubectl set image deployment myapp myapp={{workflow.parameters.image}}:${tag}

创建sensor.yaml

kubectl -n argocd -f sensor.yaml

新建eventservice.yaml,内容如下

apiVersion: v1
kind: Service
metadata:
  name: myapp-webhook
  namespace: argocd
spec:
  ports:
  - port: 12000
    protocol: TCP
    targetPort: 12000
    nodePort: 32001
  selector:
    eventsource-name: myapp-webhook
  type: NodePort

创建eventservice

kubectl -n argocd -f eventservice.yaml

简单总结

argo通过eventservice暴露eventsource服务
发到eventbus
sensor监听相应的event
触发相应的workflow

相关文章

网友评论

      本文标题:argocd入门

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