在k8s中的基本概念

时间:2022-03-10 22:59:49

在k8s中的基本概念

一.Pod
1. pod
k8s下最重要也最基本的概念,由一个根容器Pause和许多用户业务容器组成,是容器的载体.

2. pod的yaml定义格式及字段

apiVersion: v1            //版本
kind: pod //类型,pod
metadata: //元数据
name: String //元数据,pod的名字
namespace: String //元数据,pod的命名空间
labels: //元数据,标签列表
- name: String //元数据,标签的名字
annotations: //元数据,自定义注解列表
- name: String //元数据,自定义注解名字
spec: //pod中容器的详细定义
containers: //pod中的容器列表,可以有多个容器
- name: String
image: String //容器中的镜像
imagesPullPolicy: [Always|Never|IfNotPresent]//获取镜像的策略
command: [String] //容器的启动命令列表(不配置的话使用镜像内部的命令)
args: [String] //启动参数列表
workingDir: String //容器的工作目录
volumeMounts: //挂载到到容器内部的存储卷设置
- name: String
mountPath: String
readOnly: boolean
ports: //容器需要暴露的端口号列表
- name: String
containerPort: int //容器要暴露的端口
hostPort: int //容器所在主机监听的端口(容器暴露端口映射到宿主机的端口)
protocol: String
env: //容器运行前要设置的环境列表
- name: String
value: String
resources: //资源限制
limits:
cpu: Srting
memory: String
requeste:
cpu: String
memory: String
livenessProbe: //pod内容器健康检查的设置
exec:
command: [String]
httpGet: //通过httpget检查健康
path: String
port: number
host: String
scheme: Srtring
httpHeaders:
- name: Stirng
value: String
tcpSocket: //通过tcpSocket检查健康
port: number
initialDelaySeconds: 0//首次检查时间
timeoutSeconds: 0 //检查超时时间
periodSeconds: 0 //检查间隔时间
successThreshold: 0
failureThreshold: 0
securityContext: //安全配置
privileged: falae
restartPolicy: [Always|Never|OnFailure]//重启策略
nodeSelector: object //节点选择
imagePullSecrets:
- name: String
hostNetwork: false //是否使用主机网络模式,默认否
volumes: //在该pod上定义共享存储卷
- name: String
meptyDir: {}
hostPath:
path: string
secret: //类型为secret的存储卷
secretName: String
item:
- key: String
path: String
configMap: //类型为configMap的存储卷
name: String
items:
- key: String
path: String

  

3. label 和 label selector
label是k8s中的核心概念,label由key和value组成,用户自定义,用以区分和筛选pod.
如:Service和ReplicationController中筛选component为redis的pod
selector:
component: redis

二.ReplicationController
1.ReplicationController(简称Rc)
Rc控制了按期望的数量来运行pod.Rc定义包括了以下部分:
a.期待的pod副本数
b.筛选目标pod的label selector
c.但pod小于指定副本数的时候,用于创建pod的模板(template).

2.定义示例:

apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

  

3.删除Rc并不会删除rc创建的pod.为了删除所有pod,可以将replicas设置为0.
如:
kubectl scale rc php-rc --replicas 0

4.扩容
kubectl scale rc php-rc --replicas 2

5.自动控制pod副本数量最小1,最大4:
kubectl autoscale rc php-rc --min=1 --max=4

三.Deployment
在内部使用了Replica Set(可以看做是Rc的升级版本,90%与Rc相似).

三.Service
1.概念及作用:
定义了一个服务访问的入口地址,因为pod副本会有多个,同样的地址也就有多个,如果没有service,将需要考虑pod的负载均衡问题.换句话说service将一组pod组成一个集合来提供给其他资源,用户无需关注各个pod副本.
Service与前后端的Pod集群通过label selector来实现对接,rc保证了service(实际上是pod的数量)服务质量

2.IP种类
Node IP: 节点IP(物理网卡)
Pod IP: pod上的IP(Docker Engine分配)
Cluster IP: service上的IP(K8s分配,无法被ping)

