YAML

我们使用YAML,是因为它像XML或json一样,是利于读写、描述数据的一种数据格式

YAML的基本规则

  1. 大小写铭感
  2. 使用缩进表示层级关系
  3. 禁止使用TAB键,只能空格
  4. 对缩进没有要求,只要对齐就可以
  5. #表示注释
  6. 字符串可以不用引号标注

数据类型

对象(MAP):

使用冒号加上空格(: )表示一对键值,同一缩进的所有键值属于同一层级的对象

 age: 12
 name: abc

json: {‘age’: 12, ‘name’: ‘abc’}

数组

使用连字符加空格表示(- )

- a
- b
- 12

json:{‘a’, ‘b’, 12}

文件

对于Ansible,每一个YAML文件的开始都要写一个(—),表示的是一个稳健的开始

---
# 这是一个员工的信息
name: zhangsan
job: DevOps
skill: sdgsd
foods:
 - apple
 - orange
 - mantou
languages:
 python: good
 c++: good
 c: good

环境安装配置

docker pull redis:latest
docker pull wumingjian/gb-frontend:v3
docker pull forestgun007/gb-redisslave:v1

k8s+YAML的应用

创建一个redis-master、两个redis-slave、三个frontend
一个slave(集群)加一个deployment(代替RC)

# redis-master-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  replicas: 1
  selector:
    name: redis-master
  template:
    metadata:
      labels:
        name: redis-master
    spec:
      containers:
      - name: master
        image: redis
        ports:
        - containerPort: 6379
# redis-master-serice.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  ports:
    # the port that this service should serve on
  - port: 6379
    targetPort: 6379
  selector:
    name: redis-master

kubectl create -f redis-master-controller.yaml
kubectl create -f redis-master-service.yaml
kubectl get rc
kubectl get pod

# redis-slave-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      labels:
        name: redis-slave
    spec:
      containers:
      - name: worker
        image: forestgun007/gb-redisslave:v1
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 6379
redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  ports:
  - port: 6379
  selector:
    name: redis-slave

kubectl create -f redis-slave-controller.yaml
kubectl create -f redis-slave-service.yaml
kubectl get rc
kubectl get pod

# frontend-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  replicas: 3
  selector:
    name: frontend
  template:
    metadata:
      labels:
        name: frontend
    spec:
      containers:
      - name: frontend
        image: wumingjian/gb-frontend:v3  
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 80
# frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    name: fronted
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30001
  selector:
    name: frontend

kubectl create -f frontend-controller.yaml
kubectl create -f frontend-service.yaml
kubectl get rc

ip:30001

数据库主仆设置

# 在/etc/my.cnf写入
# 启用二进制日志
log-bin=mysql-bin-master
# 本机数据库ID标示
server-id=1
# 可以被从服务器复制的库。二进制需要同步的数据库名
binlog-do-db=k8s
# 不可以被从服务器复制的库
binlog-ignore-db=mysql

# wq保存退出/etc/my.cnf
systemctl restart mariadb
mysql -u root -p 
use 数据库
grant replication slave on *.* to 'slave'@'%' identified by "admin";
# 查看信息(仆的设置需要用到里面查询出的信息)
show master status;

# 导出数据库
mysqldump -uroot -p k8s > /root/k8s.sql

# 在/etc/my.cnf写入
# 从服务器id号,不能喝别的server-id一致,包括主服务器
server-id=2
# 设置该数据库是只读状态
read-only=on
# 日志
relay-log=relay-bin

# 检查是否可以登录成功
mysql -uslave -padmin -h 192.168.14.145

mysql -u root -p k8s < /root/k8s.sql
change master to master_host='192.168.14.145',master_user='slave',master_password='admin', master_log_file='mysql-bin-master.000006',master_log_pos=456;
如果保报错:This operation cannot be performed with a running slave; run STOP SLAVE first
执行:stop slave; reset slave;
START SLAVE;
SHOW SLAVE STATUS\G

最后这里要有两个yes需要有两个yes

检测

# 在仆节点写入数据

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