Search code examples
dockerkubernetesload-balancingdocker-desktop

kubernetes on docker loadbalancer service external ip pending - ubuntu


I'm trying to up a MySQL server in K8S cluster for a spring boot project. K8S is running on top of docker desktop. In order to access the DB locally, I have set up a load-balancer service. When DB server ups I the load-balancer service external IP is stuck at pending status. I have done a similar thing on a dot-net project earlier on windows OS K8S on docker without a problem where I got the external IP as localhost for sql server setup. I have attached the relevant code. Helps would be appreciated. Thanks.

yaml file

# Mysql server deployment config
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.0.35
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: mysql-config-map
                  key: mysql-root-pwd
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_USER
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_PWD
            # - name: MYSQL_DATA
            #   value: /var/opt/mysql/data/mysqldata
          volumeMounts:
            - name: mysqldb
              mountPath: /var/opt/mysql/data
      volumes:
        - name: mysqldb
          persistentVolumeClaim:
            claimName: mysql-pvc

# Cluster IP config
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-clusterip-srv
spec:
  type: ClusterIP
  selector:
  # 'mysql-deploy' app pointer
    app: mysql
  ports:
    - name: mysql
      protocol: TCP
      port: 3306
      targetPort: 3306

# Loadbalancer config - allows direct access to mysql server
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-loadbalancer-srv
spec:
  type: LoadBalancer
  selector:
  # 'mysql-deploy' app pointer
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306

MySQL logs

2023-11-20 14:58:50 2023-11-20 09:28:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.35-1.el8 started.
2023-11-20 14:58:50 2023-11-20 09:28:50+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-11-20 14:58:50 2023-11-20 09:28:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.35-1.el8 started.
2023-11-20 14:58:50 2023-11-20 09:28:50+00:00 [Note] [Entrypoint]: Initializing database files
2023-11-20 15:02:11 2023-11-20 09:32:11+00:00 [Note] [Entrypoint]: Database files initialized
2023-11-20 15:02:11 2023-11-20 09:32:11+00:00 [Note] [Entrypoint]: Starting temporary server
2023-11-20 15:02:25 2023-11-20 09:32:25+00:00 [Note] [Entrypoint]: Temporary server started.
2023-11-20 15:02:25 '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-11-20 15:02:46 2023-11-20 09:32:46+00:00 [Note] [Entrypoint]: Creating user nawaz
2023-11-20 15:02:47 
2023-11-20 15:02:47 2023-11-20 09:32:47+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-11-20 15:03:20 2023-11-20 09:33:20+00:00 [Note] [Entrypoint]: Temporary server stopped
2023-11-20 15:03:20 
2023-11-20 15:03:20 2023-11-20 09:33:20+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2023-11-20 15:03:20 
2023-11-20 14:58:50 2023-11-20T09:28:50.779763Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-11-20 14:58:50 2023-11-20T09:28:50.780163Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.35) initializing of server in progress as process 80
2023-11-20 14:58:51 2023-11-20T09:28:51.014934Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-11-20 14:59:13 2023-11-20T09:29:13.382504Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-11-20 15:00:15 2023-11-20T09:30:15.050008Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-11-20 15:02:11 2023-11-20T09:32:11.565150Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-11-20 15:02:11 2023-11-20T09:32:11.567401Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.35) starting as process 124
2023-11-20 15:02:11 2023-11-20T09:32:11.837713Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-11-20 15:02:18 2023-11-20T09:32:18.174741Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-11-20 15:02:25 2023-11-20T09:32:25.306013Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-11-20 15:02:25 2023-11-20T09:32:25.306139Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-11-20 15:02:25 2023-11-20T09:32:25.476643Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-11-20 15:02:25 2023-11-20T09:32:25.567374Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2023-11-20 15:02:25 2023-11-20T09:32:25.567569Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.35'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2023-11-20 15:02:36 Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
2023-11-20 15:02:36 Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
2023-11-20 15:02:36 Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
2023-11-20 15:02:43 Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
2023-11-20 15:02:43 Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
2023-11-20 15:02:43 Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2023-11-20 15:02:47 2023-11-20T09:32:47.069535Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.35).
2023-11-20 15:03:19 2023-11-20T09:33:19.629680Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.35)  MySQL Community Server - GPL.
2023-11-20 15:03:20 2023-11-20T09:33:20.416167Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-11-20 15:03:20 2023-11-20T09:33:20.418206Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.35) starting as process 1
2023-11-20 15:03:20 2023-11-20T09:33:20.428757Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-11-20 15:03:22 2023-11-20T09:33:22.741252Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-11-20 15:03:27 2023-11-20T09:33:27.884744Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-11-20 15:03:27 2023-11-20T09:33:27.884810Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-11-20 15:03:27 2023-11-20T09:33:27.983553Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-11-20 15:03:28 2023-11-20T09:33:28.021454Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-11-20 15:03:28 2023-11-20T09:33:28.022030Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.35'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

load-balancer service

NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes               ClusterIP      10.96.0.1       <none>        443/TCP          4h13m
mysql-clusterip-srv      ClusterIP      10.101.179.4    <none>        3306/TCP         9m17s
mysql-loadbalancer-srv   LoadBalancer   10.108.170.58   <pending>     3306:31362/TCP   9m16s

Solution

  • In a public cloud provider environment an external load balancer is provisioned and its public IP address is assigned to your service and the required network configuration to direct traffic from the load balancer to your cluster nodes is automated.

    The issue is your local kubernetes cluster does not have a mechanism to allocate an IP address to the services with the type LoadBalancer. If you really want a LoadBalancer IP, you can use the L2 mode installation of MetalLB. You need to set a suitable IPAddressPool configuration. MetalLB installation steps for kind are the same for the docker desktop as well.

    If you are on a linux machine you should be able to ping IPs from the host directly as there is connectivity from the host network to the docker network.

    NOTE

    If kubectl port-forward is not an option then go for the above installation. Otherwise, port-forwarding would be enough to connect to a database client running on your host.