-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b11f1c2
commit 9c254e1
Showing
27 changed files
with
2,086 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,44 @@ | ||
# 服务器管理笔记 | ||
# 云服务器运维笔记 | ||
|
||
+ [云服务器的初始配置]() | ||
## 目录 | ||
|
||
### 如果没有服务器 · PaaS | ||
|
||
1. [使用 netlify 托管静态网站]() | ||
1. []() | ||
|
||
### 服务器初始配置 | ||
|
||
1. [服务器初始登录配置:ssh-config](https://github.com/shfshanyue/op-note/blob/master/init.md) | ||
1. [服务器ssh key 以及 git 的配置](https://github.com/shfshanyue/op-note/blob/master/ssh-setting.md) | ||
1. [系统信息查看相关命令](https://github.com/shfshanyue/op-note/blob/master/system-info.md) | ||
1. [使用 vim 及其配置](https://github.com/shfshanyue/op-note/blob/master/vim-config.md) | ||
1. [窗口复用与 tmux](https://github.com/shfshanyue/op-note/blob/master/tmux-config.md) | ||
1. [openvpn 配置与内网安全](https://github.com/shfshanyue/op-note/blob/master/vpn-config.md) | ||
|
||
### 自动化运维 | ||
|
||
1. [使用 ansible 做自动化运维](https://github.com/shfshanyue/op-note/blob/master/ansible-guide.md) | ||
1. [ansible 中的细节问题](https://github.com/shfshanyue/op-note/blob/master/ansible-problem.md) | ||
|
||
### docker 与应用开发 | ||
|
||
### kubernetes 与应用开发 | ||
|
||
1. [搭建一个 k8s 集群]() | ||
1. [搭建 gitlab]() | ||
1. [部署 postgres]() | ||
1. [gitlab runner 配置与简单的 gitlab ci]() | ||
|
||
### 高频 linux 命令 | ||
|
||
1. [sed](https://github.com/shfshanyue/op-note/blob/master/linux-sed.md) | ||
1. [awk](https://github.com/shfshanyue/op-note/blob/master/linux-awk.md) | ||
1. [jq](https://github.com/shfshanyue/op-note/blob/master/jq.md) | ||
1. [iptables](https://github.com/shfshanyue/op-note/blob/master/iptables.md) | ||
1. [htop](https://github.com/shfshanyue/op-note/blob/master/htop.md) | ||
|
||
### 监控 | ||
|
||
1. [linux 各项监控指标](https://github.com/shfshanyue/op-note/blob/master/linux-monitor.md) | ||
1. [linux 监控与报警]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
--- | ||
title: ansible 自动化运维指南 | ||
keywords: linux,ansible,自动化运维,ansible安装 | ||
desription: 使用 ansible 可以进行批量配置,批量安装软件,省了一大部分繁琐的重复工作,提高了管理服务器的效率。本章介绍如何使用 ansible 的安装以及关于 ansible 的基本功能。建议拥有云服务器的同学都可以学习一下 ansible | ||
date: 2019-10-23 22:00 | ||
|
||
--- | ||
|
||
# 使用 ansible 做自动化运维 | ||
|
||
使用 ansible 可以进行批量配置,批量安装软件,省了一大部分繁琐的重复工作,提高了管理服务器的效率。 | ||
|
||
本章介绍如何使用 `ansible` 的安装以及关于 ansible 的基本功能。建议拥有云服务器的同学都可以学习一下 `ansible` | ||
|
||
<!--more--> | ||
|
||
+ 原文链接: [使用 ansible 做自动化运维](https://shanyue.tech/op/ansible-guide) · [github](https://github.com/shfshanyue/op-note/blob/master/ansible-guide.md) | ||
+ 系列文章: [服务器运维笔记](https://shanyue.tech/op) · [github](https://github.com/shfshanyue/op-note) | ||
|
||
## 自动化运维的必要性 | ||
|
||
我现在有两个云服务器用来瞎折腾,装的都是 centos 系统。而我在两个服务器上都会装上 `tmux`,用作多窗口管理。 | ||
|
||
但在有了服务器的早期有可能各种乱折腾,又需要多次重装系统,而每次重装系统,又需要重装一遍 `tmux`。 | ||
|
||
这就会造成一件重复度极高的事情: 安装 `tmux`。 | ||
|
||
如果在 centos 中安装 `tmux` 能够直接使用 `yum install tmux` 也就罢了,但是安装 tmux 也是一件极为琐碎的事情。 | ||
|
||
根据我在本系列文章 [窗口复用与 tmux](https://shanyue.tech/op/tmux-setting) 中提到一个 `tmux` 的安装步骤 | ||
|
||
1. 安装依赖 package | ||
1. 在 github 下载源代码,编译安装 | ||
1. 在 github 下载配置文件 | ||
|
||
**而且,在多个服务器和多次重装过程中,有可能重复以上安装步骤 N 次。** | ||
|
||
于是自动化运维存在的意义就体现了出来,它可以直接使用一条命令便完成所有服务器的安装过程 | ||
|
||
## ansible 安装及配置 | ||
|
||
ansible 是使用 python 写的一个做自动化运维的工具。在使用 ansible 之前需要明白以下两个概念 | ||
|
||
+ 本地环境: 即你的 PC,mac 或者是跳板机,在本地环境需要安装 ansible | ||
+ 远程服务器: 在远程服务器会部署自己的服务,跑应用,也是需要被管理的服务器。在远程服务器中不需要装任何应用 | ||
|
||
ansible 工作在 ssh 协议上,它只需要满足两个条件 | ||
|
||
### 1. 在本地环境安装 ansible | ||
|
||
在 mac 上,直接通过 `brew install ansible` 就可以完成安装。 | ||
|
||
如果不是 mac,可以参考 [官方安装指南](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#intro-installation-guide) | ||
|
||
不过本地环境大多都是 `mac` 或者 `windows` | ||
|
||
### 2. 在本地能够 ssh 到远程服务器 | ||
|
||
通过配置 `~/.ssh/config` 与 `ssh key` 可以达到直连免密的效果,具体参考本系列的第一篇文章 [云服务器初始登录配置](https://shanyue.tech/op/init) | ||
|
||
`~/.ssh/config` 文件如下 | ||
|
||
``` config | ||
Host shanyue | ||
HostName 172.17.68.39 | ||
User root | ||
Host shuifeng | ||
HostName 172.17.68.40 | ||
User root | ||
``` | ||
|
||
## ansible inventory | ||
|
||
通过配置 `~/.ssh/config` 后,我们为远程服务器起了别名。此时可以通过 `inventory` 进行分组管理。 | ||
|
||
`ansible` 默认的 `inventory` 配置文件为 `/etc/ansible/hosts`。 | ||
|
||
``` ini | ||
[prod] | ||
shanyue | ||
shuifeng | ||
|
||
[dev] | ||
proxy | ||
jumper ansible_port=5555 ansible_host=192.0.2.50 | ||
``` | ||
|
||
配置释义如下 | ||
|
||
1. 总共有四台服务器,shanyue,shuifeng,proxy,jumper,所有的服务器都在分组 `all` 下 | ||
1. shanyue 与 shuifeng 在分组 `prod` 下,而 proxy 与 jumper 在分组 `dev` 下 | ||
1. 在 `inventory` 中同样可以设置 `hostname`, `port` 与别名,但是建议在 ssh-config 中进行设置 | ||
|
||
## 一个简单的 ad-hoc 命令 | ||
|
||
`ad-hoc` 命令指去特定一组服务器上执行一个命令。而一个命令实际上指的是 `module`,而最常用的 `module` 是 `ping`,用以查看服务器是否正常连通 | ||
|
||
所有的module可以参考 [ansible modules](https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html) | ||
|
||
``` shell | ||
# 查看所有服务器是否能够正常连通 | ||
$ ansible all -m ping | ||
shuifeng | SUCCESS => { | ||
"changed": false, | ||
"ping": "pong" | ||
} | ||
shanyue | SUCCESS => { | ||
"changed": false, | ||
"ping": "pong" | ||
} | ||
``` | ||
|
||
## ansible playbook | ||
|
||
`ansible ad-hoc` 执行的命令过于简单,一般用于服务器的测试工作以及一些简单的小操作。而一些复杂的事情,如上述所说的 `tmux` 的安装则需要一系列脚本来完成。 | ||
|
||
`ad-hoc` 是指定服务器执行指定命令, **而 `playbook` 是指定服务器执行一系列命令。** | ||
|
||
+ hosts,用以指定服务器分组。如 prod | ||
+ role, 用以指定一系列命令的集合。如 tmux,方便复用 | ||
|
||
``` yaml | ||
- hosts: prod | ||
roles: | ||
- tmux | ||
``` | ||
### role | ||
role 指定了一系列命令,或者称做 `tasks`。每个 `task` 都可以看做一个 `ad-hoc`,由 [ansible module](https://docs.ansible.com/ansible/latest/modules/modules_by_category.html) 组成 | ||
|
||
但是在 `task` 执行的过程中,一定会有一些变量,配置文件的设置,这就是 role 的其它组成部分。如 `defaults`,`vars`,`files` 和 `templates`。`role` 的文件结构组织如下 | ||
|
||
``` txt | ||
site.yml | ||
roles/ | ||
tmux/ | ||
tasks/ | ||
handlers/ | ||
files/ | ||
templates/ | ||
vars/ | ||
defaults/ | ||
meta/ | ||
``` | ||
|
||
比如一个 tmux 的 role 做了以下 `tasks` | ||
|
||
1. 安装依赖 package | ||
1. 在 github 下载源代码,编译安装 | ||
1. 在 github 下载配置文件 | ||
|
||
配置文件参考我的 ansible 配置: [shfshanyue/ansible-op](https://github.com/shfshanyue/ansible-op/blob/master/roles/tmux/tasks/main.yml) | ||
|
||
``` yaml | ||
- name: prepare | ||
yum: | ||
name: "{{item}}" | ||
with_items: | ||
- gcc | ||
- automake | ||
- libevent-devel | ||
- ncurses-devel | ||
- glibc-static | ||
- name: install tmux | ||
git: | ||
repo: https://github.com/tmux/tmux.git | ||
dest: ~/Documents/tmux | ||
version: 2.8 | ||
- name: make tmux | ||
shell: sh autogen.sh && ./configure && make | ||
args: | ||
chdir: ~/Documents/tmux/ | ||
- name: copy tmux | ||
copy: | ||
src: ~/Documents/tmux/tmux | ||
dest: /usr/bin/tmux | ||
remote_src: yes | ||
mode: 0755 | ||
- name: clone config file | ||
when: USE_ME | ||
git: | ||
repo: https://github.com/shfshanyue/tmux-config.git | ||
dest: ~/Documents/tmux-config | ||
- name: clone config file (from .tmux) | ||
git: | ||
repo: https://github.com/gpakosz/.tmux.git | ||
dest: ~/Documents/tmux-config | ||
when: not USE_ME | ||
- name: copy config file (from .tmux) | ||
copy: | ||
src: ~/Documents/tmux-config/.tmux.conf.local | ||
dest: ~/.tmux.conf.local | ||
remote_src: yes | ||
when: not USE_ME | ||
- name: copy config file | ||
copy: | ||
src: ~/Documents/tmux-config/.tmux.conf | ||
dest: ~/.tmux.conf | ||
remote_src: yes | ||
- name: delete tmux-config | ||
file: | ||
name: ~/Documents/tmux-config | ||
state: absent | ||
``` | ||
|
||
## ansible-galaxy | ||
|
||
即 `Role` 的仓库。 | ||
|
||
有一些高频的可复用的服务组件的部署,如 `docker`,`redis` 之类,可以在 [ansible-galaxy](https://galaxy.ansible.com) 找到,而免了自己写 `role` 的麻烦。 | ||
|
||
如 [ansible-redis](https://github.com/DavidWittman/ansible-redis) | ||
|
||
``` shell | ||
# 查找关于 redis 的所有 Role | ||
$ ansible-galaxy search redis | ||
Found 387 roles matching your search: | ||
Name Description | ||
---- ----------- | ||
0x5a17ed.ansible_role_netbox Installs and configures NetBox, a DCIM suite, in a production setting. | ||
1it.sudo Ansible role for managing sudoers | ||
75629fce.ufw High-level, service-based interface for configuring UFW | ||
aalaesar.install_nextcloud Add a new Nextcloud instance in your infrastructure. The rol | ||
... | ||
$ ansible-galaxy install davidwittman.redis | ||
``` | ||
|
||
## 小结 | ||
|
||
`ansible` 以批量配置以及软件管理见长,如果你有一台自己的服务器的话,非常建议学习 `ansible`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: 带着问题学习 ansible | ||
--- | ||
|
||
# ansible 中的细节问题 | ||
|
||
如何更快地学习某门技术? | ||
|
||
+ 学习示例,比如 ansible 可以查看 [ansible/ansible-examples](https://github.com/ansible/ansible-examples) | ||
+ 带着问题来思考 | ||
|
||
于是我总结了在我初学 ansible 时所带的一些疑问 | ||
|
||
### 当某个 task 执行错误时不中断操作 | ||
|
||
添加参数 `ignore_errors: true` | ||
|
||
``` yaml | ||
- name: install pip | ||
register: pip | ||
yum: | ||
name: python-pip | ||
ignore_errors: true | ||
``` | ||
### 如何根据 task 执行结果来作为分支条件 | ||
使用 `register` 监听当前任务执行结果,`when` 作为分支条件 | ||
|
||
### 使用 git,file 等模块比直接使用 shell 模块的优势在哪里 | ||
|
||
幂等性。如使用 shell 的话, `git clone` 两次会有报错,而 git,file 诸多模块很好地保证了特定操作的幂等性。 | ||
|
||
### 如何在 task 中根据 linux 的发行版不同而做不同的操作 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 使用 ansible 批量管理 ssh key | ||
|
||
假设你在某个云服务器厂商新入手了10台linux服务器作为生产环境,它们被命名为 shanyue01-shanyue10,此时需要他们能够互相登录,应该怎么做? | ||
|
||
本系列前两个章节,我写了两篇文章: [ssh key 管理](https://shanyue.tech/op/ssh-setting) 与 [免密登录](https://shanyue.tech/op/op) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: 使用 docker/docker-compose/k8s/helm 部署 postgres | ||
keywords: postgres,helm,docker,k8s,部署postgres,使用helm部署postgres | ||
|
||
date: 2019-10-21 20:37 | ||
|
||
--- | ||
|
||
# 使用 helm 部署 postgres | ||
|
||
## 前置知识 | ||
|
||
+ | ||
|
||
## 部署 | ||
|
||
``` shell | ||
# 使用 helm v2 部署 | ||
$ helm install stable/postgresql | ||
|
||
# 使用 helm v3 部署 | ||
$ helm install postgres stable/postgresql | ||
NAME: postgres | ||
LAST DEPLOYED: 2019-10-21 20:33:59.742081417 +0800 CST m=+2.283315306 | ||
NAMESPACE: default | ||
STATUS: deployed | ||
NOTES: | ||
** Please be patient while the chart is being deployed ** | ||
|
||
PostgreSQL can be accessed via port 5432 on the following DNS name from within your cluster: | ||
|
||
postgres-postgresql.default.svc.cluster.local - Read/Write connection | ||
To get the password for "postgres" run: | ||
|
||
export POSTGRES_PASSWORD=$(kubectl get secret --namespace default postgres-postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decode) | ||
|
||
To connect to your database run the following command: | ||
|
||
kubectl run postgres-postgresql-client --rm --tty -i --restart='Never' --namespace default --image docker.io/bitnami/postgresql:11.5.0-debian-9-r60 --env="PGPASSWORD=$POSTGRES_PASSWORD" --command -- psql --host postgres-postgresql -U postgres -p 5432 | ||
|
||
|
||
|
||
To connect to your database from outside the cluster execute the following commands: | ||
|
||
kubectl port-forward --namespace default svc/postgres-postgresql 5432:5432 & | ||
PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432 | ||
``` |
Oops, something went wrong.