Service details of Kubernetes core concept

Source: Internet
Author: User
Tags k8s

Service is a very important component of k8s, the role is to act as a proxy in the pod in the container services published, provide a set of simple discovery mechanisms and service agents, that is, operations often said ' Front-end ' concept, then how it implements the proxy function and the Auto scaling service architecture, as detailed in this article.


Themain function of S ervice is to map pod corresponding port to host (proxy), or load balance, and to publish internal IP as external IP

One. Service Agent

1. Create the RC pod first (using the script from the previous section)

Vim Rc.json

{"Apiversion":  "v1", "kind":  "Replicationcontroller", "metadata": {         //set the RC metadata      "name":  "NGINX-RC"}, "Spec": {             //Setting RC Specifications      "Replicas":  2,     //set the specific number of pods      "selector": {         //by selector to match the corresponding Pod's label         "name":  " MyService "    },    " template ":  {    // Set up the pod template          "metadata": {              "Labels": {                  "name": "MyService"         }     },         "Spec": {                  "Containers": [{                         "Name":   "Nginx",                         "image":  "Nginx",                 "Imagepullpolicy":  "Ifnotpresent",     //image pull policy, divided into always,never,ifnotpresent, default is always                         "Ports": [{                           &nBSP; " Containerport ": 80           }]             }]           }    }}}

Kubectl create-f Rc.json

Kubectl Get pod

2. Create a service

①cat Myservice.yaml

ApiVersion:v1kind:Servicemetadata:name:myservice #设定Service名称, must be the only Spec:selector:name:myservice # Label ports for the pod to match: #设置端口转发规则-port:80 targetport:80 protocol:tcp

②.kubectl create-f Myservice.yaml

Service "MyService" created

③.kubectl Get Service

Existing two service, the first is the system automatically assigned service, the following one can see our own created service, access to the cluster's IP is 10.254.204.76, open port is 80

④.kubectl Describe service

Endpoints is the IP address of the container that connects the pod to the back end, because the pod is successfully connected through the label, and now the access Clusterip will be forwarded to the back-end pod

3. Proxy other Backend

① Agent MySQL Server

Vim Mysql-service.yaml

ApiVersion:v1kind:Servicemetadata:name:mysqlspec:selector:name:mysql ports:-port:3306 targetport:3306 Protocol:tcp

② Setting the backend IP (indentation must be correct, otherwise it will be an error)

ApiVersion:v1kind:Endpointsmetadata:name:mysqlsubsets:-Addresses:-ip:192.168.66.109 #节点ip ports: -port:3306

③ Creating service and endpoints

Kubectl create-f mysql-endpoints.yaml-f Mysql-service.yaml

④ View Endpoints

Kubectl describe EP

Two. Automatic Updates

1. Reduce the number of pods to one

Kubectl scale Replicationcontroller--replicas=1 NGINX-RC

Replicationcontroller "NGINX-RC" scaled

Kubectl Get RC

2. View Service Update Status

Kubectl Describe service

Three. Publish Service

Because proxy ip10.254.204.76 cannot be accessed externally, Web services need to be exposed to the external network, which requires a layer of forwarding mechanism

1.NodePort Service

① Creating Nodeport Service

Vim Nodeport-service.yaml

Apiversion:v1kind:servicemetadata:name:my-nginxspec:selector:app:nginx ports:-Name:http port:80 TA rgetport:80 protocol:tcp Type:nodeport

Kubectl create-f Nodeport-service.yaml

② Query Nodeport Service

Kubectl Describe service My-nginx

K8s created a nodeport with a range of 3000-32767, where you can access the Web service via 30664 ports, in the form of nodeip:nodeport, and if the NODEIP is an extranet IP, the traffic is distributed to the backend server

Two. LoadBalancer and external IP designation

1. You can specify an external IP that is not maintained by k8s on k8s, and you can distribute the request to the back-end server by using this IP directly on external access

① Creating a Service

Vim Loadbalancer.yaml

Apiversion:v1kind:servicemetadata:name:my-nginxspec:selector:app:nginx externalips: ["121.100.110.88"] # Specify an external IP ports:-name:http port:80 targetport:80 protocol:tcp type:loadbalancer #类型负载均衡

Kubectl create-f Loadbalancer.yaml

② Query LoadBalancer Service

Kubectl Describe service My-nginx

At this point the external access to this cluster is, http://121.100.110.88:31119

In fact, service content far more than these, these are some of the more commonly used features, if used in peacetime can refer to, the next section explains the storage volume

Service details of Kubernetes core concept

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.