To learn more about Kubernetes, here are a few advanced topics you can explore:
kubectl 是 Kubernetes 的命令行工具,用于与 Kubernetes 集群进行交互。
你可以通过以下命令安装 kubectl:
choco install kubernetes-cli
确保 kubectl 已经配置好并指向你的 Kubernetes 集群。
你可以使用以下命令查看当前配置:
kubectl config view
创建一个 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
使用 kubectl apply 命令来创建 Deployment:
kubectl apply -f deployment.yaml
使用以下命令查看创建的 Pods:
kubectl get pods
使用 kubectl expose 命令将 Deployment 暴露为一个服务:
kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80
使用以下命令查看服务:
kubectl get services
kubectl scale deployment/nginx-deployment --replicas=5
完成测试后,可以删除创建的资源:
kubectl delete service nginx-deployment
kubectl delete deployment nginx-deployment
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。
你有两种选择来解决这个问题:
如果你希望在控制平面节点上运行 Pods,可以移除这个 taint。这样,Pods 就可以被调度到这些节点上。
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
如果你希望 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 的事件日志以获取更多详细信息。
kubectl describe pod <pod-name>
[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
如果你的集群不支持 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
Sure! Let’s analyze the core features of StatefulSets, focusing on stable identities and persistent storage, using the provided YAML configuration.
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
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.
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.
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.