1. 程式人生 > >kubernetes建立資源物件

kubernetes建立資源物件

1.建立deployment資源物件

# cat nginx-deployment.yml
apiVersion: apps/v1beta2     
kind: Deployment
metadata: 
    name: nginx-deployment
spec:
    replicas: 3
    selector:
      matchLabels:
        app: nginx
    template:              #定義pod的模板
      metadata:            #當前pod的元資料
        labels:            #至少定義一個labels標籤,可以建立任意一個key:value
          app: nginx
      spec:                #當前pod的規格說明
        containers:        #容器
          - name: nginx    #容器名字
            image: nginx:1.10
            ports:
            - containerPort: 88

建立deployment資源:             kubectl create -f nginx-deployment.yml

檢視資源資訊: kubectl get deployment kubectl get rs(ReplicaSet) kubectl get pods --show-labels kubectl get pods -l app=nginx kubectl describe deployment nginx kubectl describe deployment nginx-deployment

刪除資源物件: kubectl delete -f nginx-deployment.yml kubectl delete deployment nginx kubectl delete service nginx

映象更新:     映象更新有如下三種方法:     1.kubectl set image deployment/nginx-deployment nginx=nginx:latest       2.kubectl apply -f nginx-test.yml     3.kubectl edit deployment/nginx-deployment

檢視deployment版本:     kubectl rollout history deployment/nginx-deployment                  #檢視deployment歷史版本記錄 kubectl rollout history deployment/nginx-deployment --revision=2  #檢視deployment指定歷史版本

deployment版本回滾: kubectl rollout undo deployment/nginx-deployment     kubectl rollout undo deployment/nginx-deployment --to-revision=1

擴縮容: kubectl scale deployment nginx-deployment --replicas=10 kubectl autoscale deployment nginx-deployment -min=10 -max=15 -cpu-percent=80

2.建立service資源物件

#cat nginx-service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  ports: 
    - port: 80
      targetport: 88
    selector:
      app: nginx

3.建立pod資源物件

# cat pod.yml
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

kubectl create -f pod.yml     kubectl get pods pod-test kubectl describe pod pod-test kubectl replace -f pod.yml --force   #替換資源 kubectl delete pod pod-test kubectl logs pod-test kubectl exec -it pod-test bash

Pod管理:健康檢查     提供probe機制,有兩種型別         1.livenessProbe             如果檢查失敗,將殺死容器,然後根據pod重啟策略來決定是否重啟         2.readinessProbe             如果檢查失敗,會把pod從服務代理分發後端踢除     Probe支援以下三種檢查方式:         1.httpGet             傳送http請求,返回200-400範圍狀態碼為成功         2.exec             執行shell命令返回0         3.tcpSocket             發起tcp socket建立成功

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    os: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.10
    ports:
      - containerPort: 80
    livenessProbe:   
      httpGet:
        path: /index.html
        port: 80
  restartPolicy: OnFailure

Pod管理:資料持久化和共享

#emptyDir示例:
apiVersion: v1
kind: Pod
metadata:
  name: emptydirpod
spec:
  containers:
  - image: nginx:7
    name: ednginx
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}
#hostPath示例:
apiVersion: v1
kind: Pod
metadata:
  name: hostpathpod
spec:
  containers:
  - image: nginx:1.10
    name: ednginx
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath: 
      path: /data
      type: Directory

Pod管理:httpPort

#httpPort示例
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod2
  labels: 
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.10
    ports:
      - name: http
        containerPort: 80
        hostIP: 0.0.0.0
        hostPort: 89
        protocol: TCP
      - name: https
        containerPort: 443
        hostIP: 0.0.0.0
        hostPort: 443
        protocol: TCP

登入地址:http://192.168.116.147:89/ 即可訪問nginx頁面 kubectl logs nginx-pod2 -f         #-f 相當於tailf命令一樣可以實時重新整理log

4.建立RC資源物件 ...