Kubernetes笔记 -- 核心概念(未完成)

    科技2025-04-21  6

    文章目录

    kubectlpodcontrollerserviceIngresshelm

    kubectl

    语法 kubectl [command] [type] [name] [flags] command : create, expose, get, describe, delete type : 指定资源的类型 name : 指定资源的名称 flags : 指定可选的参数 e.g. kubectl create deployment nginx --image=nginx 帮助 kubectl --help kubectl create --help kubectl create deployment --help YAML文件 k8s集群中对资源管理和资源对象的编排部署都可以通过声明样式(YAML)文件来解 决,也就是可以把需要对资源对象的操作编辑到YAML格式文件中,我们把这种文件叫 做资源清单文件,通过kubectl命令直接使用资源清单文件就可以实现对大量的资源对象 进行编排部署。 可以 kubectl create deployment ... -o yaml --dry-run=client :生成YAML文件样例文件 或 kubelctl get deployment ... -o yaml :以YAML文件形式输出其资源描述 然后通过 kubectl apply -f x.yml [root@master01 ~]# kubectl create deployment nginx02 --image=nginx -o yaml --dry-run=client > nginx02.yml [root@master01 ~]# cat nginx02.yml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx02 name: nginx02 spec: replicas: 1 selector: matchLabels: app: nginx02 strategy: {} template: metadata: creationTimestamp: null labels: app: nginx02 spec: containers: - image: nginx name: nginx resources: {} status: {} [root@master01 ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 3h33m [root@master01 ~]# kubectl get deployment nginx -o yaml > nginx.yml [root@master01 ~]#

    pod

    简介

    共享存储

    健康检查

    节点调度

    基于角色的节点调度:控制Pod具体分发到哪些节点上

    将两个节点分别定义为开发和测试角色: # kubectl label node node01 env_role=dev # kubectl label node node02 env_role=test 创建pod时在yml文件中指定: spec: nodeSelector: env_role: dev 来指定Pod要部署到哪些节点上 污点

    为不健康的节点打上标签,控制尽量不要在该节点上创建pod。

    controller

    集群上管理和运行容器的对象

    与Pod的关系 Pod是通过Controller实现应用的运维Pod和Controller之间通过label标签建立关系

    [root@master01 ~]# kubectl create deployment web01 --image=nginx --dry-run=client -o yaml > web01.yml [root@master01 ~]# cat web01.yml .... metadata: creationTimestamp: null labels: app: web01 name: web01 spec: replicas: 1 selector: matchLabels: app: web01 .... 应用升级回滚和弹性伸缩

    service

    Ingress

    创建Pod [root@master01 ~]# kubectl create deployment web --image=nginx deployment.apps/web created [root@master01 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE web-96d5df5c8-896r9 1/1 Running 0 85s 创建Service [root@master01 ~]# kubectl expose deployment web --port=80 --target-port=80 --type=NodePort service/web exposed [root@master01 ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d22h web NodePort 10.107.170.44 <none> 80:30986/TCP 26s 部署Ingress Controller

    helm

    下载网址:https://github.com/helm/helm/releases # Installation and Upgrading - Linux amd64

    安装 [root@master01 ~]# cd /usr/local/bin [root@master01 bin]# ls helm-v3.4.0-linux-amd64.tar.gz [root@master01 bin]# tar -zxf helm-v3.4.0-linux-amd64.tar.gz [root@master01 bin]# mv linux-amd64/helm . [root@master01 bin]# chown root.root helm [root@master01 bin]# rm helm-v3.4.0-linux-amd64.tar.gz -f [root@master01 bin]# rm linux-amd64 -rf 添加仓库 [root@master01 ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts "stable" has been added to your repositories [root@master01 ~]# helm repo list NAME URL stable http://mirror.azure.cn/kubernetes/charts [root@master01 ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts "aliyun" has been added to your repositories [root@master01 ~]# helm repo remove aliyun "aliyun" has been removed from your repositories [root@master01 ~]# helm repo update Hang tight while we grab the latest from your chart repositories... ...Successfully got an update from the "aliyun" chart repository Update Complete. ⎈Happy Helming![root@master01 ~]# 一键部署 [root@master01 ~]# helm search repo weave NAME CHART VERSION APP VERSION DESCRIPTION stable/weave-cloud 0.3.7 1.4.0 Weave Cloud is a add-on to Kubernetes which pro... stable/weave-scope 1.1.10 1.12.0 A Helm chart for the Weave Scope cluster visual... [root@master01 ~]# helm install ui stable/weave-scope W1030 17:55:10.529102 89432 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole W1030 17:55:10.548117 89432 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding W1030 17:55:29.571086 89432 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole W1030 17:55:29.680831 89432 warnings.go:67] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding NAME: ui LAST DEPLOYED: Fri Oct 30 17:55:10 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: You should now be able to access the Scope frontend in your web browser, by using kubectl port-forward: kubectl -n default port-forward $(kubectl -n default get endpoints \ ui-weave-scope -o jsonpath='{.subsets[0].addresses[0].targetRef.name}') 8080:4040 then browsing to http://localhost:8080/. For more details on using Weave Scope, see the Weave Scope documentation: https://www.weave.works/docs/scope/latest/introducing/ [root@master01 ~]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2020-10-30 17:55:10.130405063 +0800 CST deployed weave-scope-1.1.10 1.12.0 [root@master01 ~]# helm status ui NAME: ui LAST DEPLOYED: Fri Oct 30 17:55:10 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: You should now be able to access the Scope frontend in your web browser, by using kubectl port-forward: kubectl -n default port-forward $(kubectl -n default get endpoints \ ui-weave-scope -o jsonpath='{.subsets[0].addresses[0].targetRef.name}') 8080:4040 then browsing to http://localhost:8080/. For more details on using Weave Scope, see the Weave Scope documentation: https://www.weave.works/docs/scope/latest/introducing/ [root@master01 ~]# [root@master01 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE weave-scope-agent-ui-27fcd 1/1 Running 0 89s weave-scope-agent-ui-6mskp 1/1 Running 0 89s weave-scope-agent-ui-s4plk 1/1 Running 0 89s weave-scope-agent-ui-t4thq 1/1 Running 0 89s weave-scope-cluster-agent-ui-5cbc84db49-bv2kz 1/1 Running 0 89s weave-scope-frontend-ui-6698fd5545-br8bd 1/1 Running 0 89s web-96d5df5c8-896r9 1/1 Running 0 66m [root@master01 ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d ui-weave-scope ClusterIP 10.110.64.123 <none> 80/TCP 94s web NodePort 10.107.170.44 <none> 80:30986/TCP 64m [root@master01 ~]# kubectl edit service ui-weave-scope .... type: NodePort .... service/ui-weave-scope edited [root@master01 ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d ui-weave-scope NodePort 10.110.64.123 <none> 80:32018/TCP 4m43s web NodePort 10.107.170.44 <none> 80:30986/TCP 67m
    Processed: 0.009, SQL: 8