+-
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