kubernetes资源类别介绍

类别名称
资源对象 Pod、ReplicaSet、ReplicationController、Deployment、StatefulSet、DaemonSet、Job、CronJob、HorizontalPodAutoscaling
配置对象 Node、Namespace、Service、Secret、ConfigMap、Ingress、Label、ThirdPartyResource、 ServiceAccount
存储对象 Volume、Persistent Volume
策略对象 SecurityContext、ResourceQuota、LimitRange

pod状态介绍

1、pending(挂起)  例如没有适合的节点运行pod

2、running (运行)

3、fAILED (失败)

4、Succeeded(成功)

5、Unknown (例如kubelet挂了)

创建Pod经历阶段

 apiservice  -> etcd -> statefulset(调度) -> node节点

Pod生命周期的重要行为:

1、初始化容器

2、容器探测: 1、liveness  探测容器是否处于存活状态  2、readiness 容器中的程序是否正常提供服务

 pod重启策略

  restartPolicy 

1、Always 总是重启 (默认策略)

2、OnFailure 状态错误时重启

3、Never  挂了不重启

4、Default  

pod控制器:

  replicaset:代用户创建指定的pod数,并确保pod一直处于用户期望的状态,如果少了就添加pod,多了就干掉多的pod,支持自动扩缩容。

 ###Deployment:控制replicaset进行控制pod,支持滚动更新及回滚等操作,管理无状态应用最后的工具。

DaenmonSet:用于确保集群的每个节点只运行一个特定的pod(新增节点会自动添加该pod),通常用来实现系统及的托管任务。

Job:按照用户指定的pod数量启动pod,当pod任务完成后pod挂了不会重启(任务未完成会重启),只是完成一次性任务的服务。

Cronjob:在job基础上周期性完成任务。(只能管控无状态群体)

StatefuiSet:管理有状态应用,每个应用单独管理、拥有独有标识、独有数据记忆,一旦节点故障,添加的新节点会重新初始化操作。例如redis cluster 节点(少用)

Operator:

Ingress Controller:独立运行一个或一组pod资源 ,通常就是一个应用程序,该程序拥有7层代理能力。可选择 Haproxy、nginx、envoy、traefik(适合微服务)

Ingress资源:可以直接通过编辑注入到ingress Controller中并保存及重载配置文件。

Helm:k8s官方提供 类似yum

 Services:

kube-proxy 始终监视着api-service中有关services的变动信息。一旦有service的资源的变动或创建,kube-proxy都会将当前节点的规则转换会service能访问的规则。(一般为iptables或ipvs规则)

service三种模型(代理模式) 4层代理;

1.userspace  1.1之前   内核空间->用户空间(kube-proxy)->内核空间(service ip)分发(效率低)

2.iptables  (1.10之前)

3.ipvs  (1.11开始使用)

 #service类型(核心资源之一)

ExtrnalName:集群外部引入到集群内部

ClusterIP(默认):集群ip地址(集群内部可达集群外部不可访问)

NodePort:用于集群的  client -> NodeIP -> ClusterPoet -> PodIP:containerPort

LoadBalancer:负载均衡方式

##特殊服务(无头服务)

既service集群无clusterIP,将ClusterIP设置为None。将service名称直接解析到pod的ip。

资源记录:

SVN_NAME.NS_NAME.DOMAIN.LTD.

svc.cluster.local.

资源记录:
SVC_NAME.NS_NAME.DOMAIN.LTD.

svc.cluster.local.

redis.default.svc.cluster.local.

对象URL格式:
/apis/[GROUP]/[VERSION]/namespace/[NAMESPACE_NAME]/[KIND]/[OBJECT_ID]

 
 #获取资源配置清单信息
#1.获取api-version资源信息
kubectl  api-versions


#获取yaml文件编写需要的内容
kubectl  explain  [资源名字]

#查看创建pod需要的信息

kubectl explain pods

#查看pod中spec需要的信息

kubectl explain pods.spec


kubernetes 中yaml文件数据定义介绍

apiVersion: api版本
kind: 资源类型
metadata: #元数据
name: 名字
namespace:所在命名空间
labels: 标签信息(可以多个)
##标签是key:value格式的key,value最长只能使用63个字符
# key只能是以数字、之母、_、-、点(.)这五类的组合,
#value可以为空,但只能以数字及字母开头或结尾
app: 标签内容
annotations: #注释(不具备什么功能 就是注释 )
zhushi: ”lalalalalalalal saddas”
spec:期望状态
containers:容器信息(可以多个名称云镜像)
- name: 自定义name名称
image:镜像名
- name:
image:
nodeSelector:#节点选择器(如给指定运行在disk为ssd的node上)
disk: ssd
imagePullPolicy:#是否使用本地或远端的下载镜像
#1、Always
#2、Never
#3、IfNotPresent
livenessProbe:#存活性探针

    #1、exec #命令

    #2、httpGet #http请求 指定ip:port

    #3、tcpSocket  #

    readinessProbe:#就绪状态探针

     #1、exec #命令

    #2、httpGet #http请求 指定ip:port

    #3、tcpSocket  #

   



