Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CircleCI & Argo CD による GitOps / GitOps with Cir...

CircleCI & Argo CD による GitOps / GitOps with CircleCI and Argo CD

2019/08/05 デプロイしNight in 福岡の LT 発表内容です

pm11 (Shinichiro Todaka)

August 05, 2019
Tweet

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ w !QNʢ4IJOJDIJSP5PEBLBʣ w גࣜձࣾ"QSFOEFS w ϑϩϯτΤϯυΠϯϑϥΤϯδχΞ w ࡢ೥ɺؔ౦෱Ԭ΁Ҡॅ✈ w

    ಘҙδϟϯϧ͸ϑϩϯτΤϯυʢ3FBDUʣٴͼɺ"84 ߏ ੒؅ཧपΓͰ͢ w ࠷ۙ͸LTपΓΛ௥͔͚͍ͬͯ·͢ w ͱ♨͕޷͖ !3
  2. ࡞Δ΋ͷ w ߏ੒ w ,VCFSOFUFTΫϥελ w &,4Ͱ࡞੒ w /HJOYͷJOEFYIUNMΛදࣔ͢ΔΞϓϦέʔγϣϯ w

    "SHP$%ͷίϯϙʔωϯτ w (JU)VC w $JSDMF$* w ࢦఆͷλάͰΠϝʔδͷϏϧυ w ͦͷΠϝʔδλάͰNBOJGFTUΛஔ͖׵͑ɺ13Λ࡞੒ w 13࡞੒ʹ͸IVCΛ࢖༻ w ͪͳΈʹ(JU0QTͷຊ࣭Ͱ͸͋Γ·ͤΜ !12
  3. ΫϥελΛཱͯΔ $ brew install weaveworks/tap/eksctl $ eksctl version [ℹ] version.Info{BuiltAt:"",

    GitCommit:"", GitTag:"0.2.1"} $ eksctl create cluster \ --name sample-cluster \ --region us-west-2 \ --nodes 2 \ --nodes-min 1 \ --nodes-max 2 \ --node-type t2.medium \ --version=1.13 \ --tags environment=test !14
  4. ΞϓϦέʔγϣϯʢ/HJOYʣͷ࡞੒ $ cat Dockerfile FROM nginx:1.17.2 ADD index.html /usr/share/nginx/html/ EXPOSE

    80 CMD nginx -g "daemon off;" $ cat index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> Hello GitOps!! </body> </html> !15
  5. $JSDMF$*ͷηοτΞοϓ  $ cat .circleci/config.yml orbs: aws-ecr: circleci/[email protected] version: 2.1

    workflows: build_and_push_image: jobs: - aws-ecr/build-and-push-image: repo: gitops-sample tag: 1.0.0 filters: branches: only: - master !17
  6. $JSDMF$*ͷηοτΞοϓ  create_pull_request: docker: - image: circleci/golang:1.11-stretch steps: - checkout

    - run: name: Install hub command command: | curl -sSLf https://github.com/github/hub/releases/download/v2.12.3/hub-linux-amd64-2.12.3.tgz | \ tar zxf - --strip-components=1 -C /tmp/ && \ sudo mv /tmp/bin/hub /usr/local/bin/hub - run: name: Replace definition & create a pull request command: | cd /home/circleci/project sed -i -e 's/\(gitops-sample:\)[0-9\.]\+.*$/\11.0.0/g' infra/default/application.yaml export BRANCH=`date "+%Y%m%d-%H%M%S"` git config --global user.email "[email protected]" git config --global user.name "CIBot" git checkout --no-track -b ${BRANCH} git commit -am '[ci skip] bump version' hub push origin ${BRANCH} hub pull-request \ --message="Update Image" \ --base=${CIRCLE_PROJECT_USERNAME}:master \ --head=${CIRCLE_PROJECT_USERNAME}:${BRANCH} !18
  7. $JSDMF$*ͷηοτΞοϓ  $ cat .circleci/config.yml ... jobs: create_pull_request: ... workflows:

    build_and_push_image: jobs: - aws-ecr/build-and-push-image: ... - create_pull_request: requires: - aws-ecr/build-and-push-image filters: branches: only: - master !19
  8. $JSDMF$*ͷηοτΞοϓ  AWS_ECR_ACCOUNT_URL=<<YOUR AWS_ACCOUNT_ID>>.dkr.ecr.us- west-2.amazonaws.com AWS_ACCESS_KEY_ID=<<YOUR ACCESS_KEY_ID>> AWS_SECRET_ACCESS_KEY=<<YOUR SECRET_ACCESS_KEY>> AWS_REGION=us-west-2

    GITHUB_TOKEN=<<YOUR PERSONAL_ACCESS_KEY>> w ҎԼΛ$JSDMF$*ͷ&OWJSPONFOU΁ఆ͓͖ٛͯ͠·͢ w ·ͨ8SJUFՄೳͳ伴ͷηοτΛ࡞੒͠ɺެ։伴Λ (JU)VC΁ൿີ伴Λ$JSDMF$*΁ొ࿥͓͖ͯ͠ɺ1FSTPOBM "DDFTT5PLFO΋ొ࿥͠·͢ !20
  9. ΞϓϦέʔγϣϯͷNBOJGFTU࡞੒ $ cat infra/default/application.yaml apiVersion: apps/v1 kind: Deployment metadata: name:

    nginx-deployment namespace: default labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: XXXXXXXXXXX.dkr.ecr.us-west-2.amazonaws.com/gitops-sample: 1.0.0 ports: - containerPort: 80 ... snip ... !21
  10. ΞϓϦέʔγϣϯʢ/HJOYʣͷల։ # ద༻ʢk = kubectlʣ $ k apply -f infra/default/application.yaml

    $ k get pods -n default NAME READY STATUS RESTARTS AGE nginx-deployment-76cfc87d8c-4jwsh 1/1 Running 0 4h23m nginx-deployment-76cfc87d8c-6fxp2 1/1 Running 0 4h23m $ curl <<Service Endpoint>> <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> Hello GitOps!! </body> </html> !22
  11. "SHP$%ͷల։ʢʣ # Ϋϥελ΁ల։ʢk = kubectlʣ $ k create namespace argocd

    $ k apply -n argocd -f https://raw.githubusercontent.com/ argoproj/argo-cd/stable/manifests/install.yaml # LB Λ͚ͭͯΞΫηεͰ͖ΔΑ͏ʹ $ k patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}' # CLI ͷΠϯετʔϧ $ brew tap argoproj/tap $ brew install argoproj/tap/argocd # ϩάΠϯʢadmin:{argocd-serverͷPod໊} ͰϩάΠϯՄೳʣ $ argocd login <<Service Endpoint>> !23
  12. "SHP$%ͷల։ʢʣ # ΞϓϦέʔγϣϯΛ࡞੒ $ argocd app create gitops-sample \ --repo

    https://github.com/pm11/argo-cd-example.git \ --path infra/default \ --dest-server https://kubernetes.default.svc \ --dest-namespace default # ϦϙδτϦΛඥ͚ͮΔ $ argocd repo add https://github.com/pm11/argo-cd- example.git !24
  13. ΞϓϦέʔγϣϯʢ/HJOYʣΛมߋ $ git diff index.html ... @@ -4,6 +4,6 @@

    <title>Welcome to nginx!</title> </head> <body> -Hello GitOps!! +Hello GitOps with CircleCI!! </body> </html> !28
  14. ΞϓϦέʔγϣϯʢ/HJOYʣΛมߋ $ git diff .circleci/config.yml ... @@ -17,7 +17,7 @@

    jobs: cd /home/circleci/project - sed -i -e 's/\(gitops-sample:\)[0-9\.]\+.*$/\11.0.0/g' infra/ default/application.yaml + sed -i -e 's/\(gitops-sample:\)[0-9\.]\+.*$/\11.0.1/g' infra/ default/application.yaml export BRANCH=`date "+%Y%m%d-%H%M%S"` ... @@ -34,7 +34,7 @@ workflows: jobs: - aws-ecr/build-and-push-image: repo: gitops-sample - tag: 1.0.0 + tag: 1.0.1 filters: !29
  15. ΞϓϦέʔγϣϯʢ/HJOYʣΛมߋ $ curl <<Service Endpoint>> <!DOCTYPE html> <html> <head> <title>Welcome

    to nginx!</title> </head> <body> Hello GitOps with CircleCI!! </body> </html> !34