Using Cloudsqlproxy to initiate connection to mysql from the shell

The cloud sql proxy is a really useful way of managing connectivity to cloudSQL instances. The proxy provides secure access to your Cloud SQL Second Generation instances without having to whitelist IP addresses or configure SSL.

Accessing Cloud SQL instances using the Cloud SQL Proxy offers these advantages:

  • Secure connections: The proxy automatically encrypts traffic to and from the database using TLS 1.2 with a 128-bit AES cipher; SSL certificates are used to verify client and server identities.
  • Easier connection management: The proxy handles authentication with Cloud SQL, removing the need to provide static IP addresses.

There are a couple of pre-requisites before this will work:

  • Only Second generation instances can benifit from this feature.
  • Cloud SQL Admin API must be enabled
  • The instance must have  either a public IPv$ address or be configured to use a private IP.
  • The proxy needs to be given GCP authentication credentials, either an individual or a service account.

Full details of exactly what to do are here – 

Some notes on what I did on macOS sierra:

  1. Created a Service Account, gave it the Cloud SQL Client role and then generated a new private key (json key)
curl -o cloud_sql_proxy

Unpack the dmg file, then give execute permissions on the proxy file.

chmod +x cloud_sql_proxy

Then start the proxy:

./cloud_sql_proxy -instances=project:region:instance=tcp:3306 \

The proxu will now start listening on your local machine for connection attempts against port 3306:

2018/11/09 11:53:10 Listening on for project:region:instance
2018/11/09 11:53:10 Ready for new connections

Then in another terminal window, connect as normal to a mysql instance using the localhost IP:

mysqlsh -udbamohsin -p -h127.0.0.1

This should make a connection to the cloudSQL instance using the proxy and the connection will show in the proxy log:

2018/11/09 11:53:42 New connection for "project:region:instance"

Deleting files older than X days

A couple of different ways to purge files older than a certain amount of days.

find -type f -mtime +350 -name 'CopyCluster*' -execdir rm -- '{}' \;

If you want to be very precise with the number of days ago you want to delete from then you can find the number of days from now using:

days=$(( ( $(date '+%s') - $(date -d '1 months ago' '+%s') ) / 86400 ))
echo $days

and then use the variable to delete

find /logs/CopyCluster* -mtime +$days -type f -delete

To do this all in one command

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '1 months ago' '+%s') ) / 86400 ))" -delete



Adding a new project into kubectl/kubectx cli

I use two main command line tools when it comes to kubernetes – kubectx and kubectl

Kubectx is a great little utility for switching between kubernetes clusters, and kubectl is the official utility to manage and deploy kubernetes applications.

Adding a context into kubectx/kubectl

Go to the google cloud console and navigate to Kubernetes Engine and then click the connect button on the cluster, which will provide the gcloud command you need to run on your machine to configure access to the cluster locally.

as an example, lets say we have just added gke-dbemohsin-cluster-sandbox 

MYMAC:cloud-architecture dbamohsin$ kubectx

To rename a context in kubectx:

kubectx SANDBOX=gke-dbemohsin-cluster-sandbox
Context "gke-dbemohsin-cluster-sandbox" renamed to "SANDBOX".

To add a wrapper alias around context switching, expecially if some context run under different gcloud accounts, you can add the commands to your bash_profile:

alias k8-sandbox='gcloud config configurations activate dbemohsin; kubectx SANDBOX'

and to call the alias:

MYMAC:~ dbamohsin$ k8-sandbox
Switched to context "SANDBOX".


Troubleshooting Kubernetes to GCP Mongo Atlas connectivity – so where do you start?

**Update – 03/09/2018**

Since I wrote the original post we have seen further connectivity issues getting Istio working with mongo replica sets. The error looks something like this;

pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector “Primary()”

This has been raised as an issue with Istio 1.0.1 –

We believe that Istio is not correctly routing traffic when it comes to replica set hosts, and is using the host list in the service entry for load balancing rather than just a List of nodes which can be talked to externally. This is a big problem for a clustering based data store such as MongoDB where connection flow is incredibly important. This is a hypothetical theory we have rather than something that has been concluded.

