Post

Kubernetes Manager

helm

命令警告

1
2
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /Users/cc/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /Users/cc/.kube/config
  • 处理方法

    1
    2
    
      chmod g-rw ~/.kube/config
      chmod o-r ~/.kube/config
    

repo

1
2
3
4
5
6
7
8
9
helm repo add ind-games --username username --password password url # 添加库
helm repo list # 查看repo列表
helm repo update # 更新全部
helm repo update ind-games # 更新指定 (推荐)
helm search repo ind-games # 搜索全部
helm search repo ind-games | grep bonus-texas # 搜索筛选指定
helm cm-push bonus-texas-0.0.21.tgz ind-games # 推送压缩包
helm cm-push <dir> ind-games # 推送文件
helm cm-push . ind-games # 推送当前文件

plugin (插件管理)

1
helm plugin install https://github.com/chartmuseum/helm-push

Chart

1
2
3
4
helm lint # 查看chart语法是否正确
helm template --debug # 本地渲染
helm install bonus-texas . --dry-run --debug # 服务器渲染模板的好方法,然后返回生成的清单文件
helm list -n games # 已部署的list

Applistion Manger

1
2
3
4
5
6
7
8
9
10
11
helm pull ind-games/bonus-texas # pull 压缩包
helm pull ind-games/bonus-texas --untar # pull 并且解压

helm install <dir> -n games # install
helm uninstall bonus-texas -n game # uninstall

# 更新应用
helm upgrade -i bonus-texas ind-games/bonus-texas -f games.yaml -n games
helm upgrade -i bonus-texas <dir> -f games.yaml -n games
helm upgrade -i bonus-texas . -f games.yaml -n games

kubeclt

  • 关键字说明

    • resource-type
      • pod
      • deployment
      • service
    • service-type
      • ClusterIP
      • NodePort
      • LoadBalancer
      • ExternalName
  • 安装 kubectl
  • 登录到 Kubernetes 集群中

    • 获取 cat /root/kuber/config
    • 放入到目标机器的 ~/.kube/config
  • 查看版本

    1
    2
    
    kubectl version
    kubectl get nodes
    
  • 设置环境
1
  export KUBECONFIG="/etc/kubernetes/admin.conf"
  • 多环境设置

    • 注意配置(考虑到每个都会是 kubernetes.admin,需要做修改)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
          server: https://172.13.5.11:6443
        name: ind-dev
      contexts:
      - context:
          cluster: ind-dev
          user: ind-dev
        name: ind-dev
      current-context: ind-dev
      kind: Config
      preferences: {}
      users:
      - name: ind-dev
        user:
      
    • KUBECONFIG=indonesia_dev:indonesia_dev_local_test kubectl config view --flatten > config
    • 把生成文件替换 ~/.kube/config

基本命令

kubectl config

1
2
3
4
5
6
7
kubectl config get-contexts # 列出所有上下文
kubectl config use-context <context-name> # 切换上下文
kubectl config view # 验证配置
 # 创建新的上下文
kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name> --namespace=<namespace>
# 配置集群
kubectl config set-cluster <cluster-name> --server=<api-server-url> --certificate-authority=<ca-file>

kubectl get

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
31
32
33
# pod
kubectl get pods -A
kubectl get pods -n <namespace>
kubectl get pods -n <namespace> -o wide <pod>
# services
kubectl get services
kubectl get service -n <namespace>
kubectl get service bonus-texas -n games

# deployments
kubectl get deployments
kubectl get deployments -n <namespace>

# node
kubectl get nodes

# configmaps
kubectl get configmaps
kubectl get configmaps -n <namespace>

# Secrets
kubectl get secrets
kubectl get secrets  -n <namespace>

# Persistent Volumes (pv)
kubectl get pv

# other
kubectl get pods,deployment,replicaset

# 查询关联后的节点
kubectl get endpoints

kubectl port-forward(转发)

1
kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017

kubectl log

1
2
3
kubectl logs ferry-6cb8d5697c-tdbzc -n games
kubectl logs -f ferry-6cb8d5697c-tdbzc -n games
kubectl logs -f ferry-6cb8d5697c-tdbzc --tail= 10 -n games

