Search code examples
kubernetesgoogle-kubernetes-enginekubectlminikubemetallb

Kubernetes / Metallb single entrypoint


I'm building a K8 cluster for a school project. It's bare metal and uses metallb as a loadbalancer. Each service works in a separate pod:

  • Nginx
  • Wordpress
  • Phpmyadmin
  • Mysql (mariadb)

In the phpmyadmin file, I need to link my mysql server with something like this:

$cfg['Servers'][$i]['host'] = "mysql-server-name";

I've tried to use the node's IP:

kubectl get node -o=custom-columns='DATA:status.addresses[0].address' | sed -n 2p

adding the port :3306 but I realised that none of my services could be reached through the browser with this method. For instance the node's Ip:5050 should redirect me to my wordpress but it doesn't. Is there any way to get a single IP that I can use to make my pods communicate between them ? I must add that each service works appart when I use the svc IP instead of the nodes.

Here's the configmap I use for metallb:

kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.99.100-192.168.99.200

Solution

  • The reason the node IP doesn't expose your application to other apps is that the pods in the kubernetes cluster don't listen to the requests coming to the node by default. In other words, the port on the pod is not connected to the port on the node.
    The service resource is what you need to make that connection.
    Services have different types. A service of type cluster IP will assign an IP internal to the cluster to the app. If you don't want to access your mysql database directly from the internet, this is what you would want.
    Here is an example service of type cluster IP for your project.

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-service
      namespace: metallb-system
    spec:
      selector:
        app: Mysql
      ports:
        - protocol: TCP
          port: 80
          targetPort: 3306
    

    Selector selects pods that carry the label app=mysql.
    Port is the port that the service will listen to.
    TargetPort is the port that mysql is listening to.
    When you create the service you can find it's IP by running this command

    kubectl get services -n metallb-system
    

    Under CLUSTER-IP column note the IP of the service you created.
    So in this case, if mysql is listening to 3306, you can reach it through this service on the service IP on port 80.
    If you want to expose your wordpress app to the internet, use either the NodePort or LoadBalancer service types. Here is the reference for service types.