본문 바로가기
IT 개발/클라우드

[쿠버네티스] 쿠버네티스 클러스터 관리와 보안: 운영자 가이드

by 이것 저것 모든것 2025. 2. 6.
728x90

들어가며

이전 포스트에서 다룬 애플리케이션 배포와 운영 팁에 이어, 이번에는 쿠버네티스 클러스터 자체의 관리와 보안에 대해 알아보겠습니다.

1. 클러스터 접근 제어

RBAC (Role-Based Access Control)

# Role 생성
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: development
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

---
# RoleBinding 생성
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: development
subjects:
- kind: User
  name: jane
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Service Account 관리

apiVersion: v1
kind: ServiceAccount
metadata:
  name: app-service-account
  namespace: production

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: app-service-account-binding
  namespace: production
subjects:
- kind: ServiceAccount
  name: app-service-account
  namespace: production
roleRef:
  kind: Role
  name: app-role
  apiGroup: rbac.authorization.k8s.io

2. 네트워크 보안

Network Policy 상세 설정

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: strict-network-policy
spec:
  podSelector:
    matchLabels:
      app: secure-app
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          purpose: production
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 8080
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          purpose: production
    ports:
    - protocol: TCP
      port: 5432  # DB 접근

TLS 인증서 관리

# Certificate 리소스 (cert-manager 사용)
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: app-tls
  namespace: production
spec:
  secretName: app-tls-secret
  duration: 2160h  # 90일
  renewBefore: 360h  # 15일 전 갱신
  subject:
    organizations:
    - My Company
  commonName: app.example.com
  dnsNames:
  - app.example.com
  - www.app.example.com
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer

3. 컨테이너 보안

Pod Security Context

apiVersion: v1
kind: Pod
metadata:
  name: secure-pod
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    fsGroup: 2000
  containers:
  - name: secure-app
    image: secure-app:1.0
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop:
        - ALL
      readOnlyRootFilesystem: true

Image Security

# ImagePullSecret 생성
apiVersion: v1
kind: Secret
metadata:
  name: registry-secret
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: <base64-encoded-docker-config>

---
# Pod에서 사용
apiVersion: v1
kind: Pod
metadata:
  name: private-app
spec:
  imagePullSecrets:
  - name: registry-secret
  containers:
  - name: app
    image: private-registry.example.com/app:1.0
728x90

4. 클러스터 모니터링과 감사

Audit Policy 설정

apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
  resources:
  - group: ""
    resources: ["secrets", "configmaps"]
- level: Metadata
  resources:
  - group: ""
    resources: ["pods", "services"]

Prometheus Operator 설정

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: cluster-monitor
spec:
  selector:
    matchLabels:
      k8s-app: kubernetes-nodes
  endpoints:
  - port: https
    scheme: https
    interval: 30s
    bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
    tlsConfig:
      insecureSkipVerify: true

5. 백업과 복구

Velero를 이용한 백업 설정

apiVersion: velero.io/v1
kind: Backup
metadata:
  name: daily-backup
spec:
  includedNamespaces:
  - production
  - staging
  excludedResources:
  - secrets
  ttl: 720h  # 30일
  hooks:
    resources:
    - name: backup-hook
      includedNamespaces:
      - production
      labelSelector:
        matchLabels:
          app: database
      pre:
        - exec:
            command:
            - /bin/sh
            - -c
            - pg_dump > /backup/db.sql

6. 클러스터 유지보수

노드 유지보수 프로세스

# 1. 노드 드레인
kubectl drain <node-name> --ignore-daemonsets

# 2. 노드 작업 수행
# ... 시스템 업데이트, 커널 패치 등 ...

# 3. 노드 복구
kubectl uncordon <node-name>

자동 노드 복구 설정

apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
  name: auto-repair
handler: autorepair
scheduling:
  nodeSelector:
    kubernetes.io/os: linux
  tolerations:
  - key: node-role.kubernetes.io/master
    operator: Exists
    effect: NoSchedule

7. 보안 체크리스트

1. 클러스터 레벨

  • RBAC 정책 구현
  • 네트워크 정책 설정
  • 감사 로깅 활성화
  • etcd 암호화 설정
  • 컨트롤 플레인 보안 강화

2. 워크로드 레벨

  • Pod Security Policy 적용
  • 컨테이너 취약점 스캔
  • 시크릿 관리 시스템 사용
  • 리소스 제한 설정
  • 네트워크 분리 구현

마치며

클러스터 관리와 보안은 지속적인 관리와 모니터링이 필요한 영역입니다. 주요 포인트는:

  1. 최소 권한 원칙 적용
  2. 정기적인 보안 감사 수행
  3. 자동화된 백업 및 복구 절차 구현
  4. 지속적인 모니터링 및 알림 체계 구축

이것으로 쿠버네티스 시리즈를 마무리하겠습니다. 실제 운영 환경에서 이러한 가이드라인들을 참고하여 안전하고 효율적인 클러스터 운영에 도움이 되길 바랍니다.

728x90