As a result of this issue we are no longer using the outbound port whitelisting feature in Istio, as it turned out this feature had been the cause of a few unrelated issues in our environment. Although the post below is still valid in terms of troubleshooting possible connectivity issues, we are no longer using Service Entries to for outbound traffic interception.


Original Post

In the last few weeks, We have started to prove out running a latency sensitive application in a k8s container against a database running on mongoDB Atlas which is also in GCP.


We currently have a service running in Kubernetes in a europe-west4 container, but the metrics are showing that it runs around 5x slower on average when it connects to an on premise mongo database running 3.2.

We wanted to get the database closer to the application, and since the general availability of GCP as a region in Mongo Atlas, this became a lot easier to try out. MongoDB kindly gave us some Atlas credits to try this out, and within 10-15 minutes, I had the following running in Mongo Atlas:

  • 3 node replica set running v3.6 with WiredTiger
    • 2 nodes in Europe-West4 (Netherlands)
    • 1 node in Europe-West1 (Belgium)
  • M30 Production Cluster
    • 7.5GB RAM
    • 2 vCPUs
    • Storage
      • 100GB Storage
      • 6000 IOPS
      • Auto Expand Storage
      • Encrypted Disk

“Only Available in AWS”

Get used to hearing and seeing this both in the documentation, and when discussing with Support or Account Managers. As expected, not all features are available in GCP yet, the main ones that are frustrating us (As of August 2018) are VPC Peering and Encyption at Rest.

VPC Peering

Atlas supports VPC peering with other AWS VPCs in the same region. MongoDB does not support cross-region VPC peering. For multi-region clusters, you must create VPC peering connections per-region.

Depending on the volume we push to and from our databases, lack of support for VPC Peering may be cost prohibitive as you end up paying egress costs twice, both as your data leaves your K8s application project for data entry and also when it leaves GCP Mongo Atlas on data retrieval. Mongo generally give a cost estimation of 7-10% of cluster size for Egress costs.

Encryption at Rest

The following restrictions apply to Encyption at Rest on an Atlas cluster:

  • Continuous Backups are not supported. When enabling backup for a cluster using Encryption at Rest, you must use Cloud Provider Snapshots from AWS or Azure to encrypt your backup snapshots.
  • You cannot enable Encryption at Rest for clusters running on GCP.

So, currently we cannot take advantage of this option because we would eventually want to run backups via Atlas.

Administrators who deploy clusters on GCP and want to enable backup should keep those clusters in a separate project from deployments that use Encryption at Rest or Cloud Provider Snapshots.


We changed the uri connection string for our test application running in k8s to point towards the mongo atlas cluster. Something like this:

uri = mongodb://,,

However, the application was failing its readiness check (healthcheck on pod startup could not connect to the mongo database).

Troubleshooting Avenues

Kubernetes is not easy to diagnose issues in – especially for novices like me!

Using kubectl logs on the pod was not a great help in this instance, but that might be down to application configuration and what it sends to stdin, stdout, stderr (which is the basis of kubectl logs).

This doesnt tell me much in terms of error logging:

kubectl logs my-k8s-service-5f9b978fc8-d6ddc -n my-k8s-service -c masterUsing config environment: PREPROD[24/Aug/2018:13:29:17] ENGINE Bus STARTING[24/Aug/2018:13:29:17] ENGINE Started monitor thread 'Autoreloader'.[24/Aug/2018:13:29:17] ENGINE Started monitor thread '_TimeoutMonitor'.[24/Aug/2018:13:29:17] ENGINE Serving on[24/Aug/2018:13:29:17] ENGINE Bus STARTED

Replicate the connectivity issue locally on the pod

We knew our readiness check was failing on connectivity so runnng an Interactive bash terminal inside the pod and trying to make a connection to the Atlas replica set without the application was a fair shout.

kubectl exec -it my-k8s-service-5f9b978fc8-d6ddc -c master bash -n my-k8s-service

Then I generated a manual mongo uri connection using python (the application runs in python) and tried to retrive the database collections.

//connect using uri
/usr/bin/scl enable rh-python35 python
from pymongo import MongoClient
uri = "mongodb://,,"
client = MongoClient(uri)
db = client.myDB
collection = db.myCollection

