一、K8S-yaml的使用及命令
YAML配置文件管理對象 對象管理: # 創建deployment資源 kubectl create -f nginx-deployment.yaml # 查看deployment kubectl get deploy # 查看ReplicaSet kubectl get rs # 查看pods所有標簽 kubectl get pods --show-labels # 根據標簽查看pods kubectl get pods -l app=nginx # 滾動更新鏡像 kubectl set image deployment/nginx-deployment nginx=nginx:1.11 或者 kubectl edit deployment/nginx-deployment 或者 kubectl apply -f nginx-deployment.yaml # 實時觀察發布狀態: kubectl rollout status deployment/nginx-deployment # 查看deployment歷史修訂版本 kubectl rollout history deployment/nginx-deployment kubectl rollout history deployment/nginx-deployment --revision=3 # 回滾到以前版本 kubectl rollout undo deployment/nginx-deployment kubectl rollout undo deployment/nginx-deployment --to-revision=3 # 擴容deployment的Pod副本數量 kubectl scale deployment nginx-deployment --replicas=10 # 設置啟動擴容/縮容 kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
二、POD
實例1:三種策略
apiVersion: v1 kind: Pod metadata: name: pod-test labels: os: centos spec: containers: - name: hello image: centos:7 env: - name: Test value: "123456" command: ["bash","-c","while true;do date;sleep 1;done"] restartPolicy: OnFailure
支持三種策略:
Always:當容器終止退出后,總是重啟容器,默認策略。
OnFailure:當容器異常退出(退出狀態碼非0)時,才重啟容器。
Never:當容器終止退出,從不重啟容器。
實例2:數據持久化和共享
apiVersion: v1 kind: Pod metadata: name: pod-test1 labels: test: centos spec: containers: # 第一個容器 - name: hello-write image: centos:7 command: ["bash","-c","for i in {1..1000};do echo $i >> /data/hello;sleep 1;done"] # 第二個容器 - name: hello-read image: centos:7 command: ["bash","-c","for i in {1..1000};do cat $i >> /data/hello;sleep 1;done"] volumeMounts: - name: data mountPath: /data # 數據卷 volumes: - name: data hostPath: path: /data
實例3:pod的端口映射
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - name: http containerPort: 80 hostIP: 0.0.0.0 hostPort: 80 protocol: TCP - name: https containerPort: 443 hostIP: 0.0.0.0 hostPort: 443 protocol: TCP
實例4
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - containerPort: 80 livenessProbe: httpGet: path: /index.html port: 80
詳細解析
apiVersion: v1 #指定api版本,此值必須在kubectl apiversion中 kind: Pod #指定創建資源的角色/類型 metadata: #資源的元數據/屬性 name: web04-pod #資源的名字,在同一個namespace中必須唯一 labels: #設定資源的標簽,詳情請見http://blog.csdn.net/liyingke112/article/details/77482384
k8s-app: apache version: v1 kubernetes.io/cluster-service: "true" annotations: #自定義注解列表 - name: String #自定義注解名字 spec:#specification of the resource content 指定該資源的內容 restartPolicy: Always #表明該容器一直運行,默認k8s的策略,在此容器退出后,會立即創建一個相同的容器 nodeSelector: #節點選擇,先給主機打標簽kubectl label nodes kube-node1 zone=node1 zone: node1 containers: - name: web04-pod #容器的名字 image: web:apache #容器使用的鏡像地址 imagePullPolicy: Never #三個選擇Always、Never、IfNotPresent,每次啟動時檢查和更新(從registery)images的策略, # Always,每次都檢查 # Never,每次都不檢查(不管本地是否有) # IfNotPresent,如果本地有就不檢查,如果沒有就拉取 command: ['sh'] #啟動容器的運行命令,將覆蓋容器中的Entrypoint,對應Dockefile中的ENTRYPOINT args: ["$(str)"] #啟動容器的命令參數,對應Dockerfile中CMD參數 env: #指定容器中的環境變量 - name: str #變量的名字 value: "/etc/run.sh" #變量的值 resources: #資源管理,請求請見http://blog.csdn.net/liyingke112/article/details/77452630
requests: #容器運行時,最低資源需求,也就是說最少需要多少資源容器才能正常運行 cpu: 0.1 #CPU資源(核數),兩種方式,浮點數或者是整數+m,0.1=100m,最少值為0.001核(1m) memory: 32Mi #內存使用量 limits: #資源限制 cpu: 0.5 memory: 32Mi ports: - containerPort: 80 #容器開發對外的端口 name: httpd #名稱 protocol: TCP livenessProbe: #pod內容器健康檢查的設置,詳情請見http://blog.csdn.net/liyingke112/article/details/77531584
httpGet: #通過httpget檢查健康,返回200-399之間,則認為容器正常 path: / #URI地址 port: 80 #host: 127.0.0.1 #主機地址 scheme: HTTP initialDelaySeconds: 180 #表明第一次檢測在容器啟動后多長時間后開始 timeoutSeconds: 5 #檢測的超時時間 periodSeconds: 15 #檢查間隔時間 #也可以用這種方法 #exec: 執行命令的方法進行監測,如果其退出碼不為0,則認為容器正常 # command: # - cat # - /tmp/health #也可以用這種方法 #tcpSocket: //通過tcpSocket檢查健康
# port: number lifecycle: #生命周期管理 postStart: #容器運行之前運行的任務 exec: command: - 'sh'
- 'yum upgrade -y' preStop:#容器關閉之前運行的任務 exec: command: ['service httpd stop'] volumeMounts: #詳情請見http://blog.csdn.net/liyingke112/article/details/76577520
- name: volume #掛載設備的名字,與volumes[*].name 需要對應 mountPath: /data #掛載到容器的某個路徑下 readOnly: True volumes: #定義一組掛載設備 - name: volume #定義一個掛載設備的名字 #meptyDir: {} hostPath: path: /opt #掛載設備類型為hostPath,路徑為宿主機下的/opt,這里設備類型支持很多種 原文鏈接:https://blog.csdn.net/liyingke112/article/details/76155428
Pod管理-創建/查詢/更新/刪除
基本管理: # 創建pod資源 kubectl create -f pod.yaml # 查看pods kubectl get pods pod-test # 查看pod描述 kubectl describe pod pod-test # 替換資源 kubectl replace -f pod.yaml -force # 刪除資源 kubectl delete pod pod-test
健康管理
提供Probe機制,有以下兩種類型:
livenessProbe
如果檢查失敗,將殺死容器,然后根據Pod的重啟策略來決定是否
重啟。
readinessProbe
如果檢查失敗,Kubernetes會把Pod從服務代理的分發后端剔除。
Probe支持以下三種檢查方法:
httpGet
發送HTTP請求,返回200-400范圍狀態碼為成功。
exec
執行Shell命令返回狀態碼是0為成功。
tcpSocket
發起TCP Socket建立成功。
三、Deployment
簡單例子
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - containerPort: 80
詳細解析
apiVersion: extensions/v1beta1 kind: Deployment metadata: <Object> spec: <Object> minReadySeconds: <integer> #設置pod准備就緒的最小秒數 paused: <boolean> #表示部署已暫停並且deploy控制器不會處理該部署 progressDeadlineSeconds: <integer> strategy: <Object> #將現有pod替換為新pod的部署策略 rollingUpdate: <Object> #滾動更新配置參數,僅當類型為RollingUpdate maxSurge: <string> #滾動更新過程產生的最大pod數量,可以是個數,也可以是百分比 maxUnavailable: <string> # type: <string> #部署類型,Recreate,RollingUpdate replicas: <integer> #pods的副本數量 selector: <Object> #pod標簽選擇器,匹配pod標簽,默認使用pods的標簽 matchLabels: <map[string]string> key1: value1 key2: value2 matchExpressions: <[]Object> operator: <string> -required- #設定標簽鍵與一組值的關系,In, NotIn, Exists and DoesNotExist key: <string> -required- values: <[]string> revisionHistoryLimit: <integer> #設置保留的歷史版本個數,默認是10 rollbackTo: <Object> revision: <integer> #設置回滾的版本,設置為0則回滾到上一個版本 template: <Object> -required- metadata: spec: containers: <[]Object> #容器配置 - name: <string> -required- #容器名、DNS_LABEL image: <string> #鏡像 imagePullPolicy: <string> #鏡像拉取策略,Always、Never、IfNotPresent ports: <[]Object> - name: #定義端口名 containerPort: #容器暴露的端口 protocol: TCP #或UDP volumeMounts: <[]Object> - name: <string> -required- #設置卷名稱 mountPath: <string> -required- #設置需要掛載容器內的路徑 readOnly: <boolean> #設置是否只讀 livenessProbe: <Object> #就緒探測 exec: command: <[]string> httpGet: port: <string> -required- path: <string> host: <string> httpHeaders: <[]Object> name: <string> -required- value: <string> -required- scheme: <string> initialDelaySeconds: <integer> #設置多少秒后開始探測 failureThreshold: <integer> #設置連續探測多少次失敗后,標記為失敗,默認三次 successThreshold: <integer> #設置失敗后探測的最小連續成功次數,默認為1 timeoutSeconds: <integer> #設置探測超時的秒數,默認1s periodSeconds: <integer> #設置執行探測的頻率(以秒為單位),默認1s tcpSocket: <Object> #TCPSocket指定涉及TCP端口的操作 port: <string> -required- #容器暴露的端口 host: <string> #默認pod的IP readinessProbe: <Object> #同livenessProbe resources: <Object> #資源配置 requests: <map[string]string> #最小資源配置 memory: "1024Mi" cpu: "500m" #500m代表0.5CPU limits: <map[string]string> #最大資源配置 memory: cpu: volumes: <[]Object> #數據卷配置 - name: <string> -required- #設置卷名稱,與volumeMounts名稱對應 hostPath: <Object> #設置掛載宿主機路徑 path: <string> -required- type: <string> #類型:DirectoryOrCreate、Directory、FileOrCreate、File、Socket、CharDevice、BlockDevice - name: nfs nfs: <Object> #設置NFS服務器 server: <string> -required- #設置NFS服務器地址 path: <string> -required- #設置NFS服務器路徑 readOnly: <boolean> #設置是否只讀 - name: configmap configMap: name: <string> #configmap名稱 defaultMode: <integer> #權限設置0~0777,默認0664 optional: <boolean> #指定是否必須定義configmap或其keys items: <[]Object> - key: <string> -required- path: <string> -required- mode: <integer> restartPolicy: <string> #重啟策略,Always、OnFailure、Never nodeName: <string> nodeSelector: <map[string]string> imagePullSecrets: <[]Object> hostname: <string> hostPID: <boolean> status: <Object> ———————————————— 原文鏈接:https://blog.csdn.net/weixin_44006354/article/details/100634729
四、Service
服務類型
服務類型: ClusterIP 分配一個內部集群IP地址,只能在集群內部訪問(同Namespace內的Pod),默認ServiceType。 NodePort 分配一個內部集群IP地址,並在每個節點上啟用一個端口來暴露服務,可以在集群外部訪問。 訪問地址:<NodeIP>:<NodePort> LoadBalancer 分配一個內部集群IP地址,並在每個節點上啟用一個端口來暴露服務。 除此之外,Kubernetes會請求底層雲平台上的負載均衡器,將每個Node([NodeIP]:[NodePort])作為后端添加進去。 ExternalName 通過CNAME將Service與externalName的值映射。要求kube-dns的版本為v1.7+。
實例1
apiVersion: v1 kind: Service metadata: labels: run: nginx name: nginx namespace: default spec: ports: - port: 88 targetPort: 80 selector: app: nginx
實例2:指定ip
apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: nginx ports: - name: http protocol: TCP port: 888 targetPort: 80 # 可以指定ip clusterIP: "10.10.10.123" # - name: https # protocol: TCP # port: 443 # targetPort: 9377
實例3:發布服務
apiVersion: v1 kind: Service metadata: name: nginx-service labels: app: nginx spec: selector: app: nginx ports: - name: http port: 8080 targetPort: 80 nodePort: 30001 type: NodePort
鏈接:
https://www.cnblogs.com/fuyuteng
kubernetes核心組件kube-proxy:
https://www.cnblogs.com/fuyuteng/p/11598768.html
未完待續。。。。。。