brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jun 10. 2023

EKS 8탄-16. EKS 자동화2- Flux CD

EKS 8탄 - 7주차

본 내용은 CloudNet 주말 스터디 내용을 참고로 작성되었습니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9 dfa44 a27 ff431 dafdd2 edacc8 a1863  

계속 테스트하며  내용과 설명이 업데이트됩니다.



Flux로 Git 소스를 가지고, 쿠버네티스에 배포 할수 있다!!





<1> Flux CD - 쿠버네티스를 위한 gitops 도구

<2> GitOps Dashboard - Weave GitOps

<3> Weave GitOps 접속을 위한 Ingress 설정

<4> hello world (kustomize)

<5> 애플리케이션 생성,삭제 과정

<6> 공식 Docs 샘플 실습 (선택)





<1> Flux CD - 쿠버네티스를 위한 gitops 도구


1

Flux  CD  ? 

A set of Continuous and progressive Delivery solutions for Kuberntes


유튜브 자료   -  자막을  한국어로 해서 보세요


Why GitOps  and Flux

https://www.youtube.com/watch?v=NwAgATWoEcM


참고 블로그

https://malwareanalysis.tistory.com/612



2

EC2에 Flux CLI 설치 및 Bootstrap


https://fluxcd.io/flux/installation/



3

Github Token 발급 ?


https://github.com/topasvga1/topasvga

개인 아이콘 > Setting > 맨아래  Developer settings   > Personal access tokens  Tockens(classic) > 

(암호 입력 gb1!)

맨아래 Generate token 

Generate token 


// GITHUB_TOKEN을   형식   ghp_###  복사해둔다.


https://github.com/topasvga1/fleet-infra





4

# Flux CLI 설치

curl -s https://fluxcd.io/install.sh | sudo bash 

. <(flux completion bash)



# 버전 확인

flux --version

flux version 2.0.0-rc.5



5

# 자신의 Github 토큰과 유저이름 변수 지정

export GITHUB_TOKEN=<your-token>

export GITHUB_USER=<your-username>



export GITHUB_TOKEN=ghp_###

export GITHUB_USER=topasvga1



6

# Bootstrap

# Flux로 깃옵스에 레파지토리를 만든다.

fleet-infra 라고 깃헙 계정에 레파지토리가 생성된다.

## Creates a git repository fleet-infra on your GitHub account.

## Adds Flux component manifests to the repository.

## Deploys Flux Components to your Kubernetes Cluster.

## Configures Flux components to track the path /clusters/my-cluster/ in the repository.


// bootstrap 으로 레파지토리를 만든다.


flux bootstrap github --owner=$GITHUB_USER --repository=fleet-infra --branch=main --path=./clusters/my-cluster --personal


(1분)




Github에 private  fleet-infra 가 생긴다.






7

# 설치 확인 ?


flux-system 네임스페이스에 pod들이 설치된다.


kubectl get pods -n flux-system

-------------------------------------------------

NAME                                       READY   STATUS    RESTARTS   AGE

helm-controller-fbdd59577-knhzg            1/1     Running   0          29s

kustomize-controller-6b67b54cf8-5f42s      1/1     Running   0          29s

notification-controller-78f4869c94-wmxb4   1/1     Running   0          29s

source-controller-75db64d9f7-xhmg7         1/1     Running   0          29s



8

# 전체 리소스 확인 , crd 확인

kubectl get-all -n flux-system

kubectl get crd | grep fluxc



9

# 깃 리파지토리도 있다~~


gitrepository 확인


kubectl get gitrepository -n flux-system

NAME          URL                                          AGE   READY   STATUS

flux-system   ssh://git@github.com/topasvga1/fleet-infra   60s   True    stored artifact for revision 'main@sha1:e85fa0a392557f733d79b51f6a25408830af2d92'




10

자신의 Github에 private 저장소 확인하자~

my-cluster 폴더에 설치된  fleet-infra  확


https://github.com/





<2> GitOps Dashboard - Weave GitOps



1

링크 → flux 대시보드 설치 : admin / password

