环境:centos7.2—centos7.6
前提:安装好docker k8s
安装的步骤和方法
第一步:创建configmap(全局配置文件)
apiVersion: v1
kind: ConfigMap
metadata:
name:postgres-config
labels:
app: postgres
data:
POSTGRES_DB:postgres
POSTGRES_USER:postgres
POSTGRES_PASSWORD:123456
第二步:创建持久存储声明和存储
根据数据量大小来定义持久存储大小。
kind:PersistentVolume
apiVersion:v1
metadata:
name: postgres-pv-volume
labels:
type: local
app: postgres
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/mnt/data"
---
kind:PersistentVolumeClaim
apiVersion:v1
metadata:
name: postgres-pv-claim
labels:
app: postgres
spec:
storageClassName: manual
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Gi
第三部:创建部署
apiVersion:extensions/v1beta1
kind:Deployment
metadata:
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.4
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
第四部:创建服务
apiVersion:v1
kind:Service
metadata:
name: postgres
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres
分别执行下列命令:
kubectl create -f postgres-configmap.yaml
kubectlcreate -f postgres-storage.yaml
kubectlcreate -f postgres-deployment.yaml
kubectlcreate -f postgres-service.yaml
然后使用命令查看数据库端口号
kubectlget svc postgres
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
postgres NodePort 172.107.54.253 5432:31090/TCP 5m
我们需要使用端口31070从kubernetes集群中存在的机器/节点连接到PostgreSQL,并使用前面configmap中给出的凭据。
$psql -h localhost -U postgres --password -p 31090 postgres
Passwordfor user postgres:
psql(10.4)
Type"help" for help.
postgresdb=#
(记得点赞,感谢)