John Mark Causing

System Administrator | Hosting Support Engineer

Bacolod City, Philippines

+639393497018

John Mark Causing

System Administrator | Hosting Support Engineer

Bacolod City, Philippines

+639393497018

Reading this article will guide us on how to setup basic steps to expose a Kubernetes service. Example: Accessing an Nginx web server from a Multipass Ubuntu VMs using Kubernetes deployment and service.

Things we need:

Deployment

Below is where we deploy a simply nginx web server with 1 replica that runs on port 8080

cat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.4
        ports:
        - containerPort: 80
EOF

Install MetalLB

The the commands below to install MetalLB. This is required for us to expose our service (access the nginx web server on our machine / not just inside the VMs (using Multipass))

Source: https://makeoptim.com/en/service-mesh/kubeadm-kubernetes-istio-setup#metallb

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/metallb.yaml
# On first install only
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"

Get the IP addresses of your worker nodes.

kubectl get nodes -o wide

Below we got the node1 and node two IP’s (doesn’t matter if they are internal)

Now we need to set an IP range for each node. Node1 is 172.29.94.50-172.29.94.254 and Node2 is 172.29.91.50-172.29.91.254

Which means that our service can generate an IP between 172.29.94.50-172.29.94.254 and is 172.29.91.50-172.29.91.254

kubectl apply -f - <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 172.29.94.50-172.29.94.254
      - 172.29.91.50-172.29.91.254
EOF

You can also edit the MetalLB config by running:

kubectl edit configmap -n metallb-system

Expose the service!

The code below shows that we selected the deployment nginx , we set the type to LoadBalancer and we name the service my-service

kubectl expose deployment nginx --type=LoadBalancer --name=my-service

Check the demo

Remember we are using Multipass to create our VMs, right? The goal here is to access the service inside and outside our Multipass servers.

Inside multipass

root@k8s-master:~# curl 172.29.94.50 -n 10
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

Outside multipass (Windows command prompt)

Google Chrome from my Windows 10 Machine