https://docs.gitops.weave.works/docs/installation/weave-gitops/#next-steps


# gitops 도구 설치


curl --silent --location "https://github.com/weaveworks/weave-gitops/releases/download/v0.24.0/gitops-$(uname)-$(uname -m).tar.gz" | tar xz -C /tmp


sudo mv /tmp/gitops /usr/local/bin


gitops version


n (분석 n)



2

#  대시보드 설치

PASSWORD="password"


gitops create dashboard ww-gitops --password=$PASSWORD


(1분)



3

# 확인

flux -n flux-system get helmrelease

NAME            REVISION        SUSPENDED       READY   MESSAGE

ww-gitops       4.0.24          False           True    Release reconciliation succeeded



4

kubectl -n flux-system get pod,svc


NAME                                           READY   STATUS    RESTARTS   AGE

pod/helm-controller-fbdd59577-njk67            1/1     Running   0          66m

pod/kustomize-controller-6b67b54cf8-l24cq      1/1     Running   0          66m

pod/notification-controller-78f4869c94-hp8hd   1/1     Running   0          66m

pod/source-controller-75db64d9f7-xjb8q         1/1     Running   0          66m

pod/ww-gitops-weave-gitops-68785f4c77-dz65q    1/1     Running   0          64m


NAME                              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE

service/notification-controller   ClusterIP   10.100.101.155   <none>        80/TCP     66m

service/source-controller         ClusterIP   10.100.121.78    <none>        80/TCP     66m

service/webhook-receiver          ClusterIP   10.100.157.181   <none>        80/TCP     66m

service/ww-gitops-weave-gitops    ClusterIP   10.100.232.74    <none>        9001/TCP   64m






<3> Weave GitOps 접속을 위한 Ingress 설정


위브 = 꾸미다.짜다.

접속을 위해 인그레스 설정


1

도메인

MyDomain=masterseo1.link

MyDomain=taeho11.co.kr



2

인증서

콘솔에서 cert manger 에서 설정하자.

taeho11.co.kr


*.taeho11.co.kr



3

route53에서 인증 설정하자.

*.taeho11.co.kr  cname value



4

CERT_ARN=`aws acm list-certificates --query 'CertificateSummaryList[].CertificateArn[]' --output text`


echo $CERT_ARN

arn:aws:acm:ap-northeast-2:319485572629:certificate/a594dbb242b9521




5

# Ingress 설정


cat <<EOT > gitops-ingress.yaml

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  name: gitops-ingress

  annotations:

    alb.ingress.kubernetes.io/certificate-arn: $CERT_ARN

    alb.ingress.kubernetes.io/group.name: study

    alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}, {"HTTP":80}]'

    alb.ingress.kubernetes.io/load-balancer-name: myeks-ingress-alb

    alb.ingress.kubernetes.io/scheme: internet-facing

    alb.ingress.kubernetes.io/ssl-redirect: "443"

    alb.ingress.kubernetes.io/success-codes: 200-399

    alb.ingress.kubernetes.io/target-type: ip

spec:

  ingressClassName: alb

  rules:

  - host: gitops.$MyDomain

    http:

      paths:

      - backend:

          service:

            name: ww-gitops-weave-gitops

            port:

              number: 9001

        path: /

        pathType: Prefix

EOT


kubectl apply -f gitops-ingress.yaml -n flux-system



(1분후 웹 접속)

https://gitops.taeho11.co.kr/



인그레스 ADDRESS 주소 확인



6

# 배포 확인


kubectl get ingress -n flux-system

-------------------------------------------------------------------

gitops-ingress   alb     gitops.taeho11.co.kr   myeks-ingress-alb-1644550780.ap-northeast-2.elb.amazonaws.com   80      9s


// elb가 생성되어야 접속 가능~~~~~




7

# GitOps 접속 정보 확인 >> 웹 접속 후 정보 확인


echo -e "GitOps Web https://gitops.$MyDomain"


GitOps Web https://gitops.taeho11.co.kr


admin/pass



8

Application 둘러보기






<4> hello world (kustomize)


쿱스트마이즈

