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:

choco install kubernetes-cli

2、配置 kubectl:

确保 kubectl 已经配置好并指向你的 Kubernetes 集群。
你可以使用以下命令查看当前配置:

kubectl config view

3、创建一个简单的 Deployment:

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

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:

kubectl apply -f deployment.yaml

5、查看 Pods:

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

kubectl get pods

6、暴露 Deployment:

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

kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80

7、查看服务:

使用以下命令查看服务:

kubectl get services

Scale the Deployment:

kubectl scale deployment/nginx-deployment --replicas=5

8、清理资源:

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

kubectl delete service nginx-deployment
kubectl delete deployment nginx-deployment

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

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.
根据 kubectl describe pod 的输出,
Pods 处于 Pending 状态的原因是没有可用的节点可以调度这些 Pods。
具体来说,所有节点都有一个 taint {node-role.kubernetes.io/control-plane: },
而这些 Pods 没有相应的 toleration 来容忍这个 taint。
什么是 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 就可以被调度到这些节点上。

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

方法二:为 Pods 添加 Toleration

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

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 文件后,重新应用它。

kubectl apply -f deployment.yaml

检查节点状态:

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

kubectl get nodes

查看 Pod 的事件日志:

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

kubectl describe pod <pod-name>

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

[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、删除现有的服务:

kubectl delete service nginx-deployment

2、创建 NodePort 类型的服务:

kubectl expose deployment nginx-deployment --type=NodePort --port=80

3、检查服务:

kubectl get services
[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

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

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.
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 3
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

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.
volumeClaimTemplates:
- metadata:
    name: www
  spec:
    accessModes: [ "ReadWriteOnce" ]
    resources:
      requests:
        storage: 1Gi
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

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.

你可能感兴趣的:(kubernetes,学习,容器)