第十六周

1、搭建DHCP服务,为局域网内用户提供10.1.1.0/24网段的IP,且租约期默认为48小时

yum install dhcp

vim /etc/dhcp/dhcpd.conf

option domain-name "example.org";

option domain-name-servers 10.1.1.10;

default-lease-time 172800;

max-lease-time 691200;

log-facility local7;

subnet 10.1.1.0 netmask 255.255.255.0 {

      range 10.1.1.100 10.1.1.200;

      option routers 10.1.1.2;

}

systemctl start dhcpd

客户端便可自动获取ip

2、在上述实验基础上,实现DHCP中继

DHCP服务器:

vim /etc/dhcp/dhcpd.conf

option domain-name "example.org";

option domain-name-servers 10.1.1.10;

default-lease-time 172800;

max-lease-time 691200;

log-facility local7;

subnet 10.1.1.0 netmask 255.255.255.0 {

      range 10.1.1.100 10.1.1.200;

      option routers 10.1.1.2;

}

subnet 10.1.2.0 netmask 255.255.255.0 {

      range 10.1.2.100 10.1.2.200;

      option routers 10.1.2.2;

      option domain-name-servers 10.1.2.10;

}

route add -net 10.1.2.0 netmask 255.255.255.0 ens33

systemctl restart dhcpd

systemctl restart network

DHCP中继:两个网卡,一个与dhcp服务器同一网络,一个与客户端在另外一个网络

yum install dhcp

vim /etc/sysctl.conf

net.ipv4.ip_forward=1

sysctl -p

/usr/sbin/dhcrelay 10.1.1.10

客户端:可以自动获取到 10.1.2.0 的网络

3、借助Ansible Playbook自动化搭建LNMP环境(可借助yum)

yum install ansible

vim /etc/ansible/hosts

[LNMPserver]

192.168.0.137

ssh-keygen -t rsa -P ""

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

vim /etc/ansible/playbooks/lnmp.yaml

- hosts: LNMPserver

  remote_user: root

  tasks:

  - name: install {{ lnmp }}

    yum: name={{ lnmp }} state=latest

    vars:

    lnmp:

      - php

      - httpd

      - mariadb-server

      - php-mysql

      - php-fpm

      - php-mbstring

      - php-mcrypt

    when: ansible_os_family == "RedHat"

  - name: copy phpmyadmin file

    copy: src=/root/phpMyAdmin-4.0.10.20-all-languages/  dest=/var/www/html/pma/

  - name: copy mysql file

    copy: src=/etc/my.cnf.d/server.cnf  dest=/etc/my.cnf.d/

  - name: start httpd

    service: name=httpd state=started

  - name: start mariadb

    service: name=mariadb state=started

  - name: modify mysql passwd login

    shell: mysqladmin -uroot  password "ouyang0810"

ansible-playbook /etc/ansible/playbooks/lnmp.yaml

在客户端浏览即可:

http://192.168.0.137/pma/index.php

4、采用Ansible Role方式自动化搭建LNMP

yum install ansible

vim /etc/ansible/hosts

[LNMPserver]

192.168.0.137

ssh-keygen -t rsa -P ""

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

mkdir -p /etc/ansible/roles/lnmp/{tasks,vars,templates,files,handlers,meta,default}

cp -a /root/phpMyAdmin-4.0.10.20-all-languages/* /etc/ansible/roles/lnmp/files/pma/

mkdir -p /etc/ansible/roles/lnmp/{files,handlers,tasks,templates,vars}

vim /etc/ansible/playbooks/lnmp.yaml

- hosts: LNMPserver

  remote_user: root

  roles:

  - lnmp

vim /etc/ansible/roles/lnmp/tasks/main.yaml

- name: install {{ lnmp }}

  yum: name={{ lnmp }}  state=installed

  when: ansible_os_family == "RedHat"

- name: copy lnmap zip

  copy: src=phpMyAdmin-4.0.10.20-all-languages.zip dest=/var/www/html/

- name: unzip phpmyadmin

  shell: cd /var/www/html;unzip phpMyAdmin-4.0.10.20-all-languages.zip;cp phpMyAdmin-4.0.10.20-all-languages/config.sample.inc.php phpMyAdmin-4.0.10.20-all-languages/config.inc.php;ln -s phpMyAdmin-4.0.10.20-all-languages pma

- name: nginx template

  template: src=server.conf.j2 dest=/etc/my.cnf.d/

  tags: conf

  notify: restart mariadb

- name: start httpd

  service: name=httpd state=started

- name: start mariadb

  service: name=mariadb state=started

- name: modify mysql passwd login

  shell: mysqladmin -uroot  password "ouyang0810"

vim /etc/ansible/roles/lnmp/handlers/main.yaml

- name: restart mariadb

  service: name=mariadb state=restarted

vim /etc/ansible/roles/lnmp/templates/server.conf.j2 

[mysqld]

log_bin=mysql-bin

innodb_file_per_table=ON

skip_name_resolve=ON

vim /etc/ansible/roles/lnmp/vars/main.yaml

lnmp:

    - php

    - httpd

    - mariadb-server

    - php-mysql

    - php-fpm

    - php-mbstring

    - php-mcrypt

ansible-playbook  /etc/ansible/playbooks/lnmp.yaml

在客户端浏览即可:

http://192.168.0.137/pma/index.php

你可能感兴趣的:(第十六周)