在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參數指定
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>