k8s service objects
Outline
Service service is one of the core words in Kubernetes objects, Kubernetes in each of the service actually is, we often mention the micro-service architecture in a micro service, before explaining Pod, RC and other resource objects are actually laying the groundwork to explain Kubernetes Service , the logic below shows the Pod, RC and Service,
You can see above Chart, service by service tag selector positioned back-end pod, provided that the service on the back-end of the selector must correspond Pod label to find the corresponding Pod, while the preceding frontend can be accessed through the service-to-back serving pod, while the default IP service type is divided into:
- ClusterIP: mainly for internal cluster provides access to services
- NodePort: outside the cluster can be accessed, the access mode for the host: port number
Here I created a nginx service and a service to provide services, as follows:
[root@master ~]# cat nginx.yaml apiVersion: v1 kind: Service metadata: name: serivce-mynginx namespace: default spec: type: NodePort selector: app: mynginx ports: - name: nginx port: 80 targetPort: 80 nodePort: 30080 --- apiVersion: apps/v1 kind: Deployment metadata: name: deploy namespace: default spec: replicas: 2 selector: matchLabels: app: mynginx template: metadata: labels: app: mynginx spec: containers: - name: nginx image: lizhaoqwe/nginx:v1 ports: - name: nginx containerPort: 80
Yaml file execution
[root@master ~]# kubectl create -f test.yaml service/serivce-mynginx created deployment.apps/deploy created
View pod and service status
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE deploy-696bccb9fd-9zk2f 1/1 Running 0 138m deploy-696bccb9fd-vcgs5 1/1 Running 0 138m [root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d23h serivce-mynginx NodePort 10.103.92.182 <none> 80:30080/TCP 138m
verification
External access service issues
For a more profound understanding kubernetes, we need to understand kubernetes in the IP 3
- NodeIP: NodeIP is kubernetes physical network adapter for each node IP address, the physical network is a real, all servers belong to this network can communicate directly through the network, including the host access within the host out of the cluster also need NodeIP
- PodIP: PodIP each Pod address, which is assigned in accordance with docker engine IP address docker0 bridge, usually a virtual Layer 2 network, kubernetes in the Pod in a container to access additional containers in a Pod is to communicate through a virtual Layer 2 network PodIP located, and true TCP / IP traffic through the physical network adapter where the outflow of NodeIP
- ClusterIP: He is a virtual IP, but more like a "fake" the IP network, for the following reasons:
- ClusterIP kubernetes service only acting on the object by kubernetes management and distribution ip address
- ClusterIP not be Ping, because there is no one entity to respond to network objects
- ClusterIP service Port binding only make up a specific communications port, if you want to access outside the cluster need to do some extra work
- In kubernets cluster communication between NodeIP, PodIP and ClusterIP network, it uses a special routing rule a programmatic way of kubernets own design
Outline
Service service is one of the core words in Kubernetes objects, Kubernetes in each of the service actually is, we often mention the micro-service architecture in a micro service, before explaining Pod, RC and other resource objects are actually laying the groundwork to explain Kubernetes Service , the logic below shows the Pod, RC and Service,
You can see above Chart, service by service tag selector positioned back-end pod, provided that the service on the back-end of the selector must correspond Pod label to find the corresponding Pod, while the preceding frontend can be accessed through the service-to-back serving pod, while the default IP service type is divided into:
- ClusterIP: mainly for internal cluster provides access to services
- NodePort: outside the cluster can be accessed, the access mode for the host: port number
Here I created a nginx service and a service to provide services, as follows:
[root@master ~]# cat nginx.yaml apiVersion: v1 kind: Service metadata: name: serivce-mynginx namespace: default spec: type: NodePort selector: app: mynginx ports: - name: nginx port: 80 targetPort: 80 nodePort: 30080 --- apiVersion: apps/v1 kind: Deployment metadata: name: deploy namespace: default spec: replicas: 2 selector: matchLabels: app: mynginx template: metadata: labels: app: mynginx spec: containers: - name: nginx image: lizhaoqwe/nginx:v1 ports: - name: nginx containerPort: 80
执行yaml文件
[root@master ~]# kubectl create -f test.yaml service/serivce-mynginx created deployment.apps/deploy created
查看pod和service状态
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE deploy-696bccb9fd-9zk2f 1/1 Running 0 138m deploy-696bccb9fd-vcgs5 1/1 Running 0 138m [root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d23h serivce-mynginx NodePort 10.103.92.182 <none> 80:30080/TCP 138m
验证
外部访问service的问题
为了更加深刻理解kubernetes,我们需要弄明白kubernetes里的3中IP
- NodeIP:NodeIP是kubernetes中每个节点的物理网卡IP地址,是一个真实存在的物理网络,所有属于这个网络的服务器都能通过这个网络直接通讯,包括集群外的主机访问集群内的主机也需要NodeIP
- PodIP: PodIP each Pod address, which is assigned in accordance with docker engine IP address docker0 bridge, usually a virtual Layer 2 network, kubernetes in the Pod in a container to access additional containers in a Pod is to communicate through a virtual Layer 2 network PodIP located, and true TCP / IP traffic through the physical network adapter where the outflow of NodeIP
- ClusterIP: He is a virtual IP, but more like a "fake" the IP network, for the following reasons:
- ClusterIP kubernetes service only acting on the object by kubernetes management and distribution ip address
- ClusterIP not be Ping, because there is no one entity to respond to network objects
- ClusterIP service Port binding only make up a specific communications port, if you want to access outside the cluster need to do some extra work
- In kubernets cluster communication between NodeIP, PodIP and ClusterIP network, it uses a special routing rule a programmatic way of kubernets own design