Create A Cluster

Kubernetes is a production-grade, open-source platform that orchestrates the placement (scheduling) and execution of application containers within and across computer clusters.
1
ControlPlane负责管理集群。
Node是一个VM或者物理计算机。每个节点都有一个Kubelet,它是用来管理计算节点和计算节点与控制平面通信的代理。节点还应该有处理容器操作的工具,如containerd或Docker。处理生产流量的Kubernetes集群应该至少有三个节点。
当你在Kubernetes上部署应用时,告诉控制平面开启应用容器。控制平面调度容器在集群的计算节点上运行。计算节点和控制平面通过控制平面开放的KubernetesAPI进行通信。用户也可以直接使用KubernetesAPI与集群进行交互。
Kubernetes集群可以被部署在物理机或者虚拟机上。刚开始Kubernetes部署时可以使用Minikube。Minikube是一个轻量级Kubernetes的实现,在你的本地机器上创建一个虚拟机并且部署一个只包含一个计算节点的简化集群。Minkube CLI 提供了使用集群进行工作的基础引导操作,包含start,stop,status,delete。在线指南将提供一个预装Minikube的在线终端。

Use Minikube

$ minikube version
minikube version: v1.18.0
commit: ec61815d60f66a6e4f6353030a40b12362557caa-dirty
$ minikube start
* minikube v1.18.0 on Ubuntu 18.04 (kvm/amd64)
* Using the none driver based on existing profile

X The requested memory allocation of 2200MiB does not leave room for system overhead (total system memory: 2460MiB). You may face stability issues.
* Suggestion: Start minikube with less memory allocated: 'minikube start --memory=2200mb'

* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=2, Memory=2460MB, Disk=194868MB) ...
* OS release is Ubuntu 18.04.5 LTS
* Preparing Kubernetes v1.20.2 on Docker 19.03.13 ...
  - kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
  - Generating certificates and keys ...
  - Booting up control plane ...
  - Configuring RBAC rules ...
* Configuring local host environment ...
* Verifying Kubernetes components...
  - Using image gcr.io/k8s-minikube/storage-provisioner:v4
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
$ kubectl cluster-info
Kubernetes control plane is running at https://172.17.0.32:8443
KubeDNS is running at https://172.17.0.32:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
$ kubectl get nodes
NAME       STATUS   ROLES                  AGE    VERSION
minikube   Ready    control-plane,master   8m9s   v1.20.2

Deploy An APP

Using kubectl to Create a Deployment

一旦你有一个启动的kubernetes集群,就可以在上面部署容器化应用程序。首先创建一个kubernetes Deployment配置文件。Deployment配置文件限制kubernetes怎样创建和更新应用实例。一旦创建了Deplyment,Kubernetes控制平面将调度包含在Deployment中的应用实例在集群的节点上运行。
一旦应用实例被创建,Kubernets Deployment Controller将持续监控这些实例。如果运行一个实例的节点挂掉或者被删除,Deployment Controller将会在新的节点上启动这个实例来替代。这为机器挂掉和维持提供了自我修复机制。

Deploy your first app on Kubernetes

2
你可以使用kubectl来创建和管理Deployment。Kubectl使用Kubernetes API 和集群进行交互。
当你创建一个Deployment,你需要指定应用的容器镜像和想要运行的副本的数量。可以通过更新Deployment来修改这些信息。
Pod是Kubenetes应用的基本运行单元。每个Pod代表了运行在集群上的一部分工作负载。

$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
$ kubectl proxy #Terminal 1
$ curl http://localhost:8001/version #Terminal 2
{
  "major": "1",
  "minor": "20",
  "gitVersion": "v1.20.2",
  "gitCommit": "faecb196815e248d3ecfb03c680a4507229c2a56",
  "gitTreeState": "clean",
  "buildDate": "2021-01-13T13:20:00Z",
  "goVersion": "go1.15.5",
  "compiler": "gc",
  "platform": "linux/amd64"
}$ 
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/
<a href="/api/v1/namespaces/default/pods/">Moved Permanently</a>.

