{"id":15931,"date":"2026-04-26T13:40:26","date_gmt":"2026-04-26T05:40:26","guid":{"rendered":"https:\/\/top.duoku.icu\/libs\/15931.html"},"modified":"2026-04-26T13:40:26","modified_gmt":"2026-04-26T05:40:26","slug":"kubernetesk8s-%e4%bd%bf%e7%94%a8%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.srclibs.com\/index.php\/2026\/04\/26\/kubernetesk8s-%e4%bd%bf%e7%94%a8%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/","title":{"rendered":"Kubernetes(K8S) \u4f7f\u7528\u8be6\u7ec6\u6559\u7a0b"},"content":{"rendered":"<div style=\"text-align:center;margin:30px 0;\"><img decoding=\"async\" src=\"https:\/\/top.duoku.icu\/wp-content\/uploads\/2026\/04\/image-93.png\" alt=\"Kubernetes(K8S) \u4f7f\u7528\u8be6\u7ec6\u6559\u7a0b\" style=\"max-width:100%;height:auto;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);\"><\/div>\n<h1>Kubernetes(K8S) \u4f7f\u7528\u8be6\u7ec6\u6559\u7a0b<\/h1>\n<h2>Kubernetes \u6982\u8ff0<\/h2>\n<p>Kubernetes\uff08\u7b80\u79f0 K8s\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bb9\u5668\u7f16\u6392\u5e73\u53f0\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u3001\u6269\u5c55\u548c\u7ba1\u7406\u3002\u5b83\u7531 Google \u5728 2014 \u5e74\u5f00\u6e90\uff0c\u73b0\u5728\u662f Cloud Native Computing Foundation (CNCF) \u65d7\u4e0b\u7684\u65d7\u8230\u9879\u76ee\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9700\u8981 Kubernetes\uff1f<\/h3>\n<ul>\n<li><strong>\u5bb9\u5668\u7f16\u6392<\/strong>\uff1a\u7ba1\u7406\u5927\u91cf\u5bb9\u5668\u5316\u5e94\u7528\u7684\u751f\u547d\u5468\u671f<\/li>\n<li><strong>\u81ea\u52a8\u6269\u7f29\u5bb9<\/strong>\uff1a\u6839\u636e\u8d1f\u8f7d\u81ea\u52a8\u8c03\u6574\u8d44\u6e90<\/li>\n<li><strong>\u81ea\u6211\u4fee\u590d<\/strong>\uff1a\u5bb9\u5668\u6545\u969c\u65f6\u81ea\u52a8\u91cd\u542f\u6216\u91cd\u65b0\u8c03\u5ea6<\/li>\n<li><strong>\u8d1f\u8f7d\u5747\u8861<\/strong>\uff1a\u5185\u7f6e\u670d\u52a1\u53d1\u73b0\u548c\u8d1f\u8f7d\u5747\u8861<\/li>\n<li><strong>\u6301\u7eed\u90e8\u7f72<\/strong>\uff1a\u652f\u6301\u6eda\u52a8\u66f4\u65b0\u548c\u56de\u6eda<\/li>\n<\/ul>\n<h3>K8s \u6838\u5fc3\u67b6\u6784<\/h3>\n<pre><code>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                     \u63a7\u5236\u5e73\u9762\uff08Control Plane\uff09            \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u2502\n\u2502  \u2502   API Server \u2502  \u2502   Scheduler \u2502  \u2502  Controller \u2502    \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2502\n\u2502         \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518             \u2502\n\u2502                        \u250c\u2500\u25bc\u2500\u2500\u2510                          \u2502\n\u2502                        \u2502Etcd\u2502                          \u2502\n\u2502                        \u2514\u2500\u2500\u2500\u2500\u2518                          \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                          \u2193\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                     \u5de5\u4f5c\u8282\u70b9\uff08Worker Nodes\uff09             \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u2502\n\u2502  \u2502   Kubelet   \u2502  \u2502   Kube-proxy\u2502  \u2502   Container \u2502    \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2502    Runtime  \u2502    \u2502\n\u2502                                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n<p><strong>\u63a7\u5236\u5e73\u9762\u7ec4\u4ef6<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>API Server<\/strong>\uff1aK8s \u7684\u6838\u5fc3 API \u63a5\u53e3\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u901a\u8fc7\u5b83<\/li>\n<li><strong>Scheduler<\/strong>\uff1a\u8d1f\u8d23\u5c06 Pod \u8c03\u5ea6\u5230\u5408\u9002\u7684\u8282\u70b9<\/li>\n<li><strong>Controller Manager<\/strong>\uff1a\u7ba1\u7406\u5404\u79cd\u63a7\u5236\u5668\uff0c\u5982 Deployment\u3001ReplicaSet<\/li>\n<li><strong>Etcd<\/strong>\uff1a\u5206\u5e03\u5f0f\u952e\u503c\u5b58\u50a8\uff0c\u4fdd\u5b58\u96c6\u7fa4\u6240\u6709\u72b6\u6001<\/li>\n<\/ul>\n<p><strong>\u5de5\u4f5c\u8282\u70b9\u7ec4\u4ef6<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>Kubelet<\/strong>\uff1a\u8282\u70b9\u4ee3\u7406\uff0c\u7ba1\u7406 Pod \u751f\u547d\u5468\u671f<\/li>\n<li><strong>Kube-proxy<\/strong>\uff1a\u7ef4\u62a4\u7f51\u7edc\u89c4\u5219\u548c\u8d1f\u8f7d\u5747\u8861<\/li>\n<li><strong>Container Runtime<\/strong>\uff1a\u5bb9\u5668\u8fd0\u884c\u65f6\uff08Docker\u3001containerd \u7b49\uff09<\/li>\n<\/ul>\n<h2>\u6838\u5fc3\u8d44\u6e90\u5bf9\u8c61<\/h2>\n<h3>1. Pod\uff08\u57fa\u672c\u5355\u4f4d\uff09<\/h3>\n<p>Pod \u662f K8s \u4e2d\u6700\u5c0f\u7684\u8c03\u5ea6\u5355\u5143\uff0c\u4e00\u4e2a Pod \u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u5bb9\u5668\u3002<\/p>\n<p>&#8220;`yaml<br \/>\napiVersion: v1<br \/>\nkind: Pod<br \/>\nmetadata:<br \/>\n  name: my-app-pod<br \/>\n  labels:<br \/>\n    app: my-app<br \/>\nspec:<br \/>\n  containers:<\/p>\n<ul>\n<li>name: nginx<\/li>\n<\/ul>\n<p>    image: nginx:1.21<br \/>\n    ports:<\/p>\n<ul>\n<li>containerPort: 80<\/li>\n<\/ul>\n<p>    resources:<br \/>\n      requests:<br \/>\n        memory: &#8220;64Mi&#8221;<br \/>\n        cpu: &#8220;250m&#8221;<br \/>\n      limits:<br \/>\n        memory: &#8220;128Mi&#8221;<br \/>\n        cpu: &#8220;500m&#8221;<br \/>\n    livenessProbe:<br \/>\n      httpGet:<br \/>\n        path: \/health<br \/>\n        port: 80<br \/>\n      initialDelaySeconds: 10<br \/>\n      periodSeconds: 5<br \/>\n    readinessProbe:<br \/>\n      httpGet:<br \/>\n        path: \/ready<br \/>\n        port: 80<br \/>\n      initialDelaySeconds: 5<br \/>\n      periodSeconds: 3<\/p>\n<pre><code>\n<h3>2. Service\uff08\u670d\u52a1\u53d1\u73b0\uff09<\/h3>\n\nService \u4e3a Pod \u63d0\u4f9b\u7a33\u5b9a\u7684\u7f51\u7edc\u8bbf\u95ee\u5165\u53e3\uff0c\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u548c\u670d\u52a1\u53d1\u73b0\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: v1<br \/>\nkind: Service<br \/>\nmetadata:<br \/>\n  name: my-service<br \/>\nspec:<br \/>\n  selector:<br \/>\n    app: my-app<br \/>\n  ports:<\/p>\n<ul>\n<li>protocol: TCP<\/li>\n<\/ul>\n<p>    port: 80<br \/>\n    targetPort: 8080<br \/>\n  type: ClusterIP  # ClusterIP, NodePort, LoadBalancer, ExternalName<\/p>\n<pre><code>\n<strong>Service \u7c7b\u578b<\/strong>\uff1a\n<ul>\n<li><strong>ClusterIP<\/strong>\uff1a\u96c6\u7fa4\u5185\u90e8\u8bbf\u95ee\uff08\u9ed8\u8ba4\uff09<\/li>\n<li><strong>NodePort<\/strong>\uff1a\u5728\u6bcf\u4e2a\u8282\u70b9\u4e0a\u5f00\u653e\u7aef\u53e3<\/li>\n<li><strong>LoadBalancer<\/strong>\uff1a\u521b\u5efa\u5916\u90e8\u8d1f\u8f7d\u5747\u8861\u5668<\/li>\n<li><strong>ExternalName<\/strong>\uff1a\u6620\u5c04\u5230\u5916\u90e8 DNS \u540d\u79f0<\/li>\n<\/ul>\n\n<h3>3. Deployment\uff08\u90e8\u7f72\u7ba1\u7406\uff09<\/h3>\n\nDeployment \u7ba1\u7406\u65e0\u72b6\u6001\u5e94\u7528\u7684\u90e8\u7f72\uff0c\u652f\u6301\u6eda\u52a8\u66f4\u65b0\u548c\u56de\u6eda\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\n  name: my-deployment<br \/>\nspec:<br \/>\n  replicas: 3<br \/>\n  selector:<br \/>\n    matchLabels:<br \/>\n      app: my-app<br \/>\n  template:<br \/>\n    metadata:<br \/>\n      labels:<br \/>\n        app: my-app<br \/>\n    spec:<br \/>\n      containers:<\/p>\n<ul>\n<li>name: nginx<\/li>\n<\/ul>\n<p>        image: nginx:1.21<br \/>\n        ports:<\/p>\n<ul>\n<li>containerPort: 80<\/li>\n<\/ul>\n<pre><code>\n<strong>Deployment \u547d\u4ee4\u64cd\u4f5c<\/strong>\uff1a\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa Deployment<\/h1>\n<p>kubectl apply -f deployment.yaml<\/p>\n<h1>\u67e5\u770b Deployment \u72b6\u6001<\/h1>\n<p>kubectl get deployments<\/p>\n<h1>\u6eda\u52a8\u66f4\u65b0<\/h1>\n<p>kubectl set image deployment\/my-deployment nginx=nginx:1.22<\/p>\n<h1>\u56de\u6eda\u66f4\u65b0<\/h1>\n<p>kubectl rollout undo deployment\/my-deployment<\/p>\n<h1>\u67e5\u770b\u66f4\u65b0\u5386\u53f2<\/h1>\n<p>kubectl rollout history deployment\/my-deployment<\/p>\n<h1>\u67e5\u770b\u66f4\u65b0\u72b6\u6001<\/h1>\n<p>kubectl rollout status deployment\/my-deployment<\/p>\n<pre><code>\n<h3>4. ConfigMap\uff08\u914d\u7f6e\u7ba1\u7406\uff09<\/h3>\n\nConfigMap \u5c06\u914d\u7f6e\u4fe1\u606f\u4e0e\u5e94\u7528\u7a0b\u5e8f\u5206\u79bb\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: v1<br \/>\nkind: ConfigMap<br \/>\nmetadata:<br \/>\n  name: app-config<br \/>\ndata:<br \/>\n  APP_ENV: production<br \/>\n  LOG_LEVEL: info<br \/>\n  DB_HOST: database-service<\/p>\n<pre><code>\n<strong>\u5728 Pod \u4e2d\u4f7f\u7528 ConfigMap<\/strong>\uff1a\n<\/code><\/pre>\n<p>yaml<br \/>\nspec:<br \/>\n  containers:<\/p>\n<ul>\n<li>name: my-app<\/li>\n<\/ul>\n<p>    image: my-app:1.0<br \/>\n    envFrom:<\/p>\n<ul>\n<li>configMapRef:<\/li>\n<\/ul>\n<p>        name: app-config<br \/>\n    # \u6216\u8005\u5355\u72ec\u6307\u5b9a<br \/>\n    env:<\/p>\n<ul>\n<li>name: APP_ENV<\/li>\n<\/ul>\n<p>      valueFrom:<br \/>\n        configMapKeyRef:<br \/>\n          name: app-config<br \/>\n          key: APP_ENV<br \/>\n    volumeMounts:<\/p>\n<ul>\n<li>name: config-volume<\/li>\n<\/ul>\n<p>      mountPath: \/etc\/config<br \/>\n  volumes:<\/p>\n<ul>\n<li>name: config-volume<\/li>\n<\/ul>\n<p>    configMap:<br \/>\n      name: app-config<\/p>\n<pre><code>\n<h3>5. Secret\uff08\u654f\u611f\u4fe1\u606f\uff09<\/h3>\n\nSecret \u7528\u4e8e\u5b58\u50a8\u5bc6\u7801\u3001\u4ee4\u724c\u7b49\u654f\u611f\u4fe1\u606f\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: v1<br \/>\nkind: Secret<br \/>\nmetadata:<br \/>\n  name: db-secret<br \/>\ntype: Opaque<br \/>\nstringData:<br \/>\n  username: admin<br \/>\n  password: mysecretpassword<\/p>\n<h1>\u6216\u8005\u4f7f\u7528 base64 \u7f16\u7801<\/h1>\n<p>data:<br \/>\n  username: YWRtaW4=<br \/>\n  password: bXlzZWNyZXRwYXNzd29yZA==<\/p>\n<pre><code>\n<h3>6. Namespace\uff08\u547d\u540d\u7a7a\u95f4\uff09<\/h3>\n\nNamespace \u63d0\u4f9b\u8d44\u6e90\u9694\u79bb\uff0c\u5c06\u96c6\u7fa4\u5212\u5206\u4e3a\u591a\u4e2a\u865a\u62df\u96c6\u7fa4\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: v1<br \/>\nkind: Namespace<br \/>\nmetadata:<br \/>\n  name: production<br \/>\n  labels:<br \/>\n    env: production<\/p>\n<pre><code>\n<strong>\u4f7f\u7528 Namespace<\/strong>\uff1a\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl create namespace production<\/p>\n<h1>\u67e5\u770b\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl get namespaces<\/p>\n<h1>\u5728\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u64cd\u4f5c<\/h1>\n<p>kubectl get pods -n production<\/p>\n<h1>\u5207\u6362\u9ed8\u8ba4\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl config set-context &#8211;current &#8211;namespace=production<\/p>\n<pre><code>\n<h2>\u5e38\u7528 kubectl \u547d\u4ee4<\/h2>\n\n<h3>\u96c6\u7fa4\u4fe1\u606f<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u67e5\u770b\u96c6\u7fa4\u4fe1\u606f<\/h1>\n<p>kubectl cluster-info<\/p>\n<h1>\u67e5\u770b\u8282\u70b9\u72b6\u6001<\/h1>\n<p>kubectl get nodes<\/p>\n<h1>\u67e5\u770b\u8282\u70b9\u8be6\u7ec6\u4fe1\u606f<\/h1>\n<p>kubectl describe nodes <node-name><\/p>\n<h1>\u67e5\u770b\u96c6\u7fa4\u7ec4\u4ef6\u72b6\u6001<\/h1>\n<p>kubectl get componentstatus<\/p>\n<pre><code>\n<h3>Pod \u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa Pod<\/h1>\n<p>kubectl run nginx &#8211;image=nginx:1.21<\/p>\n<h1>\u5217\u51fa\u6240\u6709 Pod<\/h1>\n<p>kubectl get pods<\/p>\n<h1>\u67e5\u770b\u6240\u6709\u547d\u540d\u7a7a\u95f4\u7684 Pod<\/h1>\n<p>kubectl get pods &#8211;all-namespaces<\/p>\n<h1>\u67e5\u770b Pod \u8be6\u7ec6\u4fe1\u606f<\/h1>\n<p>kubectl describe pod <pod-name>\n<h1>\u67e5\u770b Pod \u65e5\u5fd7<\/h1>\n<p>kubectl logs <pod-name>\n<h1>\u67e5\u770b\u6307\u5b9a\u5bb9\u5668\u7684\u65e5\u5fd7<\/h1>\n<p>kubectl logs <pod-name> -c <container-name><\/p>\n<h1>\u5b9e\u65f6\u67e5\u770b\u65e5\u5fd7<\/h1>\n<p>kubectl logs -f <pod-name>\n<h1>\u8fdb\u5165\u5bb9\u5668\u6267\u884c\u547d\u4ee4<\/h1>\n<p>kubectl exec -it <pod-name> &#8212; \/bin\/sh<\/p>\n<h1>\u590d\u5236\u6587\u4ef6<\/h1>\n<p>kubectl cp <pod-name>:\/path\/file \/local\/path<br \/>\nkubectl cp \/local\/path <pod-name>:\/path\/file<\/p>\n<pre><code>\n<h3>\u90e8\u7f72\u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa Deployment<\/h1>\n<p>kubectl create deployment nginx &#8211;image=nginx:1.21<\/p>\n<h1>\u5e94\u7528\u914d\u7f6e<\/h1>\n<p>kubectl apply -f deployment.yaml<\/p>\n<h1>\u67e5\u770b Deployment<\/h1>\n<p>kubectl get deployments<\/p>\n<h1>\u6269\u7f29\u5bb9<\/h1>\n<p>kubectl scale deployment nginx &#8211;replicas=5<\/p>\n<h1>\u5220\u9664 Deployment<\/h1>\n<p>kubectl delete deployment nginx<\/p>\n<pre><code>\n<h3>Service \u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa Service<\/h1>\n<p>kubectl expose deployment nginx &#8211;port=80 &#8211;target-port=80 &#8211;type=NodePort<\/p>\n<h1>\u67e5\u770b Service<\/h1>\n<p>kubectl get services<\/p>\n<h1>\u67e5\u770b Service \u8be6\u7ec6\u4fe1\u606f<\/h1>\n<p>kubectl describe service <service-name><\/p>\n<h1>\u7aef\u53e3\u8f6c\u53d1<\/h1>\n<p>kubectl port-forward svc\/my-service 8080:80<br \/>\nkubectl port-forward pod\/nginx-pod 8080:80<\/p>\n<pre><code>\n<h3>\u914d\u7f6e\u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa ConfigMap<\/h1>\n<p>kubectl create configmap app-config &#8211;from-literal=APP_ENV=production<\/p>\n<h1>\u4ece\u6587\u4ef6\u521b\u5efa ConfigMap<\/h1>\n<p>kubectl create configmap app-config &#8211;from-file=config.properties<\/p>\n<h1>\u67e5\u770b ConfigMap<\/h1>\n<p>kubectl get configmaps<\/p>\n<h1>\u67e5\u770b ConfigMap \u5185\u5bb9<\/h1>\n<p>kubectl describe configmap app-config<\/p>\n<h1>\u5220\u9664 ConfigMap<\/h1>\n<p>kubectl delete configmap app-config<\/p>\n<pre><code>\n<h3>\u547d\u540d\u7a7a\u95f4\u64cd\u4f5c<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u521b\u5efa\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl create namespace dev<\/p>\n<h1>\u5220\u9664\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl delete namespace dev<\/p>\n<h1>\u67e5\u770b\u547d\u540d\u7a7a\u95f4\u8d44\u6e90<\/h1>\n<p>kubectl get all -n dev<\/p>\n<pre><code>\n<h3>\u6545\u969c\u6392\u67e5<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u67e5\u770b Pod \u4e8b\u4ef6<\/h1>\n<p>kubectl get events &#8211;sort-by=.lastTimestamp<\/p>\n<h1>\u67e5\u770b Pod \u8be6\u7ec6\u72b6\u6001<\/h1>\n<p>kubectl describe pod <pod-name>\n<h1>\u67e5\u770b\u8282\u70b9\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5<\/h1>\n<p>kubectl top nodes<\/p>\n<h1>\u67e5\u770b Pod \u8d44\u6e90\u4f7f\u7528\u60c5\u51b5<\/h1>\n<p>kubectl top pods<\/p>\n<h1>\u67e5\u770b\u65e5\u5fd7\uff08\u591a\u884c\uff09<\/h1>\n<p>kubectl logs <pod-name> &#8211;tail=100<\/p>\n<h1>\u8fdb\u5165\u5bb9\u5668\u8c03\u8bd5<\/h1>\n<p>kubectl debug -it <pod-name> &#8211;image=busybox<\/p>\n<h1>\u5bfc\u51fa\u914d\u7f6e<\/h1>\n<p>kubectl get pods -o yaml > pod.yaml<br \/>\nkubectl get pods -o json > pod.json<\/p>\n<pre><code>\n<h2>\u90e8\u7f72\u573a\u666f\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n<h3>\u573a\u666f\u4e00\uff1aWeb \u5e94\u7528\u90e8\u7f72<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\n  name: web-app<br \/>\nspec:<br \/>\n  replicas: 3<br \/>\n  selector:<br \/>\n    matchLabels:<br \/>\n      app: web-app<br \/>\n  template:<br \/>\n    metadata:<br \/>\n      labels:<br \/>\n        app: web-app<br \/>\n    spec:<br \/>\n      containers:<\/p>\n<ul>\n<li>name: web<\/li>\n<\/ul>\n<p>        image: myapp:1.0<br \/>\n        ports:<\/p>\n<ul>\n<li>containerPort: 8080<\/li>\n<\/ul>\n<p>        resources:<br \/>\n          requests:<br \/>\n            memory: &#8220;256Mi&#8221;<br \/>\n            cpu: &#8220;250m&#8221;<br \/>\n          limits:<br \/>\n            memory: &#8220;512Mi&#8221;<br \/>\n            cpu: &#8220;500m&#8221;<br \/>\n        livenessProbe:<br \/>\n          httpGet:<br \/>\n            path: \/health<br \/>\n            port: 8080<br \/>\n          initialDelaySeconds: 15<br \/>\n          periodSeconds: 10<br \/>\n        readinessProbe:<br \/>\n          httpGet:<br \/>\n            path: \/ready<br \/>\n            port: 8080<br \/>\n          initialDelaySeconds: 5<br \/>\n          periodSeconds: 5<br \/>\n&#8212;<br \/>\napiVersion: v1<br \/>\nkind: Service<br \/>\nmetadata:<br \/>\n  name: web-app-service<br \/>\nspec:<br \/>\n  selector:<br \/>\n    app: web-app<br \/>\n  ports:<\/p>\n<ul>\n<li>port: 80<\/li>\n<\/ul>\n<p>    targetPort: 8080<br \/>\n  type: LoadBalancer<\/p>\n<pre><code>\n<h3>\u573a\u666f\u4e8c\uff1a\u6570\u636e\u5e93\u90e8\u7f72<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: apps\/v1<br \/>\nkind: StatefulSet<br \/>\nmetadata:<br \/>\n  name: mysql<br \/>\nspec:<br \/>\n  serviceName: mysql<br \/>\n  replicas: 3<br \/>\n  selector:<br \/>\n    matchLabels:<br \/>\n      app: mysql<br \/>\n  template:<br \/>\n    metadata:<br \/>\n      labels:<br \/>\n        app: mysql<br \/>\n    spec:<br \/>\n      containers:<\/p>\n<ul>\n<li>name: mysql<\/li>\n<\/ul>\n<p>        image: mysql:8.0<br \/>\n        env:<\/p>\n<ul>\n<li>name: MYSQL_ROOT_PASSWORD<\/li>\n<\/ul>\n<p>          valueFrom:<br \/>\n            secretKeyRef:<br \/>\n              name: mysql-secret<br \/>\n              key: root-password<br \/>\n        ports:<\/p>\n<ul>\n<li>containerPort: 3306<\/li>\n<\/ul>\n<p>        volumeMounts:<\/p>\n<ul>\n<li>name: data<\/li>\n<\/ul>\n<p>          mountPath: \/var\/lib\/mysql<br \/>\n  volumeClaimTemplates:<\/p>\n<ul>\n<li>metadata:<\/li>\n<\/ul>\n<p>      name: data<br \/>\n    spec:<br \/>\n      accessModes: [&#8220;ReadWriteOnce&#8221;]<br \/>\n      resources:<br \/>\n        requests:<br \/>\n          storage: 10Gi<\/p>\n<pre><code>\n<h3>\u573a\u666f\u4e09\uff1a\u591a\u5bb9\u5668 Pod<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\napiVersion: v1<br \/>\nkind: Pod<br \/>\nmetadata:<br \/>\n  name: sidecar-pattern<br \/>\nspec:<br \/>\n  containers:<\/p>\n<ul>\n<li>name: app<\/li>\n<\/ul>\n<p>    image: myapp:1.0<br \/>\n    ports:<\/p>\n<ul>\n<li>containerPort: 8080<\/li>\n<li>name: log-sidecar<\/li>\n<\/ul>\n<p>    image: fluentd:latest<br \/>\n    volumeMounts:<\/p>\n<ul>\n<li>name: logs<\/li>\n<\/ul>\n<p>      mountPath: \/var\/log\/app<\/p>\n<ul>\n<li>name: metrics-sidecar<\/li>\n<\/ul>\n<p>    image: prometheus-node-exporter:latest<br \/>\n    ports:<\/p>\n<ul>\n<li>containerPort: 9100<\/li>\n<\/ul>\n<p>  volumes:<\/p>\n<ul>\n<li>name: logs<\/li>\n<\/ul>\n<p>    emptyDir: {}<\/p>\n<pre><code>\n<h2>\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n<h3>1. \u8d44\u6e90\u9650\u5236<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\nresources:<br \/>\n  requests:<br \/>\n    memory: &#8220;256Mi&#8221;<br \/>\n    cpu: &#8220;250m&#8221;<br \/>\n  limits:<br \/>\n    memory: &#8220;512Mi&#8221;<br \/>\n    cpu: &#8220;500m&#8221;<\/p>\n<pre><code>\n<h3>2. \u5065\u5eb7\u68c0\u67e5<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\nlivenessProbe:<br \/>\n  httpGet:<br \/>\n    path: \/health<br \/>\n    port: 8080<br \/>\n  initialDelaySeconds: 15<br \/>\n  periodSeconds: 10<\/p>\n<p>readinessProbe:<br \/>\n  httpGet:<br \/>\n    path: \/ready<br \/>\n    port: 8080<br \/>\n  initialDelaySeconds: 5<br \/>\n  periodSeconds: 5<\/p>\n<p>startupProbe:<br \/>\n  httpGet:<br \/>\n    path: \/startup<br \/>\n    port: 8080<br \/>\n  failureThreshold: 30<br \/>\n  periodSeconds: 10<\/p>\n<pre><code>\n<h3>3. \u6807\u7b7e\u4e0e\u9009\u62e9\u5668<\/h3>\n\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u5b9a\u4e49\u6807\u7b7e<\/h1>\n<p>metadata:<br \/>\n  labels:<br \/>\n    app: my-app<br \/>\n    env: production<br \/>\n    tier: frontend<\/p>\n<h1>\u4f7f\u7528\u9009\u62e9\u5668<\/h1>\n<p>selector:<br \/>\n  matchLabels:<br \/>\n    app: my-app<br \/>\n    env: production<\/p>\n<pre><code>\n<h3>4. \u547d\u540d\u7a7a\u95f4\u9694\u79bb<\/h3>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u6309\u73af\u5883\u5212\u5206\u547d\u540d\u7a7a\u95f4<\/h1>\n<p>kubectl create namespace development<br \/>\nkubectl create namespace staging<br \/>\nkubectl create namespace production<\/p>\n<pre><code>\n<h3>5. \u5b89\u5168\u5b9e\u8df5<\/h3>\n\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u4f7f\u7528\u975e root \u7528\u6237\u8fd0\u884c<\/h1>\n<p>securityContext:<br \/>\n  runAsNonRoot: true<br \/>\n  runAsUser: 1000<br \/>\n  runAsGroup: 1000<br \/>\n  fsGroup: 1000<\/p>\n<h1>\u53ea\u8bfb\u6839\u6587\u4ef6\u7cfb\u7edf<\/h1>\n<p>securityContext:<br \/>\n  readOnlyRootFilesystem: true<br \/>\n&#8220;`<\/p>\n<h2>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<h3>\u573a\u666f\u4e00\uff1a\u5fae\u670d\u52a1\u67b6\u6784<\/h3>\n<p>\u4f7f\u7528 K8s \u90e8\u7f72\u5fae\u670d\u52a1\u67b6\u6784\uff0c\u6bcf\u4e2a\u5fae\u670d\u52a1\u4f5c\u4e3a\u72ec\u7acb\u7684 Deployment\uff0c\u901a\u8fc7 Service \u66b4\u9732\uff0c\u5b9e\u73b0\u670d\u52a1\u53d1\u73b0\u548c\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<h3>\u573a\u666f\u4e8c\uff1a\u6301\u7eed\u90e8\u7f72\uff08CI\/CD\uff09<\/h3>\n<p>\u7ed3\u5408 Jenkins\u3001GitLab CI \u7b49\u5de5\u5177\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u90e8\u7f72\uff0c\u652f\u6301\u6eda\u52a8\u66f4\u65b0\u548c\u5feb\u901f\u56de\u6eda\u3002<\/p>\n<h3>\u573a\u666f\u4e09\uff1a\u6df7\u5408\u4e91\u90e8\u7f72<\/h3>\n<p>\u5728\u79c1\u6709\u4e91\u548c\u516c\u6709\u4e91\u4e4b\u95f4\u90e8\u7f72 K8s \u96c6\u7fa4\uff0c\u5b9e\u73b0\u8d44\u6e90\u5f39\u6027\u4f38\u7f29\u548c\u5bb9\u707e\u5907\u4efd\u3002<\/p>\n<h3>\u573a\u666f\u56db\uff1aAI\/ML \u8bad\u7ec3<\/h3>\n<p>\u4f7f\u7528 K8s \u8c03\u5ea6 GPU \u8282\u70b9\uff0c\u6279\u91cf\u5904\u7406 AI \u8bad\u7ec3\u4efb\u52a1\uff0c\u652f\u6301\u4efb\u52a1\u961f\u5217\u548c\u8d44\u6e90\u6c60\u5316\u3002<\/p>\n<h3>\u573a\u666f\u4e94\uff1a\u65e5\u5fd7\u4e0e\u76d1\u63a7<\/h3>\n<p>\u90e8\u7f72 Prometheus + Grafana \u76d1\u63a7\u4f53\u7cfb\uff0c\u914d\u5408 Fluentd\/Fluent Bit \u5b9e\u73b0\u65e5\u5fd7\u6536\u96c6\u548c\u5206\u6790\u3002<\/p>\n<p>&#8212;<\/p>\n<p><strong>\u603b\u7ed3<\/strong>\uff1aKubernetes \u662f\u4e91\u539f\u751f\u65f6\u4ee3\u7684\u4e8b\u5b9e\u6807\u51c6\uff0c\u638c\u63e1 K8s \u7684\u6838\u5fc3\u6982\u5ff5\u548c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5c06\u5927\u5927\u63d0\u5347\u4f60\u7684\u5bb9\u5668\u5316\u5e94\u7528\u7ba1\u7406\u80fd\u529b\u3002\u5efa\u8bae\u591a\u5b9e\u8df5\uff0c\u719f\u6089 kubectl \u547d\u4ee4\uff0c\u7406\u89e3 YAML \u914d\u7f6e\uff0c\u9010\u6b65\u638c\u63e1\u66f4\u9ad8\u7ea7\u7684 K8s \u7279\u6027\u3002<\/p>\n<p><strong>\u4e0b\u4e00\u6b65\u5b66\u4e60<\/strong>\uff1a<\/p>\n<ul>\n<li>Helm \u5305\u7ba1\u7406<\/li>\n<li>K8s \u7f51\u7edc\u63d2\u4ef6\uff08Calico\u3001Flannel\uff09<\/li>\n<li>K8s \u5b58\u50a8\uff08PV\u3001PVC\uff09<\/li>\n<li>K8s \u5b89\u5168\uff08RBAC\u3001NetworkPolicy\uff09<\/li>\n<li>\u670d\u52a1\u7f51\u683c\uff08Istio\uff09<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Kubernetes(K8S) \u4f7f\u7528\u8be6\u7ec6\u6559\u7a0b Kubernetes \u6982\u8ff0 Kubernetes\uff08\u7b80\u79f0 K8s\uff09&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-15931","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/comments?post=15931"}],"version-history":[{"count":0,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15931\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/media?parent=15931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/categories?post=15931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/tags?post=15931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}