티스토리 뷰

반응형

Kubernetes에서 보안 및 성능 최적화하기

1. 개요

Kubernetes에서 PostgreSQL 및 Nginx Reverse Proxy를 운영할 때, 보안 및 성능 최적화는 필수적입니다. 이 글에서는 네트워크 보안, 접근 제어, 성능 튜닝 등의 최적화 방법을 설명합니다.


2. 보안 최적화

2.1 네트워크 보안: 네트워크 정책(Network Policy) 적용

반응형

기본적으로 Kubernetes에서는 Pod 간의 모든 통신이 허용됩니다. 특정 서비스 간의 트래픽을 제한하려면 Network Policy를 설정해야 합니다.

아래 예제는 PostgreSQL Pod에 대한 외부 접근을 차단하고, Nginx Ingress Controller만 허용하는 정책입니다.

📌 network-policy.yaml 파일 생성

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: postgres-network-policy
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/name: postgresql
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: nginx-ingress
    ports:
    - protocol: TCP
      port: 5432

적용:

kubectl apply -f network-policy.yaml

이제 PostgreSQL은 Ingress Controller를 통해서만 접근 가능합니다.


2.2 Role-Based Access Control (RBAC) 설정

Kubernetes에서는 사용자의 권한을 제어하기 위해 **RBAC (Role-Based Access Control)**을 활용할 수 있습니다.

📌 rbac.yaml 파일 생성 (PostgreSQL 관리자 전용)

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: postgres-admin-role
rules:
- apiGroups: [""]
  resources: ["pods", "services", "persistentvolumeclaims"]
  verbs: ["get", "list", "create", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: postgres-admin-binding
  namespace: default
subjects:
- kind: User
  name: postgres-admin
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: postgres-admin-role
  apiGroup: rbac.authorization.k8s.io

적용:

kubectl apply -f rbac.yaml

이제 postgres-admin 사용자만 PostgreSQL 관련 리소스를 관리할 수 있습니다.


3. 성능 최적화

3.1 PostgreSQL 성능 튜닝

Kubernetes에서 PostgreSQL 성능을 최적화하려면 Shared Buffers, Work Mem, Connection Pooling 등을 조정해야 합니다.

📌 PostgreSQL ConfigMap 설정 (postgres-config.yaml)

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  namespace: default
data:
  postgresql.conf: |
    shared_buffers = 256MB
    work_mem = 64MB
    max_connections = 100

적용:

kubectl apply -f postgres-config.yaml

이제 PostgreSQL Pod에서 이 설정을 사용하도록 해야 합니다.

containers:
  - name: postgresql
    image: bitnami/postgresql:latest
    volumeMounts:
      - name: config-volume
        mountPath: /opt/bitnami/postgresql/conf
volumes:
  - name: config-volume
    configMap:
      name: postgres-config

3.2 Nginx Reverse Proxy 성능 튜닝

Nginx의 성능을 최적화하기 위해, Keepalive Connections, Buffering, Worker Process 설정을 조정해야 합니다.

📌 Nginx ConfigMap 설정 (nginx-config.yaml)

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    worker_processes auto;
    events {
      worker_connections 1024;
    }
    http {
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      keepalive_timeout 65;
      types_hash_max_size 2048;
    }

적용:

kubectl apply -f nginx-config.yaml

이제 Nginx Ingress Controller가 최적화된 설정을 사용하게 됩니다.


4. 로깅 및 모니터링

4.1 Prometheus & Grafana 설치

Kubernetes에서 PostgreSQL 및 Nginx를 모니터링하려면 Prometheus와 Grafana를 활용할 수 있습니다.

📌 Helm을 이용한 설치

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

# Prometheus 설치
helm install prometheus prometheus-community/kube-prometheus-stack

Grafana UI에 접근하려면 다음 명령어를 실행합니다.

kubectl port-forward svc/prometheus-grafana 3000:80

이제 브라우저에서 http://localhost:3000에 접속하여 Grafana 대시보드를 확인할 수 있습니다.


5. 결론

이제 Kubernetes에서 PostgreSQL과 Nginx Reverse Proxy의 보안을 강화하고 성능을 최적화하는 방법을 배웠습니다. 다음 단계에서는 자동화된 배포 및 운영 방법을 다룰 예정입니다.

 

 

※ 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함
반응형