Kubernetes 超入门

Kubernetes 超入门

Kubernetes 是一种让使用者管理Cluster 的一种工具,能更轻鬆的管理node。本身提供Service Discovery 与 load-balance 的功能,像是自动配置IP等等。此外也提供进一步的操作,如设定记忆体限制、资料映射的区域等等。但更重要的事情是提供Self-healing 与Automated rollouts and rollbacks,让正在运行的node 可以自动的上下线,甚至可以自行修复node。

From personal blog

Pod

是在kubernetes 中最小单位,一个Pod指的是一个小的服务集,换句话说Pod 包含许多的container即微服务,但一个pod 不代表是一个Node。若是对比 docker 生态系,Pod 类似 docker-compose,可设定记忆体配置、mount disk 、cpu 数量等等,且都是透过yaml档案进行设置。

Pod 可以想成 group of container

Pod ymal 设定

Pod ymal 档案主要有几个必填项目[1]:

apiVersion - Which version of the Kubernetes API you're using to create this objectkind - What kind of object you want to createmetadata - Data that helps uniquely identify the object, including a name string, UID, and optional namespacespec - What state you desire for the object

那一般来说一个Pod 的设定档会包含两个Object, spec、status这两个Object,spec 主要是定义Pod 有哪些container运行与设定,status主要是提供Pod状态让 controller (control plane)去更新、撤销[2]。换句话说spec 是在 yaml 档案进行设定Pod的状态,然而status则是需透过 api或者cmd的方式读取目前Pod 的状态。

例如下列 nginx 範例。

apiVersion: v1kind: Podmetadata:  name: nginx-demospec:  containers:  - name: nginx    image: nginx:1.14.2    ports:    - containerPort: 80

範例实作

由于k8s 本身的硬体需求比较庞大,因此各家机构纷纷推出自己的k8s开放工具,如Ubuntu基金会的Micro-K8s 或者 Rancher 的 k3s...等等,本範例使用k3s 进行解说,测试环境作业系统为ubuntu 20.04。

Attachment demo.yml

apiVersion: v1kind: Podmetadata:  name: nginx-demospec:  containers:  - name: nginx    image: nginx:1.14.2    ports:    - containerPort: 80

step 1.安装k3s

// install k3scurl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=latest sh -

step 2.执行demo.yml 档案

sudo kubectl apply -f demo.yml

step 3.取得status

sudo kubectl get pod/nginx-demo -o yaml

status example

Reference

[1] https://kubernetes.io/docs/home/
[2] https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
[3] https://rancher.com/docs/k3s/latest/


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章