我正在做一个学校项目,我有裸机 VPS,我正在尝试使用 Postgres 数据库(必须可以从外部访问,即:5432)部署 Web 应用程序(nginx 入口)。我在 Google 和 StackOverflow 上浏览了数十个链接,但实际上没有任何结果 - 我还在Connection refused. Is the server running on host "<>" and accepting TCP/IP connections on port 5432?
我已经通过几个简单的步骤成功部署了 MetalLB:
- 安装 metallb
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/metallb.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl apply -f - -n kube-system
- 创建 config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: production
protocol: layer2
addresses:
- <VPS-external-IP>-<VPS-external-IP>
- 创建 LoadBalancer 服务
apiVersion: v1
kind: Service
metadata:
name: nginx-balancer
namespace: nginx
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
- name: https
port: 443
protocol: TCP
targetPort: 443
- name: db
port: 5432
protocol: TCP
targetPort: 5432
selector:
app: nginx
type: LoadBalancer
- 并创建了 Nginx 部署(nginx-deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
- containerPort: 443
现在在runnign之后kubectl apply -f config.yaml
,kubectl apply -f nginx-deployment.yaml
我能够解决“欢迎使用nginx!” curl <VPS-external-IP>:80
输出:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-7f8d9cf649-cssrb 1/1 Running 0 117m
nginx-deployment-7f8d9cf649-f7q9w 1/1 Running 0 117m
nginx-deployment-7f8d9cf649-fmntb 1/1 Running 0 117m
kubectl get svc -n nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.108.97.186 <none> 80/TCP,443/TCP 26m
nginx-balancer LoadBalancer 10.103.72.75 46.36.38.200 80:32529/TCP,443:30432/TCP,5432:31081/TCP 42m
kubectl get pods -n metallb-system
NAME READY STATUS RESTARTS AGE
controller-57c458c998-r78wn 1/1 Running 0 13h
speaker-clr6j 1/1 Running 0 13h
kubectl get configmap -n metallb-system
NAME DATA AGE
config 1 13h
kube-root-ca.crt 1 13h
但真正的问题现在来了。我需要使用 :5432 访问 Postgres 数据库,但老实说,我只是迷失在 Kubernetes 中......
我已经为 Postgres 创建了所有必要的东西,以使其运行,例如:
- PersistentVolume 和 PersistentVolumeClaim(我认为这里没有必要分享,因为它只是基本的 PersistentVolume 和 PersistentVolumeClaim yaml)
- 秘密(我认为这里没有必要分享,因为它只是yaml的基本秘密)
- 配置映射
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-configmap
namespace: postgres
data:
POSTGRES_DB: db_production
- Postgres 部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
namespace: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:latest
ports:
- containerPort: 5432
envFrom:
- secretRef:
name: postgres-secrets
- configMapRef:
name: postgres-configmap
volumeMounts:
- name: postgres-pv-claim
mountPath: /var/lib/pgsql/data
volumes:
- name: postgres-pv-claim
persistentVolumeClaim:
claimName: postgres-pv-claim
- 当然 - Postgres 服务
apiVersion: v1
kind: Service
metadata:
name: db
namespace: postgres
labels:
run: postgres
spec:
selector:
name: postgres
ports:
- port: 5432
targetPort: 5432
protocol: TCP
postgres 本身似乎工作。
kubectl get pods -n postgres
NAME READY STATUS RESTARTS AGE
postgres-deployment-74fff7c576-6kb5q 1/1 Running 0 96m
kubectl get svc -n postgres
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.102.5.192 <none> 5432/TCP 93m
kubectl get deployments -n postgres
NAME READY UP-TO-DATE AVAILABLE AGE
postgres-deployment 1/1 1 1 94m
kubectl get pv -n postgres
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
postgres-pv 1Gi RWO Retain Bound postgres/postgres-pv-claim manual 55m
kubectl get pvc -n postgres
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
postgres-pv-claim Bound postgres-pv 1Gi RWO manual 55m
(我已经删除并重新创建了很多次(因此为什么这么低的年龄...... :-())
因此,即使我在 5432 上运行了 postgres/db 服务,并且在 Nginx 上运行了 MetalLB LoadBalancer,它只是不会将 5432 暴露给世界。我会对所有建议感到非常高兴,因为我开始放松一开始的炒作,即设置 Kubernetes 会有多好...... :-)
谢谢你。
更新 31.1.2022
我已经使用 Kubeadm 逐步安装了 Kubernetes,并使用了几个在线教程和官方文档。的输出kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:25:17Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:19:12Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
和kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:24:08Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}