Monday, 17 April 2023

create a GKE instance on GCP. Also add steps to create a small sample application on java.

 here are the detailed steps to create a GKE instance on GCP and deploy a sample Java application:

  1. Create a GCP project:

    • Log in to the Google Cloud Console and create a new project.
    • Select a unique project ID and project name.
    • Enable billing for the project.
  2. Enable the Kubernetes Engine API:

    • In the Cloud Console, go to the Kubernetes Engine API page.
    • Click "Enable".
  3. Install and set up the Cloud SDK:

    • Download and install the Google Cloud SDK.
    • Authenticate with your Google Cloud account.
  4. Create a Kubernetes cluster:

    • Open the Cloud Console.
    • In the left sidebar, select "Kubernetes Engine" > "Clusters".
    • Click "Create Cluster".
    • Enter a name for the cluster.
    • Choose the zone for the cluster.
    • Select the number of nodes for the cluster.
    • Click "Create".
  5. Configure kubectl to connect to the cluster:

    • In the Cloud Console, go to the Kubernetes Engine > Clusters page.
    • Click "Connect" for the cluster you just created.
    • Run the command that appears in the Cloud Console.
  6. Deploy a sample Java application:

    • Create a new directory for your application.

    • In the directory, create a new file called "Dockerfile" with the following contents:

      VOLUME /tmp ADD target/myapp.jar myapp.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myapp.jar"]```


    • Create a new file called "deployment.yaml" with the following contents:

      kind: Deployment metadata: name: myapp-deployment labels: app: myapp spec: replicas: 1 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: gcr.io/[PROJECT_ID]/myapp:v1 ports: - containerPort: 8080```

    • Replace [PROJECT_ID] with your actual project ID.

    • In the same directory, run the following command to build and push the Docker image:

      docker build -t gcr.io/[PROJECT_ID]/myapp:v1 . docker push gcr.io/[PROJECT_ID]/myapp:v1

    • Run the following command to deploy the application:

      kubectl apply -f deployment.yaml

  7. Expose the application to the internet:

    • Run the following command to create a Kubernetes service:

      kubectl expose deployment myapp-deployment --type=LoadBalancer --port=80 --target-port=8080

    • Wait for the service to be created.

    • Run the following command to get the external IP address of the service:

      kubectl get service myapp-deployment

    • Visit the external IP address in your web browser to see your application running.

No comments:

Post a Comment