05 初识核心概念pod

unlisted · suofeiya's blog

#kubernetes

Table of Contents

写在前面 #

前面的系列文章已介绍kubernetes架构,安装,升级和快速入门,读者通过文章的实操已对kubernetes已有初步的认识和理解,从本章开始逐步介绍kubernetes中的基础概念概念和核心概念,基础概念包括:namespace,labels,annotations,pods,volumes等;核心概念包含kubernetes中各种controller,包含以下几种:

本文从最基础的概念pod开始讲解,后续逐步介绍应用部署,存储,负载均衡等相关的控制器,kubernetes内部由多个不同的控制器组成,每个控制器完成不同的功能。

1. 深入学习pod #

1.1 Container和Pod概念 #

容器是一种便携式,轻量级别的容器虚拟化技术,使用linux cggroup技术实现各种资源的隔离,如cpu,memory,pid,mount,IPC等,相比于虚拟化技术如KVM,容器技术更加轻量级,它的产生主要解决环境的环境发布的问题,目前主流的容器技术是docker,说到容器,一般都等同于docker。

要运行容器首先需要有镜像,应用和应用依赖的环境运行在容器中,在kubernetes中不会直接运行container,而是运行pod,一个pod里面包含多个container,container之间共享相同的namespace,network,storage等。镜像存储在私有镜像或者公有镜像中,运行时通过docker image pull的方式拉取到本地运行,images的拉取策略包含有两种:

Pods是kubernetes中最小的调度单位,Pods内运行一个或者多个container,container之间共享pod的网络ip资源,存储volume资源,计算等资源,方便pod内部的container之间能够实现快速的访问和交互。

imgPod概念介绍

如上图所示,Pod的使用方式通常包含两种:

1.2 如何创建pod #

kubernetes交互的方式通常分为四种:

kubernetes中通过定义生申明式的方式定义资源,即通过在yaml文件中定义所需的资源,kubernetes通过controller-manager按照yaml文件中定义的资源去生成所需的资源(match the current state to desired state)。通常在kubernetes中通过yaml文件的方式定义资源,然后通过kubectl create -f 文件.yaml的方式应用配置,如下演示创建一个nginx应用的操作。

1、编写yaml文件,定义一个pod资源

 1[root@node-1 demo]# cat nginx.yaml 
 2apiVersion: v1
 3kind: Pod
 4metadata:
 5  name: nginx-demo
 6  labels:
 7    name: nginx-demo
 8spec:
 9  containers:
10  - name: nginx-demo
11    image: nginx:1.7.9
12    imagePullPolicy: IfNotPresent
13    ports:
14    - name: nginx-port-80
15      protocol: TCP
16      containerPort: 80

关于配置文件,说明如下:

2、创建pod应用

1[root@node-1 demo]# kubectl apply -f nginx.yaml 
2pod/nginx-demo created

3、访问应用

 1获取容器的IP地址
 2[root@node-1 demo]# kubectl get pods -o wide 
 3NAME                    READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
 4demo-7b86696648-8bq7h   1/1     Running   0          8h    10.244.1.11   node-2   <none>           <none>
 5demo-7b86696648-8qp46   1/1     Running   0          8h    10.244.1.10   node-2   <none>           <none>
 6demo-7b86696648-d6hfw   1/1     Running   0          8h    10.244.1.12   node-2   <none>           <none>
 7nginx-demo              1/1     Running   0          50s   10.244.2.11   node-3   <none>           <none>
 8
 9访问站点内容
10[root@node-1 demo]# curl http://10.244.2.11
11<!DOCTYPE html>
12<html>
13<head>
14<title>Welcome to nginx!</title>
15<style>
16    body {
17        width: 35em;
18        margin: 0 auto;
19        font-family: Tahoma, Verdana, Arial, sans-serif;
20    }
21</style>
22</head>
23<body>
24<h1>Welcome to nginx!</h1>
25<p>If you see this page, the nginx web server is successfully installed and
26working. Further configuration is required.</p>
27
28<p>For online documentation and support please refer to
29<a href="http://nginx.org/">nginx.org</a>.<br/>
30Commercial support is available at
31<a href="http://nginx.com/">nginx.com</a>.</p>
32
33<p><em>Thank you for using nginx.</em></p>
34</body>
35</html>

