目錄node
示例流程以下:mysql
默認刪除,會按照上線的流程,等待寬限期30snginx
kubectl delete POD --namespace=xxx
設置寬限期爲0,會當即刪除,沒有寬限期git
kubectl delete POD --namespace=xxx --force --grace-period=0
template.spec.hostname:
pod_name 設置pod的主機名sql
ImagePullPolicy:docker
注意:shell
IfNotPresent
,但:latest
標籤的鏡像默認爲Always
。:latest
標籤,而開發環境中能夠藉助:latest
標籤自動拉取最新的鏡像。官網資料api
apiVersion: v1 kind: ReplicationController # 定義資源類型 metadata: name: zabbix-db namespace: zabbix spec: replicas: 1 selector: app: zabbix-db template: metadata: name: zabbix-db labels: app: zabbix-db spec: terminationGracePeriodSeconds: 30 # 容器平滑退出時間,默認30s hostname: zabbix-db # 設置容器的主機名 containers: - name: zabbix-db image: mysql:5.7.22 env: - name: MYSQL_DATABASE value: zabbix - name: MYSQL_USER value: zabbix - name: MYSQL_PASSWORD value: zabbix - name: MYSQL_ROOT_PASSWORD value: Abc123@ ports: - containerPort: 3306 volumeMounts: - mountPath: /var/lib/mysql readOnly: false name: zabbix-database volumes: - name: zabbix-database nfs: server: 172.30.80.222 path: "/data/zabbix/zabbix_db/mysql"
Deployment爲Pod和Replica Set(升級版的 Replication Controller)提供聲明式更新。比於RC,Deployment直接使用kubectl edit deployment/deploymentName 或者kubectl set方法就能夠直接升級(原理是Pod的template發生變化,例如更新label、更新鏡像版本等操做會觸發Deployment的滾動升級)。服務器
官方資料session
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx labels: app: nginx spec: minReadySeconds: 30 # 滾動升級時,容器準備就緒時間最少爲30s replicas: 5 selector: matchLabels: app: nginx strategy: rollingUpdate: # 當type爲RollingUpdate時,纔會進行設置 maxSurge: 25% # 當定義爲25%時,容器會先新建百分之25的pod,而後開始滾動升級 maxUnavailable: 25% # 每次升級的百分比,也能夠是絕對數(5),默認值25% type: RollingUpdate # 滾動升級方式,有Recreate:立馬關閉全部pod進行升級,RollingUpdate:採用百分比的方式滾動升級 template: # 模板 metadata: creationTimestamp: null labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 imagePullPolicy: IfNotPresent resources: # 資源限制 requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" env: # 設置變量 - name: DB_SERVER_HOST value: zabbix-db-server - name: MYSQL_DATABASE value: zabbix - name: MYSQL_USER value: zabbix - name: MYSQL_PASSWORD value: zabbix - name: MYSQL_ROOT_PASSWORD value: Abc123@ - name: ZBX_HISTORYSTORAGEURL value: http://192.168.2.171:9200 - name: ZBX_HISTORYSTORAGETYPES value: uint,dbl,str,log,text ports: - containerPort: 80 livenessProbe: #livenessProbe是K8S認爲該pod是存活的,不存在則須要kill掉,而後再新啓動一個,以達到RS指定的個數。 httpGet: path: / port: 80 scheme: HTTP initialDelaySeconds: 30 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 readinessProbe: #readinessProbe是K8S認爲該pod是啓動成功的,這裏根據每一個應用的特性,本身去判斷,能夠執行command,也能夠進行httpGet。 httpGet: path: / port: 80 scheme: HTTP initialDelaySeconds: 30 timeoutSeconds: 5 successThreshold: 1 restartPolicy: Always # 啓動失敗時,會重試啓動 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort sessionAffinity: ClientIP selector: app: nginx ports: # 將容器的80端口映射到master主機的8888端口 - port: 80 # pod端口 nodePort: 8888 # 宿主機上的端口
Horizontal Pod Autoscaler根據觀察到的CPU利用率自動調整複製控制器,部署或副本集中的容器數量(或者,經過 自定義指標 支持,根據其餘一些應用程序提供的指標)。請注意,Horizontal Pod Autoscaling不適用於沒法縮放的對象,例如DaemonSet。
建立一個pod,必須添加資源請求和限制參數
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx labels: app: nginx spec: minReadySeconds: 30 replicas: 5 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 imagePullPolicy: IfNotPresent resources: requests: # 在使用HPA自動擴展時,必須使用資源請求和資源限制 memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" ports: - containerPort: 80 restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort sessionAffinity: ClientIP selector: app: nginx ports: # 將容器的80端口映射到master主機的8888端口 - port: 80 nodePort: 8888
建立HPA
apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler # 指定對象類型 metadata: name: nginx-hpa # 名字 labels: # 標籤 app: hpa version: v0.0.1 spec: scaleTargetRef: apiVersion: v1 kind: Deployment name: nginx # 建立Deployment時,指定的名字 minReplicas: 1 # 最小pod maxReplicas: 10 # 最大pod targetCPUUtilizationPercentage: 70 # CPU用到70%,自動擴展一個pod
查看
# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-hpa Deployment/nginx 0% / 70% 1 10 1 4d
查看詳情
# kubectl describe hpa/nginx-hpa Name: nginx-hpa Namespace: default Labels: app=hpa version=v0.0.1 Annotations: <none> CreationTimestamp: Thu, 02 Aug 2018 16:39:55 +0800 Reference: Deployment/nginx Metrics: ( current / target ) resource cpu on pods (as a percentage of request): 0% (0) / 70% Min replicas: 1 Max replicas: 10 Conditions: Type Status Reason Message ---- ------ ------ ------- AbleToScale True ReadyForNewScale the last scale time was sufficiently old as to warrant a new scale ScalingActive True ValidMetricFound the HPA was able to succesfully calculate a replica count from cpu resource utilization (percentage of request) ScalingLimited True TooFewReplicas the desired replica count was less than the minimum replica count Events: <none>
StatefulSet Name | Service Name | |
---|---|---|
apiVersion: v1 kind: Service metadata: name: zoo01 labels: app: zoo01 spec: ports: - port: 2888 name: leader-listen - port: 3888 name: leader-vote clusterIP: None selector: app: zoo01 --- apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: zoo01 labels: app: zoo01 spec: serviceName: "zoo01-service" replicas: 1 template: metadata: creationTimestamp: null labels: app: zoo01 spec: terminationGracePeriodSeconds: 30 hostname: zoo01 containers: - name: zoo01 image: zookeeper:3.5 imagePullPolicy: IfNotPresent resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" env: - name: ZOO_MY_ID value: "1" - name: ZOO_SERVERS value: "server.1=zoo01:2888:3888 server.2=zoo02:2888:3888 server.3=zoo03:2888:3888" ports: - containerPort: 2181 - containerPort: 2888 - containerPort: 3888 restartPolicy: Always
PV
apiVersion: v1 kind: PersistentVolume metadata: name: gitlab-pv namespace: dev spec: capacity: storage: 10Gi # 大小 accessModes: - ReadWriteOnce # 讀寫模式 persistentVolumeReclaimPolicy: Recycle storageClassName: slow mountOptions: # 掛載選項 - hard - nfsvers=4.1 nfs: # nfs服務器地址 path: /data/gitlab server: 172.30.80.222
PVC
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: gitlab-pvc namespace: dev labels: type: nfs spec: accessModes: - ReadWriteOnce # PV和PVC的綁定關係,主要依靠讀寫模式和存儲大小 storageClassName: slow resources: requests: storage: 10Gi selector: matchLabels: name: gitlab-pv
Pod
介紹:
Pod.spec.nodeSelector經過kubernetes的label-selector機制選擇節點,由調度器調度策略匹配label,然後調度Pod到目標節點,該匹配規則屬於強制約束。
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: zabbix-server namespace: zabbix spec: replicas: 1 selector: matchLabels: app: zabbix-server template: metadata: name: zabbix-server labels: app: zabbix-server spec: nodeSelector: # 基於node的lable進行調度 kubernetes.io/hostname: 172.30.80.220 # 指定主機的lable標籤 containers: - name: zabbix-server image: zabbix/zabbix-server-mysql:latest env: - name: DB_SERVER_HOST value: zabbix-db-server - name: MYSQL_DATABASE value: zabbix - name: MYSQL_USER value: zabbix - name: MYSQL_PASSWORD value: zabbix - name: MYSQL_ROOT_PASSWORD value: Abc123@ - name: ZBX_HISTORYSTORAGEURL value: http://192.168.2.171:9200 - name: ZBX_HISTORYSTORAGETYPES value: uint,dbl,str,log,text ports: - containerPort: 10051