1. docker registiry pull Error http: server gave HTTP response to HTTPS client

1
2
docker pull 192.168.0.10:5000/centos
Error response from daemon: Get https://192.168.0.10:5000/v2/: http: server gave HTTP response to HTTPS client

解决办法: 在daemon.json里面添加insecure-registries

1
2
3
4
# Mac ~/.docker/daemon.json 
# Centos: /etc/docker/daemon.json

{"experimental":false,"debug":true,"insecure-registries":["192.168.0.10:5000"]}

重启docker

背景

自己搭建的docker registry, docker pull 报错

1
filesystem layer verification failed for digest sha256:xxx

问题原因

应该是镜像的某一层layer校验不通过,参考:
https://github.com/distribution/distribution/issues/2168

解决办法

删除镜像,重新上传一次镜像

打开registry删除功能

进入到registry容器里,打开delete配置

1
2
3
4
5
6
7
8
docker container exec -it registry sh

vi /etc/docker/registry/config.yml

# 添加以下配置
storage:
delete:
enabled: true

重启容器

1
docker container restart registry
删除镜像

这里以 zcloud/calico:v3.8.2 举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看镜像列表
curl -XGET 10.38.4.212:5000/v2/_catalog
# 查看镜像的tags
curl -XGET 10.38.4.212:5000/v2/zcloud/calico/tags/list
# 查看镜像详情
curl -XGET 10.38.4.212:5000/v2/zcloud/calico/manifests/v3.8.2

# 获取镜像digest
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET 10.38.4.212:5000/v2/zcloud/calico/manifests/v3.8.2

# 删除镜像manifest
curl -I -X DELETE http://registry-internal.adp.aliyuncs.com:5000/v2/zcloud/calico/manifests/sha256:106a5b6fe7da462a562949c44d83a6327fea91bac93da1b61263113e7504963a

# 删除镜像文件
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml

重启
docker container restart registry

** 如果此时重新push同名镜像, 要等几分钟才才能pull到 **

every progrommer had his “Hello World !” in mind, as list bellow, mine

bash

1
2
#!/bin/bash
echo "Hello World!"

java

1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

C

1
2
3
4
5
6
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}

python

1
2
3
#!/usr/bin/python
print "Hello World"

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

说明

刚刚接触golang, 准备用golang gin mongodb写一个CRUD小Demo,把遇到的知识点记录下来, 给自己以后参考

定义Post struct

1
2
3
4
5
6
7
8
9
10
11
12
# module/post.go
type Post struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Title string `bson:"title,omitempty" json:"title,omitempty"`
Content string `bson:"content,omitempty" json:"content,omitempty"`
Type int `bson:"type,omitempty" json:"type,omitempty"`
Ctime int64 `bson:"ctime,omitempty" json:"ctime,omitempty"`
Utime int64 `bson:"utime,omitempty" json:"utime,omitempty"`
UserID int `bson:"user_id,omitempty" json:"user_id,omitempty"`
ReferURL string `bson:"refer_url,omitempty" json:"refer_url,omitempty"`
Extra string `bson:"extra,omitempty" json:"extra,omitempty"`
}

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 方法一
post := models.Post{primitive.NewObjectID(), "title", "content", 1, time.Now().Unix(), time.Now().Unix(), 1, "http://www.baidu.com", ""}
models.CreatePost(post)

# 方法二
var post models.Post
post.ID = primitive.NewObjectID()
post.Title = "这是一个title4"
post.Content = "这是content,有长度"
post.Type = 1
post.Ctime = time.Now().Unix()
post.Utime = time.Now().Unix()
post.UserID = 1
post.ReferURL = "http://www.baidu.com"
post.Extra = ""

models.CreatePost(post)

背景

使用Linux安装软件,有时候需要设置环境变量,有的文档建议设置在.bashrc里, 有的建议.bash_profile里
一般都能生效,那么区别是什么呢?

介绍

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

bash_profile: 登录试shell执行
bashrc: 非登录式shell执行

什么是(非)登录试shell

登录试: 当你敲入用户名和密码登录的终端, 或通过ssh登录机器. .bash_profile会被调用
非登录: 当你已经登录了机器,在桌面环境打开终端, 或者在终端里面调用了/bin/bash, 此时.bashrc会被调用

为什么会有这两个文件

比如,你希望第一次登录机器的时候,想看到机器的负载, 那么把uptime命令写到.bash_profile里面就可以了.
如果写到.bashrc里面, 你每次打开终端, 都会看到机器负载。

推荐方法

当你设置PATH环境变量的时候, 把变量写到.bashrc
在.bash_profile里面加载.bashrc

1
2
3
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

参考连接

bash_profile_vs

背景

在Linux上执行history命令默认不现实命令执行时间,对于问题排查不是很友好.
通过设置,可以在history命令前面加上命令执行时间

