# Kubernetes - Service, Ingress
###### tags: `Kubernetes`
## Foreword
> In the Kubernetes cluster, we want to connect to the Pod we can use the “port-forward” to achieve the port forwarding. However, we have many Pods need to connect. We can use the Service to connect.
>
## Service
### Definition
> An abstract way to expose an application running on a set of Pods as a network service. Every Pod get its own IP address, however in Deployment the Pod may be create or delete. The IP address of Pod will be changed.
>
### Type of Service
* ClusterIP
Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.
* NodePort
Exposes the Service on each Node's IP at a static port. You'll be able to contact the NodePort Service, from outside the cluster, by requesting `<NodeIP>:<NodePort>`.
* Loadbalancer
If your Kubernetes Cluster is installed on Amazon or Google Cloud Platform. We can use the Loadbalancer from cloud provider. Exposes the Service externally using a cloud provider's load balancer.
## Practice
> If we want to create a service on k8s we need to create a deployment first.
>
> `kubectl apply -f demo-deployment.yml`
>
````yaml=1
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: blue-nginx
spec:
replicas: 2
template:
metadata:
labels:
app: blue-nginx
spec:
containers:
- name: nginx
image: hcwxd/blue-whale
ports:
- containerPort: 3000
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: purple-nginx
spec:
replicas: 2
template:
metadata:
labels:
app: purple-nginx
spec:
containers:
- name: nginx
image: hcwxd/purple-whale
ports:
- containerPort: 3000
````
> Then use `kubectl expose deploy blue-nginx --type=NodePort --name=my-deployment-service` to create Service.
>
> We also can use the yaml file to create Service.
>
````yaml=1
apiVersion: v1
kind: Service
metadata:
name: blue-service
spec:
type: NodePort
selector:
app: blue-nginx
ports:
- protocol: TCP
port: 80
targetPort: 3000
NodePort: 3003
---
apiVersion: v1
kind: Service
metadata:
name: purple-service
spec:
type: NodePort
selector:
app: purple-nginx
ports:
- protocol: TCP
port: 80
targetPort: 3000
NodePort: 3002
````
* Type is NodePort
* mtargetPort -> the port of container allow to connect
* port -> the targetPort of Pod mapping to the Cluster IP of Service
* nodePort -> the targetPort of Pod mapping to the IP of Node
* selector -> select which Pod we want to expose
> Now we can use `<Node IP>:<Node Port>` to connect the Pod.
## Ingress
### Definition
> An API object that manages external access to the services in a cluster, typically HTTP. Ingress may provide load balancing, SSL termination and name-based virtual hosting.
>
### Introduction
> When we have many services, there have many ports to manage. If we want to easy to manage. We can use the Ingress.
>
> Follow this figure, we can see the how service and Ingress to work.
>

> Use the ingress we only just use the hostname, then we can connect to the Pod. Because the ingress can automatically distribute.
> In Kubernetes there have no ingress-controller, so we need to install the ingress-controller.
>
> (Installation guide https://hackmd.io/@CharlieChan/SkKXtrSgv)
>
### Practice
> We can use this yaml file to create the Ingress.
>
````yaml=1
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web
spec:
rules:
- host: blue.demo.com
http:
paths:
- backend:
serviceName: blue-service
servicePort: 80
- host: purple.demo.com
http:
paths:
- backend:
serviceName: purple-service
servicePort: 80
````
> Than we can use the `kubectl get ingress` to get the information. If we use the ingress, we need to edit the `/etc/hosts` let the DNS Server to find the hostname.
>
````=1
echo <node IP> blue.demo.com >> /etc/hosts
echo <node IP> purple.demo.com >> /etc/hosts
````
> Now we can type the `blue.demo.com` on browser, then we can see the picture like this.
>

## Reference
> https://medium.com/@C.W.Hu/kubernetes-implement-ingress-deployment-tutorial-7431c5f96c3e
>
> https://kubernetes.io/docs/concepts/services-networking/ingress/
>