---
title: Externally exposing a LXD-based Kubernetes service
description: This article originally appeared on Rye Terrell’s blog   So you’ve conjured
  up a Kubernetes cluster on top of LXD on your dev box. Cool. You’ve created a deployment,
  you’ve got a service directing traffic to it, and you can query it from your box.
  Sweet. Time to demo this to your boss! “Hey boss,”  […]
url: https://canonical.com/blog/externally-exposing-a-lxd-based-kubernetes-service?format=md
---

1. [Blog](https://canonical.com/blog)
2. Article

---

[Canonical](https://canonical.com/blog/author/canonical "More about Canonical")

1 February 2018

# Externally exposing a LXD-based Kubernetes service

[Development](https://canonical.com/blog/tag/development)
[kubernetes](https://canonical.com/blog/tag/kubernetes)
[LXD](https://canonical.com/blog/tag/lxd)

---

Share the article

This article originally appeared on [Rye Terrell’s blog](https://medium.com/@ryeterrell/externally-exposing-a-lxd-based-kubernetes-service-821311461950)

So you’ve [conjured up](https://hackernoon.com/cluster-in-a-box-how-to-deploy-one-or-more-kubernetes-clusters-to-a-single-box-76ff3b8e92d7) a Kubernetes cluster on top of LXD on your dev box. Cool. You’ve created a deployment, you’ve got a service directing traffic to it, and you can query it from your box. Sweet. Time to demo this to your boss!

“Hey boss,” starts your email, “check it out — I’ve got our product running in a k8s cluster! Just click here to see it for yourself: https://…”

Oops, what IP do you send your boss? You can’t use the internal LXD container IP. You can’t use the IP of your dev box, no traffic is going to reach the relevant container. Damn. What to do?

There’s actually a few ways to solve this. Here, I’ll cover an iptables one-liner that will forward traffic on a particular port to the proper container.

Let’s make sure we’re on the same page. First, I’ll create a “[hello-world](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/#creating-a-service-for-an-application-running-in-two-pods)” deployment:

```
$ kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
```

Then I’ll create an associated service (with type NodePort, since I want to expose it externally):

```
$ kubectl expose deployment hello-world --type=NodePort --name=example-service
```

Now we should be in roughly the same place. Let’s grab the NodePort for our service:

```
$ kubectl describe services example-service
```

```
Name:                     example-service
Namespace:                default
Labels:                   run=load-balancer-example
Annotations:              <none>
Selector:                 run=load-balancer-example
Type:                     NodePort
IP:                       10.152.183.175
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30386/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
```

And find one of the nodes it’s running on:

```
$ kubectl get pods --selector="run=load-balancer-example" --output=wide
```

```
NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE
hello-world-58f9949f8-2cqw7   1/1       Running   0          1h        10.1.7.2     juju-2282c0-7
hello-world-58f9949f8-k5zvl   1/1       Running   0          1h        10.1.102.6   juju-2282c0-3
```

Next we’ll need to find the IP address associated with that node:

```
$ lxc info juju-2282c0-3 | grep eth0
```

```
eth0: inet 10.218.5.81 vethV8TI50
```

Finally, using the node IP and the NodePort information we just collected, we’ll set up an iptables rule (note that the port 8080 is the port I’ll expose on my host):

```
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 8080 -j DNAT --to-destination 10.218.5.81:30386
```

Feel free to use *iptables-save* and *iptables-persistent* to allow your new rule to survive a reboot.

Now we can test it out from another host (note that 35.169.124.27 is the IP of my host):

```
$ curl 35.169.124.27:8080
Hello Kubernetes!
```

Great! Alright, go finish that email.

### Want to know more?

On February 7th, technical lead Stephane Graber will be presenting a webinar for [Ubuntu Product Month](https://pages.ubuntu.com/ubuntu_product_month.html) that will dive into how LXD works, what it does, how it can be used in the enterprise, and even provide an opportunity for Q&A.

[Register For Webinar](https://www.brighttalk.com/webcast/6793/298235?utm_source=insights&utm_medium=blog&utm_campaign=FY18_Cloud_LXD_WBN_IntroToLXD)

## Sign up for our newsletter

Get the latest Canonical news and updates in your inbox.

Work email:

\*I agree to receive information about Canonical's
products and services.

By submitting this form, I confirm that I have read and agree to [Canonical's Privacy Policy](https://canonical.com/legal/dataprivacy).

Sign up

## Share on

---

## Related posts

[### Ubuntu Server: a platform made for enterprise scale](https://canonical.com/blog/ubuntu-server-a-platform-made-for-enterprise-scale)

A platform is an environment that allows software to run smoothly across the infrastructure, runtime, and application layers. The key word there is “smoothly”: a good platform...

[Rhys Knipe](https://canonical.com/blog/author/rhysknipe)

7 July 2026

[### Introducing MicroCloud Cluster Manager](https://canonical.com/blog/introducing-microcloud-cluster-manager)

Canonical introduces the beta release of MicroCloud Cluster Manager, a new way to discover, organize, and operate your MicroCloud environments from a single, unified interface.

[Miona Aleksic](https://canonical.com/blog/author/mionaalex)

20 March 2026

[### How to set up a micro lab: four principles for a reliable homelab](https://canonical.com/blog/how-to-set-up-a-reliable-homelab)

After over a decade of running a homelab, I have learned a few difficult lessons. Although it begins as a “lab,” you inevitably end up with something you want to keep. If a...

[Jake Nabasny](https://canonical.com/blog/author/slapcat)

17 March 2026

[### Deploy your Spring Boot application to production](https://canonical.com/blog/deploy-spring-application-to-production)

In this article we walk through the steps required to deploy a Spring Boot application to production using Juju and Kubernetes. The goal is to showcase the integration of the...

[Javier de la Puente](https://canonical.com/blog/author/javierdelapuente)

13 January 2026