安装

bash

1
2
HISTTIMEFORMAT=%Y-%m-%d %H:%M:%S
HISTSIZE=1000 # 历史命令数量

zsh

1
2
HIST_STAMPS="%Y-%m-%d %H:%M:%S"
HISTSIZE=50000

加载配置

1
2
3
4
5
6
# bash
source ~/.bashrc

#zsh
source ~/.zshrc

效果

1
2
3
4
5
6
7
8
9
10
history
1 2018-07-28 16:36:10 cd /opt/workspace
2 2018-07-28 16:36:20 vim linux_history.md
3 2018-07-28 16:36:33 env
4 2018-07-28 16:36:40 vim linux_history.md
5 2018-07-28 16:37:52 ls
6 2018-07-28 16:37:56 hexo server
7 2018-07-28 16:38:19 ls
8 2018-07-28 16:38:21 vim linux_history.md

准备

配置docker registry 加速docker

配置

1
2
3
4
5
6
7
8
9
10
11
sudo vim /etc/docker/daemon.json

{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

# 重启docker
sudo systemctl restart docker
# 查看镜像是否加载,我本机没有加载成功
sudo docker info

拉取镜像

1
2
3
4

docker pull ubuntu:14.04
docker image ls # 查看本地镜像

启动实例

1
2
3
docker run ubuntu:14.04 # 如果本地没有这个image, 会从镜像仓库拉取并启动
docker run -it ubuntu:14.04 /bin/bash # 启动并进入到实例

退出实例

1
2
输入exit 或者ctrl+d 都会退出实例
如果想退出且不停止实例, 使用ctrl+p+q组合键退出

登录实例

1
2
docker [container] exec -it $containerId /bin/bash # 新启动一个bash
docker [container] attach $containerId #登录到旧的tty

制作镜像

新建一个目录,创建 Dockerfile

docker build -t 镜像名 .
docker run -p4000:80 镜像名
docker tag 镜像名 用户名/repository:tag
docker login # 输入用户名不带邮箱后缀

docker push 用户名/repository:tag

准备

Centos : Centos7 64位
docker-ce: Docker-ce-17

安装

1.卸载本地原有docker

1
sudo yum remove docker-common docker-selinux docker-engine

2.安装docker依赖软件和yum源

1
2
3
4
5
sudo yum install yum-utils
sudo yum install device-mapper-persistent-data
sodu yum instlal lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce

3.启动docker

1
sudo systemctl start docker

4.验证docker

1
sudo docker run hello-world

5.添加运行docker账号

docker的daemno会启动一个socket,只有root和docker组用户可以访问, 普通账号无法访问
所以需要把普通用户添加到docker组才可以访问

1
2
sudo groupadd docker
sudo usermod -aG docker work

6.设置开启启动

1
sudo systemctl enable docker

参考文档

准备

pyenv : 管理多个Python版本, 是从rbevn(管理多个ruby版本)改造来的
virtualenv: 管理工作空间的python环境, 可以让每个app的python环境相互隔离, 相当于给每个app都复制一份python

安装

pyenv 已经集成了 virtualenv, 所以不需要单独安装virtualenv了.

安装pyenv比较简单

1
2
3
4
5
6
7
8
# 下载到本地:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# 设置环境变量
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
# 加载环境变量
source ~/.bash_profile

使用

1
2
3
4
5
pyenv install --list #查看可安装python版本
pyenv install 2.7.12
pyenv local 2.7.12 # 将本地设置成2.7.12版本
pyenv global 2.7.12 # 将系统设置成2.7.12版本
pyenv local system # 将本地设置成系统默认版本

卸载 pyenv

1
rm -rf $(pyenv root)

virtualenv

1
2
3
4
pyenv virtualenv 2.7.12 VENV27 # 将会在pyenv的version目录下新建一个VENV27的python环境

pyenv activate VENV27 # 使用VENV27环境
pyenv deactivate VENV27 # 退出VENV27环境

设置pip源

国内访问pip默认源超时严重, 建议设置阿里或豆瓣的pip源

1
2
3
4
vim ~/.pip/pip.conf
[global]
trusted-host = mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple

Linux 普通用户su到另一个普通用户

工作中很少使用root用户,存在使用个人用户,切换到work账户的场景
比如我的个人账户是mihenkii, 要切换的目标账户是work

实现

1
2
3
4
5
6
7
8

sudo vim /etc/pam.d/su

auth [success=ignore default=1] pam_succeed_if.so user = work
auth sufficient pam_succeed_if.so use_uid user = mihenkii

# 解释: 第一行是确定目标用户是work,如果是work,且调用的用户是mihenkii,那么第二行mihenkii会成功授权切换到work

参考连接

stackexchange