In order to facilitate everyone to learn Kubernetes system, I organized a Kubernetes learning series of articles, covering the basic knowledge of Kubernetes, installation steps and the related content of the whole Kubernetes system, I believe we read this series, To have a deeper understanding of Kubernetes.

The target

  • Understand Kubernetes Pod
  • Understand Kubernetes Node
  • Learn how to debug deployment problems
  • Know how to expose an application through Service

Kubernetes Pods

Creating a Deployment Deployment in Kubernetes creates a Pod on Node, which is a collection of containers and container-related resources in Kubernetes. Containers in pods share IP and port resources.

A Pod is a group of one or more application containers (such as Docker or rkt) and includes shared storage (volumes), IP address and information about how to run them.

Kubernetes Nodes

A Pod always runs on a Node, which can be either a physical machine or a virtual machine. In the previousKubernetes foundation articleA Node contains components such as Kubelet, container environment, and Kube-Proxy. The structure is shown in the figure below:

Kubectl – Kubernetes CLI management tool

Kubectl is a command line management tool for Kubernetes. Here are some features for obtaining information. When kubectl is running, it will look up resources in the default namespace. If we need to look up resources in our own namespace, we need to specify –namespace= XXX.

kubectl get

Get namespace

$ kubectl get namespaces
NAME          STATUS    AGE
default       Active    2m
kube-public   Active    2m
kube-system   Active    2m
Copy the code

To obtain the Pods

$ kubectl get pods
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-5dbf48f7d4-4k8zz   1/1       Running   0          3m
Copy the code

Get Pods details

$ kubectl describe pods
Name:           kubernetes-bootcamp-5dbf48f7d4-4k8zz
Namespace:      default
Node:           host01/172.17.0.45
Start Time:     Tue, 17 Jul 2018 03:02:04 +0000
Labels:         pod-template-hash=1869049380
                run=kubernetes-bootcamp
Copy the code

kubectl log

See the log

$ kubectl logs $POD_NAMEKubernetes Bootcamp App Started At: The 2018-07-17 T03:02:05. 794 z | Running On: kubernetes dbf48f7d4 bootcamp - 5-4 k8zz Running On: Kubernetes dbf48f7d4 bootcamp - 5-4 k8zz | Total Requests: 1 | App Uptime: 527.706 seconds | Log Time: The 2018-07-17 T03:10:53. 500 zCopy the code

kubectl exec

Execute commands in Pod

$ kubectl exec $POD_NAME env
PATH=/usr/local/sbin:/usr/local/ bin: / usr/sbin, / usr/bin, / sbin, / bin HOSTNAME dbf48f7d4 = kubernetes - bootcamp - 5-4 k8zz KUBERNETES_PORT = TCP: / / 10.96.0.1:443 KUBERNETES_PORT_443_TCP = TCP: / / 10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO = TCP KUBERNETES_PORT_443_TCP_PORT = 443 KUBERNETES_PORT_443_TCP_ADDR = 10.96.0.1 KUBERNETES_SERVICE_HOST = 10.96.0.1 KUBERNETES_SERVICE_PORT = 443 443 NPM_CONFIG_LOGLEVEL KUBERNETES_SERVICE_PORT_HTTPS = = info NODE_VERSION = 6.3.1 HOME = / rootCopy the code

Start bash in the container

$ kubectl exec -ti $POD_NAME bash
root@kubernetes-bootcamp-5dbf48f7d4-4k8zz:/# cat server.js
Copy the code

Kubernetes Service

In a K8S cluster, a Pod has a separate IP and a separate life cycle. When a Node fails, ReplicationController migrates pods on that Node to other nodes in the cluster. If there are multiple PODS and the same Service is provided for the front-end application, the front-end does not care which Pod is invoked in the background. In this case, Service is used.

A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them.

A Service in Kubernetes is an abstraction of a set of pods and access policies in a cluster. You can define them with YAML, JSON, and target Pods are usually defined with LabelSelector. Through the Type field, the service defines several ways in which the application is exposed:

  • ClusterIP: by default, services are provided externally using the ClusterIP address. This mode can only be accessed within a cluster.
  • NodePort: Provides external services on a specified port of a Node using the NAT technology. External applications are accessed in *:* mode.
  • LoadBalancer: accesses services using external load balancing facilities.
  • ExternalName, which is provided by Kube-DNS since version 1.7.

The service provides the ability to distribute traffic between a set of Pods, and it is the service abstraction layer that allows Kubernetes to scale without affecting the application. Typically, services identify operable objects through labels and selectors. Label can be specified at object creation time or modified at run time.



Viewing Service Status

$kubectl get services NAME TYPE cluster-ip external-ip PORT(S) AGE Kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 44sCopy the code

Exposure to external services

$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service "kubernetes-bootcamp"Exposed $kubectl get servicesNAME TYPE cluster-ip external-ip PORT(S) AGE Kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 2m kubernetes-bootcamp NodePort 10.99.175.225 < None > 8080:32172/TCP 5sCopy the code

View service details

$ kubectl describe service/kubernetes-bootcamp Name: kubernetes-bootcamp Namespace: default Labels: Annotations: Run =kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.99.175.225 Port: <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset> 32172/TCP Endpoints: 172.18.0.2:8080 Session Affinity: None External Traffic Policy: Cluster Events: < None >Copy the code

The Node in this example has no external IP, so it is empty. Leverage internal IP testing.

$curl 172.17.0.11:32172 Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-hmc69 | v=1Copy the code

Query Pod and Service by label

$ kubectl get pods -l run=kubernetes-bootcamp NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-5c69669756-hmc69 1/1 Running 0 8m $ kubectl get services -l = run=kubernetes-bootcamp error: name cannot be provided when a selector is specified $ kubectl get services -l run=kubernetes-bootcamp NAME TYPE Cluster-ip external-ip PORT(S) AGE kubernetes-bootcamp NodePort 10.99.175.225 < None > 8080/32172 /TCP 6mCopy the code

The new labels

$ kubectl label pod $POD_NAME app=v1
pod "kubernetes-bootcamp-5c69669756-hmc69" labeled
$ kubectl describe pods $POD_NAMEName: kubernetes-bootcamp-5C69669756 - hMC69 Namespace: default Node: minikube/172.17.0.11 Start Time: Tue, 17 Jul 2018 05:20:35 +0000 Labels: app=v1 pod-template-hash=1725225312 run=kubernetes-bootcamp $ kubectl get pods -l app=v1 NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-5c69669756-hmc69 1/1 Running 0 11mCopy the code

Remove the service

$kubectl get services NAME TYPE cluster-ip external-ip PORT(S) AGE Kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 12m Kubernetes-bootcamp NodePort 10.99.175.225 < None > 8080:32172/TCP 10m $kubectl delete service -l run=kubernetes-bootcamp  service"kubernetes-bootcamp"Deleted $kubectl get services NAME TYPE cluster-ip external-ip PORT(S) AGE Kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 12mCopy the code

References:

  1. Viewing Pods and Nodes
  2. Kubernetes basis
  3. Using a Service to Expose Your App