kubectl rollout

1
2
3
4
5
kubectl rollout history deployment/ferry -n common # 查看历史记录
kubectl rollout undo deployment/ferry -n common  # 回滚到上次部署
kubectl rollout undo deployment/ferry --to-revision=2 -n common # 回滚到指定版本
kubectl rollout status -w deployment/ferry -n common # 查看是否会滚成功
kubectl rollout restart deployment/ferry -n common # 轮替重启

kubectl scale (伸缩)

1
2
3
4
5
kubectl scale --replicas=2 deployment/ferry -n common # 伸缩到几个副本
# 当前的一个副本修改成1个副本
kubectl scale --current-replicas=2 --replicas=1 deployment/ferry -n common
kubectl scale --replicas=3 -f ferry.yaml # 文件修改
kubectl scale --replicas=5 rc/a rc/b rc/c # 多个控制

kubectl top

1
kubectl top pod -n games # 查看资源使用状况

kubectl describe

1
2
kubectl describe pod ferry-6cb8d5697c-tdbzc -n common # 查看pods详情
kubectl describe configmap hall-customize -n huoys | more # 查看配置

kubectl exec

kubectl exec -it <pod-name> -- <command> [flags]

1
2
3
4
5
kubectl exec -it my-pod -- /bin/bash
kubectl exec -it my-pod -- cat /var/log/my-log.log
kubectl exec -it my-pod --container=my-container -- /bin/bash
# 在特定命名空间中执行命令
kubectl exec -it my-pod -n my-namespace -- /bin/bash

kubectl set

1
2
3
4
5
6
7
8
9
10
11
12
# 设置镜像
kubectl set image deployment/my-deployment my-container=my-image:v2
# 设置环境变量
kubectl set env deployment/my-deployment ENV_VARIABLE=value
# 设置标签
kubectl set labels pod/my-pod new-label=value
# 设置资源限制
kubectl set resources deployment/my-deployment --limits=cpu=1,memory=512Mi
# 设置镜像仓库凭证
kubectl set image-credentials secret/my-secret registry=https://registry.example.com
# 设置部署策略
kubectl set strategy deployment/my-deployment rolling

kubeclt expose

  • --name=<service-name>:服务名字
  • --port=<port>:服务的端口
  • --target-port=<target-port>:要映射到后端 Pod 的目标端口
1
2
3
4
5
kubectl expose <resource-type> <resource-name> --type=<service-type> --name=<service-name> --port=<port> --target-port=<target-port>
kubectl expose deployment my-nginx --type=ClusterIP --name=my-nginx-service --port=80 --target-port=80
kubectl expose pod my-app --type=NodePort --name=my-app-service --port=8080 --target-port=80
kubectl expose deployment my-nginx --type=LoadBalancer --name=my-nginx-service --port=80 --target-port=80
kubectl expose pod my-pod --type=ClusterIP --name=my-headless-service --cluster-ip=None

kubectl edit

编辑前先备份好

1
kubectl edit <resource-type> <resource-name>

kubectl craete

  • my-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    
    apiVersion: v1
    kind: Pod
    metadata:
    name: my-pod
    spec:
    containers:
      - name: nginx
        image: nginx
    
1
2
3
4
5
6
7
8
  kubectl create <resource-type> <resource-name> [flags]
  kubectl create pod my-pod --image=nginx
  kubectl create service clusterip my-service --tcp=80:80 --pod=my-pod
  kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
  kubectl create namespace my-namespace
  kubectl create deployment my-deployment --image=nginx --replicas=3
  # 使用 from-file 创建 可以是文件和目录
  kubectl create configmap configmap-name --from-file=configmap*.yaml

kubectl label

kubectl label <resource-type> <resource-name> <label-key>=<label-value> [flags]

1
2
3
4
5
6
7
8
9
10
# 添加
kubectl label pod my-pod environment=development
kubectl label node my-node zone=us-west-1
kubectl label service my-service app=my-app
# 覆盖
kubectl label pod my-pod environment=production --overwrite
# 删除
kubectl label pod my-pod environment-
# 查看
kubectl get pods --show-labels

