Skip to main content
Kubernetes is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. You can connect Kubernetes to , and deploy within your Kubernetes clusters. This guide explains how to connect a Kubernetes cluster to , configure persistent storage, and deploy in your kubernetes cluster.

Prerequisites

To follow the steps on this page:

Integrate TimescaleDB in a Kubernetes cluster

To connect your Kubernetes cluster to your :
  1. Create a default namespace for your components
    1. Create a namespace:
      kubectl create namespace timescale
      
    2. Set this namespace as the default for your session:
      kubectl config set-context --current --namespace=timescale
      
    For more information, see Kubernetes Namespaces.
  2. Create a Kubernetes secret that stores your credentials Update the following command with your connection details, then run it:
    kubectl create secret generic timescale-secret \
     --from-literal=PGHOST=<host> \
     --from-literal=PGPORT=<port> \
     --from-literal=PGDATABASE=<dbname> \
     --from-literal=PGUSER=<user> \
     --from-literal=PGPASSWORD=<password>
    
  3. Configure network access to
    • Managed Kubernetes: outbound connections to external databases like work by default. Make sure your cluster’s security group or firewall rules allow outbound traffic to IP.
    • Self-hosted Kubernetes: If your cluster is behind a firewall or running on-premise, you may need to allow egress traffic to . Test connectivity using your connection details:
      nc -zv <host> <port>
      
      If the connection fails, check your firewall rules.
  4. Create a Kubernetes deployment that can access your Run the following command to apply the deployment:
    kubectl apply -f - <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: timescale-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: timescale-app
      template:
        metadata:
          labels:
            app: timescale-app
        spec:
          containers:
          - name: timescale-container
            image: postgres:latest
            envFrom:
              - secretRef:
                  name: timescale-secret
    EOF
    
  5. Test the connection
    1. Create and run a pod that uses the connection details you added to timescale-secret in the timescale namespace:
      kubectl run test-pod --image=postgres --restart=Never \
       --env="PGHOST=$(kubectl get secret timescale-secret -o=jsonpath='{.data.PGHOST}' | base64 --decode)" \
       --env="PGPORT=$(kubectl get secret timescale-secret -o=jsonpath='{.data.PGPORT}' | base64 --decode)" \
       --env="PGDATABASE=$(kubectl get secret timescale-secret -o=jsonpath='{.data.PGDATABASE}' | base64 --decode)" \
       --env="PGUSER=$(kubectl get secret timescale-secret -o=jsonpath='{.data.PGUSER}' | base64 --decode)" \
       --env="PGPASSWORD=$(kubectl get secret timescale-secret -o=jsonpath='{.data.PGPASSWORD}' | base64 --decode)" \
       -- sleep infinity
      
    2. Launch a psql shell in the test-pod you just created:
      kubectl exec -it test-pod -- bash -c "psql -h \$PGHOST -U \$PGUSER -d \$PGDATABASE"
      
    You start a psql session connected to your .
You have successfully integrated Kubernetes with .