Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-21 09:14:37 Viewed : 229
Deploy to Kubernetes:
Create Kubernetes deployment and service YAML files.
yaml# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-java-app
spec:
replicas: 3
selector:
matchLabels:
app: my-java-app
template:
metadata:
labels:
app: my-java-app
spec:
containers:
- name: my-java-app
image: username/my-java-app:1.0
ports:
- containerPort: 8080
YAML code represents a Kubernetes Deployment configuration. This Deployment is defining how to run and manage instances of a containerized application named "my-java-app" in a Kubernetes cluster. Lets break down the components of this YAML file:
yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: my-java-app
apiVersion
: Specifies the API version of the Kubernetes object. In this case, its using the apps/v1
API version.
kind
: Specifies the type of Kubernetes object. Here, its a Deployment.
metadata
: Contains metadata about the Deployment.
name
: Specifies the name of the Deployment, which is "my-java-app" in this case.yamlspec:
replicas: 3
selector:
matchLabels:
app: my-java-app
spec
: Describes the desired state for the Deployment.
replicas
: Specifies the desired number of replicas (instances) for the application. In this case, its set to 3, meaning Kubernetes will maintain three instances of the application.
selector
: Defines how the Deployment selects which Pods to manage.
matchLabels
: Specifies that the Deployment should manage Pods with the label app: my-java-app
.yaml template:
metadata:
labels:
app: my-java-app
spec:
containers:
- name: my-java-app
image: username/my-java-app:1.0
ports:
- containerPort: 8080
template
: Specifies the Pod template for the Deployment. The template is used to create new Pods.
metadata
: Contains metadata for the Pods created from this template.
labels
: Specifies the labels to be applied to Pods created from this template. In this case, it is app: my-java-app
.spec
: Describes the specification for the Pods created from this template.
containers
: Specifies the containers to run in the Pods.
name
: Specifies the name of the container, which is "my-java-app" in this case.
image
: Specifies the Docker image to use for the container. It refers to the image tagged as username/my-java-app:1.0
.
ports
: Specifies the ports to expose on the container.
containerPort
: Specifies that the container will listen on port 8080.In summary, this Deployment YAML file is instructing Kubernetes to maintain three replicas of a containerized Java application named "my-java-app." The Pods created from this Deployment will have the label app: my-java-app
, and each Pod will run a container based on the Docker image username/my-java-app:1.0
, exposing port 8080. This configuration is a common setup for deploying containerized applications in a scalable and manageable way within a Kubernetes cluster.
yaml# service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-java-app
spec:
selector:
app: my-java-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
The provided YAML code represents a Kubernetes Service configuration. Lets break down each section:
yamlapiVersion: v1
kind: Service
metadata:
name: my-java-app
apiVersion
: Specifies the API version of the Kubernetes object. In this case, its using the v1
API version for a Service.
kind
: Specifies the type of Kubernetes object. Here, its a Service.
metadata
: Contains metadata about the Service.
name
: Specifies the name of the Service, which is "my-java-app" in this case.yamlspec:
selector:
app: my-java-app
spec
: Describes the desired state for the Service.
selector
: Specifies how the Service identifies which Pods to target.
app: my-java-app
: Specifies that the Service should route traffic to Pods with the label app: my-java-app
. This label matches the label used in the Deployment, establishing a connection between the Service and the Pods.yaml ports:
- protocol: TCP
port: 80
targetPort: 8080
ports
: Specifies the ports that the Service will listen on and how it will forward traffic to the targeted Pods.
protocol: TCP
: Specifies that the Service will use the TCP protocol.
port: 80
: Specifies the port on which the Service will listen. External traffic will be directed to this port.
targetPort: 8080
: Specifies the target port on the Pods to which the Service will forward traffic. This should match the port that the application inside the Pods is listening on.
yaml type: LoadBalancer
type: LoadBalancer
: Specifies the type of Service. In this case, its a LoadBalancer type. This type is typically used in cloud environments where the cloud provider provisions a load balancer to distribute external traffic across the instances of the Service.In summary, this Service YAML file defines a Kubernetes Service named "my-java-app" that exposes the Pods created by the Deployment to external traffic on port 80. It routes traffic to the Pods based on their label (app: my-java-app
). The Service is of type LoadBalancer, meaning it is expected to be externally accessible, and in a cloud environment, a load balancer will be provisioned to manage incoming traffic. This type of Service is suitable for scenarios where you want to make your application accessible from outside the Kubernetes cluster