Ansible自动部署kubernetes应用服务,并设置自定义启动参数
需求,在多台服务器一键部署kubernetes应用服务,并完成依赖的数据库/中间件的初始化。同时支持自定义启动参数。思路:1)将application.yml中的配置文件(如spring.application.datasource.url)变量作为ansible的变量暴露出来。2)k8s中ConfigMap中使用ansible暴露出来的参数3)k8s中Deployment设置启动参数ar...
·
需求,在多台服务器一键部署kubernetes应用服务,并完成依赖的数据库/中间件的初始化。同时支持自定义启动参数。
思路:
1)将application.yml中的配置文件(如spring.application.datasource.url)变量作为ansible的变量暴露出来。
2)k8s中ConfigMap中使用ansible暴露出来的参数
3)k8s中Deployment设置启动参数args[] 引用k8s中ConfigMap
ansible目录结构
.
└── v1.0
├── playbook.yaml
├── res
│ └── sql
│ └── local_account.sql
├── roles
│ ├── app
│ │ ├── tasks
│ │ │ └── main.yaml
│ │ ├── templates
│ │ │ └── deploy.yaml.j2
│ │ └── vars
│ │ └── main.yaml
│ └── mysqlinit
│ ├── tasks
│ │ └── main.yaml
│ └── vars
│ └── main.yaml
└── tags.yaml
playbook.yaml
- name: 1.mysqlinit
hosts: mysqlinit
roles:
- role: mysqlinit
become: yes
- name: 2.start deploy demo
hosts: app
roles:
- role: app
become: yes
mysqlinit:初始化数据库表结构等
start deploy demo:部署k8s应用服务
mysql初始化roles
- name: "create database"
shell: "mysql -h 127.0.0.1 -u{{mysql_username}} -p{{mysql_password}} -P3306 -e 'create database `cornorstone`; use `cornorstone`;CREATE TABLE user_info (
id varchar(255) NOT NULL,
name varchar(32) DEFAULT NULL,
pwd varchar(64) DEFAULT NULL,
PRIMARY KEY (id)
) ; '"
ignore_errors: True
部署k8s应用roles
vars/main.yaml (变量信息)
mysql_host: "jdbc:mysql://xxxxxx:3306/xxxx?useSSL=false"
mysql_username: "root"
mysql_password: "xxxxxx"
redis_host: "xxxxx"
redis_port: "6379"
redis_password: "123456"
templates/deploy.yaml.j2
---
apiVersion: v1
kind: Service
metadata:
name: show-demo-service
spec:
type: NodePort
selector:
app: show-demo
ports:
- port: 80
targetPort: 8088
nodePort: 30089
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: show-demo-deployment
spec:
replicas: 2
template:
metadata:
labels:
app: show-demo
spec:
containers:
- name: show-demo
image: registry.cn-shanghai.aliyuncs.com/glodoncornerstone/show_demo:latest
imagePullPolicy: IfNotPresent
args: [
"--spring.datasource.master.jdbc-url=$(mysql_host)",
"--spring.datasource.master.username=$(mysql_username)",
"--spring.datasource.master.password=$(mysql_password)",
"--spring.datasource.slave.jdbc-url=$(mysql_host)",
"--spring.datasource.slave.username=$(mysql_username)",
"--spring.datasource.slave.password=$(mysql_password)",
"--spring.redis.host=$(redis_host)",
"--spring.redis.port=$(redis_port)",
"--spring.redis.password=$(redis_password)"
]
env:
- name: mysql_host
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: mysql_host
- name: mysql_username
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: mysql_username
- name: mysql_password
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: mysql_password
- name: redis_host
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: redis_host
- name: redis_port
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: redis_port
- name: redis_password
valueFrom:
configMapKeyRef:
name: show-demo-configmap
key: redis_password
ports:
- containerPort: 8088
---
apiVersion: v1
kind: ConfigMap
metadata:
name: show-demo-configmap
data:
mysql_host: "{{mysql_host}}"
mysql_username: "{{mysql_username}}"
mysql_password: "{{mysql_password}}"
redis_host: "{{redis_host}}"
redis_port: "{{redis_port}}"
redis_password: "{{redis_password}}"
tasks/main.yaml
- name: init deploy.yaml
shell: "kubectl delete -f /deploy.yaml"
ignore_errors: True
- name: "copy deploy.yaml"
template:
src: "deploy.yaml.j2"
dest: "/"
- name: remane deploy.yaml
shell: "cp /deploy.yaml.j2 /deploy.yaml"
ignore_errors: True
- name: execute apply -f deploy.yaml
shell: "kubectl apply -f /deploy.yaml"
ignore_errors: True
更多推荐
已为社区贡献1条内容
所有评论(0)