$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .it"\n"}}{{end}}')a.name}}{{"
$  echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-57978f5f5d-hwt2h
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "kubernetes-bootcamp-57978f5f5d-hwt2h",
    "generateName": "kubernetes-bootcamp-57978f5f5d-",
    "namespace": "default",
    "uid": "37b01e92-9be3-44be-bb12-a2e1874fb37a",
    "resourceVersion": "542",
    "creationTimestamp": "2021-10-13T03:20:25Z",
    "labels": {
      "app": "kubernetes-bootcamp",
      "pod-template-hash": "57978f5f5d"
    },
    "ownerReferences": [
      {
        "apiVersion": "apps/v1",
        "kind": "ReplicaSet",
        "name": "kubernetes-bootcamp-57978f5f5d",
        "uid": "b25a5a9d-a25b-4629-ae7c-c3ff757ca9ce",
        "controller": true,
        "blockOwnerDeletion": true
      }
    ],
    "managedFields": [
      {
        "manager": "kube-controller-manager",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2021-10-13T03:20:25Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {"f:metadata":{"f:generateName":{},"f:labels":{".":{},"f:app":{},"f:pod-template-hash":{}},"f:ownerReferences":{".":{},"k:{\"uid\":\"b25a5a9d-a25b-4629-ae7c-c3ff757ca9ce\"}":{".":{},"f:apiVersion":{},"f:blockOwnerDeletion":{},"f:controller":{},"f:kind":{},"f:name":{},"f:uid":{}}}},"f:spec":{"f:containers":{"k:{\"name\":\"kubernetes-bootcamp\"}":{".":{},"f:image":{},"f:imagePullPolicy":{},"f:name":{},"f:resources":{},"f:terminationMessagePath":{},"f:terminationMessagePolicy":{}}},"f:dnsPolicy":{},"f:enableServiceLinks":{},"f:restartPolicy":{},"f:schedulerName":{},"f:securityContext":{},"f:terminationGracePeriodSeconds":{}}}
      },
      {
        "manager": "kubelet",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2021-10-13T03:20:29Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {"f:status":{"f:conditions":{"k:{\"type\":\"ContainersReady\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Initialized\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Ready\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}}},"f:containerStatuses":{},"f:hostIP":{},"f:phase":{},"f:podIP":{},"f:podIPs":{".":{},"k:{\"ip\":\"172.18.0.4\"}":{".":{},"f:ip":{}}},"f:startTime":{}}}
      }
    ]
  },
  "spec": {
    "volumes": [
      {
        "name": "default-token-5zwzm",
        "secret": {
          "secretName": "default-token-5zwzm",
          "defaultMode": 420
        }
      }
    ],
    "containers": [
      {
        "name": "kubernetes-bootcamp",
        "image": "gcr.io/google-samples/kubernetes-bootcamp:v1",
        "resources": {
          
        },
        "volumeMounts": [
          {
            "name": "default-token-5zwzm",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "IfNotPresent"
      }
    ],
    "restartPolicy": "Always",
    "terminationGracePeriodSeconds": 30,
    "dnsPolicy": "ClusterFirst",
    "serviceAccountName": "default",
    "serviceAccount": "default",
    "nodeName": "minikube",
    "securityContext": {
      
    },
    "schedulerName": "default-scheduler",
    "tolerations": [
      {
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      },
      {
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      }
    ],
    "priority": 0,
    "enableServiceLinks": true,
    "preemptionPolicy": "PreemptLowerPriority"
  },
  "status": {
    "phase": "Running",
    "conditions": [
      {
        "type": "Initialized",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2021-10-13T03:20:25Z"
      },
      {
        "type": "Ready",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2021-10-13T03:20:29Z"
      },
      {
        "type": "ContainersReady",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2021-10-13T03:20:29Z"
      },
      {
        "type": "PodScheduled",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2021-10-13T03:20:25Z"
      }
    ],
    "hostIP": "172.17.0.34",
    "podIP": "172.18.0.4",
    "podIPs": [
      {
        "ip": "172.18.0.4"
      }
    ],
    "startTime": "2021-10-13T03:20:25Z",
    "containerStatuses": [
      {
        "name": "kubernetes-bootcamp",
        "state": {
          "running": {
            "startedAt": "2021-10-13T03:20:28Z"
          }
        },
        "lastState": {
          
        },
        "ready": true,
        "restartCount": 0,
        "image": "jocatalin/kubernetes-bootcamp:v1",
        "imageID": "docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af",
        "containerID": "docker://feac327a3e14b734ecd123f01f7cced9724b543428a419a5b70edf5d01bee956",
        "started": true
      }
    ],
    "qosClass": "BestEffort"
  }
}

Q.E.D.


南七技校练习时长四年的练习生