前言

如今的互联网行情,作为Java开发人员,对于Docker + K8S 这两个技术点是必须掌握的,本专题就让我带领大家揭开Docker+K8S的神秘面纱,如果您还未曾了解过,那么也不要紧,只需跟着操作流程动手实验,用心思考,不论是面试,还是工作,想必定不会让各位失望~

在 Docker 篇 我们给到五个小节的内容加以阐述,分别是:

What is Docker

老生常谈,我们说,学习一门开源技术,第一点:去官网查看文档:
https://www.docker.com/

Modernize your applications, accelerate innovation Securely build, share and run modern applications anywhere

在任何地方安全地构建、共享和运行现代应用程序,这就是Docker。

我们看看文档:

https://docs.docker.com/get-started/

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Docker是一个供开发者和系统管理员开发、部署和运行带有容器的应用程序的平台。使用Linux容器来部署应用程序称为容器化。容器并不是什么新事物,但它用于轻松部署应用程序却是新事物。

服务部署的前世今生

那么该如何理解这段晦涩难懂的文字呢? 我们举个栗子:
不妨从一个需求开始 :开发好了一个项目shopping,部署上线
远古时代
在这里插入图片描述
这样部署存在的问题在于:

  • 成本高
  • 部署慢
  • 浪费资源
  • 硬件限制
  • 不利于迁移扩展

于是服务部署进入了 虚拟化时代
在这里插入图片描述

优点:相对利用好资源,相对容易扩展等。

缺点:虚拟机太重了,一上来占用较多物理资源,移植性差,资源利用率低等。

接下来进入重点:
容器时代
在这里插入图片描述

ok,我们现在在理解一下Docker的概念是不是就比较清晰了呢?

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Docker是一个供开发者和系统管理员开发、部署和运行带有容器的应用程序的平台。使用Linux容器来部署应用程序称为容器化。容器并不是什么新事物,但它用于轻松部署应用程序却是新事物。

发现还是比较容易理解的,但是这里有一句“Containers are not new”,也就是容器化技术很早就出现了,比如常见的容器化技术有OpenVZ,LXC,RKT等

Docker的优势和应用场景

通过官网www.docker.com 进入Solutions版块:

  1. 有助于Microservices的落地和部署

  2. 充分利用物理机资源,同时能够整合服务器资源

  3. 提高开发效率,测试效率,部署效率,有利于DevOps的落地,CICD

  4. 云原生落地,应用更好地迁移

Image and Container

作为Docker中最重要的两个概念,不得不提。那就是 Image 和 Container。

What is Image?

通过官网www.docker.com 进入What is a container版块:

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Docker容器映像是一个轻量级的、独立的、可执行的软件包,它包含了运行应用程序所需的一切:代码、运行时、系统工具、系统库和设置。

What is Container?

通过官网www.docker.com 进入What is a container版块:

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. 

容器是一个标准的软件单元,它将代码及其所有依赖项打包起来,以便应用程序能够快速、可靠地从一个计算环境运行到另一个计算环境。

Relation between image and container

Container images become containers at runtime and in the case of Docker containers- images become containers when they run on Docker Engine.

容器图像在运行时变成了容器,在Docker容器的情况下——当它们运行在Docker引擎上时,图像就变成了容器。

View from Docs

从官网www.docker.com 进入Resources
->Docs
->Get started
->Get started with Docker
->Orientation
->Images and containers

A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.

容器通过运行映像启动。映像是一个可执行的包,它包含运行应用程序所需的一切——代码、运行时、库、环境变量和配置文件。

A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.

容器是映像的运行时实例——映像在执行时在内存中成为什么(即带有状态的映像,或用户进程)。您可以使用docker ps命令查看正在运行的容器列表,就像在Linux中一样。

Containers and virtual machines

从官网www.docker.com 进入
->Resources
->Docs
->Get started
->Get started with Docker
->Orientation
->Containers and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

容器在Linux上本机运行,并与其他容器共享主机的内核。它运行一个独立的进程,并不比其他可执行文件占用更多的内存,这使得它是轻量级的。

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

相比之下,虚拟机(VM)运行一个成熟的“guest”操作系统,通过管理程序对主机资源进行虚拟访问。一般来说,vm提供的环境资源比大多数应用程序所需的资源要多。

在这里插入图片描述

Docker Engine and Architecture

https://docs.docker.com/engine/docker-overview/

Docker Engine

 Docker Engine is a client-server application with these major components:

Docker Engine是一个客户端-服务器应用程序,包含以下主要组件:

  • A server which is a type of long-running program called a daemon process (the dockerd command).
    一种称为守护进程(dockerd命令)的长时间运行的程序类型的服务器。

  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
    一个REST API,它指定程序可以使用的接口来与守护进程对话并指示它做什么。

  • A command line interface (CLI) client (the docker command).
    命令行(CLI)客户端(docker命令)。

在这里插入图片描述

Docker Architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

Docker采用client-server架构。Docker客户端与Docker守护进程对话,守护进程承担构建、运行和分发Docker容器的繁重工作。Docker客户端和守护进程可以运行在同一个系统上,或者你可以连接一个Docker客户端到一个远程Docker守护进程。Docker客户端和守护进程通过UNIX套接字或网络接口使用REST API进行通信。
在这里插入图片描述

Install and Experience

https://docs.docker.com/install/linux/docker-ce/centos/

在Win10上准备centos7

和大家说明一下,我们的目的仅仅是要安装一个centos7,然后在centos7上安装docker

如果搞不定vagrant+virtualbox的方式,也可以直接使用VM搭建一个centos7

