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.
相关推荐
楠了个难14 分钟前
HDMI色块移动——FPGA学习笔记13
笔记·学习
dawnsky.liu17 分钟前
红帽 Quay- 配置镜像代理缓存
kubernetes·openshift
dxgzg1 小时前
ps学习。
学习
Sunsets_Red1 小时前
Linux 系统
linux·运维·服务器·c++·学习·系统架构·系统安全
mljy.2 小时前
STL简介
c++·学习
会蹦的鱼3 小时前
React学习day07-ReactRouter-抽象路由模块、路由导航、路由导航传参、嵌套路由、默认二级路由的设置、两种路由模式
javascript·学习·react.js
深蓝海拓8 小时前
迭代器和生成器的学习笔记
笔记·python·学习
Richardlygo9 小时前
(k8s)kubernetes集群基于Containerd部署
云原生·容器·kubernetes
小小的木头人10 小时前
Docker vs. containerd 深度剖析容器运行时
运维·docker·容器
weixin_4432906911 小时前
【Docker】安装及使用
docker·容器·eureka