Kustomize는 kustomization 파일을 통해 쿠버네티스 오브젝트를 사용자가 원하는 대로 변경하는(customize) 독립형 도구이다.




1

참고 

https://github.com/sungwook-practice/fluxcd-test


github에 있는 nginx manifest를 쿠버네티스에 배포합니다. 

배포할 때 kusotmize를 사용한다.


Kustomize를 사용 하면 선언적 "kustomization" 파일을 사용하여 Kubernetes 매니페스트 파일을 관리할 수 있습니다. 

 https://www.eksworkshop.com/docs/introduction/kustomize/









2

# 소스 생성 : 유형은  git, helm, oci, bucket 이 가능하다.

# flux create source {소스 유형}




예제를 nginx 사용

# 여기서는 소스를  git으로  사용

# 악분(최성욱)님이 준비한 repo로 git 소스 생성


GITURL="https://github.com/sungwook-practice/fluxcd-test.git"


flux create source git nginx-example1 --url=$GITURL --branch=main --interval=30s



플럭스가 GIT을 원천 소스로 해서 사용한다.

생성!


플럭스가 nginx 사용 확인

 nginx-example1




3

# 소스 확인


flux get sources git


(aws-topas3-06-26-iam@myeks:ack-system) [root@myeks-bastion ~]# flux get sources git

NAME            REVISION                SUSPENDED       READY   MESSAGE

nginx-example1  main@sha1:4478b54c      False           True    stored artifact for revision 'main@sha1:4478b54c'




kubectl -n flux-system get gitrepositories

-------------------------------------------------------------------------------------------------

NAME          URL                                          AGE   READY   STATUS

nginx-example1   https://github.com/sungwook-practice/fluxcd-test.git   2m53s   True    stored artifact for revision 'main@sha1:4478b54cb7a8eaf1ee2665e2b3dd5bcfd55e9da9'





4

watch -d kubectl get ingress,svc,pods -A





<5> 애플리케이션 생성,삭제 과정 (선택)



1

flux 애플리케이션 생성 ?

: 유형(kustomization) 

깃 소스 경로( —path ./nginx) → gitops 웹 대시보드에서 확인



2

# [터미널] 모니터링

watch -d kubectl get pod,svc nginx-example1



3

# flux 애플리케이션 생성 : 

nginx-example1


flux create kustomization nginx-example1 --target-namespace=default --interval=1m --source=nginx-example1 --path="./nginx" --health-check-timeout=2m



4

# 확인

 k ns default

Context "aws-topas3-06-26-iam@myeks.ap-northeast-2.eksctl.io" modified.

Active namespace is "default".



kubectl get pod,svc nginx-example1

NAME                 READY   STATUS    RESTARTS   AGE

pod/nginx-example1   1/1     Running   0          8m20s


NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE

service/nginx-example1   ClusterIP   10.100.151.110   <none>        80/TCP    8m20s




kubectl get kustomizations -n flux-system

NAME             AGE   READY   STATUS

flux-system      70m   True    Applied revision: main@sha1:e85fa0a392557f733d79b51f68830af2d92

nginx-example1   55m   True    Applied revision: main@sha1:4478b54cb7a8eaf1ee26dd5bcfd55e9da9




# flux로도 볼수 있다!!


flux get kustomizations

NAME            REVISION                SUSPENDED       READY   MESSAGE

flux-system     main@sha1:e85fa0a3      False           True    Applied revision: main@sha1:e85fa0a3

nginx-example1  main@sha1:4478b54c      False           True    Applied revision: main@sha1:4478b54c






5

애플리케이션 삭제



6

# [터미널] 모니터링

watch -d kubectl get pod,svc nginx-example1




7

# flux 애플리케이션 삭제 >> 파드와 서비스는? flux 애플리케이션 생성 시 

--prune 옵션 false(default 값) = 쿠버네티스 리소스는 삭제 되지 않는다.


flux get kustomizations

flux delete kustomization nginx-example1



kubectl get pod,svc nginx-example1




8

# flux 애플리케이션 다시 생성 :  --prune 옵션 true


