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状态介绍node

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

二、running (运行)git

三、fAILED (失败)github

四、Succeeded(成功)redis

五、Unknown (例如kubelet挂了)api

建立Pod经历阶段app

 apiservice  -> etcd -> statefulset(调度) -> node节点负载均衡

Pod生命周期的重要行为:tcp

一、初始化容器ide

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

 

 pod重启策略

  restartPolicy 

一、Always 老是重启 (默认策略)

二、OnFailure 状态错误时重启

三、Never  挂了不重启

四、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:#是否使用本地或远端的下载镜像
#一、Always
#二、Never
#三、IfNotPresent
livenessProbe:#存活性探针

    #一、exec #命令

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

    #三、tcpSocket  #

    readinessProbe:#就绪状态探针

     #一、exec #命令

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

    #三、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 "
例子

 

一、  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扩容或收缩方法

一、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


。。。。省略

 二、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=[版本]

 

三、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

 

 

四、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  #节点端口(动态分配,能够不定义)

 

五、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   #(上面的文件)

相关文章
相关标签/搜索