本博客持续更新…用于记录 k8s 使用过程中的很多的小技巧,也希望你能提供更多的小技巧来~
图形化管理工具 lens
图形化 k8s 管理工具: https://github.com/lensapp/lens
我觉得可以少部署一个 dashboard,并且比官方的 dashboard 好看很多
重启 deployment 命令
我一开始总是 delete 一次 apply 一次,感觉很蠢,又换成调整 scle 但是还是很慢,查了之后发现原来本来就有重启的命令
1
   | kubectl rollout restart deployment nginx-dep
   | 
 
查看链接配置信息
1
   | kubectl config view --minify --raw
   | 
 
kubectx
当你需要使用 kubectl 操作多个集群的时候,可以使用 kubectx 切换 context,非常方便
多集群管理切换工具:https://github.com/ahmetb/kubectx
更新 configmap 脚本
对于配置文件 configmap 的更新我真的没有找到合适的命令,直接 使用 kubectl edit 那么原来的文件是没有被更改的,会导致配置不同步。后面会尝试找找还有没有更好的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | #!/bin/bash
 
  filename=$1 namespace=default
  app=`echo $filename | cut -d . -f1`
  if [ -z $app ];then   echo "filename is empty!"   exit fi
  if [ -z $namespace ];then   echo "namespace is empty!"   exit fi
  echo "start to reload [$app] configmap"
  kubectl get configmap $app-config -o yaml -n $namespace
  echo "---------------------start delete-----------------------------" kubectl delete configmap $app-config -n $namespace
  echo "---------------------start create-----------------------------" kubectl create configmap $app-config --from-file=$app.yaml -n $namespace
  sleep 1 kubectl get configmap $app-config -o yaml -n $namespace
   | 
 
单文件 subpath 挂载
configmap 修改无法自动热更新
1 2 3 4 5 6 7 8 9 10 11 12 13
   | spec:   containers:   - name: test     image: test     imagePullPolicy: Always     volumeMounts:     - name: config-volume       mountPath: /etc/test.yaml       subPath: test.yaml   volumes:     - name: config-volume       configMap:         name: test-config
   | 
 
单文件 mountPath 挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | spec:   containers:   - name: test     image: test     volumeMounts:     - name: config-volume       mountPath: "/conf"   volumes:     - name: config-volume       configMap:         name: test-config         items:          - key: test.json           path: test.json
   | 
 
挂载整个目录
1 2 3 4 5 6 7 8 9 10 11
   | spec:   containers:     - name: test       image: test       volumeMounts:       - name: config-volume         mountPath: /etc/config   volumes:     - name: config-volume       configMap:         name: special-config
   | 
 
不同命名空间的服务相互访问
原来 statefulset 的访问方式是不一样的哦
https://kubernetes.io/zh/docs/concepts/services-networking/dns-pod-service/
POD
1 2 3 4
   | {pod-ip}.{namespace}.pod.cluster.local //例如某pod的ip为 1.2.3.4,在命名空间default与DNS名称cluster.local将有一个域名:1-2-3-4.default.pod.cluster.local。 {pod-ip}.{namespace}.svc.cluster.local {pod-name}.{namespace}.svc.cluster.local {pod-name}.{subdomain}.{namespace}.svc.cluster.local // subdomain是在创建pod设定的属性,和hostname可以一起设置
  | 
 
StatefulSet
1 2
   | {pod-name}.{service-name}.{namespace}.svc.cluster.local 可以进入到pod中查看/etc/hosts
  | 
 
Service
1 2 3 4
   | {service-name}.{namespace}.svc.cluster.local
  服务例子: redis-service.redis.svc.cluster.local //redis-service 服务名 redis namespace
  |