flux create kustomization nginx-example1 --target-namespace=default --prune=true --interval=1m --source=nginx-example1 --path="./nginx" --health-check-timeout=2m



9

# 확인

flux get kustomizations


kubectl get pod,svc nginx-example1



10

# flux 애플리케이션 삭제 >> 파드와 서비스는? 

flux로 지울수 있다.


flux delete kustomization nginx-example1



y를 누르는 질문이 나와야 한다.



모니터링 상태  = 지워짐


Every 2.0s: kubectl get pod,svc nginx-example1                                                                                                                                               Wed Jun 28 18:00:31 2023

Error from server (NotFound): pods "nginx-example1" not found

Error from server (NotFound): services "nginx-example1" not found




flux get kustomizations

kubectl get pod,svc nginx-example1




11

# flux 소스 삭제

flux delete source git nginx-example1


//prune 옵션 true 로  쿠버네티스에서도 삭제 된다.

// 디폴트는 false로 쿠버네티스에서 삭제되지 않는다.



12

# 소스 확인

flux get sources git


kubectl -n flux-system get gitrepositories






<6> 공식 Docs 샘플 실습 (선택)


1

https://fluxcd.io/flux/get-started/


# Clone the git repository : 자신의 Github 의 Username, Token 입력

git clone https://github.com/$GITHUB_USER/fleet-infra

Username for 'https://github.com': <자신의 Github 의 Username>

Password for 'https://gasida@github.com': <자신의 Github의 Token>


# 폴더 이동

cd fleet-infra

tree



2

Add podinfo repository to Flux

https://github.com/stefanprodan/podinfo



# GitRepository yaml 파일 생성

flux create source git podinfo --url=https://github.com/stefanprodan/podinfo --branch=master --interval=30s --export > ./clusters/my-cluster/podinfo-source.yaml



3

# GitRepository yaml 파일 확인

cat ./clusters/my-cluster/podinfo-source.yaml | yh


# Commit and push the podinfo-source.yaml file to the fleet-infra repository >> Github 확인

git config --global user.name "Your Name"

git config --global user.email "you@example.com"

git add -A && git commit -m "Add podinfo GitRepository"

git push

Username for 'https://github.com': <자신의 Github 의 Username>

Password for 'https://gasida@github.com': <자신의 Github의 Token>



4

# 소스 확인

flux get sources git

kubectl -n flux-system get gitrepositories




5

Deploy podinfo application : Configure Flux to build and apply the kustomize directory located in the podinfo repository

https://fluxcd.io/flux/get-started/#deploy-podinfo-application




6

# [터미널]

watch -d kubectl get pod,svc


# Use the flux create command to create a Kustomization that applies the podinfo deployment.


flux create kustomization podinfo --target-namespace=default --source=podinfo --path="./kustomize" --prune=true --interval=5m --export > ./clusters/my-cluster/podinfo-kustomization.yaml



7

# 파일 확인

cat ./clusters/my-cluster/podinfo-kustomization.yaml | yh


# Commit and push the Kustomization manifest to the repository:

git add -A && git commit -m "Add podinfo Kustomization"

git push


# 확인

kubectl get pod,svc

kubectl get kustomizations -n flux-system

flux get kustomizations

tree




8

Watch Flux sync the application


# [터미널]

watch -d kubectl get pod,svc


# 파드 갯수 변경 시도 >> 어떻게 되는가?

kubectl scale deployment podinfo --replicas 1

...

kubectl scale deployment podinfo --replicas 3

...


#

flux delete kustomization podinfo

flux delete source git podinfo


#

flux uninstall --namespace=flux-system




9

Github에 fleet-infra Repo 제거하기




10

GitOps with Amazon EKS Workshop | Flux and ArgoCD

https://www.youtube.com/watch?v=dONzzCc0oHo&feature=youtu.be








다음과정

https://brunch.co.kr/@topasvga/3309



전체 보기

https://brunch.co.kr/@topasvga/3217


감사합니다.



                    

매거진의 이전글 EKS 8탄-15. EKS 자동화1- ACK

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari