乐趣区

关于kubernetes:Kubernetes实用技巧

  • kubectl 命令太多太长记不住?

    • 查看资源缩写
    kubectl describe 回车 
  • 配置 kubectl 主动补全命令
source <(kubectl completion bash)
  • kubectl 写 yaml 太累,找样例太麻烦?

    • 用 run 命令生成
    kubectl run --image=nginx my-deploy -o yaml --dry-run > my-deploy.yaml
  • 用 get 命令导出
kubectl get statefulset/foo -o=yaml --export > new.yaml
  • Pod 亲和性上面字段的拼写遗记了
kubectl explain pod.spec.affinity.podAffinity
  • 监控集群组件

    • 集群整体状态
    kubectl cluster-info 
  • 更多集群信息
kubectl cluster-info dump
  • 组件 metrics
curl localhost:10250/stats/summary
  • 组件监控情况
curl localhost:10250/healthz
  • 治理 K8s 组件日志

    # 组件日志
    /var/log/kube-apiserver.log
    /var/log/kube-proxy.log
    /var/log/kube-controller-manager.log
    /var/log/kubelet.log
  • 应用 systemd 治理
 journalctl -u kubelet 
  • 应用 K8s 插件部署
 kubectl logs -f kube-proxy
  • 治理 K8s 利用日志

    • 从容器规范输入截获
     kubectl logs -f {pod name} -c {container name}
     docker logs -f {docker name}
  • 日志文件挂载到主机目录
 apiVersion: v1
 kind: Pod
 metadata:
   name: test-pod
 spec:
   containers:
   - image: test-webserver
     name: test-container
     volumeMounts:
     - mountPath: /log
       name: log-volume
   volumes:
   - name: log-volume
     hostPath:
       path: /var/k8s/log
  • Deployment 降级与回滚

    • 创立 Deployment
    kubectl run {deployment} --image={image} --replicas={rep.}
  • 降级 Deployment
kubectl set image deployment/nginx-deployment nginx=nignx:1.9.1
kubectl set resources deployment/nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi
  • 降级策略
minReadySeconds: 5
strategy:
  type: RollingUpdata
    maxSurge: 1 #默认 25%
    maxUnavailable: 1 #默认 25%
  • 暂停 Deployment
kubectl rollout pause deployment/nginx-deployment
  • 复原 Deployment
kubectl rollout resume deployment/nginx-deployment
  • 查问降级状态
kubectl rollout status deployment/nginx-deployment
  • 查问降级历史
kubectl rollout history deploy/nginx-deployment
kubectl rollout history deploy/nginx-deployment --revision=2
  • 回滚
kubectl rollout undo deployment/nginx-deployment --to-revision=2
  • 利用弹性伸缩
kubectl scale deployment nginx-deployment --replicas=10
  • 对接了 Heapster, 和 HPA 联动后
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
退出移动版