kubernetes

deploy postgreSQL on kubernetes

PSAwesome 2022. 12. 20. 00:18
반응형

이번 글은 postgres 배포입니다.

곧 사용할 것 같은 RDB이고, 큰 비중은 없어서 postgres 선택했습니다.

 

아래와 같은 순서로 진행됩니다.

  1. 네임스페이스 작성
  2. 컨피그맵 작성
  3. 스토리지 작성
    1. 퍼시스턴트 볼륨
    2. 퍼시스턴트 볼륨 클래임
  4. 배포(deployment) 작성
  5. 서비스 작성
  6. 배포 실행
  7. hello world 입력
# vim postgres-ns

apiVersion: v1
kind: Namespace
metadata:
  name: db
  • 네임스페이스 생성
# vim postgres-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  namespace: db
  labels:
    app: postgres
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: admin
  POSTGRES_PASSWORD: over090
  • configmap 생성

 

# vim postgres-storage.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  namespace: db
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  • PersistentVolume, PersistentVolumeClaim 생성

 

# postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
  • deploy 생성

 

# vim postgres-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: db
  labels:
    app: postgres
spec:
  selector:
    app: postgres
  ports:
    - port: 5432
  • service 생성

 

kubectl apply -f .
  • 작성한 파일 모두 deploy

 

k exec -it postgres-67d4f7cc7d-dqjms -n db -- psql -h localhost -U admin --password -p 5432 postgresdb

 

반응형