Home > Enterprise >  Do a simple apache server with kubernetes
Do a simple apache server with kubernetes

Time:09-17

i'm having an hard time to start learning kubernetes the documentations isn't really simple i want to realize simple trick for the moment i'm familliar with docker and i wan't to learn orchestration with kubernetes . i've realized the following yml file :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
spec:
  selector:
    matchLabels:
      app: test
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: ubuntu:20.04
        command: ["/scripts/ubuntu.sh"]
        ports:
        - containerPort: 80
        - containerPort: 443
        volumeMounts:
        - name: scripts
          mountPath: /scripts
      volumes:
      - name: scripts
        configMap:
          name: scripts
          defaultMode: 0744

in my sh file i've got the following :

#!/bin/sh
apt install -y apache2 && service apache2 start && tail -f /dev/null

the idea is to launch an ubuntu , install apache2 , start the service , keep container alive with a tail , and being able to go on the mapped 80 port from container to my host .

i think i'm doing something wrong when i launch kubectl apply -f i get : deployment.apps/test-deployment created but nothing up on my localhost unfortunatly ...

anyone already had that problem ?

PS: i'm using docker desktop on windows .

Edit :

Now i got my pods running but i cannot access my apache on localhost:80 heres my actual config :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    app: apache
spec:
  selector:
    matchLabels:
      app: test
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: ubuntu:20.04
        command: ["/bin/sh", "-c", "apt update -y && apt install -y apache2 && service apache2 start && tail -f /dev/null"]
        ports:
        - name: http
          containerPort: 80
        env:
        - name: DEBIAN_FRONTEND
          value: noninteractive
---
apiVersion: v1
kind: Service
metadata:
  name: apache-service
spec:
  selector:
    app: apache
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30001

CodePudding user response:

Problem Fixed :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    app: apache
spec:
  selector:
    matchLabels:
      app: test
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: ubuntu:20.04
        command: ["/bin/sh", "-c", "apt update -y && apt install -y apache2 && service apache2 start && tail -f /dev/null"]
        ports:
        - name: http
          containerPort: 80
        env:
        - name: DEBIAN_FRONTEND
          value: noninteractive
---
apiVersion: v1
kind: Service
metadata:
  name: apache-service
spec:
  # selector:
  #   app: test
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: http
    # nodePort: 30001

my problem was coming from the selector in service that i was using wrongly i'll check how it work exactly but now i'm having my service providing my ubuntu with apache on localhost perfectly .

CodePudding user response:

My solution : kubectl port-forward <podsname> <local port>:80 allowed me to map my pods from port 80 internal to the port i needed , i'm just having an issue , i cannot use port forward to map port 80 from my host to port 80 from my pods/container , is anyone having this issue ?

Fixed : i had my service running already on port 80 that's why i couldnt port forward manually on it .

  • Related