前面我们我们学习过kubernetes支持滚动升级RollingUpdate,弹性扩容replicas等特性,如何给Pod做滚动升级保障业务不中断,如何提高Pod的副本个数保障高可用呢?答案是:不支持。Pod是单个,无法支持一些高级特性,高级特性需要通过高级的副本控制器如ReplicaSets,Deployments,StatefulSets,DaemonSets等才能支持。Pod在实际应用中很少用,除了测试和运行一些简单的功能外,实际使用建议使用Deployments代替,Pod的定义以Template的方式嵌入在副本控制器中。

1.3. 如何编写yaml文件 #

前面我们提到过kubernetse是申明式的方式部署应用,应用的部署都定义在yaml文件中来实现,如何来编写应用的yaml文件呢,下面我来分享两个实际使用的技巧:

1、通过定义模版快速生成,kubectl create apps -o yaml --dry-run的方式生成,--dry-run仅仅是试运行,并不实际在k8s集群中运行,通过指定-o yaml输出yaml格式文件,生成后给基于模版修改即可,如下:

 1[root@node-1 demo]# kubectl create deployment demo --image=nginx:latest  --dry-run -o yaml
 2apiVersion: apps/v1
 3kind: Deployment
 4metadata:
 5  creationTimestamp: null
 6  labels:
 7    app: demo
 8  name: demo
 9spec:
10  replicas: 1
11  selector:
12    matchLabels:
13      app: demo
14  strategy: {}
15  template:
16    metadata:
17      creationTimestamp: null
18      labels:
19        app: demo
20    spec:
21      containers:
22      - image: nginx:latest
23        name: nginx
24        resources: {}
25status: {}

2、explain命令,explain命令堪称是语法查询器,可以查到每个字段的含义,使用说明和使用方式,如想要查看Pod的spec中containers其他支持的字段,可以通过kubectl explain Pod.spec.containers的方式查询,如下:

 1[root@node-1 demo]# kubectl explain Pods.spec.containers
 2KIND:     Pod
 3VERSION:  v1
 4
 5RESOURCE: containers <[]Object>
 6
 7DESCRIPTION:
 8     List of containers belonging to the pod. Containers cannot currently be
 9     added or removed. There must be at least one container in a Pod. Cannot be
10     updated.
11
12     A single application container that you want to run within a pod.
13
14FIELDS:
15   args	<[]string> #命令参数
16     Arguments to the entrypoint. The docker image's CMD is used if this is not
17     provided. Variable references $(VAR_NAME) are expanded using the
18     container's environment. If a variable cannot be resolved, the reference in
19     the input string will be unchanged. The $(VAR_NAME) syntax can be escaped
20     with a double $$, ie: $$(VAR_NAME). Escaped references will never be
21     expanded, regardless of whether the variable exists or not. Cannot be
22     updated. More info:
23     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
24
25   image	<string> #镜像定义
26     Docker image name. More info:
27     https://kubernetes.io/docs/concepts/containers/images This field is
28     optional to allow higher level config management to default or override
29     container images in workload controllers like Deployments and StatefulSets.
30
31   ports	<[]Object> #端口定义
32     List of ports to expose from the container. Exposing a port here gives the
33     system additional information about the network connections a container
34     uses, but is primarily informational. Not specifying a port here DOES NOT
35     prevent that port from being exposed. Any port which is listening on the
36     default "0.0.0.0" address inside a container will be accessible from the
37     network. Cannot be updated.
38
39   readinessProbe	<Object> #可用健康检查
40     Periodic probe of container service readiness. Container will be removed
41     from service endpoints if the probe fails. Cannot be updated. More info:
42     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
43
44   resources	<Object> #资源设置
45     Compute Resources required by this container. Cannot be updated. More info:
46     https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
47
48...省略部分输出...
49   volumeMounts	<[]Object> #挂载存储
50     Pod volumes to mount into the container's filesystem. Cannot be updated.
51
52   workingDir	<string>
53     Container's working directory. If not specified, the container runtime's
54     default will be used, which might be configured in the container image.
55     Cannot be updated.

关于explain内容解释说明