Kubernetes deployment error with Java/Micronaut: ERR_CONNECTION_REFUSED - java

I am trying to deploy an app having 3 services - frontend (Angular), backend 1 (Java/Micronaut), and backend 2 (Java/Micronaut).
My frontend works properly but the Java apps are not working.
Sometimes, I observed it started after 20 min. of deploying a Java app, but this time it does not work even after 1 hr.
Deployment, pod service - all are in running state in Kubernetes, but when I try to hit the URL I see below error:
deployment.yaml for java app
apiVersion: apps/v1
kind: Deployment
metadata:
name: authentication-deploy
labels:
name: authentication-deploy
app: supply-chain-app
spec:
replicas: 1
selector:
matchLabels:
name: authentication-pod
app: supply-chain-app
template:
metadata:
name: authentication-pod
labels:
name: authentication-pod
app: supply-chain-app
spec:
containers:
- name: authentication
image: cawishika/authentication-service:1.1
ports:
- containerPort: 80
service.yaml for java app
apiVersion: v1
kind: Service
metadata:
name: authentication-service
labels:
name: authentication-service
app: supply-chain-app
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30006
selector:
name: authentication-pod
app: supply-chain-app
Docker file
FROM adoptopenjdk/openjdk11:latest
EXPOSE 8002
ADD target/authentication-service-0.1.jar authentication-service-0.1.jar
ENTRYPOINT ["java", "-jar", "/authentication-service-0.1.jar"]
kubectl logs podname

Your Dockerfile is exposing port 8002 (EXPOSE 8002), but your app is started on port 8080.
Additionally, your Kubernetes configuration is pointing to port 80 of your pod.
You should set it so that all three configurations use the same port.

Related

Java Spring Active profile in kubernetes Cluster

i want to start Java spring app with active profile...
I build Docker image in Gitlab CI/CD using maven wrapper ,
./mvnw compile jib:build -Dimage=image/sms-service:1
after that i deploy app in k8s....
now i want to run with active profile , what is best way? how can i define in k8s to run specific user
apiVersion: apps/v1
kind: Deployment
metadata:
name: sms-service
namespace: sms-service
spec:
selector:
matchLabels:
app: sms-service
replicas: 4 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: sms-service
spec:
template:
spec:
containers:
- name: sms-service
image: image/sms-service:1
imagePullPolicy: Always
ports:
- containerPort: 8080
imagePullSecrets:
- name: sms-service
Set the SPRING_PROFILES_ACTIVE environment variable to the profile(s) you want to run.
You can set it in the deployment yaml or at build time in your image but usually better to add it to deployment.
Create a new file, named configmap.yaml under the k8s config folder and add the following lines:
apiVersion: v1
kind: ConfigMap
metadata:
name: blabla
namespace: bla
data:
application.yaml: |
spring:
profiles:
active: prod (here goes the profile)
This tells Kubernetes to set this configuration when starting the container

Reading Kube Secrets from Java Program

I'm working on a library to read secrets from a given directory that I've got easily up and running with Docker Swarm by using the /run/secrets directory as the defined place to read secrets from. I'd like to do the same for a Kubernetes deployment but looking online I see many guides that advise using various Kubernetes APIs and libraries. Is it possible to simply read from disk as it is with Docker Swarm? If so, what is the directory that these are stored in?
Please read the documentation
I see 2 practical ways to access the k8s secrets:
Mount the secret as a file
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
Expose the secret as an environmental variable
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: redis
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password

unable to remote-debug a pod with vscode?

