javascript 闭包与模块

闭包 closure :

Observational: closure is a function instance remembering its outer variables even as that function is passed around and invoked in other scopes.

<pre>
    <a href="">Visit the Apress website</a>
    <p>I like <span>apples</span> and oranges.</p>
    <a class="myclass1 myclass2" href="">Visit the W3C website</a>
</pre>

var pre = document.getElementsByTagName("pre")[0].children;
add_the_handlers(pre);
// error 
var add_the_handlers = function (nodes) {
    var i;
    console.log(nodes.length);
    for (i = 0; i < nodes.length; i += 1) {
        nodes[i].onclick = function (e) {
            alert(i);
        };
    }
};
nodes 节点可以绑定到 [0-1-2下标],但是 alert 执行时实际是对闭包环境变量的链接引用,此时 i 已经变成了3,因此最后点击时的 i 总会显示为3.
// ok
var add_the_handlers = function (nodes) {
    var helper = function (i) {
        return function (e) {
            alert(i);
        };
    };
    var i;
    for (i = 0; i < nodes.length; i += 1) {
        nodes[i].onclick = helper(i);
    };
}
helper 创建了一个闭包,通过形参记录了每一个 i,同时 onclick 保持对该函数的引用,保持该词法环境一直存在。
onclick 触发时,每个执行函数都有自己对应的词法环境,每个环境里都有自己的 i 值。
// 更简单写法
var add_the_handlers = function (nodes) {
    console.log(nodes.length);
    for (let i = 0; i < nodes.length; i += 1) {
        nodes[i].onclick = function (e) {
            alert(i);
        };
    }
};
每次循环都会创建新的 i

模块,模块模式。利用函数作用域和闭包创建被绑定对象与私有成员的关联。

举例:

var serial_maker = function () {
    var prefix = '';
    var seq = 0;
    return {
        set_prefix: function (p) {
            prefix = String(p);
        },
        set_seq: function (s) {
            seq = s;
        },
        gensym: function () {
            var result = prefix + seq;
            seq += 1;
            return result;
        }
    }
}

var serialm = serial_maker()
serialm.set_prefix("qqqq")
serialm.set_seq(10000)
serialm.gensym()
->qqqq1000

prometheus 监控探索

Prometheus 组件使用

blackbox_exporter

  • 配置可以使用的协议模块 用于获取监控的设备的信息。 默认端口9115
  • 通过 restful 请求获取信息,配置文件里可配置支持各种协议。

alertmanager

  • 配置警报信息,配置上下游相关信息。默认端口9093

举例:配置企业微信的报警通知时,新建部门,应用要配置对应可见部门,否则会报错。参数 to_user 可@all。

prometheus server

  • 主服务用于加载各个服务组件,配置监控规则,监控任务目标。默认端口9090

重新加载配置: POST http://localhost:9090/-/reload

使用

  • 先后启动组件 alert、blackbox、prometheus,浏览 ~:9090/metrics 可查看指标。

web-frontend base

html:

some tips:

  • data-: 用户自定义属性
  • base 元素 http-equiv 属性,例如 <meta http-equiv=“refresh” content=“5;http://…”> 是指 5sec 后重新载入指定 url
  • target 属性, 新页面跳转显示方式 [_self, _blank]
  • 删除元素 <s></s>

css:

样式应用总结:

  • 1看层叠次序
  • 2看selector-specificity分数,酌情考虑 !import
  • 3看定义次序
  • 4看继承关系,酌情考虑 inherit
  • css中绝对单位:

  • css中相对单位:

  • 布局相关:

    • float: 浮动[left, right, none],clear:阻止浮动元素堆叠[left, right, both, none]。
    • positioning: 定位元素内容[static, relative, absolute, fixed]
    • display:元素盒类型
      — inline ,元素占据行内的一段内容
      — block,元素占据一行
      — inline-block, 同inline 一样,不同是垂直方向上会有空行
      — box model:padding, margin, border;box-sizing: 尺寸应用到元素盒子哪一部分。
      — 布局:colume 多列布局;flex 弹性盒;table表格

js:

  • === 和 == 等同和相等,看类型与值。
    基本类型只是看值,对象类型看应用【类型】
    基本类型相等测试会类型转换

  • null和undefine,空值和未定义。

  • hasOwnProperty ,排除原型属性。

  • 原型

    • Object.create 构造一个已经存在的对象的新实例。
    • Object.prototype 所有通过对象字面量创建的对象会连接
    • Function.prototype 函数对象会隐藏连接到这上面。
  • 扩充类型的功能,通过原型
    内部 this 被绑定为实际上级对象。

  • 闭包: 内部函数会应用外部函数的上下文环境。

Configure a Jenkins pipeline on Kubernetes with Github and Slack

Prerequisites

  • 这里使用的是 a free IBM Cloud account.

    Install the IBM Cloud command-line interface (CLI) to your work station.

  • 本机 Mac 使用 Docker Desktop.

    同时 Create a Docker Hub account.

  • Install a Kubernetes CLI (kubectl) on Mac
  • Install a Git Client.

    Sign up for a GitHub account.

  • Create a Slack account.

