1. What is Kubernetes

Kubernetes is an open source container scheduling platform across host clusters that automates the deployment, expansion, and operation of application containers and provides a container-centric infrastructure. (First line of official document)

1.1 Kubernetes for microservices

Each microservice is a separate process that calls each other through a defined restful API (AMQP)

1.1.1 Common Problems of microservices

  • Confusion caused by different service dependency libraries, need to open each service independently (improved by Docker)
  • Service registration, service discovery: The current service needs to be updated dynamically.
  • Service choreography (that is, the orchestration of Docker containers, which is difficult for Docker itself to solve the problem of cluster deployment) : which services need to be started on which hosts

1.2 Once dominant container choreography tools

  • Docker Swarm
  • Mesos + marathon
  • Kubernetes

2. Main components of Kubernetes

  • Master can simply be understood as a control center
    • Etcd: distributed K-V database, choose CP or AP according to the configuration, K8S only API Server and ETCD communication, other components and API server communication
    • API Server: Can be thought of as a prefilter for ETCD, another perspective, it and ETCD are similar to mysql and file systems.
    • Controller Manager: the core, responsible for adjusting the current state to the state that should be on etCD, contains all the implementation logic
    • Scheduler: A node is used to scheduler a pod.
  • Slave can be simply understood as worker
    • Kubelet: connect to master, register node, listen-watch local node tasks, etc
    • Kube-proxy: used for k8S service objects, described later.
    • Container runtime: In addition to Docker K8S, container implementations such as RKT are supported
  • Add-on Some functions not provided by K8S require plug-in implementation
    • DNS (Service Registration discovery)
    • CNI (Container Network Interface Implementation, EX: Fannel)

The rough run-time structure of a K8S cluster

Multiple containers run together is pod

3.Kubernetes common objects

1.pod

  • Pod is the smallest object managed by K8S. It is a group of containers that share UTS, network and IPC namespace (shared PID is also supported, disabled by default).
  • Each POD will have an infrastructure container, volumne, network are actually shared network and Volumne of this container
  • Pods enable logically closely related processes to be properly isolated to maintain some degree of correlation:
    • Some processes must be running on the same host
    • Capacity expansion must be consistent
    • , etc.

Instance profile

[lukou@khand ~] $ kubectl get po redis-master-jvxld -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/created-by: | {"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicationController","namespace":"default","name" :"redis-master","uid":"61a3a1c6-43a9-11e9-9f58-00163f007932","apiVersion":"v1","resourceVersion":"510225"}}  creationTimestamp: 2019- 03- 11T02:57:17Z
  generateName: redis-master-
  labels:
    name: redis-master
  name: redis-master-jvxld
  namespace: default
  ownerReferences:
  - apiVersion: v1
    controller: true
    kind: ReplicationController
    name: redis-master
    uid: 61a3a1c6-43a9-11e9-9f58-00163f007932
  resourceVersion: "510247"
  selfLink: /api/v1/namespaces/default/pods/redis-master-jvxld
  uid: 61a49472-43a9-11e9-9f58-00163f007932
spec:
  containers:
  - image: kubeguide/redis-master
    imagePullPolicy: Always
    name: master
    ports:
    - containerPort: 6379
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-m6g3l
      readOnly: true
    livenessProbe:
      httpGet:
        path: /
        port: 8080
  dnsPolicy: ClusterFirst
  nodeName: 127.0. 01.
  restartPolicy: Always
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  volumes:
  - name: default-token-m6g3l
    secret:
      defaultMode: 420
      secretName: default-token-m6g3l
Copy the code

2. Controllers such as RC and RS

  • Ensure POD health
  • Determine and dynamically adjust the number of pods running in the cluster, and reschedule new pods to start if pods fail
  • Similar to crontab, it performs scheduled tasks

In addition to RC and RS, there are also controllers such as deamonSet, job and cronjob

Instance profile

apiVersion: v1
kind: ReplicationController
metadata:
  name: kubia
spec:
  replicas: 3
  selector:
    app: kubia
template:
  metadata:
    labels:
      app: kubia
  spec:
    containers:
    - name: kubia
      image: luksa/kubia
      ports:
        - containerPort: 8080
Copy the code

Rc manages Pods, but pods are not started by RC. Rc synchronizes pods configuration files to API Server, Scheduler allocates nodes, and Kubelet running nodes.

3.service

  • Pod is not durable, it will be restarted constantly, resulting in pod IP is changing at any time, and the number of POD will be dynamic change, it is difficult for the client to communicate with pod directly, service is used to solve this problem
  • Service provides a unified entry point for pods that provide the same service
  • The binding IP of a service does not change during its lifetime

