Kubernetes-Docker基础

2021-03-12 01:32

阅读:369

标签:ext4   doc   git   ted   format   uid   说明   tps   x86   

1. Docker版本

docker-ce.x86_64 # 社区版
docker-ee # 企业版
docker镜像站:https://hub.docker.com/
Kubernetes:https://kubernetes.io/zh/
阿里云镜像站:https://developer.aliyun.com/special/mirrors/notice

  • 查看docker版本
#:docker version
Client:
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.13.8
 Git commit:        afacb8b7f0
 Built:             Fri Dec 18 12:15:19 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.8
  Git commit:       afacb8b7f0
  Built:            Fri Dec  4 23:02:49 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.3-0ubuntu2.2
  GitCommit:        
 runc:
  Version:          spec: 1.0.1-dev
  GitCommit:        
 docker-init:
  Version:          0.18.0
  GitCommit:   

2. 环境准备

  • 修改主机名
#: hostnamectl set-hostname worker
  • 开启端口转发
#: sysctl -a | grep forward
***
net.ipv4.ip_forward = 1
***
  • 关闭Selinux
#: vim /etc/sysconfig/selinux 或者 setenforce 0
SELINUX=permissive
  • 关闭swap
swapoff /swap.img

注释/etc/fstab

root@k8s:~# cat /etc/fstab 
# /etc/fstab: static file system information.
#
# Use ‘blkid‘ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# 
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-EbCUAM6ESxObgKwpg7QLLPDJCF82GAj2oYMBzo5mS8PFCe3jnENIvqjzUPGGMGIX / ext4 defaults 0 0
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/ba31387b-7c3e-46eb-bdf0-192cdd38f590 /boot ext4 defaults 0 0
#/swap.img	none	swap	sw	0	0
  • 关闭防火墙
systemctl stop firewalld
  • 安装docker
#: wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repo.d
#: yum repolist
#: yum -y install docker-ce docker-ce-cli containerd.io
#: systemctl start docker
  • 配置阿里云镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json 

3. Docker常用操作

  • 镜像下载管理
docker search nginx 
docker pull nginx:latest
root@node1:~# docker images
REPOSITORY                                                  TAG                 IMAGE ID            CREATED             SIZE
mysql                                                       5.7                 a70d36bc331a        7 weeks ago         449MB
busybox                                                     latest              b97242f89c8a        7 weeks ago         1.23MB
nginx                                                       latest              f6d0b4767a6c        8 weeks ago         133MB
metallb/speaker                                             main                252ca41551e9        2 months ago        84.3MB
metallb/controller                                          main                3a44dd1ab374        2 months ago        75.3MB
centos                                                      latest              300e315adb2f        3 months ago        209MB
  • 运行一个容器
docker run -dit --name=nginx web1
docker ps -a
  • 清除已退出的容器
docker ps -a | grep Exited | awk ‘{print $1}‘
docker ps -a | grep Exited | awk ‘{print $1} > /tmp/stop-c
while read c; do docker rm $c; done 
  • 导出镜像
docker save docker.io/nginx > nginx.tar
  • 加载离线镜像包
docker load -i nginx.tar
  • 查看镜像生成命令
查看详细命令:docker history --no-trunc docker.io/nginx
root@node1:~# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
f6d0b4767a6c        8 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
           8 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B                  
           8 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
           8 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
           8 weeks ago         /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
           8 weeks ago         /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB              
           8 weeks ago         /bin/sh -c #(nop) COPY file:e7e183879c35719c…   1.2kB               
           8 weeks ago         /bin/sh -c set -x     && addgroup --system -…   63.7MB              
           8 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
           8 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.5.0        0B                  
           8 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.6     0B                  
           8 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
           8 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
           8 weeks ago         /bin/sh -c #(nop) ADD file:422aca8901ae3d869…   69.2MB              
root@node1:~# 

4. 容器常规操作

root@node1:~# docker run -dit --restart=always --name=c1 centos:latest
314ffbcdfa193f38d73dffa026dd816a917dad0d3367916ea5a71fba593d6beb
root@node1:~# 
# 启停容器
docker stop/start c1
# 删除容器
docker rm c1 -f
# 访问容器
attach: 容器中要有bash/sh
docker attach c1
# 容器中运行命令
docker exec c1 /bin/bash -c "cat /etc/redhat-release"
docker exec c1 /bin/bash -c "yum install net-tools -y"
  • 通过正在运行的容器创建镜像
docker commit c1 my-centos
  • 运行一个mysql数据库
docker run -dit --restart=always -e MYSQL_ROOT_PASSWORD=redhat --name=my-db mysql:5.7
Mysql数据库使用前参考使用说明:https://hub.docker.com/_/mysql
* 查看容器中环境变量
root@node1:~# docker run -dit --restart=always -e MYSQL_ROOT_PASSWORD=redhat --name=my-db mysql:5.7
0d96ee5757c8323732daf153d0d7b2d7d1d1b0c0e18d22ccab9c23c40600aead
root@node1:~# docker exec -it my-db bash
root@0d96ee5757c8:/# env 
MYSQL_MAJOR=5.7
HOSTNAME=0d96ee5757c8
PWD=/
MYSQL_ROOT_PASSWORD=redhat
HOME=/root
MYSQL_VERSION=5.7.33-1debian10
GOSU_VERSION=1.12
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
root@0d96ee5757c8:/# 
* 查看容器详细信息
docker inspect my-db
  • 实现MySQL容器持久存储
docker run -dit --restart=always --name=my-db2 -e MYSQL_ROOT_PASSWORD=redhat -v /mysql:/var/lib/mysql mysql:5.7

5. 容器网络

root@node1:~# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
a390d4dd97e4        bridge              bridge              local
7ecb1cba6445        host                host                local
5c842fd3da29        none                null                local
root@node1:~# dokcer network inspect a390d4dd97e4
  • 创建容器网络
man 1 docker-network-create
docker network create --driver=bridge --subnet=10.0.0.0/24 network1
* 指定network1创建容器
docker run -dit --restart=always --name=c2 --network=network1 centos:7

在docker服务器上使用容器IP可以访问到容器服务器,对于生产网络不可见
技术图片

  • 将容器网络发布到生产网络
docker run -dit --restart=always --name=web1 -p 80:80 --network=network1 nginx
  • 容器互通测试,部署wordpress+mysql 基于link实现容器互通
docker pull wordpress
docker pull mysql:5.7
#: docker run -dit --restart=always --name=mysql -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -e MYSQL_DATABASE=wordpress mysql:5.7
#: docker run -dit --restart=always --name=wordpress -e WORDPRESS_DB_HOST=x.x.x.x -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=wordpress -p 80:80 wordpress
* 或者
#: docker run -dit --restart=always --name=mysql -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -e MYSQL_DATABASE=wordpress -v /wp-mysql:/var/lib/mysql mysql:5.7
#: docker run -dit --restart=always --name=wordpress --link mysql:db -e WORDPRESS_DB_HOST=db -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=wordpress -v /wpdata:/var/www/html -p 80:80 wordpress 

To be continue!

Kubernetes-Docker基础

标签:ext4   doc   git   ted   format   uid   说明   tps   x86   

原文地址:https://www.cnblogs.com/bigdad/p/14506872.html


评论


亲,登录后才可以留言!