例如:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: default
  labels: 
    app: my-pod
     
spec:
  containers:
  - name: my-pod
    image: nginx
  - name: mybusybox
    image: busybox
    command:
    - "/bin/sh"
    - "-c"
    - "echo `date` >>/tmp/aa.txt "
例子

1、  replicaset创建例子

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myreplicaset
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      test_node: k8s-node1
  template:
    metadata:
      name: my-replicaset-pod
      labels:
        test_node: k8s-node1
    spec:
      containers:
      - name: my-rep
        image: nginx
        ports:
        - name: http
          containerPort: 80

replicaset扩容或收缩方法

1、edit在线编辑

[root@k8s-m ~]# kubectl  edit rs  myreplicaset

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  creationTimestamp: 2018-09-02T12:12:07Z
  generation: 1
  name: myreplicaset
  namespace: default
  resourceVersion: "63280"
  selfLink: /apis/extensions/v1beta1/namespaces/default/replicasets/myreplicaset
  uid: 6958fc28-aea9-11e8-96d6-000c2924d722
spec:
  replicas: 2 ##数量修改
  selector:
    matchLabels:
      test_node: k8s-node1

selector:
matchLabels:
test_node: k8s-node1
template:
metadata:
creationTimestamp: null
labels:
test_node: k8s-node1
name: my-replicaset-pod
spec:
containers:
- image: nginx ##修改镜像可完成在线升级镜像 (不过需要干掉之前的pod让他重新创建)
imagePullPolicy: Always
name: my-rep
ports:
- containerPort: 80


。。。。省略

 2、Deployment的yaml文件例子

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydeploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      test_node: k8s-node1
  template:
    metadata:
      labels:
        test_node: k8s-node1
    spec:
      containers:
      - name: mydeploy-pod
        image: nginx
        ports:
        - name: http
          containerPort: 80

Deployment创建

kubectl apply -f mydeploy.yaml (可以使用apply、apply既可以创建也可以更新 )

Deployment更爱rs的pod数量或更新,之家修改Deployment的yaml文件即可,将rs的数量改变或镜像改变即可。

然后执行  kubectl   apply  -f  mydeploy.yaml (deploy的yaml文件可以执行多次)

deploy每次改变它都会同步到etcd中,然后apiserver发现他与etcd中的状态不同,然后修改到它的到期望的状态。实现现有状态到期望状态的改变。

##查看deploy更新历史信息 kubectl rollout history deployment [depoly名]

kubectl rollout history deployment mydeploy

###回滚

kubectl rollout undo deployment [deploy名]  (默认上一个版本)

##指定版本

kubectl rollout undo deployment [deploy名]   --to-revision=[版本]

3、DaenmonSet

 DaenmonSet例子

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myds
  namespace: default
spec:
  selector:
    matchLabels:
      test_node: k8s-node1
  template:
    metadata:
      labels:
        test_node: k8s-node1
    spec:
      containers:
      - name: filebeat
        image: filebeat
        env:  ##传递环境变量
          - name: REDIS_HOST
            value: redis.default.svc.cluster.local
          - name: REDIS_LOG_LEVEN
            value: info

启动 

kubectl apply -f mydaemonset.yaml

4、service创建

apiVersion: v1
kind: Service
metadata: 
  name: svc-redis
  namespace: default
spec:

# selector:

   #   disk: ssd

  clusterIP: 10.96.96.96
  type: ClusterIP
  ports:
  - port: 6379 #service端口
    targetPort: 6379 #pod端口

创建

kubectl  apply -f service-redis.yaml

4.1 NodePort类型service创建

apiVersion: v1
kind: Service
metadata: 
  name: svc-redis
  namespace: default
spec:
  clusterIP: 10.96.96.96
  type: NodePort
  ports:
  - port:  80 #serivce端口
    targetPort: 80 #pod端口
    nodePort:  30000  #节点端口(动态分配,可以不定义)

5、Ingress Controller安装

#创建命名空间

kubectl  create  namespace nginx-ingress

##安装

git clone https://github.com/kubernetes/ingress-nginx.git

cd ingress-nginx/deploy

kubectl apply -f ./

##cat deploy-demo.yaml 
apiVersion: v1 kind: Service metadata: name: nyapp namespace: default spec: selector: app: myapp ports: - name: http targetPort: 80 port: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deploy namespace: default spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: ikubernetes/myapp:v2 ports: - name: http containerPort: 80

 kubectl  apply -f deploy-demo.yaml

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  type: NodePort
  ports:
  - name: http 
    port: 80 #service
    targetPort: 80 #容器
    protocol: TCP

      nodePort: 30080


  - name: https
    port: 443 #service
    targetPort: 443 #容器
    protocol: TCP

      nodePort: 30443


  selector:
    app: ingress-nginx

kubectl  get svc -n ingress-nginx   #(上面的文件)

原文地址:https://www.cnblogs.com/zhangb8042/p/9572701.html