Search code examples
phpwordpressazureazure-aks

not showing endpoint for service in AKS and label and selectors are mis match


hi i'm new to AKS (azure kubernetes service) below is my YAML which creates service(php-svc) and pod(wordpressb). there are two issues i'm facing.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpressb
spec:
  replicas: 1
  selector:
    matchlabels:
      app: wordpressb
  template:
    metadata:
      labels:
        app: wordpressb
    spec:
      containers:
      - name: wordpressb
        image: acrakswordpress.azurecr.io/wordpress:v1
        ports:
          - containerPort: 80
        env:
        - name: DATABASE_HOST
          value: "<<serverName>>.mysql.database.azure.com"
        - name: DATABASE_USERNAME
          value: "<<dbName>>"
        - name: DATABASE_PASSWORD
          value: "<<dbPassword>>"
        - name: DATABASE_NAME
          value: "<<dbName>>"
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
                - key: "app"
                  operator: In
                  value: wordpressb
            topologyKey: "kubernetes.io/hostname"

apiVersion: v1
kind: Service
metadata:
  name: php-svc
spec:
  type: LoadBalancer
  ports:
    - port: 80
  selector:
    app: wordpressb
  1. nothing shows up when i try to access the external IP address of service - my best guess is it's not working as there is no end point in the service - below shows when i execute kubectl describe service php-svc
Name:                     php-svc
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=wordpressb
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.0.74.48
IPs:                      10.0.74.48
LoadBalancer Ingress:     52.140.2.45
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30580/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  44s   service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   34s   service-controller  Ensured load balancer
  1. base on my understanding this issue is due to label-selector mismatch. it is matching in the YAML code (mentioned above) - but mismatched when executing it - below shows when i execute kubectl describe pod wordpress-blog
Name:             wordpress-blog
Namespace:        default
Priority:         0
Service Account:  default
Node:             aks-nodepool1-41875026-vmss000000/10.224.0.5
Start Time:       Sun, 19 May 2024 21:48:19 +0530
Labels:           run=wordpress-blog
Annotations:      <none>
Status:           Running
IP:               10.244.1.17
IPs:
  IP:  10.244.1.17
Containers:
  wordpress-blog:
    Container ID:   containerd://758ad6ebc7cb0603e0bfed305070a3c990b6cc318069c73fb1649ec1555183a7
    Image:          wordpress
    Image ID:       docker.io/library/wordpress@sha256:f468bab53528df6f87dfe11a80de26eff57e0f515e243d9dec73a02c80c273a7
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 19 May 2024 21:48:22 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jfb56 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-jfb56:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

ideally i should see wordpress page when i try to hit external IP address of the service (php-svc)


Solution

  • As already discussed, your deployment YAML had a typo in the selector field. matchlabels should be matchLabels (note the capitalization of 'L'). As you have fixed that and still not getting the external IP, I am sharing you the yaml files for your ease of setup.

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pv-claim
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 20Gi
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mysql
    spec:
      selector:
        matchLabels:
          app: mysql
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - image: mysql:5.7
            name: mysql
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: rootpassword
            - name: MYSQL_DATABASE
              value: wordpress
            - name: MYSQL_USER
              value: wordpress
            - name: MYSQL_PASSWORD
              value: wordpresspassword
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
          volumes:
          - name: mysql-persistent-storage
            persistentVolumeClaim:
              claimName: mysql-pv-claim
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      ports:
        - port: 3306
      selector:
        app: mysql
    

    Create WordPress Deployment and Service YAML

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: wordpressb
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: wordpressb
      template:
        metadata:
          labels:
            app: wordpressb
        spec:
          containers:
          - name: wordpressb
            image: arkoacr.azurecr.io/wordpress:v1
            ports:
            - containerPort: 80
            env:
            - name: WORDPRESS_DB_HOST
              value: mysql.default.svc.cluster.local:3306
            - name: WORDPRESS_DB_USER
              value: wordpress
            - name: WORDPRESS_DB_PASSWORD
              value: wordpresspassword
            - name: WORDPRESS_DB_NAME
              value: wordpress
    
    

    service

    apiVersion: v1
    kind: Service
    metadata:
      name: php-svc
    spec:
      type: LoadBalancer
      ports:
        - port: 80
          targetPort: 80
      selector:
        app: wordpressb
    

    apply them

    kubectl apply -f mysql-deployment.yaml
    kubectl apply -f wordpress-deployment.yaml
    kubectl apply -f service.yaml
    

    enter image description here

    it should work. enter image description here

    Check service

    kubectl get svc| grep php
    

    enter image description here

    enter image description here

    By ensuring the correct labels and selectors match between the Deployment and Service, your Service should properly route traffic to the correct Pods.

    References: