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

 

 

 

 對象管理實例:vim nginx.deployment.yamlhtml

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
View Code

 

 

服務發現實例:vim nging.service.yamlnginx

apiVersion: v1
kind: Service
metadata:
  labels:
    run: nginx
  name: nginx
  namespace: default
spec:
  ports:
  - port: 88
    targetPort: 80
  selector:
    app: nginx
View Code

清理:vim

# kubectl delete -f nginx-deployment.yaml 後端

# kubectl delete -f nginx-service.yamlcentos

 POD做用:api

     Pod狀態  建立/查詢/更新/刪除  重啓策略  健康檢查  數據持久化和共享  hostPort  問題定位bash

 

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

 

 

支持三種策略:app

Always:當容器終止退出後,老是重啓容器,默認策略。 tcp

OnFailure:當容器異常退出(退出狀態碼非0)時,才重啓容器。ide

Never:當容器終止退出,從不重啓容器。

Pod實例:vim pod.yaml

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
View Code

 

Pod管理-健康檢查:

提供Probe機制,有如下兩種類型:
livenessProbe
若是檢查失敗,將殺死容器,而後根據Pod的重啓策略來決定是否
重啓。
readinessProbe
若是檢查失敗,Kubernetes會把Pod從服務代理的分發後端剔除。
Probe支持如下三種檢查方法:
httpGet
發送HTTP請求,返回200-400範圍狀態碼爲成功。
exec
執行Shell命令返回狀態碼是0爲成功。
tcpSocket
發起TCP Socket創建成功。

實例:vim pod2.yaml

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
View Code

 

Pod管理-數據持久化和共享:

vim pod3.yaml

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
View Code

 

Pod端口映射:

vim pod4.yaml

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
相關文章
相關標籤/搜索