0条评论
还没有人评论过~
1. 内容
2. 创建、运行及共享容器镜像
介绍
深入学习前,先看看如何创建一个简单的应用、打包成容器镜像、在远程集群或本地集群运行
步骤简介
安装并运行Docker
创建一个简单的php应用
为镜像创建Dockerfile
构建容器镜像
$ docker images
php local b26e7de6427c 9 hours ago 64.7MB
php 7-zts-alpine3.9 0adbdb1b2250 2 weeks ago 64.7MB
运行容器镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b3eb8cbab22 php:local "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 0.0.0.0:8000->8000/tcp php-container
探索运行容器的内部
$ docker exec -it php-container ls -al /
total 68
drwxr-xr-x 1 root root 4096 May 28 23:13 .
drwxr-xr-x 1 root root 4096 May 28 23:13 ..
-rwxr-xr-x 1 root root 0 May 28 23:13 .dockerenv
drwxr-xr-x 1 root root 4096 May 11 03:27 bin
drwxr-xr-x 5 root root 340 May 28 23:13 dev
drwxr-xr-x 1 root root 4096 May 28 23:13 etc
drwxr-xr-x 1 root root 4096 May 11 03:04 home
-rw-r--r-- 1 root root 33 May 28 23:12 index.php
# ps -ef
PID USER TIME COMMAND
1 root 0:00 /usr/local/bin/php -S 0.0.0.0:8000
7 root 0:00 sh
12 root 0:00 ps -ef
# ls -al /
total 68
drwxr-xr-x 1 root root 4096 May 29 03:58 .
drwxr-xr-x 1 root root 4096 May 29 03:58 ..
-rwxr-xr-x 1 root root 0 May 29 03:58 .dockerenv
drwxr-xr-x 1 root root 4096 May 11 03:27 bin
drwxr-xr-x 5 root root 340 May 29 03:58 dev
drwxr-xr-x 1 root root 4096 May 29 03:58 etc
drwxr-xr-x 1 root root 4096 May 11 03:04 home
-rw-r--r-- 1 root root 33 May 28 23:12 index.php
drwxr-xr-x 1 root root 4096 May 11 03:27 lib
drwxr-xr-x 5 root root 4096 May 9 20:49 media
drwxr-xr-x 2 root root 4096 May 9 20:49 mnt
drwxr-xr-x 2 root root 4096 May 9 20:49 opt
dr-xr-xr-x 262 root root 0 May 29 03:58 proc
drwx------ 1 root root 4096 May 29 03:59 root
drwxr-xr-x 2 root root 4096 May 9 20:49 run
drwxr-xr-x 2 root root 4096 May 9 20:49 sbin
drwxr-xr-x 2 root root 4096 May 9 20:49 srv
dr-xr-xr-x 13 root root 0 May 29 03:58 sys
drwxrwxrwt 1 root root 4096 May 11 03:27 tmp
drwxr-xr-x 1 root root 4096 May 11 03:27 usr
drwxr-xr-x 1 root root 4096 May 11 03:04 var
停止和删除容器
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c113cc387a6d php:local "docker-php-entrypoi…" 8 minutes ago Exited(137) 56 seconds ago php-container
向镜像仓库推送镜像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php local 9ed8b05c3334 13 hours ago 64.7MB
yeedom/php v1 9ed8b05c3334 13 hours ago 64.7MB
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: yeedom
Password:
Login Succeeded
$ docker push yeedom/php:v1
The push refers to repository [docker.io/yeedom/php]
d281f172ef70: Pushed
8a1be1ed4590: Mounted from library/php
b39144a13ab7: Mounted from library/php
060efcf419e8: Mounted from library/php
f38edf0edb30: Mounted from library/php
0b0394abec4c: Mounted from library/php
21f626200b4c: Mounted from library/php
414e112bbb2c: Mounted from library/php
3575e617b5f4: Mounted from library/php
f1b5933fe4b5: Mounted from library/php
v1: digest: sha256:448c8eeca87506bbc6d41190073acdcab094aa0b0bd3c2551c4ced947f422ebb size: 2409
$ docker rmi yeedom/php:v1
Untagged: yeedom/php:v1
Untagged: yeedom/php@sha256:448c8eeca87506bbc6d41190073acdcab094aa0b0bd3c2551c4ced947f422ebb
$ docker run -p 8000:8000 -d yeedom/php:v1
Unable to find image 'yeedom/php:v1' locally
v1: Pulling from yeedom/php
Digest: sha256:448c8eeca87506bbc6d41190073acdcab094aa0b0bd3c2551c4ced947f422ebb
Status: Downloaded newer image for yeedom/php:v1
a0d96dc65d6795ba5542754bd3a07e424da672960f545f5cf36e14fa02cac46f
3. 配置Kubernetes集群
用Minikube运行一个本地单节点Kubernetes集群
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready43m v1.14.2
为kubectl配置别名和命令行补齐
4. 在Kubernetes上运行第一个应用
介绍
正常来说,部署一个Kubernetes程序需要包含部署的所有组件描述的配置文件,因为第一次使用,所以用最简单的方法运行Kubernetes程序
部署php应用
kube-shell> kubectl run php-container --image=yeedom/php:v1 --port=8000 --generator=run/v1
replicationcontroller "php-container" created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
php-container-mdkmq 0/1 ContainerCreating 0 7
$ kubectl describe pod php-container-mdkmq
Name: php-container-mdkmq
Namespace: default
Priority: 0
PriorityClassName:
Node: minikube/10.0.2.15
Start Time: Thu, 30 May 2019 07:58:10 +0800
Labels: run=php-container
Annotations:
Status: Running
…………………………
访问web应用
$ kubectl expose rc php-container --type=LoadBalancer --name php-service
service "php-service" exposed
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1443/TCP 37d
php-service LoadBalancer 10.105.206.79 localhost 8000:32040/TCP 6s
$ curl http://localhost:8000
$ kubectl get service php-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
php-service LoadBalancer 10.105.206.79 localhost 8000:32040/TCP 3m
系统的逻辑部分
水平伸缩应用
kubectl get rc
NAME DESIRED CURRENT READY AGE
php-container 1 1 1 52m
kubectl get pod
NAME READY STATUS RESTARTS AGE
php-container-xlhzh 1/1 Running 0 53m
kubectl scale rc php-container --replicas=3
replicationcontroller "php-container" scaled
kubectl get pods
NAME READY STATUS RESTARTS AGE
php-container-f879k 1/1 Running 0 5s
php-container-n2pwj 1/1 Running 0 5s
php-container-xlhzh 1/1 Running 0 54m
$ curl http://localhost:8000
php-container-n2pwj
2019-05-30 13:23:38
$ curl http://localhost:8000
php-container-xlhzh
2019-05-30 13:23:50
$ curl http://localhost:8000
php-container-f879k
2019-05-30 13:23:54
查看应用运行在哪个节点上
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
php-container-f879k 1/1 Running 0 50m 10.1.1.102 docker-for-desktop
php-container-n2pwj 1/1 Running 0 50m 10.1.1.101 docker-for-desktop
php-container-xlhzh 1/1 Running 0 1h 10.1.1.100 docker-for-desktop
kubectl describe pod php-container-f879k
Name: php-container-f879k
Namespace: default
Node: docker-for-desktop/192.168.65.3
Start Time: Thu, 30 May 2019 21:10:32 +0800
Labels: run=php-container
Annotations:
Status: Running
IP: 10.1.1.102
Controlled By: ReplicationController/php-container
5. 小结