In Part-1 we deployed a Php, Redis application to kubernetes in local cluster using minikube. In this tutorial we will deploy the same application to Google Cloud GKE and will use manage redis service Cloud Memorystore instead of redis container.
Cloud Memorystore is a Fully-managed in-memory data store service for Redis.Cloud Memorystore for Redis provides a fully managed in-memory data store service built on scalable, secure, and highly available infrastructure managed by Google. Use Cloud Memorystore to build application caches that provides sub-millisecond data access. Cloud Memorystore is compatible with the Redis protocol, allowing easy migration with zero code changes.
Application code can be found on Github
After cloning Application code from Github it will look like
-root -src -index.php -redis.php -Dockerfile-php -Dockerfile-redis -php-apache.yaml -redis.yaml
In this tutorial we will use Cloud memorystore instead of Redis so we don't need to build Dockerfile-redis or to deploy redis.yaml. If you are deploying in local minikube cluster or wan't to use your own container for redis then you can build and deploy those easily.
To connect to Cloud Memorystore from GKE your cluster must be of version 1.8 or higher and has IP aliases enabled. If you have a cluster of version 1.8 or higher and has IP alias enabled then skip this step. You can only enable IP alias while creating the cluster. If your cluster is version 1.7 or lower, or your version 1.8 or higher cluster doesn't have IP aliases enabled, follow these workaround steps before trying to connect to your memorystore instance.
Run these commands, replacing RESERVED_IP_RANGE with the reserved IP range of your instance:
git clone https://github.com/bowei/k8s-custom-iptables.git
If you don't know the reserved IP range of your instance, you can find out via the console (advanced options) or by entering this command:
gcloud redis instances describe INSTANCE_ID --region=REGION
Login to your Google cloud account in terminal using
gcloud auth login
Set your Project
gcloud config set project PROJECT_ID
Connect your kubectl client to your cluster using
gcloud container clusters get-credentials CLUSTER_NAME --zone CLUSTER_ZONE --project PROJECT_ID
Build your Docker image for php and push it to any container registry
docker build -t php-apache-gke:latest -f Dockerfile-php .
docker tag IMAGE_ID DOCKER_HUB_USERNAME/php-apache-gke:latest
docker push DOCKER_HUB_USERNAME/php-apache-gke:latest
Now we have pushed our image to dockerhub, you can use any other container registry like google cloud container registry.
Now create Cloud memorystore instance then create a configmap in your cluster to store your insatance connection details using
kubectl create configmap redishost --from-literal=REDIS_HOST=INSTANCE_IP --from-literal=REDIS_PORT=6379
One final change before deployment, Change image in php-apache.yaml to the image you push to the registry, remove line imagePullPolicy: Never and change Service type from NodePort to LoadBalancer
kubectl apply -f php-apache.yaml
To test Deployment use kubectl get pods it will show your pod running
To access your application using the LoadBalancer run kubectl get services it will give EXTERNAL-IP adderess of you service then you will be able to access your application at that IP_ADDRESS and to test memorystore connection go to IP_ADDRESS/redis.php.
Hope you enjoyed this, If you have any question don't hesitate to reach out on twitter
You can follow me on twitter @ilyash00 where I share some cool stuff about programming and occasionally create some zines