Instance profile

apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels: 
    name: redis-master
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    name: redis-master
Copy the code
[lukou@khand ~]$kubectl get SVC NAME cluster-ip external-ip PORT(S) AGE kubernetes 10.254.0.1 < None > 443/TCP 26d Redis-master 10.254.61.141 < None > 6379/TCP 21DCopy the code

You can query the IP address and port of a service using environment variables or DNS

  • The new POD stores the IP and port of all created services into environment variables
[lukou@khand ~]$ kubectl get pods
NAME                 READY     STATUS    RESTARTS   AGE
redis-master-jvxld   1/1       Running   0          21d
[lukou@khand ~]$ kubectl exec redis-master-jvxld env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=redis-master-jvxld KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT = TCP: / / 10.254.0.1:443 KUBERNETES_PORT_443_TCP = TCP: / / 10.254.0.1:443 KUBERNETES_PORT_443_TCP_PROTO = TCP KUBERNETES_PORT_443_TCP_PORT = 443 KUBERNETES_PORT_443_TCP_ADDR = 10.254.0.1 KUBERNETES_SERVICE_HOST = 10.254.0.1 KUBERNETES_SERVICE_PORT=443 HOME=/root [lukou@khand ~]$ kubectl delete po --all pod"redis-master-jvxld"deleted [lukou@khand ~]$ kubectl get pods NAME READY STATUS RESTARTS AGE redis-master-b7mkh 0/1 ContainerCreating 0 6s [lukou@khand ~]$ ^C [lukou@khand ~]$ kubectl get pods NAME READY STATUS RESTARTS AGE redis-master-b7mkh 1/1 Running 0 1m  [lukou@khand ~]$ kubectlexec redis-master-b7mkh env
PATH=/usr/local/sbin:/usr/local/ bin: / usr/sbin, / usr/bin, / sbin, / bin HOSTNAME = redis - master - b7mkh KUBERNETES_PORT = TCP: / / 10.254.0.1:443 KUBERNETES_PORT_443_TCP = TCP: / / 10.254.0.1:443 KUBERNETES_PORT_443_TCP_ADDR = 10.254.0.1 REDIS_MASTER_SERVICE_PORT = 6379 KUBERNETES_SERVICE_PORT = 443 6379 KUBERNETES_SERVICE_HOST REDIS_MASTER_PORT_6379_TCP_PORT = = 10.254.0.1 REDIS_MASTER_SERVICE_HOST = 10.254.61.141 REDIS_MASTER_PORT = TCP: / / 10.254.61.141:6379 REDIS_MASTER_PORT_6379_TCP = TCP: / / 10.254.61.141:6379 KUBERNETES_PORT_443_TCP_PROTO = TCP KUBERNETES_SERVICE_PORT_HTTPS = 443 KUBERNETES_PORT_443_TCP_PORT = 443 REDIS_MASTER_PORT_6379_TCP_PROTO = TCP REDIS_MASTER_PORT_6379_TCP_ADDR = 10.254.61.141 HOME=/rootCopy the code
  • Register the cluster domain name through the DNS service, similarredis-master.default

The service created previously had only a fixed IP within the cluster

[lukou@khand ~]$kubectl get SVC NAME cluster-ip external-ip PORT(S) AGE kubernetes 10.254.0.1 < None > 443/TCP 26d Redis-master 10.254.61.141 < None > 6379/TCP 21DCopy the code

If you need access outside the cluster, there are three ways

  • The default service type is ‘ClusterIP’. Change it to NodePort
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels: 
    name: redis-master
spec:
  type: NodePort
  ports:
    - port: 6379
      targetPort: 6379
      nodePort: 30123  // The range of valid ports is 30000-32767
  selector:
    name: redis-master
Copy the code
[lukou@khand ~]$kubectl get SVC NAME cluster-ip external-ip PORT(S) AGE kubernetes 10.254.0.1 < None > 443/TCP 26d Redis-master 10.254.61.141 < Nodes > 6379:30123/TCP 21DCopy the code
  • Create a loadBalance
  • Create Ingress resource

4.deployment

  • In short, Deployment is one layer above RC and is used to manage RC
  • The main function is to manage the version of the POD running
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: kubia
spec:
  replicas: 3
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
      - image: luksa/kubia:v1
        name: nodejs
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
Copy the code

CMD kubectl set image deployment kubia nodejs=luksa/kubia:v2

The approximate implementation logic is that deployment continuously reduces the replicas of the old version of RC and increases the replicas of the new version

Just as RC does not directly control pod, deployment is similar in that deployment declaratively notifies the API Server of the results of the upgrade, the logic of which is handled by the Development Controller