使用microk8s搭建单机k8s环境踩过的坑

环境

阿里云国内 ubuntu18.04,因为k8s的镜像在k8s.gcr.io下,这个地址指向的是谷歌的服务器,所以被墙了,国内无法正常访问此仓库。而且microk8s版本更新,导致目前网上找到的方法无效,是针对老版本的,目前我使用的是v1.15.0。

安装步骤

  1. 安装microk8s

    1
    2
    apt install snapd
    snap install microk8s --classic
  2. 查看microk8s状态

    1
    microk8s.status

在这里你可以看到插件的安装情况

  1. microk8s.kubectl重命名为kubectl
    1
    snap alias microk8s.kubectl kubectl

以上为正常的安装步骤,请参考官网 https://microk8s.io/docs/

给microk8s配梯子

  1. 安装ss

    1
    2
    apt-get install python-pip
    pip install git+https://github.com/shadowsocks/shadowsocks.git@master
  2. 连接梯子

    1
    nohup sslocal -s (ip) -p (port) -k (password) &
  3. 设置microk8s通过梯子拉取镜像。编辑 /var/snap/microk8s/current/args/containerd-env

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # To start containerd behind a proxy you need to add an HTTPS_PROXY
    # environment variable in this file. HTTPS_PROXY is of the following form:
    # HTTPS_PROXY=http://username:password@proxy:port/
    # where username: and password@ are optional. eg:
    #
    HTTPS_PROXY=socks5://127.0.0.1:1080
    #
    #
    # Remember to restart the containerd daemon after editing this file:
    #
    # sudo systemctl restart snap.microk8s.daemon-containerd.service
    #
    #

    # Attempt to change the maximum number of open file descriptors
    # this get inherited to the running containers
    #
    ulimit -n 65536 || true

这里注意,之前版本是修改 /var/snap/microk8s/current/args/dockerd-env,新版本有已经没有这个文件了。目前我安装的microk8s版本是v1.15.0,不排除可能在以后的版本发生变化。

  1. 重启启动daemon-containerd服务
    1
    sudo systemctl restart snap.microk8s.daemon-containerd.service
分享到

使用NODE写爬虫,抓取搜狗词库

使用到的库

  1. cheerio (俗称node版的JQuery)
  2. axios (网络请求库)

获取所有词库列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as cheerio from 'cheerio';
import axios from 'axios';

const host = 'https://pinyin.sogou.com';

export async function getLexicons() {
console.info('开始爬取搜狗词库列表...');
const startTime = new Date();
const result = [];
let $ = await getPage('/dict/cate/index');
const cateUrls = [];
$('.nav_list a').each((idx, ele) => cateUrls.push($(ele).attr('href')));
for (let pageUrl of cateUrls) {
const cateId = pageUrl.split('/cate/index/')[1].split('?')[0];
while (pageUrl) {
$ = await getPage(pageUrl);
$('.dict_detail_block').each((idx, ele) => {
const titleEle = $(ele).children('.dict_detail_title_block').first().children().first().children().first();
const name = $(titleEle).text();
const hrefSplit = $(titleEle).attr('href').split('/');
const cid = hrefSplit[hrefSplit.length - 1];
const updateTime = $(ele).children('.dict_detail_show').first().children().first().children().last().text();
result.push({
cid,
name,
cateId,
version: updateTime,
});
});
$('#dict_page_list ul li span a').each((idx, ele) => {
if (($(ele).text() === '下一页')) {
pageUrl = $(ele).attr('href');
} else {
pageUrl = null;
}
});
}
}
console.info(`爬取搜狗词库列表完成,共${result.length}条,耗时 ${new Date().getTime() - startTime.getTime()}ms`);
return result;
}

async function getPage(url: string): Promise<CheerioStatic> {
try {
const res = await axios.get(host + url);
return cheerio.load(res.data);
} catch (error) {
console.warn(error.message);
console.info('重试中...');
return getPage(url);
}
}

下载并解析词库文件

使用SCEL包解析搜狗词库

1
git@github.com:coldcafe/SCEL.git

获取词库中的所有词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
async getWords(id: string, name: string): Promise<any[]> {
console.time('加载词库');
const url = `http://download.pinyin.sogou.com/dict/download_cell.php?id=${d}&name=${name}`;
return new Promise((resolve, reject) => {
try {
http.get(encodeURI(url), res => {
const data = [];
res.on('data', [].push.bind(data)).on('end', () => {
const buf = Buffer.concat(data);
const dict = SCEL.parseBuffer(buf);
console.timeEnd('加载词库');
resolve(dict.words);
});
});
} catch (error) {
reject(error);
}
});
}

分享到

