diff --git a/Containerd/Docs/Ctr基础命令.md b/Containerd/Docs/Ctr基础命令.md index 870d0aa..4d47137 100644 --- a/Containerd/Docs/Ctr基础命令.md +++ b/Containerd/Docs/Ctr基础命令.md @@ -22,10 +22,18 @@ **更多参数** - - `--hosts-dir "/etc/containerd/certs.d"`:指定了包含镜像仓库证书的目录的路径。 + ```bash + ctr --debug=true i pull --hosts-dir=/etc/containerd/certs.d docker.io/library/nginx:latest + ``` - - `-k`:忽略 TLS 验证过程中的证书错误。 - - `--plain-http=true`:此选项指明在拉取镜像时使用未加密的 HTTP 协议,而不是加密的 HTTPS。 + | 参数部分 | 说明 | 详细解释 | + | :-------------------------------------: | :--: | :----------------------------------------------------------: | + | **--debug=true** | | - 启用详细日志输出 - 显示执行过程中的详细信息 - 便于故障排查 | + | **i** | | `images` 的缩写,表示进行镜像相关操作 | + | **pull** | | 拉取(下载)镜像到本地 | + | **--hosts-dir=/etc/containerd/certs.d** | | - 指定证书和注册表配置目录 - 用于配置私有注册表或镜像仓库的认证信息 - 可以覆盖默认的 registry 配置 | + | -k | | 忽略 TLS 验证过程中的证书错误 | + | --plain-http=true | | 此选项指明在拉取镜像时使用未加密的 HTTP 协议,而不是加密的 HTTPS | - 推送镜像 @@ -33,6 +41,4 @@ ctr -n k8s.io image push -u <账户>:<密码> docker.io/library/nginx:latest ``` - **更多参数** - - - `--plain-http=true`:此选项指明在拉取镜像时使用未加密的 HTTP 协议,而不是加密的 HTTPS。 \ No newline at end of file + \ No newline at end of file diff --git a/Docker/Docs/Docker使用文档/Docker镜像批量打包.md b/Docker/Docs/Docker使用文档/Docker镜像打包.md similarity index 50% rename from Docker/Docs/Docker使用文档/Docker镜像批量打包.md rename to Docker/Docs/Docker使用文档/Docker镜像打包.md index bcf93d3..31617a2 100644 --- a/Docker/Docs/Docker使用文档/Docker镜像批量打包.md +++ b/Docker/Docs/Docker使用文档/Docker镜像打包.md @@ -1,6 +1,36 @@ > 本文作者:丁辉 -# Docker镜像批量打包 +# Docker镜像打包 + +## 默认打包 + +1. 打包 + + ```bash + docker save -o image.tar nginx:latest + ``` + +2. 导入 + + ```bash + docker load -i image.tar + ``` + +## 打包并压缩 + +1. 打包并压缩 + + ```bash + docker save nginx:latest | gzip -c > image.tar.gz + ``` + +2. 解压并导入 + + ```bash + gunzip -c image.tar.gz | docker load + ``` + +## 批量打包 - 第一种 diff --git a/File/Conf/k8s.conf b/File/Conf/k8s.conf index 958b175..b1e7929 100644 --- a/File/Conf/k8s.conf +++ b/File/Conf/k8s.conf @@ -1,4 +1,6 @@ net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 +net.ipv6.conf.all.disable_ipv6 = 1 +net.ipv6.conf.default.disable_ipv6 = 1 net.ipv4.ip_forward = 1 -vm.swappiness = 0 \ No newline at end of file +vm.swappiness = 0 diff --git a/File/Yaml/ingress-values-hostnetwork.yaml b/File/Yaml/ingress-values-hostnetwork.yaml new file mode 100644 index 0000000..345c117 --- /dev/null +++ b/File/Yaml/ingress-values-hostnetwork.yaml @@ -0,0 +1,67 @@ +controller: + # 开启监控 + metrics: + enabled: true + serviceMonitor: + enabled: false + + # 配置镜像加速 + image: + registry: k8s.mirror.nju.edu.cn + + # 使用主机网络时,利用 Kubernetes 集群内的 DNS 解析服务 + dnsPolicy: ClusterFirstWithHostNet + + # 使用本地网络 + hostNetwork: true + + # Pod 使用 DaemonSet 方式运行 Deployment 或 DaemonSet + kind: DaemonSet + + # 只允许调度到具有 ingress="true" 的节点上,[ kubectl label node xxx ingress="true" ] + nodeSelector: + kubernetes.io/os: linux + ingress: "true" + + # 禁用后状态字段会报告 Ingress 控制器 Pod 所在节点的 IP 地址或节点列表的 IP 地址 + publishService: + enabled: false + + # 启用 Kubernetes Service + service: + enabled: false + + # 配置镜像加速 + admissionWebhooks: + patch: + image: + registry: k8s.mirror.nju.edu.cn + + # 设置为集群默认 ingress 控制器 + ingressClassResource: + default: true + + # 配置资源限制 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 1000m + memory: 1Gi + + # 配置 Pod 安全上下文 + podSecurityContext: + fsGroup: 101 + runAsUser: 101 + + # 配置安全上下文 + containerSecurityContext: + allowPrivilegeEscalation: false # 禁止特权提升 + capabilities: + add: ["NET_BIND_SERVICE"] + drop: + - ALL # 去除所有能力 + runAsNonRoot: true # 以非 root 用户运行 + seccompProfile: + type: RuntimeDefault # 启用默认系统调用过滤 \ No newline at end of file diff --git a/File/Yaml/ingress-values-loadbalancer.yaml b/File/Yaml/ingress-values-loadbalancer.yaml new file mode 100644 index 0000000..c3883cd --- /dev/null +++ b/File/Yaml/ingress-values-loadbalancer.yaml @@ -0,0 +1,81 @@ +# 未完成的 YAML 配置文件片段 +controller: + # 开启监控 + metrics: + enabled: true + serviceMonitor: + enabled: false + + # 配置镜像加速 + image: + registry: k8s.mirror.nju.edu.cn + + # Pod 使用 DaemonSet 方式运行 Deployment 或 DaemonSet + kind: Deployment + # 副本数 + replicaCount: 2 + + # 只允许调度到具有 ingress="true" 的节点上,[ kubectl label node xxx ingress="true" ] + nodeSelector: + kubernetes.io/os: linux + ingress: "true" + + # 禁用后状态字段会报告 Ingress 控制器 Pod 所在节点的 IP 地址或节点列表的 IP 地址 + publishService: + enabled: false + + # 配置镜像加速 + admissionWebhooks: + patch: + image: + registry: k8s.mirror.nju.edu.cn + + # 设置为集群默认 ingress 控制器 + ingressClassResource: + default: true + + # 配置资源限制 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 1000m + memory: 1Gi + + # 配置 Pod 安全上下文 + podSecurityContext: + fsGroup: 101 + runAsUser: 101 + + # 配置安全上下文 + containerSecurityContext: + allowPrivilegeEscalation: false # 禁止特权提升 + capabilities: + add: ["NET_BIND_SERVICE"] + drop: + - ALL # 去除所有能力 + runAsNonRoot: true # 以非 root 用户运行 + seccompProfile: + type: RuntimeDefault # 启用默认系统调用过滤 + + service: + type: LoadBalancer + # 在这里指定你的浮动 IP + loadBalancerIP: "" + externalIPs: + - "" + # 根据你的环境,可能不需要额外的注解,或者可能需要设置特定的注解 + annotations: + # 如果你的环境需要,可以添加以下注解(例如,使用 MetalLB) + # metallb.universe.tf/address-pool: default + # 或者,如果你使用 keepalived,可能需要以下注解来避免外部负载均衡器提供商尝试分配 IP + # 例如,在 OpenStack 中,你可以指定使用 keepalived 的 IP + # 注意:具体注解取决于你的环境和云提供商集成,如果没有特定的集成,可能不需要注解。 + # 对于裸机 keepalived,通常只需要指定 loadBalancerIP 即可。 + + # 其他服务配置,如端口 + ports: + http: 80 + https: 443 + externalTrafficPolicy: Local \ No newline at end of file diff --git a/File/Yaml/istio-config.yaml b/File/Yaml/istio-config.yaml new file mode 100644 index 0000000..42f88f9 --- /dev/null +++ b/File/Yaml/istio-config.yaml @@ -0,0 +1,33 @@ +apiVersion: install.istio.io/v1alpha1 +kind: IstioOperator +metadata: + namespace: istio-system +spec: + hub: docker.io/istio + tag: 1.28.1 + + components: + base: + enabled: true # Istio 基础组件 + pilot: + enabled: true # Istio 控制平面(Istiod) + # 进出口流量启用 + ingressGateways: + - name: istio-ingressgateway + enabled: true + egressGateways: + - name: istio-egressgateway + enabled: false + + # Most default values come from the helm chart's values.yaml + # Below are the things that differ + values: + defaultRevision: "" # 控制 sidecar 注入的默认版本 + global: + istioNamespace: istio-system # Istio 安装的命名空间 + configValidation: true # 启用配置验证 + gateways: + istio-ingressgateway: {} + istio-egressgateway: {} + ztunnel: + resourceName: ztunnel # 为 Ztunnel DaemonSet 指定名称 \ No newline at end of file diff --git a/Helm/Helm部署Nginx-Ingress.md b/Helm/Helm部署Nginx-Ingress.md index fae0b2d..7b9bca3 100644 --- a/Helm/Helm部署Nginx-Ingress.md +++ b/Helm/Helm部署Nginx-Ingress.md @@ -18,58 +18,13 @@ helm repo update ``` -2. 编写模版文件 +2. 下载模版文件 ```bash - vi ingress-values.yaml + wget https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/ingress-values-hostnetwork.yaml ``` - 内容如下 - - ```yaml - controller: - # 开启监控 - metrics: - enabled: true - serviceMonitor: - enabled: true - - # 配置镜像加速 - image: - registry: k8s.mirror.nju.edu.cn - - # 使用主机网络时,利用 Kubernetes 集群内的 DNS 解析服务 - dnsPolicy: ClusterFirstWithHostNet - - # 使用本地网络 - hostNetwork: true - - # Pod 使用 DaemonSet 方式运行 - kind: DaemonSet - - # 只允许调度到具有 ingress="true" 的节点上,[ kubectl label node xxx ingress="true" ] - nodeSelector: - kubernetes.io/os: linux - ingress: "true" - - # 禁用后状态字段会报告 Ingress 控制器 Pod 所在节点的 IP 地址或节点列表的 IP 地址 - publishService: - enabled: false - - # 启用 Kubernetes Service - service: - enabled: false - - # 配置镜像加速 - admissionWebhooks: - patch: - image: - registry: k8s.mirror.nju.edu.cn - - # 设置为集群默认 ingress 控制器 - ingressClassResource: - default: true - ``` + **修改参数并保存** 3. 配置节点标签 @@ -82,7 +37,7 @@ ```bash helm install ingress-nginx \ ingress-nginx/ingress-nginx \ - -f ingress-values.yaml + -f ingress-values-hostnetwork.yaml ``` ## 卸载 diff --git a/网关/Ingress/创建Ingress资源提供访问.md b/网关/Ingress/创建Ingress资源提供访问.md index 6beee5f..d0c0ec2 100644 --- a/网关/Ingress/创建Ingress资源提供访问.md +++ b/网关/Ingress/创建Ingress资源提供访问.md @@ -4,13 +4,18 @@ ## 准备工作 -- Nginx Deployment +1. 部署 Nginx 资源 -- Nginx Service: + ```yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment.yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-svc.yaml + ``` - Name: nginx-service +2. 查看部署情况 - Port: 80 + ```bash + kubectl get deploy,svc + ``` ## 示例 @@ -22,40 +27,70 @@ 2. 创建 Ingress 资源 - ```yaml - cat < 本文作者:丁辉 + +# Istio重定向HTTP为HTTPS + +## 部署基础服务 + +1. 部署 Nginx 资源 + + ```yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment.yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-svc.yaml + ``` + +2. 查看部署情况 + + ```bash + kubectl get deploy,svc + ``` + +## 配置 Istio 对外访问 + +1. 部署 Nginx Gateway 资源 + + ```yaml + cat < 本文作者:丁辉 + +# 使用Istio开启对外访问 + +## Istio开启对外访问 + +### 部署基础服务 + +1. 部署 Nginx 资源 + + ```yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment.yaml + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-svc.yaml + ``` + +2. 查看部署情况 + + ```bash + kubectl get deploy,svc + ``` + +### 配置 Istio 对外访问 + +1. 部署 Nginx Gateway 资源 + + - HTTP + + ```yaml + cat < 本文作者:丁辉 + +# 官方Istio使用示例 + +[Bookinfo 应用](https://istio.io/latest/zh/docs/examples/bookinfo/) + +## 前提条件 + +如果您还没有开始,请遵循 [Istio安装和使用](https://gitee.com/offends/Kubernetes/blob/main/%E7%BD%91%E5%85%B3/Istio/Istio%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8.md) 完成 Istio 的部署工作。 + +本文使用 Istio 官方推荐的 Gateway API 部署举例。 + +## 部署 Bookinfo + + + +1. 进入示例目录 + + ```bash + cd istio-1.28.1 + ``` + +2. 部署应用 + + ```bash + kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml + ``` + +3. 要确认 Bookinfo 应用正在运行,请从某个 Pod 中(例如从 `ratings` 中)用 `curl` 命令对此应用发送一条请求 + + ```bash + kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -sS productpage:9080/productpage | grep -o ".*" + ``` + + 结果输出 `Simple Bookstore App` 即代表运行成功。 + +4. 对外开放应用 + + ```bash + kubectl apply -f samples/bookinfo/gateway-api/bookinfo-gateway.yaml + ``` + +5. 通过注解网关将服务类型更改为 `NodePort` + + ```bash + kubectl annotate gateway bookinfo-gateway networking.istio.io/service-type=NodePort --namespace=default + ``` + +6. 检查网关的状态 + + ```bash + kubectl get gateway + ``` + +7. 查看 NodePort 对外端口 + + ```bash + kubectl get svc bookinfo-gateway-istio + ``` + +8. 浏览器访问 + + ```bash + http://$ip:30000/productpage + ``` + + 刷新页面,应该会看到书评和评分发生变化, 因为请求分布在 `reviews` 服务的不同版本上 + +## 使用 Kiali 仪表板 + +1. 安装 Kiali 仪表板 + + ```bash + kubectl apply -f samples/addons/prometheus.yaml + kubectl apply -f samples/addons/kiali.yaml + ``` + +2. 修改对外访问模式为 NodePort + + ```bash + kubectl patch svc kiali -n istio-system -p '{"spec":{"type":"NodePort"}}' + ``` + +3. 查看 NodePort 对外端口 + + ```bash + kubectl get svc kiali -n istio-system + ``` + +4. 浏览器访问 + + ```bash + http://$ip:30001/kiali + ``` + +5. 刷新页面并查看拓扑图 + +## 使用 Istio 实现灰度发布 + +[官方文档](https://istio.io/latest/zh/docs/tasks/traffic-management/traffic-shifting/#apply-weight-based-routing) + +1. 定义服务可用版本 + + ```bash + kubectl apply -f samples/bookinfo/platform/kube/bookinfo-versions.yaml + ``` + +2. 首先,运行此命令将所有流量路由到各个微服务的 `v1` 版本 + + ```bash + kubectl apply -f samples/bookinfo/gateway-api/route-reviews-v1.yaml + ``` + +3. 在浏览器中打开 Bookinfo 站点。网址为 `http://$GATEWAY_URL/productpage`, 其中 `$GATEWAY_URL` 是 Ingress 的外部 IP 地址,其描述参见 [Bookinfo](https://istio.io/latest/zh/docs/examples/bookinfo/#determine-the-ingress-IP-and-port) 文档。 + + 请注意,不管刷新多少次,页面的评论部分都不会显示评价星级的内容。 这是因为 Istio 被配置为将星级评价的服务的所有流量都路由到了 `reviews:v1` 版本,而该版本的服务不访问带评价星级的服务。 + +4. 使用下面的命令把 50% 的流量从 `reviews:v1` 转移到 `reviews:v3` + + ```bash + kubectl apply -f samples/bookinfo/gateway-api/route-reviews-50-v3.yaml + ``` + +5. 等待几秒钟,等待新的规则传播到代理中生效,确认规则已被替换。刷新浏览器中的 `/productpage` 页面,大约有 50% 的几率会看到页面中带**红色**星级的评价内容。 这是因为 `reviews` 的 `v3` 版本可以访问带星级评价,但 `v1` 版本不能 + +6. 如果您认为 `reviews:v3` 微服务已经稳定,您可以通过应用 Virtual Service 规则将 100% 的流量路由 `reviews:v3` + + ```bash + kubectl apply -f samples/bookinfo/gateway-api/route-reviews-v3.yaml + ``` + +7. 现在,当您刷新 `/productpage` 时,您将始终看到带有**红色**星级评分的书评 + +8. 清理路由规则 + + ```bash + kubectl delete httproute reviews + ``` + +## 清理示例环境 + +```bash +samples/bookinfo/platform/kube/cleanup.sh +``` + diff --git a/网关/Istio/通过Istio实现灰度发布.md b/网关/Istio/通过Istio实现灰度发布.md new file mode 100644 index 0000000..e84d5d5 --- /dev/null +++ b/网关/Istio/通过Istio实现灰度发布.md @@ -0,0 +1,176 @@ +> 本文作者:丁辉 + +# 通过Istio实现灰度发布 + +部署测试示例 Nginx Deployment v1和v2 + +1. 部署 v1 版本 + + ```bash + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v1.yaml + ``` + +2. 部署 v2 版本 + + ```bash + kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v2.yaml + ``` + +## 配合 Istio APIs 实现灰度发布 + +1. 创建 Nginx Service + + ```yaml + cat < 下载支持 Docker 的版本 diff --git a/部署文档/Kubernetes基础环境准备.md b/部署文档/Kubernetes基础环境准备.md index 2a94957..ac0b4ee 100644 --- a/部署文档/Kubernetes基础环境准备.md +++ b/部署文档/Kubernetes基础环境准备.md @@ -17,6 +17,12 @@ ntpdate -u ntp.aliyun.com && date ``` +3. 配置定时任务 + + ```bash + 0 2 * * * /usr/bin/ntpdate -u ntp.aliyun.com && /bin/date + ``` + > 离线环境使用此篇文档同步 > > [NTP时间同步](https://gitee.com/offends/Linux/blob/main/Docs/NTP%E6%97%B6%E9%97%B4%E5%90%8C%E6%AD%A5.md) @@ -120,13 +126,15 @@ cat < 1.8 版本是 RKE 1.x 系列的最终版本 ```bash - wget https://github.com/rancher/rke/releases/download/v1.8.6/rke_linux-amd64 + wget https://github.com/rancher/rke/releases/download/v1.8.9/rke_linux-amd64 ``` 2. 授权 ```bash - chmod 777 rke_linux-amd64 && mv rke_linux-amd64 /usr/local/bin/rke + install -o root -g root -m 0755 rke_linux-amd64 /usr/local/bin/rke ``` ### 方法一 (不推荐怪麻烦的请看"方法二") @@ -209,6 +209,16 @@ sed -i '/^ingress:$/,/^ provider:/ s/provider: ""/provider: "none"/' cluster.ym ... ``` + **更换镜像仓库**(适用于离线环境) + + ```yaml + private_registries: + - url: registry.cn-hangzhou.aliyuncs.com # 仅为示例 + is_default: true # 所有的系统镜像都将使用该镜像仓库进行拉取 + user: $user + password: $password + ``` + 3. 基础参数修改 ```bash @@ -390,7 +400,14 @@ rke etcd snapshot-restore --config cluster.yml --name <快照名称> curl -sfL https://gitee.com/offends/Kubernetes/raw/main/File/Shell/restore-rkestate-config.sh | bash -s ``` - +## 基础命令 +- 查看集群所需镜像 + + ```bash + rke config --system-images + ``` + + diff --git a/部署文档/Rancher/如何在国内使用Rancher.md b/部署文档/Rancher/如何在国内使用Rancher.md new file mode 100644 index 0000000..1a42db4 --- /dev/null +++ b/部署文档/Rancher/如何在国内使用Rancher.md @@ -0,0 +1,7 @@ +> 本文作者:丁辉 + +# 如何在国内使用Rancher + +> 网页地址记录 + +[官方文档](https://docs.rancher.cn/docs/rancher2/best-practices/use-in-china/) \ No newline at end of file