full_container_scheme/other/demo-cmdb部署.md

121 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 部署管理服务
## 镜像
```test
lghost/cmdb
or
harbor.opsbase.cn/public/cmdb:latest
```
## Demo
```yml
apiVersion: v1
kind: Pod
metadata:
name: cmdb
namespace: test
labels:
component: cmdb
spec:
containers:
- name: cmdb
image: harbor.opsbase.cn/public/cmdb:latest
env:
- name: MYSQL_HOST # 指定root用户的用户名
value: "127.0.0.1"
- name: MYSQL_PASSWD
value: "123456"
ports:
- containerPort: 8000
- name: mysql
image: mysql:5.7
args:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_DATABASE
value: "cmdb"
```
## 创建和访问Pod
```text
# 创建namespace, namespace是逻辑上的资源池
kubectl create namespace test
# 使用指定文件创建Pod
kubectl create -f pod.yml
# 清空pod
kubectl delete pod --all -n test
# 查看pod可以简写po
kubectl -n test get pod
kubectl -n test get po -o wide
# 进入pod
kubectl -n test exec -it cmdb -c cmdb sh
# 初始化
kubectl -n test exec -it cmdb -c cmdb -- python init.py
# 进入db
kubectl -n test exec -it cmdb -c mysql bash
> mysql -p
> show databases;
```
### curl 测试接口
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"username":"admin",
"password": "123456"
}' \
http://10.244.2.10:8000/api/oauth/login/
```
### Infra容器
```bash
docker ps -a |grep cmdb
# 在n容器node节点上 发现有三个容器
kubectl -n test exec -it cmdb -c cmdb -- ifconfig
kubectl -n test exec -it cmdb -c mysql -- sh
```
>为了实现Pod内部的容器可以通过localhost通信每个Pod都会启动Infra容器然后Pod内部的其他容器的网络空间会共享该Infra容器的网络空间(Docker网络的container模式)Infra容器只需要hang住网络空间不需要额外的功能因此资源消耗极低。
pod容器命名: k8s_<container_name>_<pod_name>_<namespace>_<random_string>
### 查看pod详细信息
```
# 查看pod调度节点及pod_ip
kubectl -n test get pods -o wide
# 查看完整的yaml
kubectl -n test get po cmdb -o yaml
# 查看pod的明细信息及事件
kubectl -n test describe pod cmdb
```
### 更新服务版本
```
kubectl apply -f demo-pod.yaml
```
### 删除Pod服务
```
# 根据文件删除
kubectl delete -f demo-pod.yaml
# 根据pod_name删除
kubectl -n <namespace> delete pod <pod_name>
```
## mysql + app 拆分
kubectl delete pod --all -n test
kubectl create -f Deployment/two-pod/mysql.yml