init commit

This commit is contained in:
2024-03-19 01:05:51 +08:00
commit 199bbf2628
393 changed files with 34883 additions and 0 deletions

6
docs/ops/README.md Normal file
View File

@ -0,0 +1,6 @@
---
title: 运维
sidebar_position: 0
---
这里是服务器运维相关的内容包括CI/CD、监控、容器化、云原生等等。

4
docs/ops/_category_.json Normal file
View File

@ -0,0 +1,4 @@
{
"label": "OPS",
"position": 4
}

5
docs/ops/ci&cd/README.md Normal file
View File

@ -0,0 +1,5 @@
---
id: ci-cd
title: CI/CD
---

View File

@ -0,0 +1,131 @@
---
title: github actions 入门 We love open source
---
> github 推出了 ci/cd 服务 [https://github.com/features/actions](https://github.com/features/actions) 不过还在内测中,我申请了一下得到了内测资格
![](img/github-actions-入门.assets/47aa11c30a1b3c418ba1bb6f2fd16bb4.png)
## 介绍
github actions 是 github 推出的一款持续集成服务,这里记录下一些概念和基础的使用方法(gitlab 有 gitlab runner,现在 github 终于是推出了自己的 ci/cd 服务)
和 gitlab runner 等其它 ci/cd 工具最大的区别在于,允许引用其它仓库的脚本,并且拥有一个[actions 市场](https://github.com/marketplace?type=actions),这样可以避免很多重复的工作,需要的可以直接引用(才知道 github 有一个这样的市场功能.)
觉得这个和了 Docker hub 的思路是差不多的,可以引用不同的 docker 镜像.gitlab runner 中也可以使用 docker 来实现运行在不同的 ci/cd 运行环境
## 概念
先放出一个 workflow 文件的栗子:
```yaml
name: Greet Everyone
# This workflow is triggered on pushes to the repository.
on: [push]
jobs:
build:
# Job name is Greeting
name: Greeting
# This job runs on Linux
runs-on: ubuntu-latest
steps:
# This step uses GitHub's hello-world-javascript-action: https://github.com/actions/hello-world-javascript-action
- name: Hello world
uses: actions/hello-world-javascript-action@v1
with:
who-to-greet: "Mona the Octocat"
id: hello
# This step prints an output (time) from the previous step's action.
- name: Echo the greeting's time
run: echo 'The time was ${{ steps.hello.outputs.time }}.'
```
### workflow
Workflow 定义一个完整的包括测试,打包,发布,部署等流程.存放再`.github/workflow`目录下,使用 yaml 语法,一个 yaml 或者 yml 文件表示一个 workflow.(就是上面的栗子)
我在使用的时候觉得一个 workflow 文件完全可以满足我的所有需求,但是后来看例子,触发事件是写在最顶级的路径上,所以可能会牵扯到不同的事件运行不同的任务(job),然后写多个 workflow 文件
### job
一个 job 就是实际运行的任务,定义一个 测试/部署/发布 任务,多个 job 合起来就是一个完整的工作流.
### setps
任务所需要执行的步骤,就像一个测试的任务会包括,编译->代码分析->测试->覆盖率->提交结果
### action
一个 action 可以包括多条命令,也可以引用其它仓库的 action.还可以引用 docker 镜像:`uses: docker://alpine:3.8`,这样就更加的灵活了
## 命令
> [https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions](https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions)
再来了解一下会常使用到的一些基础命令,就拿上面的?来说
#### name
描述工作流
#### on
触发的事件,还可以选择定时触发,指定分支 tag 等...
```yaml
on:
schedule:
- cron: '0 * * * *'
on:
push:
branches:
- master
tags:
- v1
# file paths to consider in the event. Optional; defaults to all.
paths:
- 'test/*'
```
#### jobs.[id].name
job 名字
#### jobs.[id].runs-on
运行环境,linux,macos,windows 都有,具体可以看:[`jobs.<job_id>.runs-on`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idruns-on)
#### jobs.[id].steps.[]name
步骤名字
#### jobs.[id].steps.[]needs
job 的依赖,可以确定 job 的运行顺序
```yaml
needs: [job1, job2]
```
#### jobs.[id].steps.[]uses
去引用一个 action/docker 等
#### jobs.[id].setps.[]with
输入的参数,会传入 action 中
#### jobs.[id].setps.[]id
步骤的 id,可以通过 id 再引用到这个步骤
#### jobs.[id].setps.[]run
执行的命令
## 最后
测试项目:[https://github.com/CodFrm/StudyGit/actions](https://github.com/CodFrm/StudyGit/actions)
这里我写了一个例子上去,后面我会使用 actions 作为我公众号的一个自动部署的工具,后面再补上实际的使用?

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

View File

@ -0,0 +1,123 @@
---
title: Dockerfile和docker-compose
---
## Dockerfile
> 使用 Dockerfile 构建自己的镜像,这里记录一些常用的命令,这里拿我尝试做的一个 nginx 镜像来记录
> [https://github.com/huanl-php/docker-nginx/blob/master/Dockerfile](https://github.com/huanl-php/docker-nginx/blob/master/Dockerfile)
### FROM alpine:latest
选择基础的镜像,这里的话我用的是 alpine,后面的 latest 表示最新的版本,alpine 是一个非常小的 linux 发行版,如果你对镜像大小很敏感的话推荐使用这个镜像作为基础镜像
### LABEL
这一行表示这个 Dockerfile 的作者
### ENV
环境变量,当你没配置的时候将使用这些作为默认值
### EXPOSE
声明要暴露的端口,但是并不会直接帮你暴露出去,至少告诉 docker 你会暴露那些
### RUN
运行命令,这里用了&和\表示连接命令和换行
### COPY
COPY 顾名思义的复制文件 从本地复制到容器里面去
### CMD
当你容器启动的时候,将运行的命令,除此之外还有**ENTRYPOINT**
暂时就先记录这些,以后用到了再继续记录
### 另外提一下
使用 alpine 这个的时候有下面这个方法,安装包,卸载包,但是当时我构建我的 nginx 项目的时候 del 之后提示一些包不存在,时间久远我也不记得当时的操作了
```shell
apk add --no-cache --virtual .build-deps gcc
apk del .build-deps
```
然后我尝试了这种方法,只删除构建时需要的包,之后我发现我的镜像更小了= =不知道这样做会有什么不好。。。。
```shell
apk add --no-cache gcc
apk del gcc
```
## docker-compose.xml
> compose 用于容器快速部署,我这里用我的云签到的项目来
> [https://github.com/CodFrm/cas/blob/master/docker-compose.yml](https://github.com/CodFrm/cas/blob/master/docker-compose.yml) > [文档](https://docs.docker.com/compose/compose-file/)
### version
要求版本,不多说了
### services
容器服务
### db 和 web
都只是一个名字而已
### image
容器的镜像名字
### environment
环境变量
### networks
所属网络环境
### ports
暴露端口 外部端口:容器内部端口
### depends_on
依赖,会先构建所依赖的容器
### 根部的 networks
声明网络,没有将会创建
### cas_network
网络的名字
### driver
网络驱动类型,我这里是 bridge 桥接
## 最近用到的
### build
build Dockerfile 路径,直接通过 dockerfile 来构建了,不用 image
### volumes
和本地路径绑定 本地路径:容器内部路径
### external_links
将外部容器加入网络 链接的容器名字:本容器内叫的名字
### external
networks 下的一个配置项,允许外部访问 external:true
先记录到这里,解释是自己的简单理解\>\_\<

View File

@ -0,0 +1,5 @@
---
id: k8s-docker
title: Kubernetes & Docker
---

View File

@ -0,0 +1,4 @@
{
"label": "K8s & Docker",
"position": 1
}

View File

@ -0,0 +1,139 @@
---
title: docker容器初窥
---
> 了解一下,了解一下- -
> [官网](https://www.docker.com/) [英文文档](https://docs.docker.com/install/overview/) [中文文档](https://yeasy.gitbooks.io/docker_practice/)
## 安装
> 我吧我原来那台腾讯云 windows 的服务器换成了 centos 来尝试
> [参考教程](https://yeasy.gitbooks.io/docker_practice/install/centos.html)
```shell
# 安装docker-ce
## 依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
## 切换国内源
sudo yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
## 安装
sudo yum makecache fast
sudo yum install docker-ce -y
## 开机启动和开启docker
sudo systemctl enable docker
sudo systemctl start docker
```
执行完上面的步骤安装就算完成了,执行一次`docker run hello-world`没有提示错误就是成功了
## 镜像
> 这获取镜像感觉和 git 的命令有些相似,我先注册了一个账号,以后再看怎么使用
> [镜像仓库](https://hub.docker.com/explore/)
### pull
使用 pull 获取镜像,我们可以在镜像仓库中找一个我们喜欢的镜像,然后拉去下来,之后运行
```shell
# 命令格式
docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
# 这里我弄一个redis
docker pull redis
```
### 开启一个 redis 服务器
--name 是为这个容器命名,不然之后我们就只能用 UUID 来找了
-d 是以守护态运行,也就是后台运行,不然会直接的打印到我们终端上
我们可以用
docker logs test-redis 看运行日志
docker container ls 查看容器信息
docker attach test-redis 重新进入容器
docker container rm test-redis 删除容器
```shell
docker run --name test-redis -d redis
```
### 使用 cli 连接 redis
使用下面这个命令创建一个 redis 的容器 运行 redis-cli 这个命令
-i 使用交互式的操作
-t 打开一个终端
--rm 退出之后就删除这个容器
后面的-h 和-p 是主机(redis 就是主机名)和端口
```shell
docker run -it --rm redis redis-cli -h test-redis -p 6379
```
不过上面这个命令是不足够的 - -。。。运行之后并没有链接上我们之前创建的 redis 服务器,还需要加上一个 --link 的参数,使两个容器建立链接,然后我们就可以使用 redis 客户端了
```shell
docker run -it --link test-redis:redis --rm redis redis-cli -h test-redis
```
### 外部使用容器
我先给我的服务器装一个 redis-cli等会儿好测试
```shell
yum install redis -y
```
在容器没有创建前,我们可以用`-p`来映射端口
```shell
# -P 随机分配一个49000~49900的端口
# 可以用docker container ls来查看
# -p可以指定分配例如:
# -p 6379:6379 (映射所有地址到6379) -p 127.0.0.1:6300:6379 (映射到127.0.0.1上的6300端口)
# -p 127.0.0.1::6379(映射所有端口到ip上) -p 1813:1813/udp(指定udp端口)
# 之后可以使用docker port test-redis 查看映射情况
docker run --name test-redis -d -P redis
```
容器创建完成之后,我们可以用 iptables 来操作,不过这是很不好的,只是提一下可以使用这个方法
```shell
# 获取ip地址我们也可以直接的用这个ip来连接
docker inspect test-redis | grep IPAddress
# iptables映射端口
iptables -t nat -A DOCKER -p tcp --dport 6379 -j DNAT --to-destination 172.17.0.2:6379
```
推荐下面这个方法:
使用 commit 创建镜像,然后重新运行
```shell
# 创建了个my-redis的镜像
docker commit test-redis my-redis
# 我吧原来的停止删除,重新创建,这回用-p参数
docker stop test-redis
docker container rm test-redis
docker run --name test-redis -d -p 6379:6379 redis
```
完成,之后我们可以使用`redis-cli`直接访问了
```shell
[root@VM_92_235_centos ~]# redis-cli
127.0.0.1:6379> set haha qwe123
OK
127.0.0.1:6379> get haha
"qwe123"
127.0.0.1:6379> exit
[root@VM_92_235_centos ~]# docker run -it --link test-redis:redis --rm redis redis-cli -h test-redis
test-redis:6379> get haha
"qwe123"
test-redis:6379> exit
```
上面是我玩的,感觉还不错吧
## End
越用到后面越觉得容器的强大,最开始我想感觉有些占空间,又没什么用,用不到,所以用的云服务器学习,现在我想在我的电脑上装一个玩了。
还有很多内容,这一篇文章只是记录一些基本的操作,还有一个**Dockerfile**和 Makefile 类似,可以用来定制我们需要的镜像,这也是一个很重要的内容,后面再继续研究。