Deploying a Sample app on GCP Instance with GitHub Actions and kubeconfig
Introduction
In today’s rapidly evolving software development environment, mastering continuous integration and continuous deployment (CI/CD) is crucial for streamlining workflows and ensuring the reliable delivery of applications.
This article is a step-by-step guide on establishing a CI pipeline using GitHub Actions, designed to enhance your deployment practices. We will explore how to connect to a Google Cloud Platform (GCP) Kubernetes cluster using a kubeconfig file. Additionally, you’ll learn to deploy a sample Nginx application using Helm.
This setup is particularly helpful in scenarios such as rapid application development and deployment, where companies require frequent updates and seamless feature enhancements to their web platforms.
Prerequisites
Before we begin, ensure you have the following:
- A GCP account with appropriate permissions.
- A Kubernetes cluster set up on GCP.
- Basic understanding of Kubernetes and CI/CD principles.
- A GitHub repository where your project code resides.
- Helm installed locally (recommended for local testing and verification).
Creating a GCP Instance
- Create a New GCP Project
- Log in to your GCP account and create a new project.
2. Set Up the Kubernetes Engine
- Navigate to the Kubernetes Engine section and create a new cluster.
- Choose the desired configuration settings and create the cluster.
3. Configure the Instance
- Ensure your instance is properly configured to interact with your Kubernetes cluster.
Getting the Kubeconfig File
- Obtain the Kubeconfig File
- Access your Kubernetes cluster on GCP.
- Download the kubeconfig file from the cluster settings.
2. Store the Kubeconfig File Securely
- Encode the kubeconfig file in base64 format for secure storage.
- Add the encoded Kubeconfig file as a secret in your GitHub repository under the name
KUBECONFIG
.
Configuring the GitHub Actions CI Pipeline
GitHub Actions provides a powerful platform for automating workflows. Here’s a detailed breakdown of the CI pipeline configuration:
on:
push:
branches: [main]
workflow_dispatch:
jobs:
deploy:
name: Connect to Cluster and Deploy Nginx
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Helm
uses: 'koslib/helm-eks-action@master'
with:
command: |
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install nginx bitnami/nginx --namespace default --values ui/values.yaml
env:
KUBE_CONFIG_DATA: ${{ secrets.KUBECONFIG }}
Your pipeline should run successfully like this
Deploying the Nginx App
Helm Chart Installation
In the provided GitHub Actions pipeline, the Helm commands are used to add the Bitnami repository and install the Nginx Helm chart.
- helm repo add bitnami https://charts.bitnami.com/bitnami: This command adds the Bitnami repository to Helm.
- helm install nginx bitnami/nginx — namespace default — values ui/values.yaml: This command installs the Nginx Helm chart from the Bitnami repository into the
default
namespace using the custom values specified inui/values.yaml
.
Verification
After the Nginx Helm chart is installed, you need to verify the deployment by checking the status of the Nginx pods and ensuring the service is accessible.
- Check the Status of the Nginx Pods Run the following
kubectl
command to check the status of the Nginx pods
kubectl get pods -n default
You should see output similar to this
NAME READY STATUS RESTARTS AGE
nginx-578d65d5bd-abcde 1/1 Running 0 1m
2. Check the Nginx Service To verify that the Nginx service is running and accessible, use the following kubectl
command to get the service details:
kubectl get svc -n default
You should see output similar to this
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx LoadBalancer 10.96.0.1 34.123.45.67 80:30277/TCP,443:32487/TCP 1m
3. Access the Nginx Application
- If your service is of type
LoadBalancer
, note theEXTERNAL-IP
address from the output above. - Open a web browser and navigate to
http://34.123.45.67
to access the Nginx application.
Conclusion
You can deploy any application using Helm in a GCP Kubernetes cluster through a GitHub Actions CI pipeline. Verifying the deployment ensures that the Nginx application is running correctly and is accessible as expected. This setup can be extended and customized based on your specific requirements for deploying different applications or services.