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

相關文章
相關標籤/搜索