3.ports属性:
nodePort: 外部访问service,通过nodeIP:nodePort方式提供给外部访问k8s中的服务(需要配置对应service的type为NodePort,同时在节点服务器上设置转发iptables -P FORWARD ACCEPT,默认为30000-32767)
port: k8s内部访问service的端口
targetPort: 容器的端口,是pod上的端口(如无指定,默认和port相同)
流量进入路径为:nodePort -> port -> targetPort

4.服务发现:
环境变量(可进入具体的pod中查看每个service对应的环境变量)和kube-dns

5.Service的type类型:
ClusterIP: 仅仅使用一个集群内部的IP地址 - 这是默认值。选择这个值意味着你只想这个服务在集群内部才可以被访问到
请注意,这个内部访问指的是在集群内部的pod上可以访问service,
并不是集群内部节点上直接访问;在service所在pod的节点主机上是可以直接以ClusterIP:端口的形式访问到;如果pod不在该节点上,节点主机要访问该service,需要做IP转发:

ip route add 10.254.0.0/16 dev docker0

10.254.0.0是service网段

你可以在节点服务器上运行:

iptables -S -t nat | grep KUBE-SERVICES

来查看转发规则

NodePort: 在集群内部IP的基础上,在集群的每一个节点的端口上开放这个服务。你可以在外部通过<NodeIP>:NodePort地址*问到这个服务(其中NodeIP是任意节点的ip地址)
LoadBalancer: 在使用一个集群内部IP地址和在NodePort上开放一个服务之外,向云提供商申请一个负载均衡器,会让流量转发到这个在每个节点上以<NodeIP>:NodePort的形式开放的服务上。

四.存储卷(Volume)
1.示例:

spec:
containers:
- name: nginx
image: nginx:alpine
volumeMounts:
- name: web-root
mountPath: /usr/share/nginx/html
volumes:
- name: web-root
nfs:
server: 192.168.2.17
path: /data/nfs

  

2.volume取值
emptyDir: 是在pod分配到node上创建的,初始内容为空;无需指定名称,k8s会自动分配目录.pod移除时,该目录也被删除.
hostPath: 在pod上挂载宿主机上的目录.
nfs: 使用nfs网络文件系统提供的共享目录

五.命名空间
1.k8s默认使用的命名空间是default,使用kubectl get namespaces查看
2.定义命名空间:

apiVersion: v1
kind: namespace
metadata:
name: projectA

  

3.使用命名空间:

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: projectA
labels:
name: nginx-pod
spec:
containers:
- name: nginx
....

  

4.namespace可以实现多用户资源隔离

查看所有命名空间下的pods

#kubectl get pods --all-namespaces

  

六.kubectl命令行工具用法
1.语法:
kubectl [command] [TYPE] [NAME] [flags]
(1)command:子命令,用于操作k8s集群资源对象的命令.如create, delete, describe, get, apply
(2)TYPE:资源对象的类型,区分大小写,能以单数形式,复数形式或者简写形式表示.如pod,service,node等
(3)NAME:资源对象的名称,区分大小写,如果不指定,则返回全部.
(4)flags:子命令可选参数,如-s指定apiserver的url地址而不使用默认值

2.输出格式:
输出格式通过-o参数指定

在k8s中的基本概念

3.操作示例
(1).创建资源对象
根据yaml配置文件创建
kubectl create -f my-service.yaml my-pod.yaml

(2)查看资源对象
kubectl get pod,rc,service

(3)描述资源对象
kubectl describe pods

显示由rc管理的pod信息
kubectl describe pods <rc-name>

(4)删除资源对象
kubectl delete -f my-service.yaml
通过label删除
kubectl delete pods -l name=<label-name>
删除所有
kubectl delte pods --all

(5)执行容器命令
默认使用pod中的第一个容器:
kubectl exec <pod-name> date

指定pod中的容器执行命令:
kubectl exec <pod-name> -c <conatiner-name> date

通过bash获得pod中某个容器的tty,相当于登录容器:
kubectl exec -ti <pod-name> -c <conatiner-name> /bin/bash

6.查看容器日志:
查看容器输出到stdout的日志
kubectl logs <pod-name>
跟踪查看容器的日志,相当于tail -f:
kubectl logs -f <pod-name> -c <conatiner-name>