Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-21 09:24:22 Viewed : 223
Apply the configuration to your Kubernetes cluster
bashkubectl apply -f deployment.yaml kubectl apply -f service.yaml
The kubectl apply -f
command is used to apply configuration files to a Kubernetes cluster. In the context of the provided commands:
kubectl apply -f deployment.yaml
:
kubectl apply
: This command is used to apply or update a resource in a Kubernetes cluster. It reads the configuration from the specified YAML file(s) and applies it to the cluster.
-f deployment.yaml
: This specifies the path to the YAML file containing the configuration for a Kubernetes Deployment. The file, in this case, is named deployment.yaml
.
When you run this command, Kubernetes will read the deployment.yaml
file and create or update the Deployment specified in that file. The Deployment, as described in the YAML file, includes information about the desired number of replicas, the Docker image to use, and other configurations.
kubectl apply -f service.yaml
:
kubectl apply
: Same as in the previous command.
-f service.yaml
: This specifies the path to the YAML file containing the configuration for a Kubernetes Service. The file, in this case, is named service.yaml
.
When you run this command, Kubernetes will read the service.yaml
file and create or update the Service specified in that file. The Service, as described in the YAML file, includes information about how external traffic should be routed to the Pods managed by the Deployment. It defines the type of Service (LoadBalancer), the ports to listen on, and the target port on the Pods.
In summary, these commands are applying the configurations defined in the deployment.yaml
and service.yaml
files to the Kubernetes cluster. The deployment.yaml
file sets up the desired state for a Deployment, managing the applications Pods, while the service.yaml
file configures a Service to expose the Deployments Pods externally and route traffic to them. This combination is common when deploying applications in Kubernetes, as it provides a scalable and manageable way to run and expose containerized applications.
Test the Application: Once deployed, test your application:
bashkubectl get services # Note the external IP of the service
curl http://<external-ip>
The provided commands are used to retrieve information about a Kubernetes Service and then make a HTTP request to the external IP address of that Service.
kubectl get services
:
kubectl get services
: This command is used to list all the services in the current Kubernetes namespace.When you run this command, you will get a table with information about the services in the namespace, including their names, types, cluster IP addresses, external IP addresses (if applicable), and ports.
For example:
scssNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-java-app LoadBalancer 10.105.23.102 203.0.113.5 80:30123/TCP 2m
In the above output, you can see the Service named "my-java-app," its type (LoadBalancer), cluster IP, external IP, and the ports it is listening on.
curl http://<external-ip>
:
curl
: This is a command-line tool for making HTTP requests.
http://<external-ip>
: Replace <external-ip>
with the actual external IP address obtained from the previous kubectl get services
command.
This command is making an HTTP request to the external IP address of the Kubernetes Service. The assumption here is that the Service is of type LoadBalancer, and it has an externally accessible IP address.
For example:
arduinocurl http://203.0.113.5
This request is trying to access the application that is exposed through the Kubernetes Service at the specified external IP address.
Make sure to replace <external-ip>
with the actual external IP address obtained from your kubectl get services
command. Additionally, ensure that your application is running and accessible through the specified external IP address and port.
you can port-forward to one of the pods:
bashkubectl port-forward <pod-name> 8080:8080
The kubectl port-forward
command is used to create a network tunnel from your local machine to a specific Pod in a Kubernetes cluster. Lets break down the provided command:
bashkubectl port-forward <pod-name> 8080:8080
kubectl port-forward
: This is the command to set up port forwarding.
<pod-name>
: Replace this with the name of the Pod you want to forward traffic to. Pods in Kubernetes have unique names within their namespace.
8080:8080
: This specifies the port mapping for the port forwarding. It means that any traffic sent to port 8080 on your local machine will be forwarded to port 8080 on the specified Pod.
After executing this command, your local machines port 8080 is mapped to port 8080 on the specified Pod. This allows you to access services running inside the Pod as if they were running on your local machine.
For example, if you have a web application running inside the specified Pod, you can open a web browser and navigate to http://localhost:8080
to interact with the application. The traffic is tunneled through the kubectl port-forward
command to the target Pod.
Remember to keep the terminal running the kubectl port-forward
command open as long as you need the tunnel. Once you close the terminal or interrupt the command (e.g., with Ctrl+C), the port forwarding will be terminated.
This is particularly useful for debugging and accessing services running within a Pod without exposing them to the entire cluster or external network during development or troubleshooting.