或者你可以直接使用一台云服务器,上面安装了centos7

毕竟我们的目的只是为了得到一个centos7的机器,所以不必花太多精力在这个问题上折腾

我使用的环境是
【
    win10 64位
    VirtualBox-6.0.12-133076-Win   [已上传到百度网盘,下载链接见文末]
    vagrant_2.2.6_x86_64  [已上传到百度网盘,下载链接见文末]
    centos7   [已上传到百度网盘,下载链接见文末]
    XShell6 [已上传到百度网盘,下载链接见文末]
】

本次安装Docker 采用vagrant+virtual box 方式

下载安装vagrant

01 访问Vagrant官网
https://www.vagrantup.com/

02 点击Download
Windows,MacOS,Linux等

03 选择对应的版本

04 傻瓜式安装

05 命令行输入vagrant,测试是否安装成功

下载安装virtual box

01 访问VirtualBox官网
	https://www.virtualbox.org/

02 选择左侧的“Downloads”

03 选择对应的操作系统版本

04 傻瓜式安装

05 [win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误
    (1)打开服务
    (2)找到Device Install Service和Device Setup Manager,然后启动
    (3)再次尝试安装

安装centos7

01 创建centos7文件夹,并进入其中[目录全路径不要有中文字符]

02 在此目录下打开cmd,运行vagrant init centos/7
   此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7,关键是这个镜像在哪里,我已经提前准备好了,名称是virtualbox.box文件
   
03 将virtualbox.box文件添加到vagrant管理的镜像中
	(1)下载网盘中的virtualbox.box文件
    (2)保存到磁盘的某个目录,比如D:\virtualbox.box
    (3)添加镜像并起名叫centos/7:vagrant box add centos/7 D:\virtualbox.box
    (4)vagrant box list  查看本地的box[这时候可以看到centos/7]
    
04 centos/7镜像有了,根据Vagrantfile文件启动创建虚拟机
	来到centos7文件夹,在此目录打开cmd窗口,执行vagrant up[打开virtual box观察,可以发现centos7创建成功]
	
05 以后大家操作虚拟机,还是要在centos文件夹打开cmd窗口操作
	vagrant halt   优雅关闭
	vagrant up     正常启动
	
06 vagrant常用命令
	(1)vagrant ssh    
    	进入刚才创建的centos7中
    (2)vagrant status
    	查看centos7的状态
    (3)vagrant halt
    	停止/关闭centos7
    (4)vagrant destroy
    	删除centos7
    (5)vagrant status
    	查看当前vagrant创建的虚拟机
    (6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
    	但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload

至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应配置

通过Xshell连接centos7

01 使用centos7的默认账号连接
	在centos文件夹下执行vagrant ssh-config
	关注:Hostname  Port  IdentityFile
	IP:127.0.0.1
	port:2222
	用户名:vagrant
	密码:vagrant
	文件:Identityfile指向的文件private-key
	
02 使用root账户登录
	vagrant ssh   进入到虚拟机中
	sudo -i
	vi /etc/ssh/sshd_config
	修改PasswordAuthentication yes
	passwd修改密码,比如abc123
	systemctl restart sshd
	使用账号root,密码abc123进行登录

通用写法

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
    config.vm.provider "virtualbox" do |vb|
        vb.memory = "4000"
        vb.name= "jack-centos7"
        vb.cpus= 2
    end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

box的打包分发

01 退出虚拟机
	vagrant halt

02 打包
	vagrant package --output first-docker-centos7.box
	
03 得到first-docker-centos7.box
	
04 将first-docker-centos7.box添加到其他的vagrant环境中
	vagrant box add first-docker-centos7 first-docker-centos7.box
	
05 得到Vagrantfile
	vagrant init first-docker-centos7

06 根据Vagrantfile启动虚拟机
	vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]

安装docker

https://docs.docker.com/install/linux/docker-ce/centos/

01 进入centos7
	vagrant ssh
	
02 卸载之前的docker
	sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
                  
03 安装必要的依赖
	sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
    
04 设置docker仓库  [设置阿里云镜像仓库可以先自行百度,后面课程也会有自己的docker hub讲解]	
	sudo yum-config-manager \
      --add-repo \
      https://download.docker.com/linux/centos/docker-ce.repo
      
    [访问这个地址,使用自己的阿里云账号登录,查看菜单栏左下角,发现有一个镜像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors]

05 安装docker
	sudo yum install -y docker-ce docker-ce-cli containerd.io
	
06 启动docker
	sudo systemctl start docker
	
07 测试docker安装是否成功
	sudo docker run hello-world

docker基本体验

01 创建tomcat容器
	docker pull tomcat
	docker run -d --name my-tomcat -p 9090:8080 tomcat

02 创建mysql容器
	docker run -d --name my-mysql -p 3301:3306 -e MYSQL_ROOT_PASSWORD=jack123 --privileged mysql
	
03 进入到容器里面
	docker exec -it containerid /bin/bash

可能有的疑惑

  1. docker pull在哪拉取的镜像?

​ 默认是在hub.docker.com

  1. docker pull tomcat拉取的版本是?

​ 默认是最新的版本,可以在后面指定版本":"

  1. 简单先说一下命令咯
docker pull        拉取镜像到本地
docker run         根据某个镜像创建容器
-d                 让容器在后台运行,其实就是一个进程
--name             给容器指定一个名字
-p                 将容器的端口映射到宿主机的端口
docker exec -it    进入到某个容器中并交互式运行

写在最后

更多架构知识,欢迎关注本套Java系列文章Java架构师成长之路

Logo

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

更多推荐