Home > Back-end >  How can access running app in my computer localhost inside kubernetes pod?
How can access running app in my computer localhost inside kubernetes pod?

Time:10-11

i have a question about kubernetes networking.

My working senario:

  • I have a Jenkins container my localhost and this container up and running. Inside Jenkins, i have a job. Access jenkins , i use "http://localhost:8080" url. (jenkins is not runing inside kubernetes)
  • My flask app, trigger the Jenkins job with this command:
    @app.route("/create",methods=["GET","POST"])
def create():
    
    if request.method =="POST":
        dosya_adi=request.form["sendmail"]
        server = jenkins.Jenkins('http://localhost:8080/', username='my-user-name', password='my-password')
        server.build_job('jenkins_openvpn', {'FILE_NAME': dosya_adi}, token='my-token')
  • Then, i did Dockerize this flask app. My image name is: "jenkins-app"
  • If i run this command, everythings perfect:

docker run -it --network="host" --name=jenkins-app jenkins-app

But i want to do samething with kubernetes. For that i wrote this yml file.

apiVersion: v1
kind: Pod
metadata:
  name: jenkins-pod
spec:
  hostNetwork: true
  containers:
  - name: jenkins-app
    image: jenkins-app:latest
    imagePullPolicy: Never
    ports:
    - containerPort: 5000
  • With this yml file, i access the flask app using port 5000. While i want to trigger jenkins job, i get an error like this: requests.exceptions.ConnectionError

Would you suggest if there is a way to do this with Kubernetes?

CodePudding user response:

First you expose your Jenkins server:

kubectl expose pod jenkins-pod --port=8080 --target-port 5000

Then you check the existence of the service:

kubectl get svc jenkins-pod -o yaml

Use it for your Flask app to connect to your Jenkins server via this service:

... jenkins.Jenkins('http://jenkins-pod.default.svc.cluster.local:8080/...'

Note the assumption is you run everything in default namespace, otherwise change the "default" to your namespace.

CodePudding user response:

I add an endpoint, this solve my problem:

apiVersion: v1
kind: Endpoints
metadata:
  name: jenkins-server
subsets:
  - addresses:
      - ip: my-ps-ip
    ports:
      - port: 8080
  • Then, i change this line in my flask app like this:

    server = jenkins.Jenkins('http://my-ps-ip:8080/', username='my-user-name', password='my-password')

  • Related