Sunday, March 21, 2021

Kubernetes - Kubernetes services

 Service

NodePort - service makes internal pod accessible on a pod on a node.
ClusterIP -  Service creates a virtual IP inside the cluster to enable communication between different services such as a set up front end servers to a backend servers.
Load Balancer - privisions a load balaner for our application in supported cloud providers.
- disctrubites load among web servers.

NodePort Kubernetes service
- External access to application
- maping a port on node to port on the pod.

There are three port involve
- The port on the POD wehre actual webserver is running, port  80, which is infact a target port.
- This is where service forwards the request to.
- 2nd port is the port on the service itself which is port 80.
- Service is infact like a vurtual server inside node. It has its own IP address. This IP address is called the cluster IP of the service.
- Finally, we have port on the node itself,  which we use to access the web server, externally, which is known as NodePort.(30008 - 32767



Lets create a service
$ cat myserv.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
    - targetPort: 80        # target port
      port: 80        # port on service object
      nodePort: 30008    # on node
# what connect the pod?
# label and selector
  selector:
    app: myapp
   type: front-end

> kc create -f myserv.yaml
> kc get services

> curl http://192.168.1.2:30008    # ip of node

This example for single pod. what happens when you have multiple pods?
if you have multiple similar web apps and have a labels app: myapp. lets say
we have thre pod created with selector and labels,

selector:
  app: myapp

labels:
  app: myapp

same label is used as  a selector. when service is created, it look for matching pod with a label, finds 3 of them.
The service then automaticallly selects all the pod as end poitnts to forward external request coming from users.

You don't have to do any additional configuration to make this happen.

Algorithum: random



you can curl all pod with
http://<pod_ip>:port



ClusterIP:
- Front-end, backend, redis,

front end -> communicates with back-end systems - > to database systems

- Pod can go down any time and ip may change any time.
- k8s service can group the pods

service created for backend can have single access poing.
this enables easily deploy microservices.

Lets create a service definition file
$ cat serv-def.yaml
apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  type: ClusterIP        # default type
  ports:
    - targetPort: 80        # Port where backend is exposed
      port: 80        # port where service is exposed
      nodePort: 30008    # on node
# to link the service to a set of pods, we use selector, and define the labels from the pod-definition file.
# under labels and put it under selector
# label and selector
  selector:
    app: myapp
    type: back-end

Now, create a service
> kc create -f serv-def
> kc get services

service can be access using cluster IP or service name.

Service LoadBalancer
- you have multiple pods runing and they have their own IP and port. which one  do you give it to the client use. Say you have 3 instances of web services, do you give all three IP/Port to the client?
- What if one of the pod goes down?
- User need one single URL.
one way to achieve this is to create a vm with loadbalancer.

seting new VM, external laodbalancer, maintaining, managing is tedious tasks.
however, if you are on cloudplatform such as aws, google, azure, you can leverage the their native laod balancer. K8s has feature of integrating the feature of loadbalancer.

All you have to do is set the front-end service type to LoadBalance instead of nodeport.
Note: this only works with support of cloud platform such as GCP, azure, aws.

If you set type to loadbalancer on other env such as virtual box, it will have same effect as setting it to nodeport where services are exposed. on high end port on node.

it wont do any kind of external load balancer configuration.
 

steps:
1. create deployment
2. create services (ClusterIP)
3. Create services(LoadBalancer)

$ cat serv-def.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: LoadBalancer
  ports:
    - targetPort: 80   
      port: 80       
      nodePort: 30008   

Now, create a service
> kc create -f serv-def.yaml
> kc get services


Lab
1. How many Services exist on the system?

in the current(default) namespace
root@controlplane:~# kc get svc
bash: kc: command not found
root@controlplane:~# alias kc=kubectl
root@controlplane:~# kc get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   24m
root@controlplane:~#
Ans: 1

2. That is a default service created by Kubernetes at launch.
That is a default service created by Kubernetes at launch.

What is the type of the default kubernetes service?
- LoadBalancer
- ClusterIP
- NodePort
- External
ANS: ClusterIl

3. What is the targetPort configured on the kubernetes service?
- 8080
- 443
- 6443
- 10.96.0.1

Hints: Run the command kubectl describe service and look at TargetPort.

root@controlplane:~# kc describe service
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP Families:       <none>
IP:                10.96.0.1
IPs:               10.96.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         10.81.158.9:6443
Session Affinity:  None
Events:            <none>
root@controlplane:~#

5. How many labels are configured on the kubernetes service?

Hints: Run the command kubectl describe service and look at Labels.
Fron the output above, there are two labels.

6. How many Endpoints are attached on the kubernetes service?
fron the output there is only one endpoint

Hints: Run the command kubectl describe service and look at Endpoints.

7. How many Deployments exist on the system now in the current(default) namespace?
root@controlplane:~# kc get deployment
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
simple-webapp-deployment   4/4     4            4           30s
root@controlplane:~#

output shows 1 available
hint: Run the command kubectl get deployment and count the number of pods.

8. What is the image used to create the pods in the deployment?

Hints: Run the command kubectl describe deployment and look under the containers section.

root@controlplane:~# kc describe deployment
Name:                   simple-webapp-deployment
Namespace:              default
CreationTimestamp:      Mon, 22 Mar 2021 02:09:56 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               name=simple-webapp
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  name=simple-webapp
  Containers:
   simple-webapp:
    Image:        kodekloud/simple-webapp:red
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   simple-webapp-deployment-b56f88b77 (4/4 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  3m1s  deployment-controller  Scaled up replica set simple-webapp-deployment-b56f88b77 to 4
root@controlplane:~#

Ans:
    Image:        kodekloud/simple-webapp:red

9. Are you able to accesss the Web App UI?
Try to access the Web Application UI using the tab simple-webapp-ui above the terminal.

No, got error
502 bad gateway

10. Create a new service to access the web application using the service-definition-1.yaml file

Name: webapp-service
Type: NodePort
targetPort: 8080
port: 8080
nodePort: 30080
selector: simple-webapp
hint: Update the given values in the service definition file and create the service.

------
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  ports:
    - targetPort: 8080
      port: 8080
      nodePort: 30080
  selector:
    name: simple-webapp
                     
$ kc apply -f serv-def.yaml
service/webapp-service created
root@controlplane:~# kc get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          42m
webapp-service   NodePort    10.110.104.205   <none>        8080:30080/TCP   25s
root@controlplane:~#


Review
Answers
> kc get svc

2. type=clusterIP

3. target service
> kc describe svc kubernetes
look for targetport: 644

4. how may labels
2 labels from output

5. End points?
endpoint=1
6. Ho wmay deployments?
> kc get deployments

only one
7. imae used ?
 $ kc describe deployment simple-web-app-deployment | grep -i image
you see the output

8. can you access the webUI? - No
9. Create a new service?
we didn't have proper service configured

create yaml file

root@controlplane:~# kc expose deployment simple-webapp-deployment --name=webapp-service --target-port=8080 --type=NodePort --port=8080 --dry-run=client -o yaml >myapp.yaml
root@controlplane:~# vi myapp.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  name: webapp-service
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
    nodePort: 30080
  selector:
    name: simple-webapp
  type: NodePort
status:
  loadBalancer: {}

root@controlplane:~# kc apply -f myapp.yaml
service/webapp-service configured
root@controlplane:~#






No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...