MAC系统下vscode搭建java开发环境

  1. 安装jdk 1.8。
  2. 安装maven,直接使用Homebrew安装。

    1
    brew install maven
  3. 安装vscode,以及相关插件。

  • Language Support for Java(TM) by Red Hat
  • Debugger for Java
  • Maven for Java
  • Java Test Runner
  1. maven配置私服。
    MAC系统,maven的仓库目录默认在~/.m2下。
    maven配置,我们需要在~/.m2下添加settings.xml文件。
    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <localRepository>你的本地仓库地址(~/.m2/repository)</localRepository>

    <pluginGroups>
    </pluginGroups>
    <proxies>
    </proxies>
    <servers>
    <server>
    <id>nexus</id>
    <username>********</username>
    <password>********</password>
    </server>
    </servers>
    <mirrors>
    <mirror>
    <id>nexus</id>
    <mirrorOf>*</mirrorOf>
    <name>maven-public</name>
    <url>你的私有仓库URL</url>
    </mirror>
    </mirrors>
    <profiles>
    <profile>
    <id>nexus</id>
    <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
    </activation>
    <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
    <repositories>
    <repository>
    <id>nexus</id>
    <name>local private nexus</name>
    <url>你的私有仓库URL</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>releases</id>
    <name>local private nexus</name>
    <url>你的私有仓库URL</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>
    </profile>
    </profiles>
    <activeProfiles>
    <activeProfile>nexus</activeProfile>
    </activeProfiles>
    </settings>
  2. java应用日志输出调成使用terminal输出。(根据个人喜好设置)
    修改项目的.vscode目录下的launch.json文件,添加 “console”: “integratedTerminal”。
    示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    {
    "configurations": [
    {
    "type": "java",
    "name": "CodeLens (Launch) - Application",
    "request": "launch",
    "mainClass": "com.***.Application",
    "projectName": "saas",
    "console": "integratedTerminal",
    "env": {
    "SAAS_ENV": "DEV"
    }
    }
    ]
    }

现在开始你的JAVA编程之旅吧!

分享到

用python写出动听的歌声

准备

  1. 一首歌的谱子
  2. 所有要用到音阶的声音频率
  3. python环境

行动

  1. 首先我们需要一个生成正弦波的函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    def wv(t=0,f=0,v=0.5,wf=ff,sr=8000):
    '''
    t:写入时长
    f:声音频率
    v:音量
    wf:一个可以写入的音频文件
    sr:采样率
    '''
    tt=0
    dt=1.0/sr

    while tt<=t:
    s=math.sin(tt*math.pi*f)*v*(2**15)#采样,调节音量,映射到[-2^15,2^15)
    s=int(s)
    fd=struct.pack("h",s)#转换成8bit二进制数据
    wf.writeframes(fd)#写入音频文件
    tt+=dt#时间流逝
  2. 然后我们准备好简谱,然后翻译成对应的声音频率

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 使用到的音阶的频率
    note={"1":262,"2":294,"3":330,"4":349,"5":392,"6":440,"7":494,"6-":220,"0":0}
    # 简谱
    n=[
    "1","2","3","1","1","2","3","1","0",
    "3","4","5","0","3","4","5","0",
    "5","6","5","4","3","1","0","5","6","5","4","3","1","0",
    "2","6-","1","0","2","6-","1"
    ]
    # 音长
    tm=[
    2,2,2,2,2,2,2,2,1,
    2,2,2,1.5,2,2,2,2,
    1,1,1,1,2,2,1,1,1,1,1,1,2,1,
    2,2,2,2,2,2,2
    ]
  3. 执行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import wave
    import math
    import struct
    import matplotlib.pyplot as plt
    import numpy as np
    ff=wave.open("v1.wav","w")
    ff.setframerate(8000)
    ff.setnchannels(1)
    ff.setsampwidth(2)

    for i in range(len(n)):
    wv(tm[i]/4.0,note[n[i]])

    ff.close()
分享到

使用python计算各音阶频率

准备

基本的乐理知识

首先,我们要知道十二平均律。
十二平均律是音乐中最底层的系统,规定了两个单音的相对音高,就像计算机中的二进制系统规定了各种运算方式一样。简单来讲,十二平均律体系将一个“纯八度”分成12份,每份称为1个半音,两份为1个全音,以此定出所有中间的单音。

其次是我们要计算的音阶 CDEFGAB,也就是对应我们常说的 do re mi fa so la si。

那他们之间的应对关系如下图所示:

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
纯八度 小二度 大二度 小三度 大三度 纯四度 三全音 纯五度 小六度 大六度 小七度 大七度 纯八度
C #C D #D E F #F G #G A #A B C
do re mi fa so la si do

每两个相邻的单音之间是等比关系,比例是 $$2^\frac{1}{12}$$
国际标准的A4音的频率为440Hz,根据这个频率我们就可以计算出其他的音阶了。

行动

废话不多说,show me your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math

A3=220
_A3=A3*2**(1.0/12.0)
B3=A3*2**(2.0/12.0)
C4=A3*2**(3.0/12.0)
_C4=A3*2**(4.0/12.0)
D4=A3*2**(5.0/12.0)
_D4=A3*2**(6.0/12.0)
E4=A3*2**(7.0/12.0)
F4=A3*2**(8.0/12.0)
_F4=A3*2**(9.0/12.0)
G4=A3*2**(10.0/12.0)
_G4=A3*2**(11.0/12.0)
A4=440
_A4=A4*2**(1.0/12.0)
B4=A4*2**(2.0/12.0)
print C4, D4, E4, F4, G4, A4, B4

输出:

1
261.625565301 293.664767917 329.627556913 349.228231433 391.995435982 440 493.883301256

分享到