Defining node names on the yaml files with Loadbalancer and ports in AKS cluster
Tags: aks, loadbalancer, namespace, ports
Published: 2022-10-23 16:21:46

I have the three nodes on the AKS cluster configured and I have configured 2 namespaces configured in the AKS cluster.

Namespace commands:

kubectl create namespace namespace name
kubectl get namespaces
kubectl describe namespaces namespace name
kubectl delete namespaces namespace name

Yaml file: tomcat-test.yaml

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-test-helloworld
  labels:
    app: tomcat-test-helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-test-helloworld
  template:
    metadata:
      labels:
        app: tomcat-test-helloworld
    spec:
      nodeName: xxx
      containers:
      - name: tomcat-test-helloworld
        image: xxx.azurecr.io/test-helloworld:v1
        resources:
          limits:
            memory: "2Gi"
            cpu: "1200m"
        ports:
        - containerPort: 8080
      imagePullSecrets:
      - name: secret
---
apiVersion: v1
kind: Service
metadata:
    name: tomcat-test-helloworld
    annotations:
      service.beta.kubernetes.io/azure-load-balancer-internal: "true"
spec:
    type: LoadBalancer
    loadBalancerIP: xx.xx.xxx.xx
    ports:
    - port: 8080
      targetPort: 8080
    selector:
        app: tomcat-test-helloworld
```

Yaml file: tomcat-sample.yaml

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-sample-helloworld
  labels:
    app: tomcat-sample-helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-sample-helloworld
  template:
    metadata:
      labels:
        app: tomcat-sample-helloworld
    spec:
      nodeName: xxx
      containers:
      - name: tomcat-sample-helloworld
        image: xxx.azurecr.io/sample-helloworld:v1
        resources:
          limits:
            memory: "2Gi"
            cpu: "1200m"
        ports:
        - containerPort: 8080
      imagePullSecrets:
      - name: secret
---
apiVersion: v1
kind: Service
metadata:
    name: tomcat-sample-helloworld
    annotations:
      service.beta.kubernetes.io/azure-load-balancer-internal: "true"
spec:
    type: LoadBalancer
    loadBalancerIP: xx.xx.xxx.xx
    ports:
    - port: 8081
      targetPort: 8080
    selector:
        app: tomcat-sample-helloworld


```
Using nodename we can define from which node this is to be executed. Memory is defined. Secret is configured for pulling images. To use the private IP, annotation is used in the service type. Load balancer IP is defined.

kubectl apply -f tomcat-test.yaml -n namespace-name
kubectl apply -f tomcat-sample.yaml -n namespace-name

We cannot define same ports for the loadbalancers and that is the reason the port is modified as 8081 in tomcat-sample.yaml

Though you define two different application in two different namespaces with the same container port, the application depends on the image what you use and what load balancer port we define in the yaml script (specified as port under ports section in the service yaml). If load balancer port is 8080, then tomcat-test-helloworld is exposed and if load balancer port is 8081 then tomcat-sample-helloworld is exposed. 

Example from ActiveMQ port issue:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ah4ja9x71u4fs0cswmhe.png)

There can be different applications running on different ports on same load balancer IP as configured. Node name is defined to make the deployment happen in the specified node.

To create secret:

```
kubectl --kubeconfig "config-file-path" create secret docker-registry secret --docker-server=xxx.azurecr.io --docker-username=xxx --docker-password=xxx -n namespace name
```