k8s service objects k8s service objects

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:

Copy the code
[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
Copy the code

Yaml file execution

[root@master ~]# kubectl create -f test.yaml 
service/serivce-mynginx created
deployment.apps/deploy created

View pod and service status

Copy the code
[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
Copy the code

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:
  1. ClusterIP kubernetes service only acting on the object by kubernetes management and distribution ip address
  2. ClusterIP not be Ping, because there is no one entity to respond to network objects
  3. 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
  4. 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:

Copy the code
[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
Copy the code

执行yaml文件

[root@master ~]# kubectl create -f test.yaml 
service/serivce-mynginx created
deployment.apps/deploy created

查看pod和service状态

Copy the code
[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
Copy the code

验证

 

 

外部访问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:
  1. ClusterIP kubernetes service only acting on the object by kubernetes management and distribution ip address
  2. ClusterIP not be Ping, because there is no one entity to respond to network objects
  3. 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
  4. In kubernets cluster communication between NodeIP, PodIP and ClusterIP network, it uses a special routing rule a programmatic way of kubernets own design

Guess you like

Origin www.cnblogs.com/it-peng/p/11584448.html