k8s学习

To learn more about Kubernetes, here are a few advanced topics you can explore:

  1. Deployments and ReplicaSets: Learn how to manage stateless applications using Deployments and ReplicaSets.
  2. StatefulSets: Understand how to manage stateful applications.
  3. ConfigMaps and Secrets: Learn how to manage configuration data and sensitive information.
  4. Persistent Volumes and Persistent Volume Claims: Understand how to manage storage in Kubernetes.
  5. Ingress Controllers: Learn how to manage external access to services in a cluster.
  6. Helm: Explore Helm for managing Kubernetes applications.
  7. Custom Resource Definitions (CRDs): Learn how to extend Kubernetes capabilities.

1.Deployments and ReplicaSets: Learn how to manage stateless applications using Deployments and ReplicaSets.

1、安装 kubectl:

kubectl 是 Kubernetes 的命令行工具,用于与 Kubernetes 集群进行交互。

你可以通过以下命令安装 kubectl:

bash 复制代码
choco install kubernetes-cli

2、配置 kubectl:

确保 kubectl 已经配置好并指向你的 Kubernetes 集群。

你可以使用以下命令查看当前配置:

bash 复制代码
kubectl config view

3、创建一个简单的 Deployment:

创建一个 YAML 文件(例如 deployment.yaml)来定义一个 Deployment。(已经解决BUG1的版本)

示例内容如下:

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/control-plane"
        operator: "Exists"
        effect: "NoSchedule"
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

4、应用 Deployment:

使用 kubectl apply 命令来创建 Deployment:

bash 复制代码
kubectl apply -f deployment.yaml

5、查看 Pods:

使用以下命令查看创建的 Pods:

bash 复制代码
kubectl get pods

6、暴露 Deployment:

使用 kubectl expose 命令将 Deployment 暴露为一个服务:

bash 复制代码
kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80

7、查看服务:

使用以下命令查看服务:

bash 复制代码
kubectl get services

Scale the Deployment:

bash 复制代码
kubectl scale deployment/nginx-deployment --replicas=5

8、清理资源:

完成测试后,可以删除创建的资源:

bash 复制代码
kubectl delete service nginx-deployment
kubectl delete deployment nginx-deployment

BUG1: 1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane

bash 复制代码
Warning  FailedScheduling  69s (x2 over 6m9s) 
default-scheduler  0/1 nodes are available: 
1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }. 
preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.
bash 复制代码
根据 kubectl describe pod 的输出,
Pods 处于 Pending 状态的原因是没有可用的节点可以调度这些 Pods。
具体来说,所有节点都有一个 taint {node-role.kubernetes.io/control-plane: },
而这些 Pods 没有相应的 toleration 来容忍这个 taint。
bash 复制代码
什么是 Taint 和 Toleration?
Taint:
Taint 是一种机制,用于防止 Pods 被调度到某些节点上。
节点可以被标记为具有特定的 taint,
这样只有具有相应 toleration 的 Pods 才能被调度到这些节点上。

Toleration:
Toleration 是 Pods 的属性,用于声明它们可以容忍某些 taint,
从而允许它们被调度到具有这些 taint 的节点上。

问题的原因
根据 kubectl describe pod 的输出,你的 Pods 处于 Pending 状态,
因为没有可用的节点可以调度这些 Pods。
具体来说,所有节点都有一个 taint {node-role.kubernetes.io/control-plane: },
而这些 Pods 没有相应的 toleration 来容忍这个 taint。

你有两种选择来解决这个问题:

方法一:移除节点的 Taint

如果你希望在控制平面节点上运行 Pods,可以移除这个 taint。这样,Pods 就可以被调度到这些节点上。

bash 复制代码
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

方法二:为 Pods 添加 Toleration

如果你希望 Pods 能够容忍这个 taint,可以在 Deployment 的 YAML 文件中添加相应的 toleration。

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/control-plane"
        operator: "Exists"
        effect: "NoSchedule"
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

3、重新应用 Deployment:

更新 Deployment 文件后,重新应用它。

bash 复制代码
kubectl apply -f deployment.yaml

检查节点状态:

