here are the detailed steps to create a GKE instance on GCP and deploy a sample Java application:
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.
Enable the Kubernetes Engine API:
- In the Cloud Console, go to the Kubernetes Engine API page.
- Click "Enable".
Install and set up the Cloud SDK:
- Download and install the Google Cloud SDK.
- Authenticate with your Google Cloud account.
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".
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.
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:v1Run the following command to deploy the application:
kubectl apply -f deployment.yaml
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=8080Wait for the service to be created.
Run the following command to get the external IP address of the service:
kubectl get service myapp-deploymentVisit the external IP address in your web browser to see your application running.
No comments:
Post a Comment