Kubernetes学习笔记 第一阶段 炼气期(2-4 周,每周 3-5 小时)

基础知识

参考本文由 才云科技(Caicloud) 于 2019 年内部推出,现以开源的形式进行维护,网址在https://github.com/caicloud/kube-ladder

在安装好环境之后,可以开始动手实践最基本的 Kubernetes 概念。在第一阶段,我们推荐熟练使用以下常用资源和概念:Pod、Node、Label、Event、Service、Configmap & Secret、Deployment、Namespace。相关学习可以参考文档 lab2-application-and-service。
(可选)仅完成上述内容可能还不足以让我们非常熟悉 Kubernetes 的基本概念,下面列出其他可以参考的资料,大家也可以按照自己的方式去搜索相关的资料:
官方 Tutorial:Learn Kubernetes Basics
官方 Guestbook 样例:Guestbook Example

Kubernetes是什么?

了解其出现的背景和意义都是必不可少的,为什么会出现 Kubernetes?它解决了什么问题?

Kubernetes是Google多年大规模应用容器技术的经验积累和升华的重要成果,其目的实现资源管理的自动化,以及跨多个数据中心的资源利用率最大化
他还是一个完备的分布式系统支撑平台,具有完善的集群管理能力,提供了安全、多租户应用支撑、透明的注册发现和强大的故障发现和自我修复能力;提供了完善的管理工具和基于容器技术的分布式架构解决方案。

Kubernetes好处

  • 简化应用系统部署
  • 更好的利用硬件
  • 健康检查和自修复
  • 自动扩缩容

熟练使用以下常用资源和概念

Pod

那什么是POD?Pod是 Kubernetes 项目的原子调度单位。
1、为什么我们会需要 Pod?
Pod可以解决多个业务容器共享Pause容器IP和Volume存储,每一个Pod就像一个独立的逻辑机器,拥有自己的IP、主机名、进程等。
2、使用场景
我们需要部署一个Java web程序,需要Tomcat和系统WAR报,一般可以将Tomcat和WAR打在一个镜像中,但这样会有一个问题,就是不过WAR更新还是Tomcat升级,都需要重新打镜像。那我们就可以利用Pod,将Tomcat和WAR包分别各打一个镜像,将两个镜像组合在一起,他们共享同一个存储和IP。

Node

Label

给资源如Pod定义的标签,如版本标签、环境标签、分区标签等,可以通过Label Selector查询和删选拥有某些Label的资源(Label Selector类似Sql中的Select语句)。
应用场景
创建Service通过Label Selector标签关联Pod名称,来创建对应Pod的Service 服务。

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
spec:
  ports:
  - port: 8080
  selector:
    tier: frontend

在spec-selector-tier中关联Pod frontend。

Event

Service

Service 是什么?
为一组功能相同的Pod提供单一不变的接入点。内外部客户端就可以通过Service连接到Pod上。Service与其后端Pod副本集群之间则是通过Label Selecter来实现无缝对接的。
创建一个Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: app-demo
        tier: frontend
    spec:
      containers:
      - name: tomcat-demo
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

执行kubectl get endpoints
tomcat-service 172.17.0.22:8080 12s
172.17.0.22就是Pod的IP,8080为Pod开放的端口。
我们在执行

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
spec:
  ports:
  - port: 8080
  selector:
    tier: frontend
将Service与frontend结合起来,通过kubectl get svc tomcat-service -o yaml查询。
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2021-01-13T08:04:10Z"
  name: tomcat-service
  namespace: default
  resourceVersion: "1229327"
  selfLink: /api/v1/namespaces/default/services/tomcat-service
  uid: 467cfc36-4777-40e9-b0db-4f62acec6a1e
spec:
  clusterIP: 10.98.112.252
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    tier: frontend
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
clusterIP: 10.98.112.252为Service暴露的IP。

Configmap

背景和其初衷
配置参数可能会经常修改,如果不能解决配置参数的灵活修改,一个程序会有很多镜像包,Configmap可以是程序镜像和配置进行解耦。
应用场景

  • 向容器传递命令行参数
  • 为每个容器配置自定义环境变量
  • 通过特殊类型的券将配置文件挂载到容器中

实例将文件挂载到容器中
参考Kubernetes In Action,Nginx配置文件,my-nginx-config.conf内容是

server {
    listen              80;
    server_name         www.kubia-example.com;

    gzip on;
    gzip_types text/plain application/xml;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

我们想要把这个配置文件添加到Nginx的conf.d文件夹下,达到gzip压缩开启的目的。

需要先重建ConfigMap,通过kubectl create configmap fortune-config --from-file=configmap-files重建,其中上面的文件是在configmap-files文件夹下。 创建后通过kubectl get configmap fortune-config -o yaml查看。

apiVersion: v1
data:
  my-nginx-config.conf: |
    server {
        listen              80;
        server_name         www.kubia-example.com;

        gzip on;
        gzip_types text/plain application/xml;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

    }
  sleep-interval: |
    25
kind: ConfigMap
metadata:
  creationTimestamp: "2021-01-14T03:18:10Z"
  name: fortune-config
  namespace: default
  resourceVersion: "1254226"
  selfLink: /api/v1/namespaces/default/configmaps/fortune-config
  uid: 380a1233-ffd3-4fcd-8285-bba548eb6503

可以看到data下面my-nginx-config.conf文件创建了。怎么让Pod加载到这个ConfigMap呢?

apiVersion: v1
kind: Pod
metadata:
  name: fortune-configmap-volume
spec:
  containers:
  - image: luksa/fortune:env
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: fortune-config
          key: sleep-interval
    name: html-generator
    volumeMounts:
    - name: html
      mountPath: /var/htdocs
  - image: nginx:alpine
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    - name: config   //挂载ConfigMap卷到conf.d下
      mountPath: /etc/nginx/conf.d
      readOnly: true
    - name: config
      mountPath: /tmp/whole-fortune-config-volume
      readOnly: true
    ports:
      - containerPort: 80
        name: http
        protocol: TCP
  volumes:
  - name: html
    emptyDir: {}
  - name: config   // 卷定义引用fortune-config ConfigMap
    configMap:
      name: fortune-config

Secret

Deployment

Deployment是什么?
是一个更高阶资源,用于部署应用系统并以声明的方式升级应用。后面会有专门专题详细研究。

Namespace。

Namespace(命名空间)在很多情况下用于实现多租户的资源隔离。通过Kubernetes的资源配额管理,限定不同租户能占用的资源。

Docker的一些知识:“Namespace 做隔离,Cgroups 做限制,rootfs 做文件系统”,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值