I'm running a local kubernetes bundled with docker on Mac OS.
How can I expose a service, so that I can access the service via a browser on my Mac?
I've created:
a) deployment including apache httpd.
b) service via yaml:
apiVersion: v1
kind: Service
metadata:
name: apaches
spec:
selector:
app: web
type: NodePort
ports:
- protocol: TCP
port: 80
externalIPs:
- 192.168.1.10 # Network IP of my Mac
My service looks like:
$ kubectl get service apaches
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
apaches NodePort 10.102.106.158 192.168.1.10 80:31137/TCP 14m
I can locally access the service in my kubernetes cluster by wget $CLUSTER-IP
I tried to call http://192.168.1.10/ on my Mac, but it doesn't work.
This question deals to a similar issue. But the solution does not help, because I do not know which IP I can use.
Update
Thanks to Michael Hausenblas I worked out a solution using Ingress. Nevertheless there are still some open questions:
- What is the meaning of a service's externalIP? Why do I need an externalIP when I do not directly access a service from external?
- What is the meaning of the service port 31137?
- The kubernetes docs describe a method to [publish a service in minikube via NodePort][4]. Is this also possible with kubernetes bundled on docker?
The proper way to do it is to use Ingress as explained for example in this post
There are several solutions to expose services in kubernetes: http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/
Here are my solutions according to alesnosek for a local kubernetes bundled with docker:
1. hostNetwork
Dirty (the host network should not be shared for security reasons) => I did not check this solution.
2. hostPort
Does not apply to services => I did not check this solution.
3. NodePort
Expose the service by defining a nodePort:
4. LoadBalancer
EDIT @MathObsessed posted the solution in his anwer.
5. Ingress
a. Install Ingress Controller
b. Configure Ingress
kubectl apply -f apache-ing.yaml
Now I can access my apache deployed with kubernetes by calling http://localhost/
Remarks for using local-dev-with-docker-for-mac-kubernetes
Further documentation
For those still looking for an answer. I've managed to achieve this by adding another Kube service just to expose my app to localhost calls (via browser or Postman):
Try it now on: http://localhost:8080
As already mentioned in Matthias Ms answer there are several ways.
As the offical Kubernetes documentation specifically describes using a
Service
with atype
NodePort
I wanted to describe the workflow.Setup a Service with a
type
ofNodePort
Then you can check on which port the Service is exposed to via
and access it via localhost using the exposed port. E.g.