PostgresSQL----Deploy PostgresSQL based on Kubernetes

[PostgresSQL----Deploy PostgresSQL based on Kubernetes]


1. Create SC, PV and PVC storage objects

1.1 Prepare an nfs server

If there is no nfs server, you can refer to NFS----Building an NFS server to deploy one.

1.2 Compile SC, PV, PVC and other storage resource files

The following resource file contains declarations of SC, PV, and PVC resources. The following locations are mainly modified:

  • Namespace
    The namespace needs to be modified to your own namespace. There are three places in the configuration that need to be modified.
  • nfs server ip address
    Set the nfs server ip address
  • nfs server for mounting directory
    Set the directory path to mount the storage directory on the nfs server
  • Storage space size
    The storage space size is set according to actual needs. Note that the storage space size in PV and PVC must be consistent.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: sc-postgres
  namespace: my-namespacce                             # 命名空间
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  pv-postgres
  namespace: my-namespacce                             # 命名空间
  labels:
    pv: pv-postgres
spec:
  capacity:
    storage: 5Gi                                       # 存储空间大小
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: sc-postgres
  nfs:
    path: /path/to/nfs/server/                         # nfs 服务器供挂载目录
    server: nfs-server-ip                              # nfs 服务器ip地址
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-postgres
  namespace: my-namespacce                             # 命名空间
  labels:
    pvc: pvc-postgres
spec:
  storageClassName: sc-postgres
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi                                     # 存储空间大小
  selector:
    matchLabels:
      pv: pv-postgres

Name the modified configuration file, for example: postgres_pvc_pv_sc.yaml

1.3 Write the resource declaration file for deploying PostgresSQL database

Write the deployment resource file as follows, including declarations of deployment and service. You mainly need to modify the following locations:

  • Namespace
    The namespace needs to be modified uniformly to the namespace consistent with the PV and PVC deployed above.
  • Database configuration
    Configure the user name, password and database name of the database. In addition, you also need to set the maximum number of connections. The default number of connections is 100, which is difficult to meet the demand in actual applications, so it is best to set it directly during deployment.
  • External open port
    External open port is used for external access. It is usually needed in the test environment. It is usually not needed when considering security in the production environment. It is deployed according to the usage method of the test environment, that is, using the NodePort type Service, setting Externally developed ports are sufficient

There is no need to modify other configurations. If modified, they need to be unified with the above configuration file names such as PV and PVC.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: my-namespace                                 # 命名空间
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:latest
          env:
            - name: POSTGRES_USER
              value: postgres                             # 用户名
            - name: POSTGRES_PASSWORD
              value: postgres                             # 密码
            - name: POSTGRES_DB
              value: postgres                             # 数据库名
            - name: TZ
              value: Asia/Shanghai
            - name: POSTGRES_MAX_CONNECTIONS
              value: "20000"                              # 最大连接数
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: pvc-postgres

---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: my-namespace                                 # 命名空间
spec:
  selector:
    app: postgres
  ports:
    - port: 5432
      protocol: TCP
      targetPort: 5432
      nodePort: 30101                                      # 对外开放端口
  type: NodePort

Save the modified configuration file as postgres.yaml file

2. Deploy PostgresSQL

2.1 Deploy PV, PVC and other storage objects

Just execute the following command

kubectl apply -f postgres_pvc_pv_sc.yaml

2.2 Deploy PostgresSQL database

Just execute the following command

kubectl apply -f postgres.yaml

2.3 Check whether the creation is successful

Execute the following command to replace my-namespace with your own namespace. If you see that the pod status is running, it means the deployment is successful.

kubectl get pod -n my-namespace

Guess you like

Origin blog.csdn.net/redrose2100/article/details/132769633