Differences between revisions 1 and 2
Revision 1 as of 2018-10-29 18:38:40
Size: 11166
Editor: twotwo
Comment:
Revision 2 as of 2019-08-10 11:25:14
Size: 13860
Editor: twotwo
Comment: 添加学习资料
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
=== Features ===
 * 基于容器的应用部署、维护和滚动升级;
 * 负载均衡和服务发现;
 * 跨机器和跨地区的集群调度;
 * 自动伸缩;
 * 无状态服务和有状态服务;
 * 广泛的 Volume 支持;
 * 插件机制保证扩展性。
Line 25: Line 33:

== Learning ==
 * [[https://kubernetes.io/docs/home/||target="_blank"]] Kubernetes Documentation
 * [[https://kubernetes.io/docs/tutorials/kubernetes-basics/||target="_blank"]] Learn Kubernetes Basics
 * [[https://jimmysong.io/kubernetes-handbook/||target="_blank"]] Kubernetes Handbook //Kubernetes中文指南/云原生应用架构实践手册
 * [[https://kubernetes.feisky.xyz/||target="_blank"]] Kubernetes 指南 // 运维参考手册,局域网内打不开

=== 论文 ===
[[attachment:Borg-Omega-and-Kubernetes.pdf]] Google 这十几年来从这三个容器管理系统中得到的经验教训

=== 交互式教程 ===
 * [[https://www.katacoda.com/courses/kubernetes||target="_blank"]] Learn Kubernetes using Interactive Browser-Based Scenarios
 * [[https://kubernetesbootcamp.github.io/kubernetes-bootcamp/||target="_blank"]] Bootcamp Overview

=== Kubernetes 101 ===
 * [[https://www.ibm.com/cloud/garage/content/course/kubernetes-101/||target="_blank"]] IBM Kubernetes 101 Course
 * [[https://medium.com/google-cloud/kubernetes-101-pods-nodes-containers-and-clusters-c1509e409e16||target="_blank"]] Pods, Nodes, Containers, and Clusters
 * [[http://www.dasblinkenlichten.com/kubernetes-101-networking/||target="_blank"]] Kubernetes 101 – Networking
 * [[http://www.dasblinkenlichten.com/kubernetes-101-the-build/||target="_blank"]] Kubernetes 101 – Build
 * [[https://www.dasblinkenlichten.com/kubernetes-networking-101-pods/||target="_blank"]] Kubernetes 101 – Pods
 * [[https://www.dasblinkenlichten.com/kubernetes-101-the-constructs/||target="_blank"]] Kubernetes 101 – Constructs
 * [[https://www.dasblinkenlichten.com/kubernetes-networking-101-services/||target="_blank"]] Kubernetes 101 – Services
 * [[http://www.dasblinkenlichten.com/kubernetes-networking-101-ingress-resources/||target="_blank"]] Kubernetes 101 – Ingress Resources

=== CI/CD ===
 * [[https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes||target="_blank"]] Get up and running with Jenkins on Google Kubernetes Engine
 * [[https://cloud.google.com/solutions/automated-build-images-with-jenkins-kubernetes#kubernetes_architecture||target="_blank"]] Automated image builds with Jenkins, Packer, and Kubernetes
 * [[http://iocanel.com/2015/09/jenkins-setups-for-kubernetes-and-docker-workflow/||target="_blank"]] Jenkins setups for Kubernetes and Docker Workflow

Back to 微服务

See Also IstioDocker

Kubernetes

1. What is Kubernetes?

Kubernetes是一个可移植的、可扩展的开源平台,用于管理容器化的工作负载和服务,它促进了声明性配置和自动化。它有一个巨大的、快速增长的生态系统。

谷歌在2014年开源了Kubernetes项目,以谷歌在大规模运行生产工作负载方面的15年经验为基础,结合社区的最佳想法和实践。目标是构建一个软件和工具的生态系统,以减轻用户在公共云或私有云运行应用程序的负担。

1.1. Features

  • 基于容器的应用部署、维护和滚动升级;
  • 负载均衡和服务发现;
  • 跨机器和跨地区的集群调度;
  • 自动伸缩;
  • 无状态服务和有状态服务;
  • 广泛的 Volume 支持;
  • 插件机制保证扩展性。

1.2. How is Kubernetes a platform?

Kubernetes 提供了很多的功能,总会有新的场景受益于新特性。它可以简化应用程序的工作流,加快开发速度。被大家认可的应用编排通常需要有较强的自动化能力。这就是为什么 Kubernetes 被设计作为构建组件和工具的生态系统平台,以便更轻松地部署、扩展和管理应用程序。

Label 允许用户按照自己的方式组织管理对应的资源。 注解 使用户能够以自定义的描述信息来修饰资源,以适用于自己的工作流,并为管理工具提供检查点状态的简单方法。

此外,Kubernetes 控制面 (Control Plane) 是构建在相同的 APIs 上面,开发人员和用户都可以用。用户可以编写自己的控制器, 调度器等等,如果这么做,根据新加的自定义 API ,可以扩展当前的通用 CLI 命令行工具。

这种 设计 使得许多其他系统可以构建在 Kubernetes 之上。

1.3. What Kubernetes is not

Kubernetes 不是一个传统意义上,包罗万象的 PaaS (平台即服务) 系统。我们保留用户选择的自由,这非常重要。

1.4. What does Kubernetes mean? K8s?

名称 Kubernetes 源于希腊语,意为 “舵手” 或 “飞行员”, 且是英文 “governor” 和 “cybernetic”的词根。 K8s 是通过将 8 个字母 “ubernete” 替换为 8 而导出的缩写。另外,在中文里,k8s 的发音与 Kubernetes 的发音比较接近。

2. Learning

2.1. 论文

Borg-Omega-and-Kubernetes.pdf Google 这十几年来从这三个容器管理系统中得到的经验教训

2.2. 交互式教程

2.3. Kubernetes 101

2.4. CI/CD

3. Get Started

3.1. Local-machine Solutions

本地机器解决方案是开始使用Kubernetes的一种简单方法。您可以创建和测试Kubernetes集群,而不用担心消耗云资源和配额。

Minikube 是一种用于创建用于开发和测试的本地单节点Kubernetes集群的方法。安装是完全自动化的,不需要云提供商帐户。 @https://kubernetes.io/docs/setup/minikube/

3.1.1. Install and Set Up kubectl

brew install kubernetes-cli @https://kubernetes.io/docs/tasks/tools/install-kubectl/

➜  ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.0", GitCommit:"0ed33881dc4355495f623c6f22e7dd0b7632b7c0", GitTreeState:"clean", BuildDate:"2018-09-28T15:18:13Z", GoVersion:"go1.11", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.0", GitCommit:"fff5156092b56e6bd60fff75aad4dc9de6b6ef37", GitTreeState:"clean", BuildDate:"2017-03-28T16:24:30Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}
# kubectl configuration file
➜  ~ cat ~/.kube/config
# Check the kubectl configuration
➜  ~ kubectl cluster-info
# Enabling shell autocompletion(zsh)
plugins=(kubectl) in ~/.zshrc

3.1.2. Install Minikube

Need a Hypervisor on macOS: 推荐 VirtualBox

brew cask install minikube @https://kubernetes.io/docs/tasks/tools/install-minikube/

3.1.3. Running Kubernetes Locally via Minikube

https://kubernetes.io/docs/setup/minikube/

➜  ~ minikube start
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Downloading Minikube ISO ## Failed to get image, Fk GFW!
➜  ~ kubectl get pods --context=minikube 

3.1.3.1. Starting minikube behind a proxy

https://github.com/Unknwon/wuwen.org/issues/20

➜  ~ https_proxy=socks5://km.com:1080 minikube start
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
...
Error restarting cluster: restarting kube-proxy: waiting for kube-proxy to ...

安装镜像的时候使用代理,但本地运行的时候禁止代理

3.1.3.2. 使用阿里版的 Minikube

https://www.cnblogs.com/cocowool/p/minikube_setup_and_first_sample.html

➜  ~ curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.30.0/minikube-darwin-amd64 && chmod +x minikube && mv minikube /usr/local/bin/
➜  ~ minikube start --registry-mirror=https://registry.docker-cn.com
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
## 打开 Minikube 控制台
➜  ~ minikube dashboard
## run in minikube
➜  ~ minikube ssh -- pwd  
/home/docker
➜  ~ kubectl get no  
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   7m    v1.10.0

3.1.3.3. Quickstart

➜  ~ minikube start
...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
➜  ~ kubectl run my-nginx --image=nginx --replicas=2 --port=80
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/my-nginx created
➜  ~ kubectl expose deployment my-nginx --type=NodePort
service/my-nginx exposed
➜  ~ kubectl get pod                                          
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-77f56b88c8-5684c   1/1     Running   0          1m
my-nginx-77f56b88c8-zq2gm   1/1     Running   0          1m
➜  ~ curl $(minikube service my-nginx --url) 
➜  ~ kubectl delete services my-nginx
service "my-nginx" deleted
➜  ~ kubectl delete deployment my-nginx
deployment.extensions "my-nginx" deleted
➜  ~ kubectl get pod
NAME                        READY   STATUS        RESTARTS   AGE
my-nginx-77f56b88c8-5684c   0/1     Terminating   0          4m
my-nginx-77f56b88c8-zq2gm   0/1     Terminating   0          4m
➜  ~ kubectl get pod
No resources found.

3.1.3.4. 控制器模式

➜  ~ kubectl apply -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record
deployment.apps/nginx-deployment created
➜  ~ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-75675f5897-ms696   1/1     Running   0          1m
nginx-deployment-75675f5897-p6d5j   1/1     Running   0          1m
nginx-deployment-75675f5897-vvn2p   1/1     Running   0          1m
➜  ~ kubectl get deployment 
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           1m

3.2. Hosted Solutions

3.3. Cloud Solutions

4. Practice

kubectl Cheat Sheet

4.1. Deployment

http://kubernetes.kansea.com/docs/user-guide/kubectl/kubectl_apply/

$ kubectl apply -f service.yaml
$ kubectl get deploy
NAME                     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
ner-deployment           1         1         1            1           14d
std-deployment           1         1         1            1           13d
srv-deployment           1         1         1            1           102d
web-deployment           1         1         1            1           83d

4.2. Service

$ kubectl get service
NAME                       TYPE           CLUSTER-IP       EXTERNAL-IP    PORT(S)          AGE
ner-service                NodePort       10.97.214.124    <none>         8000:31076/TCP   13d
std-service                NodePort       10.105.7.27      <none>         8000:31094/TCP   13d
srv-service                NodePort       10.100.251.111   <none>         8080:32083/TCP   102d
web-service                NodePort       10.109.33.85     <none>         80:32084/TCP     83d
$ kubectl delete service ner-service

4.3. Pod

# list pod with ip&node info
$ kubectl -n dev -o wide get pod

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#cp

$ kubectl -n cp my-xxx-pod:/etc my-file
$ kubectl -n cp my-file my-xxx-pod:/etc

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#describe

$ kubectl -n dev describe pod my-xxx-pod
Containers:
  my-xxx-pod:
    Container ID:   docker://c4f800fceea4331bd39c9f6ec8094907d4fd9d49b02cf365ee298730e2ec9b23
docker exec -u root c4f800fceea4331bd39c9f6ec8094907d4fd9d49b02cf365ee298730e2ec9b23 -- /bin/bash

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#exec

kubectl get pod
kubectl describe pod my-xxx-pod #pod 详细信息
kubectl exec my-xxx-pod -it bash#进入 pod

4.4. Config with Namespace

Create namespace first: kubectl create namespace dev

apiVersion: v1
kind: Pod
metadata:
  name: redis
  namespace: dev
spec:
  containers:
  - name: redis
    image: kubernetes/redis:v1
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf

kubectl apply -f redis-pod.yaml

5. Reference

MainWiki: Kubernetes (last edited 2019-08-10 11:25:14 by twotwo)