1、使用如下yaml文件部署postgres服务
1.1、创建deployment

cat postgres_deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s.kuboard.cn/name: postgres
  name: postgres
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/name: postgres
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s.kuboard.cn/name: postgres
    spec:
      containers:
        - env:
            - name: POSTGRES_DB
              value: sonar
            - name: POSTGRES_USER
              value: sonar
            - name: POSTGRES_PASSWORD
              value: sonar@pw
          image: 'postgres:10.12'
          imagePullPolicy: IfNotPresent
          name: postgres
          ports:
            - containerPort: 5432
              name: postgres-port
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 30

1.2、创建service

cat postgres_svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s.kuboard.cn/name: postgres
  name: postgres
  namespace: default
spec:
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  ports:
    - name: postgres-port
      nodePort: 31273
      port: 5432
      protocol: TCP
      targetPort: 5432
  selector:
    k8s.kuboard.cn/name: postgres
  type: NodePort

2、应用yaml文件创建服务

kubectl apply -f postgres_deploy.yaml
kubectl apply -f postgres_svc.yaml

3、查看是否创建成功

[root@master ~]# kubectl get pod,svc
NAME                            READY   STATUS    RESTARTS   AGE
pod/postgres-7dbc947c79-gvgvb   1/1     Running   0          1h

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/postgres     NodePort    10.106.116.191   <none>        5432:31273/TCP   1h

4、postgres 服务部署完成,可以使用集群内地址或者nodeport方式进行 postgres 数据库连接

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