确保集群中有可用的节点,并且节点处于 Ready 状态。

bash 复制代码
kubectl get nodes

查看 Pod 的事件日志:

查看 Pod 的事件日志以获取更多详细信息。

bash 复制代码
kubectl describe pod <pod-name>

BUG2: nginx-deployment 的 EXTERNAL-IP 显示为 pending

bash 复制代码
[root@kube-master test_k8s]# kubectl get services

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE

kubernetes         ClusterIP      10.96.0.1       <none>        443/TCP        5d16h

nginx-deployment   LoadBalancer   10.97.157.228   <pending>     80:30956/TCP   6s

方法一:使用 NodePort 类型的服务

如果你的集群不支持 LoadBalancer 类型的服务,可以使用 NodePort 类型的服务。这样,你可以通过集群节点的 IP 地址和指定的端口访问服务。

1、删除现有的服务:

bash 复制代码
kubectl delete service nginx-deployment

2、创建 NodePort 类型的服务:

bash 复制代码
kubectl expose deployment nginx-deployment --type=NodePort --port=80

3、检查服务:

bash 复制代码
kubectl get services
bash 复制代码
[root@kube-master ~]# kubectl get services

NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE

kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP        5d23h

nginx-deployment   NodePort    10.100.180.176   <none>        80:32343/TCP   6h

2.StatefulSets: Understand how to manage stateful applications.

Sure! Let's analyze the core features of StatefulSets, focusing on stable identities and persistent storage, using the provided YAML configuration.

StatefulSet YAML

bash 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

Analysis

Stable Identities

bash 复制代码
StatefulSet: Manages the deployment and scaling of a set of Pods, 
and provides guarantees about the ordering and uniqueness of these Pods.
Pod Naming: Each Pod in a StatefulSet gets a unique, 
stable network identity. The Pods are named with a predictable pattern: 
$(statefulset name)-$(ordinal). 
For example, the Pods will be named web-0, web-1, web-2.
bash 复制代码
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 3
bash 复制代码
serviceName: The name of the headless service 
that controls the network identity of the Pods.
replicas: The number of desired Pods. In this case, 3 replicas will be created.

Persistent Storage

bash 复制代码
volumeClaimTemplates: 
Defines the PersistentVolumeClaims (PVCs) for the StatefulSet. 
Each Pod in the StatefulSet will get its own PVC, 
ensuring that each Pod has its own persistent storage.
bash 复制代码
volumeClaimTemplates:
- metadata:
    name: www
  spec:
    accessModes: [ "ReadWriteOnce" ]
    resources:
      requests:
        storage: 1Gi
bash 复制代码
metadata.name: The name of the PVC. 
Each Pod will get a PVC with a unique name based on this template 
(e.g., www-web-0, www-web-1, www-web-2).

accessModes: Specifies the access mode for the volume. 
ReadWriteOnce means the volume can be mounted as read-write by a single node.

resources.requests.storage: Specifies the amount of storage requested for each PVC.
In this case, each PVC will request 1Gi of storage.

Summary

bash 复制代码
Stable Identities: Each Pod in the StatefulSet has a unique,
stable network identity, 
which is crucial for stateful applications that require stable network identities.

Persistent Storage: Each Pod gets its own PersistentVolumeClaim,
ensuring that data is not lost when Pods are rescheduled. 
This is essential for stateful applications that require persistent storage.
相关推荐
Chef_Chen4 分钟前
从0开始学习机器学习--Day19--学习曲线
人工智能·学习·机器学习
追梦不止~1 小时前
Docker常用命令+详解
运维·docker·容器
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
GJCTYU2 小时前
阿里云多端低代码开发平台魔笔使用测评
低代码·阿里云·云原生·容器·serverless·云计算
铁锤妹妹头发多2 小时前
新手用docker真**难受
运维·docker·容器
infiniteWei2 小时前
【Lucene】原理学习路线
学习·搜索引擎·全文检索·lucene
follycat3 小时前
[极客大挑战 2019]PHP 1
开发语言·学习·网络安全·php
并不会7 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
龙鸣丿7 小时前
Linux基础学习笔记
linux·笔记·学习
Nu11PointerException9 小时前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习