简体   繁体   中英

kubernetes how do I expose pods to things outside of cluster machine?

I read the following kubernetes docs which resulted in the following yaml's to run postgresql & pgadmin in a cluster:

--- pgadmin-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgadmin-pod
  template:
    metadata:
      labels:
        app: pgadmin-pod
    spec:
      containers:
        - name: pgadmin-container
          image: dpage/pgadmin4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 80
          env:
            - name: PGADMIN_DEFAULT_EMAIL
              value: email@example.com
            - name: PGADMIN_DEFAULT_PASSWORD
              value: password

--- pgadmin-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: pgadmin-service
spec:
  type: NodePort
  ports:
    - port: 30000
      targetPort: 80
  selector:
    app: pgadmin-pod

--- postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-pod
  template:
    metadata:
      labels:
        app: postgres-pod
    spec:
      containers:
        - name: postgres-container
          image: postgres:9.6-alpine
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: database
            - name: POSTGRES_PASSWORD
              value: password
            - name: POSTGRES_USER
              value: username
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgrepvc
      volumes:
        - name: postgrepvc
          persistentVolumeClaim:
            claimName: postgres-pv-claim

--- postgres-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  type: NodePort
  ports:
    - port: 30001
      targetPort: 5432
  selector:
    app: postgres-pod

--- postgres-storage.yaml
postgres-storage.yaml
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: 5Gi

I then run the following command kubectl create -f./ which results in the following: kubernetes pods / svc's

Then I try to access pgAdmin on 10.43.225.170:30000 from outside of the cluster, but I get "10.43.225.170 took too long to respond." no matter what I try.

So how do I expose pgAdmin & postgress to the outside world, and is there a way to give them static ip's so I don't have to update ip's in connection strings each time I re-deploy on kubernetes, or do I have to use statefulset for this?


Problems here

  1. you are trying to reach node internal ip 10.43.225.170 instead of external one.
  2. nodePort service configured incorrectly. In addition you are trying to call incorrect port

You haven't specified what platform you use. I'm using GKE, so in my case its easier because I have external IP's automatically assigned during cluster node creation. But I had to manually create ingress firewall rule to allow access from outside to nodes and required ports (30000,30001)

In any case, to be able to use nodePort - you should have external IP address assigned to one of the nodes in cluster and a Firewall rule that allows ingress traffic to that port


Going next. You are trying to call <NodeIP>:spec.ports[*].port .

As per Type NodePort documentation :

Service is visible as <NodeIP>:spec.ports[*].nodePort

You need explicitly specify nodePort


I have changed a bit your deployment, can access pgAdmin after deploying it and opening corresponding ports in firewall.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgadmin-pod
  template:
    metadata:
      labels:
        app: pgadmin-pod
    spec:
      containers:
        - name: pgadmin-container
          image: dpage/pgadmin4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 80
          env:
            - name: PGADMIN_DEFAULT_EMAIL
              value: email@example.com
            - name: PGADMIN_DEFAULT_PASSWORD
              value: password

---
apiVersion: v1
kind: Service
metadata:
  name: pgadmin-service
spec:
  type: NodePort
  ports:
    - nodePort: 30000
      targetPort: 80
      port: 80
  selector:
    app: pgadmin-pod

--- postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-pod
  template:
    metadata:
      labels:
        app: postgres-pod
    spec:
      containers:
        - name: postgres-container
          image: postgres:9.6-alpine
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: database
            - name: POSTGRES_PASSWORD
              value: password
            - name: POSTGRES_USER
              value: username
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgrepvc
      volumes:
        - name: postgrepvc
          persistentVolumeClaim:
            claimName: postgres-pv-claim

---
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  type: NodePort
  ports:
    - nodePort: 30001
      targetPort: 5432
      port: 5432
  selector:
    app: postgres-pod

---
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: 5Gi

Check:

kubectl apply -f pg_my.yaml
deployment.apps/pgadmin-deployment created
service/pgadmin-service created
service/postgres-service created
persistentvolume/postgres-pv-volume created
persistentvolumeclaim/postgres-pv-claim created


#In my case I take node external ip from any node from `kubectl get nodes -o wide` output:
NAME                                       STATUS   ROLES    AGE   VERSION            INTERNAL-IP   EXTERNAL-IP
gke-cluster-1-default-pool-*******-*****   Ready    <none>   20d   v1.18.16-gke.502   10.186.0.7    *.*.*.*

curl *.*.*.*:30000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/login?next=%2F">/login?next=%2F</a>.

在此处输入图像描述 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM