IP deployment tomcat kubernetes

8/7/2018

I need that tomcat is registered with my Node IP and a port.

My question is:

At the moment that i run the command:

kubectl run tomcat-pod --image=tomcat --port=80 --labels="name=tomcat-pod"

In this moment the tomcat is running.

Then I believe that exposing like a service my tomcat with NodePort type, It will change my IP registration, because i have understanded that my server is registered with the command run?

Or what is the correct way to register my app with the Node machine using the tomcat in the container?

Thanks.

Regards.

-- Code Geas Coder
docker
kubeadm
kubernetes
tomcat

1 Answer

8/7/2018

To achieve your goal and make Tomcat deployment available on the Node machine, consider using Service type NodePort to expose Tomcat application server on the Node IP address.

Create the manifest file for Tomcat application server implementation, ensuring that you remove previous Tomcat deployment:

kubectl delete deployment tomcat-pod

Manifest file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-pod
spec:
  selector:
    matchLabels:
      run: tomcat-pod
  replicas: 1
  template:
    metadata:
      labels:
        run: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:latest
        ports:
        - containerPort: 8080

Create deployment for Tomcat in your K8s cluster:

kubectl apply -f manifest_file.yaml

Compose service exposing your Tomcat container port (by default 8080):

apiVersion: v1
kind: Service
metadata:
  name: tomcat-pod
  labels:
    run: tomcat-pod
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    run: tomcat-pod

Create service:

kubectl apply -f manifest_file.yaml

Check your created service properties: kubectl describe service tomcat-pod

Name:                     tomcat-pod
Namespace:                default
Labels:                   run=tomcat-pod
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"run":"tomcat-pod"},"name":"tomcat-pod","namespace":"default"},"spec":{"port...
Selector:                 run=tomcat-pod
Type:                     NodePort
IP:                       XXX.XX.XX.XX
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30218/TCP
Endpoints:                XXX.XX.XX.XX:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Now you can reach your Tomcat application server via Node IP address.

Be aware as NodePort is randomly selected from the default pool 30000-32767 and this value is unique for each Node in the cluster.

-- mk_sta
Source: StackOverflow