and this is what happened:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/atcloud/.local/lib/python3.5/site-packages/pymongo/", line 715, in collection_names nameOnly=True, **kws)] File "/home/atcloud/.local/lib/python3.5/site-packages/pymongo/", line 674, in list_collections read_pref) as (sock_info, slave_okay): File "/opt/rh/rh-python35/root/usr/lib64/python3.5/", line 59, in __enter__return next(self.gen) File "/home/.local/lib/python3.5/site-packages/pymongo/", line 1099, in _socket_for_reads  server = topology.select_server(read_preference) File "/home/.local/lib/python3.5/site-packages/pymongo/", line 224, in select_server address)) File "/home/.local/lib/python3.5/site-packages/pymongo/", line 183, in select_servers selector, server_timeout, address) File "/home/.local/lib/python3.5/site-packages/pymongo/", line 199, in _select_servers_loop self._error_message(selector))pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:645),SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:645),SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:645)

Failing without the application – thats a good sign as it now allows us to do some quick fail fast testing with the uri connection code above!

Lets copy the pod and start it in a testing environment – to start testing changes more aggresively

Create a mytestpod.yaml file with a basic pod configuration with the same base image as the application:

apiVersion: v1kind: Podmetadata:  annotations: "true"  name: dbamohsin-istio-test-podspec:  containers:  - args:    - "10000"    image:    imagePullPolicy: Always    name: master    command: ["/bin/sleep"]

and create it in kubernetes:

kubectl apply -f mytestpod.yaml

Then go into an interactive terminal with bash

kubectl exec -it dbamohsin-istio-test-pod bash

and run the same uri mongo connection as before. Doing this gave the same python connectivity error shown early in the post.

Again, this is a good sign as now we have moved our issue to a less strict environment where we can test changes better.

Can we test the same thing without the istio sidecar…sure!

Delete the pod:

//There are 2 ways of doing this:
kubectl delete -f mytestpod.yaml
kubectl delete pod dbamohsin-istio-test-pod

Modify mytestpod.yaml and comment out the istio sidecar annotation

metadata:  annotations:# "true"

Then re-create the pod and go into an interactive terminal with bash.

Retry the mongo Uri…and it worked!

