Skip to content

Exposing services

While pods are not accessible from outside the cluster, you can expose the http services provided by pods by using the Ingress controllers. In general we don’t allow exposing non-http applications via TCP ports, but if you really need to do that, contact us on Nautilus Support.

For a complete example see the (Tutorial)

Refer to the HAProxy Ingress Documentation to set up additional config. The annotation to use is either haproxy-ingress.github.io or ingress.kubernetes.io. DO NOT use the haproxy.org annotation to configure the Ingress.

To expose a port in your pod, you first need to create a service for it. For the pod

apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
k8s-app: test-http
spec:
containers:
- name: mypod
image: nginxdemos/hello:plain-text

The service might look like:

apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: test-svc
name: test-svc
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 80
selector:
k8s-app: test-http
type: ClusterIP

Where spec.selector.<label> should match the label of the target pod (k8s-app: test-http), and targetPort should match the port you want to expose. You can test the pod/service by creating a tunnel (kubectl port-forward service/test-svc 8080:8080) and querying service (curl http://localhost:8080/).

After that you can create the Ingress object:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
spec:
ingressClassName: haproxy
rules:
- host: test-service.nrp-nautilus.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-svc
port:
number: 8080
tls:
- hosts:
- test-service.nrp-nautilus.io

You can choose the host subdomain to be whatever you want (<whatever>.nrp-nautilus.io). This is enough to have your pod served under test-service.nrp-nautilus.io. You can test this example via curl (curl https://test-service.nrp-nautilus.io)

Using my own domain name

If you need to use your own domain, you would have to also provide a valid certificate in a secret in your namespace:

apiVersion: v1
kind: Secret
metadata:
name: my-own-hostname-tls
type: kubernetes.io/tls
data:
ca.crt: <optional base64-encoded ca>
tls.crt: <base64-encoded crt>
tls.key: <base64-encoded key>

and add a section to the Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
spec:
ingressClassName: haproxy
rules:
- host: my-own-hostname.com
http:
paths:
- backend:
service:
name: test-svc
port:
number: 8080
path: /
pathType: Prefix
tls:
- hosts:
- my-own-hostname.com
secretName: my-own-hostname-tls

Create the CNAME DNS record for your domain pointing to nrp-nautilus.io (for geo-balanced multi-region DNS record) or east.nrp-nautilus.io (just the eastern region).

Auto renewing the certificate

The Cert Manager installed in our cluster supports auto retrieving and updating ACME Let’s Encrypt certificates. To set up the HTTP challenge, create an issuer:

apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: letsencrypt
spec:
acme:
email: <your_email>
preferredChain: ''
privateKeySecretRef:
name: issuer-account-key
server: https://acme-v02.api.letsencrypt.org/directory
solvers:
- http01:
ingress:
class: haproxy
ingressTemplate:
metadata:
annotations:
ingress.kubernetes.io/ssl-redirect: 'false'
serviceType: ClusterIP

And then request the new certificate:

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
annotations:
name: my-own-hostname-cert
spec:
commonName: my-own-hostname.com
dnsNames:
- my-own-hostname.com
issuerRef:
kind: Issuer
name: letsencrypt
secretName: my-own-hostname-tls

Check the status of your certificate:

kubectl get certificate -o wide

If STATUS is not Certificate is up to date and has not expired, use kubectl describe certificate ... and kubectl describe order ... to debug the problem.

Once the certificate was issued, refer to the previous section to use one in your Ingress.

Exposing to other namespaces

Pods accept connections from their own namespace, from the internet through an Ingress or a LoadBalancer VIP, and from platform services such as the ingress controllers and monitoring. Pods in other namespaces can’t connect: the Service name (test-svc.my-namespace, test-svc.my-namespace.svc or test-svc.my-namespace.svc.cluster.local) still resolves, but the connection times out. Services on a public LoadBalancer VIP are the exception: other namespaces reach them just as the internet does.

To let other namespaces connect, add a NetworkPolicy in your namespace that selects the pods and allows them. Once a NetworkPolicy selects a pod, the pod accepts only what your NetworkPolicies allow and the defaults above no longer apply to it, so list everything it should still accept. This example, for the pod above, keeps its own namespace, the ingress controllers and monitoring, and opens its port to two namespaces:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-http-from-other-namespaces
spec:
podSelector:
matchLabels:
k8s-app: test-http
policyTypes: [Ingress]
ingress:
- from:
- podSelector: {} # pods in this namespace
- from:
- namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values: [haproxy, envoy-gateway-system, monitoring] # ingress controllers and Prometheus
- from:
- namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values: [client-namespace-a, client-namespace-b]
ports:
- port: 80
protocol: TCP

The ports in a NetworkPolicy are the pod’s ports (the Service’s targetPort): 80 here, not 8080. To open the port to every namespace, replace the last rule’s namespace list with an empty selector:

- from:
- namespaceSelector: {}
ports:
- port: 80
protocol: TCP

A rule without from allows every source, the internet and every namespace. If clients outside the cluster connect to the pod directly, not through an Ingress, add an ipBlock rule for them.

When a connection to another namespace times out

  1. From a pod in the client namespace, connect to the Service, for example kubectl exec <client-pod> -- nc -z -w 3 test-svc.my-namespace 8080. A timeout means the connection is dropped; a “connection refused” or an answer means it got through.
  2. In the destination namespace, run kubectl get networkpolicy and check that one of the policies selecting the pod allows the client namespace on the pod’s port.
  3. If no NetworkPolicy selects the pod, add one as above.
U.S. National Science Foundation

This work was supported in part by National Science Foundation (NSF) awards CNS-1730158, ACI-1540112, ACI-1541349, OAC-1826967, OAC-2112167, CNS-2100237, CNS-2120019.