Skip to content

Kubernetes

https://minikube.sigs.k8s.io/docs/start/?arch=%2Fwindows%2Fx86-64%2Fstable%2F.exe+download

helm

Kubernetes (k8s) 的​​包管理工具​

helm search hub xxxx
  • 拉取镜像
helm pull oci://ghcr.io/stefanprodan/charts/podinfo --version 6.11.2

containerd

设置代理

1. 为 containerd 配置代理

sudo tee /etc/systemd/system/containerd.service.d/proxy.conf <<'EOF'
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1,192.168.3.140,10.96.0.0/12,10.244.0.0/16,.svc,.cluster.local"
Environment="no_proxy=localhost,127.0.0.1,192.168.3.140,10.96.0.0/12,10.244.0.0/16,.svc,.cluster.local"
EOF

2. 重载并重启 containerd

sudo systemctl daemon-reload
sudo systemctl restart containerd

3. 验证代理是否生效

sudo systemctl show containerd --property Environment

应该能看到 HTTP_PROXY、HTTPS_PROXY 已经加载进去了。

4. 再次执行 kubeadm init

sudo kubeadm init --kubernetes-version=v1.36.4
  • 拉取镜像
sudo crictl pull nginx:1.25

kubectl

# 绑定到所有网络接口,局域网内其他机器可通过你的 IP 访问
kubectl port-forward svc/<service-name> 8080:80 --address 0.0.0.0 -n <namespace>

::: warning

所有kubctl 操作都在主节点进行

:::

volumes

使用卷时, 在 .spec.volumes 字段中设置为 Pod 提供的卷,并在 .spec.containers[*].volumeMounts 字段中声明卷在容器中的挂载位置。

configmap

其他

  • 查看可以使用的kind

kubectl api-resources

pods

  • 查看日志

kubectl describe pod prometheus-server-5b6649bf46-nw2qr -n monitor-sa
* 查看pods
kubectl get pods -n monitor-sa -o wide

ingress

kubectl get ingress

kubectl get ingressclass

API

from kubernetes import client, config
from kubernetes.client.rest import ApiException

def update_configmap(namespace, name, data):
    """
    更新指定 namespace 下的 ConfigMap 的 data 字段
    :param namespace: 命名空间
    :param name: ConfigMap 名称
    :param data: dict 格式,key 是文件名,value 是内容字符串
    """
    # 加载 kubeconfig 配置(集群外)或者集群内环境自动加载
    try:
        config.load_kube_config(config_file="/etc/rancher/k3s/k3s.yaml")  # 如果脚本运行在集群外用这个
    except:
        config.load_incluster_config()  # 集群内执行

    v1 = client.CoreV1Api()

    try:
        # 先读取当前 ConfigMap
        cm = v1.read_namespaced_config_map(name, namespace)
        # 更新 data
        cm.data = data
        # 发送更新请求
        v1.replace_namespaced_config_map(name, namespace, cm)
        print(f"ConfigMap '{name}' 在命名空间 '{namespace}' 更新成功。")
    except ApiException as e:
        print(f"更新 ConfigMap 失败: {e}")

if __name__ == "__main__":
    namespace = "default"
    cm_name = "nginx-config"
    new_data = {
        "default.conf": """
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 80;

    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_ignore_headers Cache-Control;

        proxy_pass http://catpd.cn;
        proxy_set_header Host $host;

        add_header X-Cache-Status $upstream_cache_status;
        add_header X-Pod-Name $hostname;
    }
}
"""
    }

    update_configmap(namespace, cm_name, new_data)

ConfigMaps

kubectl get configmaps --all-namespaces

kubectl get configmap nginx-config -n default -o yaml

DaemonSet

保证每个符合调度条件的节点都运行一个 Pod * 重启 kubectl rollout restart daemonset/nginx-ds-1

tools

crictl image ls

一个通用的 CLI 工具,用于调试 Kubernetes 中使用的任何 CRI(Container Runtime Interface)兼容的容器运行时

ctr image ls

ctr images import

kubectl config view | grep server

nodes

kubectl get nodes -o wide

cat /var/lib/rancher/k3s/server/node-token


curl -sfL https://get.k3s.io | K3S_URL=https://<MASTER_IP>:6443 K3S_TOKEN=<TOKEN> sh -s - --node-name=<NODE_NAME>

kubectl label node <NODE_NAME> node-role.kubernetes.io/worker=worker

deploymnes

  • 创建注释 需要更新完成后手动添加
    kubectl.exe annotate deployment nginx-deployment kubernetes.io/change-cause="测试注释"
    
  • 版本更新
kubectl set image deployment/nginx-deployment nginx=nginx:1.161
  • 版本回退
kubectl rollout undo deployment/nginx-deployment --to-revision=2
  • pods缩放
kubectl.exe scale deployments/nginx-deployment --replicas=10
  • 暂停上线

Namespace

主要用于​​资源隔离、权限管理和组织分类​​

同一集群中运行多个环境(开发、测试、预发布、生产)。

kubectl create namespace dev
kubectl create namespace prod
  • 访问服务 minikube.exe service hello-minikube

kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.39 -- /agnhost netexec --http-port=8080

service

::: warning

如果你正在使用 Docker Desktop 作为容器驱动来运行 minikube,需要使用 minikube 隧道。这是因为 Docker Desktop 内部的容器和宿主机是隔离的。 :::

kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

  • powershell 测试
1..10 | ForEach-Object {
    $response = curl http://127.0.0.1:58949
    Write-Output "$response"
}

执行

kubectl exec -it $POD_NAME -- bash

delete

kubectl delete service hello-node kubectl delete deployment hello-node

kubectl delte -f [yaml配置文件]

SSL

challenges

证书验证

curl http://dns.stackx.im/.well-known/acme-challenge/test

kubectl get challenges -A kubectl describe certificate cdn-cert -n default

certificate

kubectl get certificate

kubectl describe certificate cdn-cert -n default

kubectl describe clusterissuer letsencrypt-prod

kubectl describe challenge cdn-cert-1-3125132314-581949906 -n default

curl -v http://cdn.vpsdl.top/.well-known/acme-challenge/testfile