Deployment,Service,Pod是k8s最核心的3個資源對象。
Deployment:最常見的無狀態應用的控制器,支持應用的擴縮容,滾動更新等操作。
Service:爲彈性變動且存在生命週期的Pod對象提供了一個固定的訪問接口,用於服務發現和服務訪問。
Pod:是運行容器以及調度的最小單位。同一個Pod可以同時運行多個容器,這些容器共享NET,UTS,IPC.除此之外還有USER,PID,MOUNT.
ReplicationController:用於確保每個Pod副本在任意時刻都能滿足目標數量,簡單來說,它用於保證每個容器或容器組總是運行並且可以訪問的:老一代無狀態的Pod應用控制器。
ReplicaSet:新一代的無狀態的Pod應用控制器,它與RC的不同之處在於支持的標籤選擇器不同,RC只支持等值選擇器,RS還額外支持基於集合的選擇器。
StatefulSet:用於管理有狀態的持久化應用,如database服務程序,它與Deployment不同之處在於,它會爲每一個Pod創建一個獨立的持久性標識符,並確保每個Pod之間的順序性。
DaemonSet:用於確保每一個節點都運行了某個Pod的一個副本,新增的節點一樣會被添加此Pod,在節點移除時,此類Pod會被回收。
Job:用於管理運行完成後即可終止的應用,例如批量處理作業任務。
Volume:PV PVC
ConfigMap: 存儲通用的配置變量的,類似於配置文件,使用戶可以將分佈式系統中用於不同模塊的環境變量統一到一個對象中管理;而它與配置文件的區別在於它是存在集羣的“環境”中的,並且支持K8S集羣中所有通用的操作調用方式。
Secret: 用來保存小片敏感數據的k8s資源,例如密碼,token,或者祕鑰。這類數據當然也可以存放在Pod或者鏡像中,但是放在Secret中是爲了更方便的控制如何使用數據,並減少暴露的風險。
Role: 表示是一組規則權限,只能累加,Role
可以定義在一個namespace
中,只能用於授予對單個命名空間中的資源訪問的權限比如我們新建一個對默認命名空間中。
ClusterRole:
RoleBinding:
ClusterRoleBinding:
Service account:
Helm:
Namespace:名稱空間
默認的名稱空間:Default
//查看名稱空間
[root@master ~]# kubectl get ns
//查看名稱空間詳細信息
[root@master ~]# kubectl describe ns default
//創建名稱空間
[root@master ~]# kubectl create ns bdqn
[root@master ~]# kubectl explain ns
[root@master ~]# vim 111-test.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
[root@master ~]# kubectl apply -f 111-test.yaml
[root@master ~]# kubectl get ns
刪除資源的兩種方法:
[root@master ~]# kubectl delete ns test
[root@master ~]# kubectl delete -f 111-test.yaml
Ps: namespace資源對象僅用於資源對象的隔離,並不能隔絕不同名稱空間的Pod之間的的通信,那是網絡策略資源的功能。
查看指定名稱空間的資源可以使用--namespace或者-n選項
[root@master ~]# kubectl get pod --namespace=bdqn
No resources found.
簡寫:
[root@master ~]# kubectl get pod -n bdqn
No resources found.
查看本集羣運行的Pod
[root@master ~]# kubectl get pod -n kube-system
Pod
[root@master ~]# vim pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-app
image: httpd
[root@master ~]# kubectl apply -f pod.yaml
pod/test-pod created
[root@master ~]# kubectl get pod
[root@master ~]# vim pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
namespace: bdqn //添加一行
spec:
containers:
- name: test-app
image: httpd
重新生成:
[root@master ~]# kubectl apply -f pod.yaml
pod/test-pod created
查看bdqn名稱空間
[root@master ~]# kubectl get pod -n bdqn
NAME READY STATUS RESTARTS AGE
test-pod 1/1 Running 0 80s
Pod中鏡像獲取策略:
Always:鏡像標籤爲“latest”或鏡像不存在時,總是從指定的倉庫中獲取鏡像。
IfNotPresent:僅當本地鏡像不存在時才從目標倉庫中下載。
Never:禁止從倉庫中下載鏡像,即只是用本地鏡像。
PS:對於標籤“latest”或者是不存在,其默認策略下載及策略爲“Always”,而對於其他標籤的鏡像,默認策略爲“IfNotPresent”。
[root@master ~]# vim pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
namespace: bdqn
spec:
containers:
- name: test-app
image: httpd
imagePullPolicy: IfNotPresent
ports:
- protocol: TCP
containerPort: 80
[root@master ~]# kubectl delete pod -n bdqn test-pod
pod "test-pod" deleted
[root@master ~]# kubectl apply -f pod.yaml
pod/test-pod created
[root@master ~]# kubectl apply -f pod.yaml
pod/test-pod created
[root@master ~]# kubectl get pod -n bdqn
NAME READY STATUS RESTARTS AGE
test-pod 1/1 Running 0 26s
最終效果:
[root@master ~]# vim pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
namespace: bdqn
labels:
app: test-web
spec:
containers:
- name: test-app
image: httpd
imagePullPolicy: IfNotPresent
ports:
- protocol: TCP
containerPort: 90
[root@master ~]# vim svc.yaml
apiVersion: v1
kind: Service
metadata:
name: test-svc
namespace: bdqn
spec:
selector:
app: test-web
ports:
- port: 80
targetPort: 90
[root@master ~]# kubectl describe svc -n bdqn test-svc
容器的重啓策略
Always:單反Pod對象終止就將其重啓,此爲默認設定。
OnFailure:僅在Pod對象出現錯誤時纔將其重啓。
Never:從不重啓。
Pod的默認健康檢查
[root@master ~]# vim healcheck.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: healcheck
name: healcheck
spec:
restartPolicy: OnFailure
containers:
- name: healthcheck
image: busybox
args:
- /bin/sh
- -c
- sleep 20; exit 1
[root@master ~]# kubectl apply -f healcheck.yaml
[root@master ~]# kubectl get pod -w
[root@master ~]# kubectl get pod -n kube-system