Python 3.5.1 (default, Oct 21 2016, 21:37:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> from pymongo import MongoClient>>> uri = "mongodb://,,">>> client = MongoClient(uri)>>> db = client.myDB>>> collection = db.myCollection>>> db.collection_names(include_system_collections=False)['col1', 'col2', 'col3', 'col4']

so this narrowed it down to something we potentially hadnt configured in istio.

Connectivity from GCP K8s to GCP Mongo

We use Istio for service discovery/unifying traffic flow management (Auto Trader Case Study on Computer World UK). I didnt know beforehand but we restrict egress traffic out of our GCP projects and as a result we needed a service entry in kubernetes to allow 27017. Some MESH_EXTERNAL services documentation –

The good thing is that there is a specific mongo protocol available in istio…We added a service entry to our testing environment as shown below:

kind: ServiceEntry
 name: egress-mongo-all


 - number: 27017
 name: mongo
 protocol: MONGO
 resolution: DNS

and deployed:

kubectl apply -f egress-mongo-all.yaml

Changes from service entry’s are immediate to pods.

Check its deployed:

kubectl get serviceentry -n core-namespace

and double check the config:

kubectl get serviceentry -n core-namespace -o egress-mongo-atlas.yaml

All good.

Re-enable istio and retry

We re-added the istio sidecar annotation into our mytestpod.yaml file and redeployed. This time, even with Istio, the mongo connection worked fine and retrieved the data.

Other connectivity areas to check

Connectivity into GCP Mongo…Whitelists!

Defining a whitelist can be very tricky as providing a google IP range for our K8s applications is more or less impossible – or would cover most of the internet! The reason for this issue is that only the DNS is static for K8s services, so the IP for an application pod can change multiple times a day, meaning its more or less impossible to assign a strong IP whitelist bound to applications unless an automated check is implemented.

We have several different viewpoints here internally, ranging from we should have a structured IP whitelist to why do we even need a whitelist? Our Authentication and Encryption should be at a level that it can run securely with a IP Whitelist.

Dont forget client connectivity…

Bear in mind that you may have an on premise firewall between your laptop and Mongo Atlas and may need your network administrator to enable access for you to go outbound on 27017 to your Atlas Nodes.

Takeaway points

  • Isolate the issue quickly
    • We took the application out of the equation first
    • Then proved that it wasnt that specific google project by moving environments
    • and finally isolated a networking sidecar (istio)
  • Find an easy way to reproduce the issue
    • We created a simple bit of code to quickly test connectivity using the same language as the application (Python)
    • Created a dummy pod that we could quickly trash and recreate and test against BUT ensured the fundamentals of the application remained (Used the same image as the application for the dummy pod)
  • Have an environment where you can potentially break stuff. An infrastructure testing area of some kind.

How to list containers running in a kubernetes pod

Unfortunately, there isnt a simple get command that can be run to retrieve containers running inside pods. However, the custom-columns command can be exposed via kubectl to get this information out.

For more info on custom-columns visit the kubectl docs –

The following command will list pod name and container name for all pods in the default namespace

kubectl get pods,CONTAINERS:.spec.containers[*].name

The output is something like this:


You can also just get the container via the jsonpath option

kubectl get pods mongod-1 -o jsonpath='{.spec.containers[*].name}'

The reason I did a bit of research (5 minutes!) into this was because the container name is required when running the exec command in kubectl. eg:

kubectl exec -ti mongod-1 -c mongod-container bash

The remote copy of database x has not been rolled forward to a point in time that is encompassed in the local copy

When setting up database mirroring, it is possible to receieve the following error when starting the mirroring session between the Principal and Mirror databases:

The remote copy of database x has not been rolled forward to a point in time that is encompassed in the local copy

Assuming the steps taken follow a pattern similar to below:

  1. BACKUP database DBMTest on SQLINS01
  2. BACKUP log DBMTest on SQLINS01
  3. Copy db and log backup files to SQLINS02
  4. RESTORE DBMTest with norecovery
  5. RESTORE log DBMTest with norecovery
  6. create endpoints on both SQLINS01 and SQLINS02
    CREATE ENDPOINT [Mirroring]
  7. Enable mirror on MIRROR Server SQLINS02
    :connect SQLINS02
    SET Partner = N'TCP://';
  8. Enable mirror on PRINCIPAL server SQLINS01
    :connect SQLINS01
    SET Partner = N'TCP://';

The error would appear on step 8

Normally the reason for the error is because the backup file for the log or full backup has not been initialized and therefore contains more than one backup of the database on file.

Add the WITH INIT option to the backups to create a fresh initialized backup file

ORA-00020: maximum number of processes exceeded

We got the following error on our Development database

866322-Wed Mar 28 15:56:40 2018
866347:ORA-00020: maximum number of processes (1256) exceeded

An easy way to see how exhausted your processes/sessions are getting is by running:

FROM gv$resource_limit 
WHERE resource_name in ('processes','sessions')

Which will give something like this – max utilization is the high water mark level:



From the screenshot above, you can see the processes on instance 1 have been fully exhausted at some point, but currently are fine.

You can also see which machines/schemas are causing any potential process exhaustion:

select distinct
from    gv$session s,
        gv$process p
where   s.paddr       =  p.addr
and     s.inst_id     =  p.inst_id
GROUP BY         s.inst_id,
ORDER BY 4 desc;

Nice summary of the difference between Processes, Sessions and Connections from AskTom:

A connection is a physical circuit between you and the database. A connection might be one of many types — most popular begin DEDICATED server and SHARED server. Zero, one or more sessions may be established over a given connection to the database as show above with sqlplus. A process will be used by a session to execute statements. Sometimes there is a one to one relationship between CONNECTION->SESSION->PROCESS (eg: a normal dedicated server connection). Sometimes there is a one to many from connection to sessions (eg: like autotrace, one connection, two sessions, one process). A process does not have to be dedicated to a specific connection or session however, for example when using shared server (MTS), your SESSION will grab a process from a pool of processes in order to execute a statement. When the call is over, that process is released back to the pool of processes.