Key Procedure

  • 设置 KUBECONFIG 环境变量, 指向 cloud。

  • 验证是否可以连接到集群。
    kubectl version --short

    Client Version: v1.16.1
    Server Version: v1.14.9+IKS

  • 持久化 jenkins_home。因为这里使用的是单节点集群,所以 pv 类型选用的是 hostPath。

    kubectl apply -f jenkins-pv.yaml
    kubectl apply -f jenkins-pvc.yaml
    kubectl apply -f jenkins-deployment.yaml
    kubectl apply -f jenkins-service.yaml

  • 获取 Jenkins dashboard 服务地址
    export EXTERNAL_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address }')

    export NODE_PORT=30100

    echo $EXTERNAL_IP:$NODE_PORT

    184.172.229.55:30100

  • 获取 Jenkins admin 默认密码

    kubectl logs $(kubectl get pods --selector=app=jenkins -o=jsonpath='{.items[0].metadata.name}') jenkins

  • 配置凭据 github、dockerhub、kubeconfig、slack-notification

  • 安装插件:Slack-notification 和Kubernetes Cli Plugin

  • 配置 Jenkins Slack Notification 主要填写 Workspace, Credential。Default channel / member id 可不填,具体可在 Jenkinsfile 配置里指定,比如

    success { slackSend(channel: "#ok", message: "pluckhuang/podinfo:${env.BUILD_NUMBER} Pipeline is successfully completed.")}

Reference and resource

k8s in action summary ~3

第10章
要点:

  • Give replicated pods individual storage
  • Provide a stable identity to a pod
  • Create a StatefulSet and a corresponding headless governing Service
  • Scale and update a StatefulSet
Discover other members of the StatefulSet through DNS
  • Connect to other members through their host names
  • Forcibly delete stateful pods

···

  • 何谓有状态?就是说具有 stable identity,比如name,ip,storage。
  • dns srv 记录,类似负载均,只不过是 service -> map pod with ip

第十四章:
Qos class:

  • besteffort
  • burstable
  • guaranteed

Qos class 是 Quality of Service (QoS) classes 的简写,是当no CPU time at all and will be the first ones killed when memory needs to be freed for other pods. 时的依据处理方式。

resource requests limit and qos classes

which pods get killed first


第十八章

主要是介绍了 helm 的使用方法。helm 类似于 yum、apt,只不过是作为构建在 k8s 之上的包管理工具。

  • 期间了解 helm 遇到个小问题:

    helm install happy-panda stable/mariadb
    helm uninstall stable/mariadb
    helm install happy-panda stable/mariadb 会失败,原因是 uninstall stable/mariadb 并不会删除相关的 pvc。

k8s deployment vs statefulset

k8s in action summary ~2

第7章

传递参数有几种方式:CMD参数化、ENV环境变量、configmap。

configmap —from-file, 源可以是单个文件、json文件、整个目录、字面常量。
用整个目录对应一个应用配置 个人认为灵活性更高

传递 configmap 給容器:
  • 作为环境变量:envFrom 得到所有环境变量
  • 作为命令行参数:args
  • 作为configMap volume ,volume 中加载configmap配置,然后挂载配置到container。对于挂载到container目录上的configMap的变更会同步。文件不会。变通办法是挂载目录后再symlink文件。

第8章:通过api 获取环境参数

第9章:Deployment

重点是滚动升级

repicaController, obsolete:kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v2。 废弃使用主要是因为用 kubectl 以客户端方式执行、非声明式,容易中断掉入中间状态。

StrategyType: 更新策略
  • The Recreate strategy causes all old pods to be deleted before the new ones are created. Use this strategy when your application doesn’t support running multiple ver- sions in parallel and requires the old version to be stopped completely before the new one is started. This strategy does involve a short period of time when your app becomes completely unavailable.

  • The RollingUpdate strategy, on the other hand, removes old pods one by one, while adding new ones at the same time, keeping the application available throughout the whole process, and ensuring there’s no drop in its capacity to handle requests. This is the default strategy. The upper and lower limits for the number of pods above or below the desired replica count are configurable. You should use this strategy only when your app can handle running both the old and new version at the same time.

命令总结:
kubectl set image deployment kubia nodejs=pluckhuang/kubia:v2
kubectl rollout status deployment kubia
kubectl rollout undo deployment kubia
kubectl rollout history deployment kubia
kubectl patch deployment kubia -p '{"spec": {"minReadySeconds": 10}}'
RollingUpdate strategy properties:
  • maxSurge
  • maxUnavailable

A canary release is a technique for minimizing the risk of rolling out a bad version of an application and it affecting all your users. Instead of rolling out the new version to everyone, you replace only one or a small number of old pods with new ones. This way only a small number of users will initially hit the new version.

Deployment, minReadySeconds 属性说明:
  • This defaults to 0 (the Pod will be considered available as soon as it is ready).
  • pods ready:readiness probes of all its containers return a success.
  • 也就是说 pods ready 后,再等 minReadySeconds时间后,the Pod 才会被认为可用 。