Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active January 19, 2021 07:19
Show Gist options
  • Save alexellis/c29dd9f1e1326618f723970185195963 to your computer and use it in GitHub Desktop.
Save alexellis/c29dd9f1e1326618f723970185195963 to your computer and use it in GitHub Desktop.
Expose Kubernetes ClusterIP services with inlets.dev

KinD with inlets.dev

Expose Kubernetes ClusterIP services with inlets.dev

Get KinD:

# Linux

sudo curl -Lo /usr/local/bin/kind \
 https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-linux-amd64

# MacOS

sudo curl -Lo /usr/local/bin/kind \
 https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-darwin-amd64

Create the cluster

kind create cluster

Switch to the kind cluster with kubectl

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"

Create a sample service

We'll deploy a HTTP server that runs the figlet binary to generate ASCII logos

  • Define a service
apiVersion: v1
kind: Service
metadata:
  name: openfaas-figlet
  labels:
    app: openfaas-figlet
spec:
  type: ClusterIP
  ports:
    - port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: openfaas-figlet

Define a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: openfaas-figlet
  labels:
   app: openfaas-figlet
spec:
  replicas: 1
  selector:
    matchLabels:
      app: openfaas-figlet
  template:
    metadata:
      labels:
        app: openfaas-figlet
    spec:
      containers:
      - name: openfaas-figlet
        image: functions/figlet:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP

Save both files and create the objects with kubectl create -f

Now get inlets

cd /tmp/

# Download to local directory
curl -sLS https://get.inlets.dev | sh

chmod +x ./inlets
sudo mv inlets /usr/local/bin/

inlets --version
Version: 2.1.0
Git Commit: c23f6993892a1b4e398e8acf61e3dc7bfcb7c6ed

Start an exit-node on your laptop (inlets server)

Our Kubernetes cluster will connect to this server.

export token=$(head -c 16 /dev/urandom | shasum | cut -d" " -f1)
inlets server --port=8090 --token="$token" --print-token=true

Note your token when the server starts up.

Run the inlets client as a Kubernetes Deployment

Create a secret for the inlets client:

export TOKEN="" # Use the value from earlier
kubectl create secret generic inlets-token --from-literal token=${TOKEN}

Apply the Deployment YAML file, with kubectl apply -f.

Change the following two parameters:

Use your laptop's IP in place of REMOTE-IP:

- "--remote=ws://REMOTE-IP"

My IP for my WiFi interface is 192.168.1.51.

Note: your "exit-node" could be any PC that has reachability including a VPS with a public IPv4 address.

Specify the service, or services which you want to expose:

- "--upstream=http://openfaas-figlet.default:8080"

This is the sample YAML:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: inlets
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: inlets
    spec:
      containers:
      - name: inlets
        image: alexellis2/inlets:2.1.0
        imagePullPolicy: Always
        command: ["inlets"]
        args:
        - "client"
        - "--remote=ws://REMOTE-IP"
        - "--upstream=http://openfaas-figlet:8080"
        - "--token-from=/var/inlets/token"
        volumeMounts:
          - name: inlets-token-volume
            mountPath: /var/inlets/
      volumes:
        - name: inlets-token-volume
          secret:
            secretName: inlets-token

Access your service

You can now access the service inside the KinD cluster, from the inlets server port and IP.

curl 192.168.1.51:8090 -d "inlets.dev"
 _       _      _            _            
(_)_ __ | | ___| |_ ___   __| | _____   __
| | '_ \| |/ _ \ __/ __| / _` |/ _ \ \ / /
| | | | | |  __/ |_\__ \| (_| |  __/\ V / 
|_|_| |_|_|\___|\__|___(_)__,_|\___| \_/  

You could also use 127.0.0.1:8090 on your local machine.

Access multiple services

Run Nginx and expose it:

kubectl run static-web --image nginx --port 80
kubectl expose deploy/static-web --port 80 --target-port 80

Edit the upstream parameter (kubectl edit deploy/inlets):

        - "--upstream=openfaas-figlet.local=http://openfaas-figlet:8080,static-web.local=http://static-web:80"

Now setup two hosts file entries in /etc/hosts:

127.0.0.1  openfaas-figlet.local
127.0.0.1  static-web.local

Now access either:

curl -d hi http://127.0.0.1:8090 -H "Host: openfaas-figlet.local"
curl http://127.0.0.1:8090 -H "Host: static-web.local"
@alexellis
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment