kubernetes(一)之Docker基础入门

Docker入门

容器的定义

  • 运行在用户空间的独立进程
  • 与其他的用户空间程序是相互隔离
  • 一个容器运行在一个单独的用户空间
  • 底层是单独的内核空间

发展历史

  • FreeBSD jail, 2000
  • Linux VServer ,2001
    • CGroups
    • NameSpace
    • LXC
  • Docker ,2010
    • libcontainer
    • runC

docker容器编排三剑客

  • docker-mechine
  • docker-swarm
  • docker-compose

Linux NameSpace

  • 用于在一个抽象层上封装一个全局级别的可以进行切分的系统资源,主要如下的七个个级别的系统资源
NameSpacesConstantIsolates
CgroupCLONE_NEWCGROUPCgroup root directory,调派底层的cpu,内存,IO资源给容器
IPCCLONE_NEIPCSystem V IPC,POSIX message queues
NETWORKCOLNE_NENETNetWork devices,stacks,ports,etc
MountClONE_NEWNSMountPoints
PIDCOLNE_NEW_PIDProcess PID
UsersCOLNE_USERUser and Group IDs
UTSCLONE_NEW_UTSHostname and NIS domain name

docker的版本

  • docker-ee
  • docker-ce

Docker的架构

kubernetes(一)之Docker基础入门

docker的安装

  • 系统: centos7+
  • 初始化:配置主机名&ip略过
[ ~]# systemctl stop firewalld && systemctl disable firewalld
[ ~]# sed -i "s/SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config && reboot -f
[ ~]# yum install chrony wget curl git -y && systemctl enable chronyd && systemctl start chronyd && timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes    #时间同步
[ ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[ ~]# yum -y install epel-release
[ ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf  &&  sysctl -p
  • docker的安装和配置
# step 1: 安装必要的一些系统工具
 yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce

# 新增配置文件
 mkdir -p /etc/docker
 tee /etc/docker/daemon.json <<-‘EOF‘
{
  "registry-mirrors": ["https://0b8hhs68.mirror.aliyuncs.com"],
  "storage-driver": "overlay2",
  "graph":"/data/docker",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF
# 重载服务
sudo systemctl daemon-reload
sudo systemctl restart docker

docker 环境相关命令

docker info     #查看docker 信息
docker version   #查看版本信息

镜像相关命令

[ ~]# docker image -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker image COMMAND
Manage images
Commands:
  build Build an image from a Dockerfile
  history Show the history of an image
  import Import the contents from a tarball to create a filesystem image
  inspect Display detailed information on one or more images
  load Load an image from a tar archive or STDIN
  ls List images
  prune Remove unused images
  pull Pull an image or a repository from a registry
  push Push an image or a repository to a registry
  rm Remove one or more images
  save Save one or more images to a tar archive (streamed to STDOUT by default)
  tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

容器相关命令

[ ~]# docker container -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker container COMMAND
Manage containers
Commands:
  attach Attach local standard input, output, and error streams to a running container
  commit Create a new image from a container‘s changes
  cp Copy files/folders between a container and the local filesystem
  create Create a new container
  diff Inspect changes to files or directories on a container‘s filesystem
  exec Run a command in a running container
  export Export a container‘s filesystem as a tar archive
  inspect Display detailed information on one or more containers
  kill Kill one or more running containers
  logs Fetch the logs of a container
  ls List containers
  pause Pause all processes within one or more containers
  port List port mappings or a specific mapping for the container
  prune Remove all stopped containers
  rename Rename a container
  restart Restart one or more containers
  rm Remove one or more containers
  run Run a command in a new container
  start Start one or more stopped containers
  stats Display a live stream of container(s) resource usage statistics
  stop Stop one or more running containers
  top Display the running processes of a container
  unpause Unpause all processes within one or more containers
  update Update configuration of one or more containers
  wait Block until one or more containers stop, then print their exit codes

kubernetes(一)之Docker基础入门

实践

镜像实践

[ ~]# docker search redis    #查找redis镜像
[ ~]# docker pull redis:4-alpine    #拖镜像
[ ~]# docker images    #查看所有镜像
[ ~]# docker inspect redis:4-alpine     #查看镜像详情
[ ~]# docker rmi redis:4-alpine           #删除镜像
[ ~]# docker image save redis:4-alpine -o redis.tar     #导出镜像
[ ~]# docker image load -i redis.tar         #导入镜像

登陆自己的阿里云容器镜像控制台,创建命名空间,然后在创建一个redis的本地镜像仓库
kubernetes(一)之Docker基础入门

接下来就是比较常用的自己上传镜像到阿里云redis镜像仓库

[ ~]# docker login --username=valiente0822 registry.cn-hangzhou.aliyuncs.com   #登陆我的阿里云docker镜像仓库
[ ~]# docker image tag redis:4-alpine registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine   #拷贝并修改tag
[ ~]# docker push registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine    #上传镜像
[ ~]# docker pull registry.cn-hangzhou.aliyuncs.com/myimgs/redis:4-alpine      #下载镜像

注意事项:

  • 创建的如果是阿里云私有仓库,pull镜像的时候需要docker login之后才能pull

容器实践

[ ~]# docker image pull centos:7   #拉取镜像
[ ~]# docker container run -it --name c1 centos:7 /bin/bash    #交互式运行容器
[ ~]# iptables -t nat -vnL    #新开终端,发现有nat iptables规则
[ ~]# docker ps -a             #查看运行或者停止状态的容器
[ ~]# docker start c1          #启动容器
[ ~]# docker ps                  #查看运行状态的容器

相关推荐