kubectl replace

kubectl replace -f <configuration-file>

  • 可能中断服务
  • 会删除现有的创建新的
  • my-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
    spec:
      containers:
        - name: nginx
          image: nginx:1.18
    
1
  kubectl replace -f my-pod.yaml

kubectl patch

对 Kubernetes 资源进行部分更新 kubectl patch <resource-type> <resource-name> [flags]

1
2
3
4
kubectl patch pod my-pod -p '{"metadata":{"labels":{"app":"my-app"}}}'
kubectl patch deployment my-deployment -p '{"spec":{"replicas":3}}'
kubectl patch service my-service -p '{"spec":{"ports":[{"port":8080}]}}'
kubectl patch configmap my-configmap -p '{"data":{"key3":"value3"}}'

调度

1
2
3
kubectl cordon node-name # 将 node-name 节点设置为不可调度模式
kubectl drain node-name # 将当前运行的 node-name 节点上容器驱离
kubectl uncordon node-name # 执行完维护后,将节点重新加入调度

创建、删除、运行

1
2
3
kubectl run <name> --image=<container-image>
kubectl apply -f <configuration-file>
kubectl delete <resource-type> <resource-name>

Kubernetes 工作原理

参考地址

kubectl 使用介绍

官方文档

官方文档备忘清单

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[root@idn ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create        创建资源
  expose        将控制器、服务、部署或pod暴露未新的Kubernetes服务
  run           在集群上运行一个特定的镜像
  set           设置对象的特定功能

Basic Commands (Intermediate):
  explain       Documentation of resources
  get           显示一个活多个资源
  edit          编辑服务的资源
  delete        按照文件名、资源名和名称资源、标签选择器删除资源

Deploy Commands:
  rollout       管理资源推出
  scale         为deployment、replicaset controller 设置新的大小
  autoscale     自动缩放 Deployment 、replicaset 或 Replication Controller

Cluster Management Commands:
  certificate   修改证书资源
  cluster-info  显示集群信息
  top           显示资源(cpu/内存/储存)使用情况
  cordon        警戒线将节点标记为不可调度
  uncordon      将节点标记为可调度
  drain         移除节点,准备维护;容器驱离
  taint         更新一个或多个节点上污点

Troubleshooting and Debugging Commands:
  describe      显示特定资源或资源组的详细信息
  logs          打印pod中容器的日志
  attach        附加到正在运行的容器
  exec          在容器中执行命令
  port-forward  将一个或多个本地端口转发到一个pod
  proxy         运行一个到Kubernetes Api 服务代理
  cp            在容器之间复制文件和目录
  auth          检查授权

Advanced Commands:
  diff          将要应用的版本的Diff实时版本
  apply         按照文件名或者目录标准输入将配置应用到资源
  patch         补丁修改,更新资源字段
  replace       使用配置文件或stdin 来替换资源
  wait          在一个或多个资源上等待一个条件(不常用)
  convert       在不同的API版本之间转换配置文件
  kustomize     从目录或远程url 构建 kustomization 目标

Settings Commands:
  label         更新资源上的标签
  annotate      更新资源上的注释
  completion    为指定的shell(bash OR zsh) 输出完全代码

Other Commands:
  alpha         Commands for features in alpha
  api-resources 在服务器上打印支持的API资源
  api-versions  在服务端上打印支持API版本,以‘组/版本’的形式
  config        修改 Kubernetes 文件
  plugin        提供与插件交互的使用程序
  version       打印客户端与服务端的版本信息

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

node

在生产过程中会遇到服务资源不足情况,需要增加新的服务器对应用系统进行水平扩展以实现扩容,在 Kubernetes 中加一个 node 只需要在 node 节点上安装 docker 、kubelet 、kube-proxy 服务,给 kubelet 和 kube-proxy 启动启动参数重 master rul 指定 Kubernetes 集群 master 地址。kubelet 会自动注册机制,新的 node 会自动加入到 Kubernetes 集群中 image-20211124155709295

image-20211124155829434

This post is licensed under CC BY 4.0 by the author.

© Cc. Some rights reserved.

Using the Chirpy theme for Jekyll.