Thursday, December 28, 2023

Enable SSH in Azure Webapp Container

 Enable SSH in Azure Webapp Container


    This article will help you enable ssh in Azure Webapp container.

- Create a file called "init.sh" at the same location where your Dockerfile is and add below content to it.

#!/bin/bash
set -e

echo "Starting SSH ..."
service ssh start

exec "$@"

- Create a file called "sshd_config", this file will have basic SSH configuration and port for SSH
and add below content.

Port 2222 ListenAddress 0.0.0.0 LoginGraceTime 180 X11Forwarding yes Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr MACs hmac-sha1,hmac-sha1-96 StrictModes yes SyslogFacility DAEMON PasswordAuthentication yes PermitEmptyPasswords no PermitRootLogin yes Subsystem sftp internal-sftp

- Install OpenSSH service, add below lines in Dockerfile.

# Install and Run SSH
ENV SSH_PASSWD "root:Docker!"
RUN apt-get update
RUN apt-get install -y dialog \
    && apt-get update \
    && apt-get install -y openssh-server \
    && echo "$SSH_PASSWD" | chpasswd

COPY ./sshd_config /etc/ssh/
COPY ./init.sh /usr/local/bin/
RUN chmod u+x /usr/local/bin/init.sh
ENTRYPOINT ["init.sh"]

- Build Docker image and deploy it on Azure webapp, to validate SSH navigate to Azure Portal.


Login into your Azure Portal–> Navigate to App services–> Enter into your deployed App service–>
Click on SSH(from the side pane)–> Click on Go


It will open another tab in browser and login to container.





Friday, July 21, 2023

Github Action workflow to deploy flask application on Google Kubernetes Engine (GKE)

 Deploy application on GKE using Github Actions


Follow the below steps to setup Github Actions workflow.

Prerequisite:

- Create service account in GCP and generate key file.

- Convert key file into base64 format and using the output to create secret variable in github repo.

Steps:

- In your github repo create .github/workflow directory

- Under workflow directory create a file called deploy.yml and paste below code into it.

name: Build-Deploy

on:

  push:

    branches: [ "anup_gke" ]

env:

  PROJECT_ID: <google-project-id> # ${{ secrets.GKE_PROJECT }}

  GKE_CLUSTER: <GKE-Cluster-name>    # TODO: update to cluster name

  GKE_ZONE: us-central1-a   # TODO: update to cluster zone

  DEPLOYMENT_NAME: gke-test # TODO: update to deployment name

  IMAGE_FLASK: <image name>

  IMAGE_NGINX: <image name>

jobs:

  build-deploy-gke:

    name: Login, Build, Publish, and Deploy

    runs-on: ubuntu-latest

    steps:

    - name: Checkout

      uses: actions/checkout@v3

    - id: 'auth'

      name: Google Authentication

      uses: 'google-github-actions/auth@v1'

      with:

        credentials_json: '${{ secrets.GKE_SA_KEY }}'

    # Setup gcloud CLI

    - uses: google-github-actions/setup-gcloud@v1

      name: Setup gcloud CLI

    # Configure Docker to use the gcloud command-line tool as a credential

    # helper for authentication

    - name: 'Configure Docker to use the gcloud command-line tool'

      run: |-

        gcloud --quiet auth configure-docker

    # Build the Docker image for mvp-flask

    - name: Build Docker Image of mvp-flask

      run: |-

        cd source/mvp-flask

        docker build -t gcr.io/$PROJECT_ID/$IMAGE_FLASK:$GITHUB_RUN_ID .

    # Build the Docker image for mvp-nginx

    - name: Build Docker Image of mvp-nginx

      run: |-

        cd source/nginx

        docker build -t gcr.io/$PROJECT_ID/$IMAGE_NGINX:$GITHUB_RUN_ID .

    - name: Push docker images to GCR

      run: |-

        docker push gcr.io/$PROJECT_ID/$IMAGE_FLASK:$GITHUB_RUN_ID

        docker push gcr.io/$PROJECT_ID/$IMAGE_NGINX:$GITHUB_RUN_ID

    - name: 'Get GKE Credentails'

      id: 'get-credentials'

      uses: 'google-github-actions/get-gke-credentials@v1'

      with:

        cluster_name: ${{ env.GKE_CLUSTER }}

        location: ${{ env.GKE_ZONE }}

    - name: Deploy Docker Image on GKE Cluster

      run: |-

        kubectl set image deployment.apps/mvp-flask flask=gcr.io/<google-project-id>/$IMAGE_FLASK:$GITHUB_RUN_ID

        kubectl set image deployment.apps/mvp-nginx nginx=gcr.io/<google-project-id>/$IMAGE_NGINX:$GITHUB_RUN_ID

    - name: command

      run: |-

        kubectl get all

        echo "###"

        kubectl describe deployment.apps/mvp-flask

        echo "###"

        kubectl describe deployment.apps/mvp-nginx