开发 Tips(12)

2023年 1月 4日 33.4k 0

主要记录最近遇到的一些开发问题,解决方法。

1. Kubernetes 中给 Node 增加 Role: worker

1
2
3
4
kubectl get nodes
NAME         STATUS   ROLES    AGE    VERSION
i-6fns0nua   Ready    master   6d3h   v1.15.2
i-m69skuyd   Ready    <none>   6d2h   v1.15.2
1
2
kubectl label node i-m69skuyd  node-role.kubernetes.io/worker=
node/i-m69skuyd labeled
1
2
3
4
kubectl get node
NAME         STATUS   ROLES    AGE    VERSION
i-6fns0nua   Ready    master   6d3h   v1.15.2
i-m69skuyd   Ready    worker   6d2h   v1.15.2

2. 删除 Kubernetes 的一个节点

查看当前节点:

1
2
3
4
kubectl get node
NAME         STATUS   ROLES    AGE    VERSION
i-6fns0nua   Ready    master   6d3h   v1.15.2
i-m69skuyd   Ready    worker   6d2h   v1.15.2

迁移 Pod ,禁止调度待删除节点:

1
kubectl drain i-m69skuyd --delete-local-data --force --ignore-daemonsets

删除节点:

1
kubectl delete node i-m69skuyd

3. Kubernetes 上用 Helm 安装 Prometheus

首先需要创建 PV,可以参考第一章节。然后,执行命令:

1
2
3
4
5
helm install --namespace monitor --name prometheus stable/prometheus \
  --set alertmanager.persistentVolume.storageClass="local" \
  --set server.persistentVolume.storageClass="local"
helm install  --namespace monitor --name grafana stable/grafana \
  --set persistence.storageClassName="local"

其他操作可以参考,在 Minikube 集群上安装 Prometheus。

4. 如何重启 Kubernetes 中的 Pod 或服务

  • 重启 Pod
  • 如果 Pod 关联了副本控制器,直接删除 Pod,Kubernetes 会重新创建 Pod 。

    • 查看 Pod
    1
    
    kubectl get pod -n {NAMESPACE}
    
    • 删除 Pod
    1
    
    kubectl delete pod {POD_NAME} -n {NAMESPACE}
    

    另外一种是,使用 replace

    1
    
    kubectl replace --force -f pod.yaml
    

    如果没有 pod.yaml 文件,可以直接使用下面的命令:

    1
    
    kubectl get pod {POD_NAME} -n {NAMESPACE} -o yaml | kubectl replace --force -f -
    
  • 重启 Deployment
  • 将服务的副本数设置为 0,然后恢复原始的副本数。

    • 查看服务
    1
    
    kubectl get deployment -n {NAMESPACE}
    
    • 设置副本数为 0
    1
    
    kubectl scale deployment {DEPLOYMENT_NAME} --replicas=0 -n {NAMESPACE}
    
    • 恢复副本数量
    1
    
    kubectl scale deployment {DEPLOYMENT_NAME} --replicas={REPLICAS_NUM} -n {NAMESPACE}
    

    5. 通过 NodePort 的方式,暴露正在运行的服务

    在服务没有启动之前,通过修改 yaml 配置,可以暴露服务。当服务已经运行起来之后,可以通过 patch 的方式暴露服务。

    • 查看服务
    1
    
    kubectl get service -n {NAMESPACE}
    
    • 暴露端口访问
    1
    
    kubectl patch service {SERVICE_NAME} -p '{"spec":{"type":"NodePort"}}' -n {NAMESPACE}
    

    相关文章

    KubeSphere 部署向量数据库 Milvus 实战指南
    探索 Kubernetes 持久化存储之 Longhorn 初窥门径
    征服 Docker 镜像访问限制!KubeSphere v3.4.1 成功部署全攻略
    那些年在 Terraform 上吃到的糖和踩过的坑
    无需 Kubernetes 测试 Kubernetes 网络实现
    Kubernetes v1.31 中的移除和主要变更

    发布评论