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.yaml

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.yaml

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

清理:

# kubectl delete -f nginx-deployment.yaml

# kubectl delete -f nginx-service.yaml

 POD作用:

     Pod状态  创建/查询/更新/删除  重启策略  健康检查  数据持久化和共享  hostPort  问题定位

 

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

 

 

支持三种策略:

Always:当容器终止退出后,总是重启容器,默认策略。

OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。

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

 

posted @ 2018-08-11 17:56  努力乄小白  阅读(32169)  评论(1编辑  收藏  举报