Kubernetes

[Kubernetis] Using Kubernetis service objects

트리스탄1234 2024. 3. 24. 20:57
728x90
반응형

Hello neighbors ^.^
Good morning to you.

Today, I'm going to post about the types of Kubernetes service objects and how to create and manage them.
First of all, I will create a pod using the Depoyment and connect the pod using the Service.

 

Service is an abstraction method that exposes applications running as Pod services to network services. Pod has its own IP when created, but given the environment that is frequently created/deleted/expanded, it is impossible to use the service with a given Pod's IP. So, Kubernetes uses a resource called Service and servie object provide below.

 

  • Fixed IP
  • Loadbalancing for Multiple Pods

Service applies to external users or systems, as well as to Pods within the cluster.
There are three types of service objects.

  • ClusterIP: A service object used for internal use in a cluster. It is not accessible from outside.
  • NodePort—Enables access to a specific port when connecting to a worker node from outside.
  • Loadbalance : Use the cloud provider's load balancer to expose services to the outside world.
  • Ingress: Like NodePort and LoadBalancer, it is a resource used to expose an application's service to the outside world. It creates ingress resources for HTTP and HTTPS traffic from the outside to be routed in the L7 area as a service within the cluster and provides load balancing, TLS, and domain-based virtual hosting
    A simple illustration is as follows.

1. Create Deployment Yaml with the following content..

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ vi test-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
  labels:
    app: my-nginx
    tier: frontend
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - image: nginx:1.19.3
        name: my-nginx
        ports:
        - containerPort: 80

 

2. Create and query the Pod using the Deployment that you created as follows.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl apply -f test-deployment.yaml
deployment.apps/my-nginx-deployment created
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pod -o wide
NAME                                   READY   STATUS    RESTARTS   AGE   IP           NODE              NOMINATED NODE   READINESS GATES
my-nginx-deployment-55985c7fcf-q7p7t   1/1     Running   0          8s    172.17.0.6   ip-172-31-31-84   <none>           <none>
my-nginx-deployment-55985c7fcf-t7m69   1/1     Running   0          8s    172.17.0.7   ip-172-31-31-84   <none>           <none>
my-nginx-deployment-55985c7fcf-v55d4   1/1     Running   0          8s    172.17.0.3   ip-172-31-31-84   <none>           <none>

 

3. Now create another Pod using the command below.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl run curlpod --image=radial/busyboxplus:curl --command -- /bin/sh -c "while true; do echo hi; sleep 10; done"
pod/curlpod created

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pod -o wide
NAME                                   READY   STATUS    RESTARTS   AGE    IP           NODE              NOMINATED NODE   READINESS GATES
curlpod                                1/1     Running   0          22s    172.17.0.8   ip-172-31-31-84   <none>           <none>
my-nginx-deployment-55985c7fcf-q7p7t   1/1     Running   0          102s   172.17.0.6   ip-172-31-31-84   <none>           <none>
my-nginx-deployment-55985c7fcf-t7m69   1/1     Running   0          102s   172.17.0.7   ip-172-31-31-84   <none>           <none>
my-nginx-deployment-55985c7fcf-v55d4   1/1     Running   0          102s   172.17.0.3   ip-172-31-31-84   <none>           <none>
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$

 

4. Now, create a service of ClusterIP Type among the service objects and make them accessible through the service objects that you created when the service is requested with the Pods that were previously created by Deployment.
1) Create a YAML file with the following contents through VI, create it, and query it.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl apply -f test-service.yaml
service/nginx-clusterip-service created

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get service -o wide
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes                ClusterIP   10.96.0.1      <none>        443/TCP   23h   <none>
nginx-clusterip-service   ClusterIP   10.98.129.94   <none>        80/TCP    13s   app=my-nginx
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$

 

5. Now, let's request the service with Pod through Service's IP.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl exec -it curlpod -- curl http://10.98.129.94
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$

kubectl exec -it curlpod -- curl http://10.98.129.94

 

The above command is a command that sends a http request message to the service from the curlpod to the curl command.
The inquiry result received a normal http response. And it is possible through the name of the service object instead of the Cluster IP

 

The reason why it can be connected by name is that K8S DNS converts the name to IP inside the cluster.
If you look up the /etc/resolv.conf file in the cirlpod as follows, it is because the service object is specified in the search part.

 

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl exec -it curlpod -- cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local ec2.internal
options ndots:5
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$

6. Now let's create a service object of NodePort Type through below and yaml.
1) Create the yaml file as follows.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ vi test-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport-service
spec:
  type: NodePort
  selector:
    app: my-nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30007

 

2) Create a service with a yaml file and look it up.

ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl apply -f test-nodeport.yaml
service/nginx-nodeport-service created
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get svc -o wide
NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes                ClusterIP   10.96.0.1       <none>        443/TCP        24h   <none>
nginx-clusterip-service   ClusterIP   10.98.129.94    <none>        80/TCP         13m   app=my-nginx
nginx-nodeport-service    NodePort    10.97.173.120   <none>        80:30007/TCP   14s   app=my-nginx

 

If you look for a part that is different from the ClusterIP Type, it is the PORT part. If you come into port 80 from the outside, you can send it internally to 30007. Now, if you try to connect to http://Node IP:30007, you can see that the connection works as shown below.

 

7. Now let's create an Ingress-type service.
1) Create a YAML file with the following contents.

ubuntu@ip-172-31-31-84:~$ vi test-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: my-nginx.io
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx-clusterip-service
                port:
                  number: 80

If you try to access host: my-nginx.io , DNS will deliver the node's IP to the client, and the client will be able to access MY-NGINX.IO. Registration fee is required if you register with DNS. We will proceed by manually entering it into the host file and testing it.

2) Write an Ingress using the generated YAML file and inquire.

ubuntu@ip-172-31-31-84:~$ kubectl apply -f test-ingress.yaml
ingress.networking.k8s.io/my-nginx-ingress created
ubuntu@ip-172-31-31-84:~$
ubuntu@ip-172-31-31-84:~$
ubuntu@ip-172-31-31-84:~$ kubectl get ingress
NAME               CLASS   HOSTS         ADDRESS   PORTS   AGE
my-nginx-ingress   nginx   my-nginx.io             80      20s

 

3) Then enter the following information in the host file of the pc you are using.

Windows라면 C:\Windows\System32\drivers\etc\hosts 파일에,
You can add the Linux series to the /etc/hosts file.

11.22.33.44  my-nginx.io

4)If you enter the address of http://my-nginx.io into the web browser and access it, you can confirm that the normal connection is as follows.

 

https://blog.naver.com/ppp0183

728x90
반응형