I am having trouble while debugging the remote spring-boot application using vscode IDE.( I am not using any docker file in development as i am using the jib-maven plugin with skaffold to deploy on k8s. I assume this should cause any issue )
Below is the snapshot for k8s yaml file ( i have deleted few parts and replaced it with dot)
apiVersion: v1
kind: Service
metadata:
annotations:
.
.
.
.
.
.
.
.
name: ABC-service
spec:
ports:
- name: http
port: 8080
targetPort: 8080
- name: grpc
port: 50051
targetPort: 50051
selector:
app: ABC-service
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ABC-service
spec:
replicas: 1
selector:
matchLabels:
app: ABC-service
template:
metadata:
labels:
app: ABC-service
spec:
containers:
- ports:
- containerPort: 8080
- containerPort: 50051
- containerPort: 50005
env:
- name: SECURESTORE_SVC_HOST
value: securestore-svc
- name: SECURESTORE_SVC_PORT
value: "50051"
- name: IAM_SVC_HOST
value: iam-service-svc
- name: IAM_SVC_PORT
value: "50051"
name: ABC-service
imagePullPolicy: Always
image: ABC/ABC-service
imagePullSecrets:
- name: ABC-dev
Once my service is deployed , I perform below steps to debug
Executing the command in terminal
kubectl port-forward ABC-service-c59667c89-z5pzp 8080:8080
Now my service is accessible via localhost:8080/Hello
Afterwards when i try to connect my vscode debugger on port 8080 using following launch.json configuration
{
"type": "java",
"name": "Debug (Attach)",
"projectName": "MyApplication",
"request": "attach",
"hostName": "localhost",
"port": 8080
}
here debugger keeps waiting to connect but never gets attached and eventually times out. I followed many tutorials but not sure what I am messing up.
In launch.json, add
"console": "internalConsole",
Try this and see if question goes away.

kubernetes failed to start pod due to ContainerCannotRun

I am new to kubernetes. Recently set up kubernetes cluster with 1 master and 1 node.
I am able to start a docker container by running
sudo docker run <docker-image> in my node machine.
But i failed to start docker container as a pod using kubernetes yml file.
by running sudo kubectl create -f deployment.yml
I describe the pod information and saw this error message.
Last State: Terminated
Reason: ContainerCannotRun
Message: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"HOSTNAME\": executable file not found in $PATH": unknown
Exit Code: 128
docker container supposes to start a java executable.
this is my deployment file
kind: Service
apiVersion: v1
metadata:
name: service1-service
spec:
selector:
app: service1
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 26666
# Port to forward to inside the pod
targetPort: 26666
# Port accessible outside cluster
nodePort: 26666
type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: service1-depolyment
spec:
selector:
matchLabels:
app: service1
replicas: 1
template:
metadata:
labels:
app: service1
spec:
containers:
- name: service1
image: service1-docker-image
imagePullPolicy: Never
ports:
- containerPort: 26666
# args: ["HOSTNAME", "KUBERNETES_PORT"]
In this deployment file, I try to create a nginx and one java web applicaition service.
It is because i defined wrong apiVersion and kind ?
Any help would be appreciated.
Look at this error exec: \"HOSTNAME\": executable file not found in $PATH
I had a similar error since the container could not locate the docker "CMD" binary since I gave it the wrong path. Check the path to the file and that should do the trick.

Deploy WAR in Tomcat on Kubernetes

I need to create a Multibranch Jenkins job to deploy a .war file in Tomcat that should run on Kubernetes. Basically, I need the following:
A way to install Tomcat on Kubernetes platform.
Deploy my war file on this newly installed Tomcat.
I need to make use of Dockerfile to make this happen.
PS: I am very new to Kubernetes and Docker stuff and need basic details as well. I tried finding tutorials but couldn't get any satisfactory article.
Any help will be highly highly appreciated.
Docker part
You can use the tomcat docker official image
In your Dockerfile just copy your war file in /usr/local/tomcat/webapps/ directory :
FROM tomcat
COPY app.war /usr/local/tomcat/webapps/
Build it :
docker build --no-cache -t <REGISTRY>/<IMAGE>:<TAG> .
Once your image is built, push it into a Docker registry of your choice.
docker push <REGISTRY>/<IMAGE>:<TAG>
Kubernetes part
1) Here is a simple kubernetes Deployment for your tomcat image
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
labels:
app: tomcat
spec:
replicas: 1
selector:
matchLabels:
app: tomcat
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: <REGISTRY>/<IMAGE>:<TAG>
ports:
- containerPort: 8080
This Deployment definition will create a pod based on your tomcat image.
Put it in a yml file and execute kubectl create -f yourfile.yml to create it.
2) Create a Service :
kind: Service
apiVersion: v1
metadata:
name: tomcat-service
spec:
selector:
app: tomcat
ports:
- protocol: TCP
port: 80
targetPort: 8080
You can now access your pod inside the cluster with http://tomcat-service.your-namespace/app (because your war is called app.war)
3) If you have Ingress controller, you can create an Ingress ressource to expose the application outside the cluster :
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /app
backend:
serviceName: tomcat-service
servicePort: 80
Now access the application using http://ingress-controller-ip/app

Categories