-
(2024.10.17)
기본 환경 세팅
- kubernetes ingress 접근
* service: L4 수준의 로드밸런서 - ingress: 요청을 원하는 pod로 분산(로드밸런싱), L7 계층 -> 클러스터 외부에서 http요청을 통해서 라우팅해서 pod로 분산
이미지 생성
docker build --no-cache -t testweb:1.0 ./
object 생성
1. service object
- 외부에서 직접 접근이 아니라 ingress를 통해서 접근하므로 yaml -> ClusterIP type으로 생성
* port: 서비스 포트, targetPort: 포드의 포트
2. ingress object
ingress.yaml
- ingress 실행 규칙을 지정한 파일
- ingress nginx 컨트롤러 사용할 예정이므로 yaml에 관련 설정 추가
- host: 도메인 지정해서 해당 경로로 요청 전송(kuweb.beyond.com: kuweb -> 서브 도메인, beyond.com -> 도메인)
(hosts 파일을 수정해서 로컬로 테스트 가능하도록 함) - 과정: url(domain(.com)/endpoint) 요청을 했을 때 컨트롤러가 service에 도메인을 제외한 요청 전달 -> service는 엔드포인트에 지정된 pod로 요청 전송
kubectl apply ingress.yaml
kubectl get ingress, kubectl describe ingress로 확인- rules: host/endpoint로 요청을 넘기면 kuweb-service:8080의 3개 포트 중 하나로 요청을 넘긴다
ingress controller: ingress-nginx
- https://github.com/kubernetes/ingress-nginx 에서 깃 클론
- ingress-nginx/deploy/static/provider/cloud/deploy.yaml -> 366행 type: NodePort로 변경한 후 kubectl apply
- kubectl get all -n ingress-nginx했을 때 create 시 job.batch~가 1회 실행된 후 completions 됨. controller가 running status인지 확인
- 80 포트(http), 443 포트(https) 생략 가능 -> service/ingress-nginx-controller의 포트 생성 확인
- kubectl logs -f ingress-nginx-controller-7f9bbf6ddd-2nwtw -n ingress-nginx로 로그 확인
로컬에서 테스트를 진행하기 위해 hosts 파일에서 host를 등록
- 메모장 관리자 권한으로 실행
mac -> sudo vi /private/etc/hosts
window -> System32/drivers/etc/hosts - 내부에 127.0.0.1 kuweb.beyond.com 추가
- domain:port값으로 요청 전달(ex: kuweb.beyond.com:31438)
* 동작 과정: kuweb-service 위에 ingress를 두고 url open -> 서비스(kuweb-service)가 도커 클러스터로 생성된 test-web1 ~ 3에 요청 분산, 즉 서비스에 직접 연결하지 않음
Argo cd- Https 서버 통신 -> 인증서 발급 필요
- TLS: 네트워크 보안 프로토콜. 데이터를 암호화해서 보호(https 사용할 때 tls 기반으로 구현) -> ingress에 TLS 설정 추가해서 처리
1. cert-manager
https://cert-manager.io/docs/installation/
- kubernetes의 cert-manager를 통해 인증서 발급 -> ingress에 적용
- 인증서 발급, 갱신, 관리 자동화
- 로컬 테스트를 위해 자체 서명 발급
certificates: 인증서
install
helm repo add jetstack https://charts.jetstack.io
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.16.1 --set crds.enabled=true
resource 생성
- cluster-issuer.yaml 생성 -> kubectl apply -f ./cluster-issuer.yaml
apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: selfsigned-cluster-issuer spec: selfSigned: {}
- kubectl get ClusterIssuer -> ready:true 확인
issuer을 통해서 인증서 발급
- ingress에만 TLS 설정하기 위함(외부에서 접근할 때만 https 통신) // 클러스터 내부 pod끼리는 http 통신으로 효율적 관리
- kuweb-ingress.yaml 수정 -> apply
- annotation -> cert-manager cluster-issuer 설정 추가(cluster-issuer의 kind가 ClusterIssuer임)
- tls - secretName 추가: ingress에서 사용하는 Name, -hosts 추가: 인증서 적용할 도메인
- kubectl get certificate / kubectl get secret -> tls 생성 확인 -> 이제 https port로 접근해야 한다
* ingress 설정 yaml에 해당 내용 추가했기 때문에 ingress 생성 시 자동으로 생성됨
자체 인증이라서 인증서는 유효하지 않지만 tls 통신한다 (2024.10.18)
2. Argo cd 개요, Install
개요
- GitOps 패턴 사용 -> 애플리케이션의 배포, 관리 자동화
- 깃에서 설정 내용 관리하므로 안정적 배포 환경 제공
- 다양한 클러스터에 접근해서 효율적으로 상태 관리
- Health check 수행
install
- 설치파일
helm repo add argo https://argoproj.github.io/argo-helm
helm repo update
kubectl create namespace argocd
파일 이동: helm show values argo/argo-cd > argo-cd-values.yaml - argo-cd-values.yaml 파일 수정
global:domain: argocd.도메인:443port(e.g. argocd.beyond.com:32464) - agrocd-ingress.yaml 파일 생성
tls: secret를 이용해서 관리, 인증서를 적용할 hosts 설정
*argocd-server: 서비스에 접근할 때 사용하는 pod - 구성파일명을 통해서 argocd 설치
helm install argocd -n argocd argo/argo-cd -f ./argo-cd-values.yaml - secret에 tls 인증서 생성 확인
kubectl get secret -n argocd
kubectl get certificate -n argocd - 로컬에서 테스트를 진행하기 위해 hosts 파일에서 host를 등록
mac -> sudo vi /private/etc/hosts
내부에 127.0.0.1 argocd.beyond.com 추가
안전하지 않음 으로 이동 후 argo cd 리다이렉트 확인
3. 초기 설정
초기 패스워드 확인
(git)bash>
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
- 결과값: (초기 패스워드)(컴퓨터 계정 정보)
개요
- 젠킨스의 경우 서비스를 생성해서 직접 localhost로 접근하는 반면, argocd는 ingress를 통해서 url을 생성해서 도메인으로 접근
- Applications에서 관리 -> git에서 application 정보를 가져와서 git과 클러스터 내부에서 변경된 파일을 대조해서 동기화 작업
레포지토리
- 레포지토리 생성
- argo cd 설정 파일을 보관할 레포지토리 생성 -> 따로 레포지토리를 분리하므로 application별로 관리하기 용이하다
* 관리할 application 내에 설정 파일을 보관할 경우, application 이동 및 관리가 어려울 수 있다. - application 별로 폴더를 생성하여 내부에 해당 애플리케이션에 대한 yaml 파일 저장(deployment, service, ingress)
- argo cd 설정 파일을 보관할 레포지토리 생성 -> 따로 레포지토리를 분리하므로 application별로 관리하기 용이하다
- ssh 키 생성
- ssh-keygen -t ed25519 -C "메일 주소"
- github repository - settings - deploy key 등록(공개키 -> cat id_ed25519.pub) -> Allow write access 허용
- 깃 레포지토리 등록
- settings - repositories - connect repo: VIA SSH, Name(Argo cd에서 식별할 이름), project default, repo url, private key( id_ed25519)
리소스 생성
- git 저장소의 kuweb 폴더 내에 있는 yaml 설정 파일을 통해 클러스터에 리소스 생성
- applications - new app
name: 필요 시 kuweb-dev, kuweb-prod 등 설정
sync policy: 동기화 정책(개발 환경에서 automatic를 쓰기도 함)
Repository URL: argo cd와 연결된 레포지토리 주소
Path: application의 설정 정보 파일을 찾을 경로
destination: 리소스를 생성할 클러스터명(dev, main 등으로 클러스터를 분리하기도 한다)
실행 확인
- OutOfSync: git 저장소와 상태가 일치하지 않아 동기화가 필요한 상태
Sync OK, Healthy: health check 및 동기화가 이루어진 상태
Diff: git 레포지토리 상의 상태와 쿠버네티스에 배포된 상태가 일치하지 않는 경우, 차이 확인 가능
Sync: git 저장소의 내용과 클러스터에서 실행되고 있는 애플리케이션을 동기화
object 생성을 시각적으로 체크할 수 있다. - 변경사항이 있는 경우, deploy에서 버전을 변경한 후 commit change하면 sync status가 OutOfSync로 바뀌는 것 확인 가능
* Sync할 경우, 새로운 replica set을 생성해서 pod 생성 및 롤링 업데이트 진행 후 기존의 pod 삭제하고 배포 - History and Rollback 메뉴를 통해 빠른 롤백 가능
4. jenkins 연결
webhook 연결(ngrok)
- ngrok http http://localhost:30020
dockerfile 변경점
- 데이터를 복사해서 이미지 내에서 빌드를 처리 -> 앞서 만든 base image에서 같은 별칭인 이미지를 복사해 from 2번째 이미지로부터 root 디렉터리로 복사
credentials 추가
- manifests 저장소 접근 credential -> key에 private key 등록
jenkins item 생성
- pipeline
- general: giithub project -> SSH clone url copy
- pipeline: pipeline script from scm -> git -> SSH clone url copy, 저장소 접근 credential 추가
- argo cd 설정 파일을 보관하는 레포지토리(manifests)에 Jenkinsfile 생성
argo cd에서 app 생성
- source
- repository url: argo cd 설정 파일을 보관하는 레포지토리(manifests) ssh clone url copy
ssh agent plugin 플러그인 설치 후 파이프라인 테스트
- department repository 변동 사항 발생
- jenkins - department에서 웹훅 발생
- department 파이프라인 실행해서 docker-hub에 이미지 업로드까지 완료
- department pipeline 에서 이미지 생성 후 trigger 발생해서 manifests pipeline 실행
- manifests pipeline 실행으로 배포 완료 후 department 파이프라인 실행 완료(manifests pipeline 완료까지 대기)
- argo cd에서 sync 확인 후 배포 상태 확인
** 5. 무중단 배포 관련
- deploy.yaml 파일
- probe에 해당하는 요청을 보내면서 지속적으로 health check하고 기존에 있던 pod를 지우고 새로운 pod를 생성하면서 롤링 무중단 배포 환경 구성
- health check에서 실패하면 배포를 실행하지 않는다
health: health check
health/liveness: 시간을 두고 check해서 애플리케이션이 실행 가능한지 확인
health/readiness: 실제로 서비스가 가능한지 확인
- spring 설정 변경
- pom.xml:
management -> enable, health 관련 설정 추가
- pom.xml:
code//
'수업 내용 정리' 카테고리의 다른 글
[Udemy] Spring Security 예외 처리, CORs, CSRF (0) 2024.11.22 [Udemy] Spring Security Basic (1) 2024.11.21 [Udemy] Twitter4j & Kafka (0) 2024.09.23 docker 설정 (0) 2024.08.26 (2024-07-24) bootstrap.yml 설정 (1) 2024.07.24 - kubernetes ingress 접근