虚拟机基本环境

2021-02-17  本文已影响0人  jeffrey_hjf
CENTOS7

目录:
一、Vagrant 环境
二、Centos7 基础环境
三、JAVA 运行环境
四、Docker 环境
五、Kubernetes(k8s) 环境

一、vagrant环境

  1. 准备工作 (Windows)
  1. 把虚拟机加载到box容器中
//语法:vagrant box add [name] [xxx.box]
vagrant box add centos7 xxxx.box

centos7是给虚拟机起的名字 ,随意写。然后可以通过以下命令查看,当前vagrant下有那些可用

e:\vagrant_box> vagrant box list
centos7 (virtualbox, 0)
  1. 创建Vagrant主机
    通过vagrant的Vagrantfile新建三台主机
主机名称 角色 地址
k8s-master k8s-master 192.168.56.101
k8s-node1 k8s-node1 192.168.56.102
k8s-node2 k8s-node2 192.168.56.103

另存为:Vagrantfile 文件

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "k8s-master",
        :eth1 => "192.168.56.101",
        :mem => "3072",
        :cpu => "2"
    },
    {
        :name => "k8s-node1",
        :eth1 => "192.168.56.102",
        :mem => "3072",
        :cpu => "2"
    },
    {
        :name => "k8s-node2",
        :eth1 => "192.168.56.103",
        :mem => "3072",
        :cpu => "2"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos7"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :public_network, ip: opts[:eth1]
      end
  end

  config.vm.synced_folder "./labs", "/home/vagrant/labs"
  #config.vm.synced_folder "F:/linuxdir", "/linuxdir"
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end
  1. 启动虚拟机
vagrant up

输入默认用户名 : vagrant / root , 登录密码 : vagrant / vagrant

  1. Vagrant命令详解

启动虚拟机:vagrant up(第一次是)
删除虚拟机:vagrant destroy
暂停虚拟机:vagrant suspend(推荐)
关闭虚拟机:vagrant halt (耗时不推荐)
恢复被暂停的虚拟机:vagrant resume
重启虚拟机:vagrant reload(每次改了Vagrantfile配置文件记得重启)
查看虚拟机状态:vagrant status
执行shell命令:vagrant provision(用于执行一键部署)

命令 作用
vagrant box add 添加box的操作
vagrant init 初始化box的操作,会生成vagrant的配置文件Vagrantfile
vagrant up 启动本地环境
vagrant ssh 通过 ssh 登录本地环境所在虚拟机
vagrant halt 关闭本地环境
vagrant suspend 暂停本地环境
vagrant resume 恢复本地环境
vagrant reload 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy 彻底移除本地环境
vagrant box list 显示当前已经添加的box列表
vagrant box remove 删除相应的box
vagrant package 打包命令,可以把当前的运行的虚拟机环境进行打包
vagrant plugin 用于安装卸载插件
vagrant status 获取当前虚拟机的状态
vagrant global-status 显示当前用户Vagrant的所有环境状态

二、Centos7基础环境

另存为:setup.sh

#/bin/sh

# install some tools
sudo yum install -y git vim gcc glibc-static telnet


# 关闭防火墙
iptables -F
systemctl stop firewalld
systemctl disable firewalld


# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config  #永久
setenforce 0  #临时


# 关闭swap(k8s禁止虚拟内存以提高性能)
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
swapoff -a #临时


# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.56.101 k8s-master
192.168.56.102 k8s-node1
192.168.56.103 k8s-node2
EOF


# 禁止iptables对bridge数据进行处理(kubeadm初始化时会检测该选项)
## 开启内核模块
modprobe br_netfilter
# 设置网桥参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

#生效
sysctl --system 
sysctl -p  /etc/sysctl.d/k8s.conf

# 时间同步
yum install ntpdate -y
ntpdate time.windows.com

三、JAVA运行环境

1. JDK:

a. 下载jdk

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

b. 解压jdk

tar -zxvf jdk-8u101-linux-x64.tar.gz -C /usr/local/java/

c. 配置环境变量(在/etc/profile文件末尾添加)


打开 vim /etc/profile
添加一下内容

export JAVA_HOME=/usr/local/java/jdk1.8.0_211
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

d. 使/etc/profile生效

source /etc/profile

e. 验证

[root@localhost local]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

2. 安装MAVEN:

a. 下载MAVEN

https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz

b. 解压MAVEN

tar -zxvf apache-maven-3.6.2-bin.tar.gz -C /usr/local/maven/

c. 配置环境变量(在/etc/profile文件末尾添加)

export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=${MAVEN_HOME}/bin:$PATH

d. 使/etc/profile生效

source /etc/profile

e. 验证

[root@localhost local]# mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /usr/local/maven/apache-maven-3.6.2
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/java/jdk1.8.0_211/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.4.1.el7.x86_64", arch: "amd64", family: "unix"

3. 安装git:

yum install git -y

四、docker 环境

1. 配置阿里云yum源(参考:http://mirrors.aliyun.com/help/centos):

a. 备份

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mkdir repo_bak
[root@localhost yum.repos.d]# mv *.repo repo_bak/
[root@localhost yum.repos.d]# ls
repo_bak

b. 下载新的CentOS-Base.repo 到/etc/yum.repos.d/

[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2018-02-09 16:33:46--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 124.14.2.234, 124.14.2.235, 124.14.2.217, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|124.14.2.234|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2573 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[========================================================================================>] 2,573       --.-K/s   in 0s      

2018-02-09 16:33:47 (182 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2573/2573]

[root@localhost yum.repos.d]# ls
CentOS-Base.repo  repo_bak

c. 之后运行yum clean all 清除缓存,运行 yum makecache 生成新的缓存

[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors


[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
base                                                                                                       | 3.6 kB  00:00:00     
extras                                                                                                     | 3.4 kB  00:00:00     
updates                                                                                                    | 3.4 kB  00:00:00     
(1/12): base/7/x86_64/group_gz                                                                             | 156 kB  00:00:00     
(2/12): extras/7/x86_64/filelists_db                                                                       | 636 kB  00:00:07     
(3/12): extras/7/x86_64/primary_db                                                                         | 166 kB  00:00:02   

2. 安装EPEL(Extra Packages for Enterprise Linux )源:

a. 安装EPEL源

[root@localhost yum.repos.d]# yum list | grep epel-release
epel-release.noarch                         7-9                        extras   


[root@localhost yum.repos.d]# yum install -y epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================
 Package                             Arch                          Version                    Repository                     Size
==================================================================================================================================
Installing:
 epel-release                        noarch                        7-9                        extras                         14 k

Transaction Summary
==================================================================================================================================
Install  1 Package

Total download size: 14 k
Installed size: 24 k
Downloading packages:
epel-release-7-9.noarch.rpm                                                                                |  14 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-9.noarch                                                                                        1/1 
  Verifying  : epel-release-7-9.noarch                                                                                        1/1 

Installed:
  epel-release.noarch 0:7-9                                                                                                       

Complete!


[root@localhost yum.repos.d]# ls                      #多了epel.repo和epel-testing.repo
CentOS-Base.repo  epel.repo  epel-testing.repo  repo_bak

b. 再次运行yum clean all 清除缓存,运行 yum makecache 生成新的缓存
c. 查看启用的yum源和所有的yum源

[root@localhost yum.repos.d]# yum repolist enabled               #查看启用的仓库
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                        repo name                                                                    status
base/7/x86_64                                  CentOS-7 - Base - mirrors.aliyun.com                                          9,591
epel/x86_64                                    Extra Packages for Enterprise Linux 7 - x86_64                               12,277
extras/7/x86_64                                CentOS-7 - Extras - mirrors.aliyun.com                                          388
updates/7/x86_64                               CentOS-7 - Updates - mirrors.aliyun.com                                       1,929
repolist: 24,185


[root@localhost yum.repos.d]# yum repolist all                          #查看所有的仓库
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                repo name                                                                   status
base/7/x86_64                          CentOS-7 - Base - mirrors.aliyun.com                                        enabled:  9,591
centosplus/7/x86_64                    CentOS-7 - Plus - mirrors.aliyun.com                                        disabled
contrib/7/x86_64                       CentOS-7 - Contrib - mirrors.aliyun.com                                     disabled
epel/x86_64                            Extra Packages for Enterprise Linux 7 - x86_64                              enabled: 12,277
epel-debuginfo/x86_64                  Extra Packages for Enterprise Linux 7 - x86_64 - Debug                      disabled
epel-source/x86_64                     Extra Packages for Enterprise Linux 7 - x86_64 - Source                     disabled
epel-testing/x86_64                    Extra Packages for Enterprise Linux 7 - Testing - x86_64                    disabled
epel-testing-debuginfo/x86_64          Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug            disabled
epel-testing-source/x86_64             Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Source           disabled
extras/7/x86_64                        CentOS-7 - Extras - mirrors.aliyun.com                                      enabled:    388
updates/7/x86_64                       CentOS-7 - Updates - mirrors.aliyun.com                                     enabled:  1,929
repolist: 24,185

3. 卸载旧版本:

较旧的Docker版本称为docker或docker-engine。如果已安装这些程序,请卸载它们以及相关的依赖项。

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

4. 安装存储库

此操作要求服务器可以连接网络,使用 yum 进行安装,也是官网推荐的方法;


sudo yum install -y yum-utils


sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

5. 安装 docker 引擎

真正的安装,从此处开始

sudo yum install docker-ce docker-ce-cli containerd.io

6. 启动Docker

sudo systemctl start docker

7. 配置 docker 开机自启动 /关闭

#启动
systemctl enable docker.service

#关闭
systemctl disable docker.service

8. 通过运行hello-world 映像来验证是否正确安装了Docker Engine

sudo docker run hello-world

9. 重新启动docker

systemctl restart docker

10. Docker国内镜像源介绍与更换

Docker中国区官方镜像:
https://registry.docker-cn.com
网易:
http://hub-mirror.c.163.com
ustc:
https://docker.mirrors.ustc.edu.cn
中国科技大学:
https://docker.mirrors.ustc.edu.cn
阿里云:
https://cr.console.aliyun.com/

[root@localhost local]# vim /etc/docker/daemon.json  # 创建或修改 /etc/docker/daemon.json 文件,修改为如下形式
{
    "registry-mirrors" : [
    "https://registry.docker-cn.com",
    "https://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com",
    "https://cr.console.aliyun.com/"
  ]
}

#重启Docker服务
[root@localhost local]# systemctl daemon-reload
[root@localhost local]# systemctl restart docker

11. Docker相关命令

命令 作用
docker version 查看docker的版本号,包括客户端、服务端、依赖的Go等
docker info 查看系统(docker)层面信息,包括管理的images, containers数等
docker search <image> 在docker index中搜索image
docker pull <image> 从docker registry server 中下拉image
docker push <image|repository> 推送一个image或repository到registry
docker push <image|repository>:TAG 同上,指定tag
docker inspect <image|container> 查看image或container的底层信息
docker images TODO filter out the intermediate image layers (intermediate image layers 是什么)
docker images -a 列出所有的images
docker ps 默认显示正在运行中的container
docker ps -l 显示最后一次创建的container,包括未运行的
docker ps -a 显示所有的container,包括未运行的
docker logs <container> 查看container的日志,也就是执行命令的一些输出
docker rm <container...> 删除一个或多个container
docker rm docker ps -a -q 删除所有的container
docker ps -a -q | xargs docker rm 同上, 删除所有的container
docker rmi <image...> 删除一个或多个image
docker start/stop/restart <container> 开启/停止/重启container
docker start -i <container> 启动一个container并进入交互模式
docker attach <container> attach一个运行中的container
docker run <image> <command> 使用image创建container并执行相应命令,然后停止
docker run -i -t <image> /bin/bash 使用image创建container并进入交互模式, login shell是/bin/bash
docker run -i -t -p <host_port:contain_port> 将container的端口映射到宿主机的端口
docker commit <container> [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选
docker build <path> 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
docker build -t repo[:tag] 同上,可以指定repo和可选的tag
docker build - < <dockerfile> 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
docker port <container> <container port> 查看本地哪个端口映射到container的指定端口,或者用docker ps 也可以看到。

五、k8s环境

上一篇下一篇

猜你喜欢

热点阅读