Fullstack-Study-241204-250625

커리큘럼(12-30/변경)

01. Java
02. git
03. Database 
04. Jsp [Server]

05. HTML,CSS 
07. JS
06. 미니프로젝트-2W

08. SpringFramework , SrpingBoot 
19. 중간프로젝트 (1M)
10. Linux 명령어
11. AWS
12. Docker
13. Kubernetes(v)
14. React JS [Front-end]
15. App - Android
16. 최종프로젝트 (1M)

쿠버네티스

레플리카세트(ReplicaSet)

레플리카셋

# first.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp # ReplicaSet 객체의 "이름"입니다 (Service와 무관)

spec:
  selector: 
    matchLabels: 
      app: webapp # 아래에 있는 template.labels.app과 연결되서 파드 관리
  replicas: 1
  template: # 복제할 포드를 템플릿으로 정의함
    metadata:
      labels:
        app: webapp # 서비스와 연결

    spec:
      containers: 
      - name: webapp 
        image: coding404/k8s-coding404-webapp-react:v0-1
  • kind: ReplicaSet : 리소스의 유형이 레플리카셋임
  • selector.matchLabels : 레플리카셋이 관리할 Pod를 고르는 기준
  • replicas : 유지하려는 Pod 복제본 수
  • template : 레플리카셋이 복제할 Pod의 템플릿
  • spec.containers : 생성할 Pod 안에 실행할 컨테이너 목록

디플로이먼트(Deployment)

디플로이먼트

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp

spec:
  minReadySeconds: 30 # Pod가 Ready 상태가 된 후에도 최소 30초간 더 기다려야 다음 단계로 넘어감.
  selector: 
    matchLabels: 
      app: webapp # 아래에 있는 template.labels.app과 연결되서 파드 관리
  replicas: 2
  template: # 복제할 포드를 템플릿으로 정의함
    metadata:
      labels:
        app: webapp # 내가 service와 연결됨~~

    spec:
      containers: 
      - name: webapp 
        # image: coding404/k8s-coding404-webapp-react:v0 # 기존버전
				image: coding404/k8s-coding404-webapp-react:v0-1 # 버전을 올려봅니다

kubectl rollout status deploy 디플로이명

kubectl rollout restart deploy 디플로이명

kubectl rollout history deploy 디플로이명

kubectl rollout undo deploy 디플로이명 # 바로직전으로 롤백 kubectl rollout undo deploy 디플로이명 --to-revision=1

쿠버네티스 네트워크

항목 ClusterIP NodePort
기본목적 클러스터 내부통신전용 외부에서 클러스터안으로 접속허용
접근가능위치 클러스터내부에서만접근가능 외부(노드IP+포트)에서 접근가능
할당되는 IP 클러스터내 가상IP(ClusterIP) 각Node에 열린 고정포트(30000~32767)
트래픽흐름 내부Pod → Service → 연결된Pod 외부요청(NodeIP:Port) → Node → Service → Pod
사용예시 마이크로서비스 내부호출용 간단한 테스트용 외부오픈, 작은서비스용

1. NodePort

NodePort

2. ClusterIP

ClusterIp

큐브DNS(kube-dns)와 네임스페이스

큐브DNS

네임스페이스

kubectl get ns

kubectl get all -n kube-system

  1. pod에 데이터베이스 설치 후 service연결
# postgres.yaml

apiVersion: v1
kind: Pod
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  containers:
  - name: postgres
    image: postgres:14 # 포스트그레 공식 도커 이미지
    env:
    - name: POSTGRES_USER  # 초기슈퍼사용자 유저명
      value: "postgres"
    - name: POSTGRES_PASSWORD  # 비밀번호
      value: "1234"
    - name: POSTGRES_DB  # 기본 데이터베이스 이름
      value: "postgres"
    - name: PGDATA  # 데이터 저장 경로 (선택적)
      value: "/var/lib/postgresql/data/pgdata"
--- 
kind: Service
apiVersion: v1
metadata:
  name: database-postgres
spec:
  selector:
    app: postgres # 메타데이터의 label값
  ports:
    - name: dbport
      port: 5432
  type: ClusterIP # 외부에서는 볼 수 없고, 내부에서 다른 pod가 참조할 수 있도록 함
  1. webapp pod안으로 들어가서 database서비스 발견
  • 웹앱 파드로 접속 kubectl exec -it pod pod명 -- sh
  1. DNS이름이 확인 가능한 파일 열어보기

cat /etc/resolv.conf

  1. kube-dns에 database의 아이피주소 요청

nslookup database-postgres

  1. webapp내부(리눅스) 에서 database연결
apk update
apk add postgresql-client
  • postgres 접속 데이블생성
# -h 접속아이피
# -U 유저명
# -d 데이터베이스명
psql -h database-postgres -U postgres -d postgres
# 비밀번호 1234

# 테이블 생성
create table example (test varchar(255));

# 테이블 확인하기
\dt

<컨테이너명>.default.svc.cluster.local

<컨테이너명>.<네임스페이스>.svc.cluster.local