01 初识 Docker apt install -y docker.io service docker start usermod -aG docker ${USER}
docker version docker info docker ps docker pull busybox docker images
https://docs.docker.com/get-started/overview/
02 被隔离的进程 docker pull alpine docker run -it alpine sh cat /etc/os-release
隔离资源,保证系统安全,提高资源的利用率。
资源隔离提供了三种技术:namespace、 cgroup、chroot(pivot_rott)。
03 容器化的应用 Build once, Run anywhere.
应用程序不再直接和操作系统打交道,而是封装成镜像,再交给容器环境去运行。
docker rmi busybox docker run -it busybox docker run -d busybox docker run -d --name xxx busybox docker run --rm busybox echo "hello docker" docker stop xxx docker rm xxx docker ps -a docker exec xxx echo "hello docker"
05 镜像仓库 用户名/应用名:标签
官方镜像用户名是 library
。
slim 经过精简的 fat 包含了较多的辅助工具 rc 候选版本,release candidate docker build -t docker tag ngx-app chronolaw/ngx-app:1.0 docker push chronolaw/ngx-app:1.0
save 和 load 这两个镜像归档命令:
docker save ngx-app:latest -o ngx.tar docker load -i ngx.tar
06 打破次元壁 docker run -d --rm --name ubu phusion/baseimage:jammy-1.0.1 echo "hello" > a.txtdocker cp a.txt ubu:/tmp docker exec -it ubu bash docker cp ubu:/tmp/a.txt ./b.txt
容器和主机共享本地目录:
docker run -d --rm -v /tmp:/tmp --name ubu phusion/baseimage:jammy-1.0.1 docker exec -it ubu bash
docker pull python:alpine docker run -it --rm -v `pwd `:/tmp python:alpine sh
网络模式:null
host
bridge
docker run -d --rm --net=host --name=ng nginx:alpine docker exec ng ip addr docker inspect ng | grep IPAddress docker stop ng
docker run -d --rm --name=ng nginx:alpine docker inspect ng | grep IPAddress docker run -d --rm --name=rd redis docker inspect rd | grep IPAddress
分配服务端口号
docker run -d -p 80:80 --rm nginx:alpine docker run -d -p 8080:80 --rm nginx:alpine
07 玩转 Docker Container Image Registry
https://registry.hub.docker.com/_/registry/
docker run -d -p 5000:5000 registry docker tag nginx:alpine 127.0.0.1:5000/nginx:alpine docker push 127.0.0.1:5000/nginx:alpine docker rmi 127.0.0.1:5000/nginx:alpine docker pull 127.0.0.1:5000/nginx:alpine
https://docs.docker.com/registry/spec/api/
curl 127.1:5000/v2/_catalog curl 127.1:5000/v2/nginx/tags/list
registry 默认会把镜像存储在 Docker 内部目录 /var/lib/registry
。
搭建 WordPress:
docker run -d --rm \ --env MARIADB_DATABASE=db \ --env MARIADB_USER=wp \ --env MARIADB_PASSWORD=123 \ --env MARIADB_ROOT_PASSWORD=123 \ --name mariadb \ mariadb:10 docker exec -it mariadb mysql -uwp -p123 docker inspect mariadb | grep IPAddress docker run -d --rm \ --env WORDPRESS_DB_HOST=172.17.0.2 \ --env WORDPRESS_DB_USER=wp \ --env WORDPRESS_DB_PASSWORD=123 \ --env WORDPRESS_DB_NAME=db \ --name wp \ wordpress:5 docker inspect wp | grep IPAddress
vim wp.conf server { listen 80; default_type text/html; location / { proxy_http_version 1.1; proxy_set_header Host $host ; proxy_pass http://172.17.0.4; } }
docker run -d --rm \ -p 80:80 \ -v `pwd `/wp.conf:/etc/nginx/conf.d/default.conf \ --name ng \ nginx:alpine
docker logs mariadb docker logs ng docker logs wp
08 入门篇总结 docker pull alpine docker run -it alpine sh uname -a
构建自己的镜像:
https://github.com/chronolaw/k8s_study/blob/master/ch1/Dockerfile
ARG IMAGE_BASE="nginx" ARG IMAGE_TAG="1.21-alpine" FROM ${IMAGE_BASE}:${IMAGE_TAG}ENV PATH=$PATH:/tmpENV DEBUG=OFFCOPY ./default.conf /etc/nginx/conf.d/ RUN cd /usr/share/nginx/html \ && echo "hello nginx" > a.txt EXPOSE 8081 8082 8083 WORKDIR /etc/nginx
docker build -t ngx-app:1.0 . docker run -it --rm ngx-app:1.0 sh docker save ngx-app:1.0 -o ngx.tar docker load -i ngx.tar
09 Kubernetes 环境 容器编排 Container Orchestration
Kubernetes 就是一个生产级别的容器编排平台和集群管理系统。
https://minikube.sigs.k8s.io/docs/start/
minikube version minikube start --kubernetes-version=v1.23.3 minikube status minikube node list minikube ssh uname -aip add
minikube kubectl minikube kubectl -- version alias kubectl="minikube kubectl --" kubectl version kubectl run ngx --image=nginx:alpine kubectl get pod
https://kubernetes.io/zh/
10 Kubernetes 工作机制 Kubernetes 采用了现今流行的“控制面 / 数据面”(Control Plane / Data Plane)架构,集群里的计算机被称为“节点”(Node),可以是实机也可以是虚机,少量的节点用作控制面来执行集群的管理维护工作,其他的大部分节点都被划归数据面,用来跑业务应用。Master 节点实现管理控制功能,Worker 节点运行具体业务。
kubectl get node kubectl get pod -n kube-system
minikube ssh docker ps |grep kube-proxy ps -ef|grep kubelet
minikube addons list minikube dashboard
11 YAML YAML 是 JSON 的超集。
Shell 脚本和 Dockerfile 可以很好地描述“命令式”(Imperative)。“声明式”(Declarative)注重结果。
apiserver 采用了 HTTP 协议的 URL 资源理念,API 风格也用 RESTful,被称为是“API 对象”了。
kubectl api-resources kubectl get pod --v=9 kubectl explain pod kubectl explain pod.metadata kubectl run ngx --image=nginx:alpine kubectl run ngx --image=nginx:alpine --dry-run=client -o yaml > ngx-pod.yml kubectl apply -f ngx-pod.yml kubectl delete -f ngx-pod.yml
12 Pod 为了解决这样多应用联合运行的问题,同时还要不破坏容器的隔离,就需要在容器外面再建立一个“收纳舱”。
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: ngx name: ngx spec: containers: - image: nginx:alpine name: ngx resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
vim ngx-pod.yml kubectl apply -f ngx-pod.yml kubectl logs ngx kubectl get pod kubectl describe pod ngx echo 'aaa' > a.txtkubectl cp a.txt ngx:/tmp kubectl exec -it ngx -- sh
13 Job CronJob 离线业务 “单一职责”的意思是对象应该只专注于做好一件事情,不要贪大求全,保持足够小的粒度才更方便复用和管理。
“组合优于继承”的意思是应该尽量让对象在运行时产生联系,保持松耦合,而不要用硬编码的方式固定对象的关系。
kubectl create job echo-job --image=busybox --dry-run=client -o yaml > job.yml
apiVersion: batch/v1 kind: Job metadata: creationTimestamp: null name: echo-job spec: template: metadata: creationTimestamp: null spec: containers: - image: busybox name: echo-job resources: {} command: ["/bin/echo" ] args: ["hello" , "world" ] restartPolicy: Never status: {}
kubectl apply -f job.yml kubectl get job kubectl get pod kubectl logs echo-job
apiVersion: batch/v1 kind: Job metadata: creationTimestamp: null name: sleep-job spec: activeDeadlineSeconds: 60 backoffLimit: 2 completions: 4 parallelism: 2 template: metadata: creationTimestamp: null spec: containers: - image: busybox name: echo-job resources: {} command: - sh - -c - sleep $(($RANDOM % 10 + 1 )) && echo done restartPolicy: Never status: {}
kubectl apply -f sleep-job.yaml kubectl get pod -w
kubectl create cj echo-cj --image=busybox --schedule="*/1 * * * *" --dry-run=client -o yaml > echo-cj.yaml
apiVersion: batch/v1 kind: CronJob metadata: creationTimestamp: null name: echo-cj spec: jobTemplate: metadata: creationTimestamp: null name: echo-cj spec: template: metadata: creationTimestamp: null spec: containers: - image: busybox name: echo-cj resources: {} command: ["/bin/echo" ] args: ["hello" , "world" ] restartPolicy: OnFailure schedule: "*/1 * * * *" status: {}
kubectl apply -f echo-cj.yaml kubectl get cj kubectl get pod
14 ConfigMap Secret 管理配置信息 kubectl create cm info --from-literal=name=zhao --dry-run=client -o yaml > cm.yml kubectl apply -f cm.yml kubectl get cm kubectl describe cm info
kubectl create secret generic user --from-literal=name=root --dry-run=client -o yaml > secret.yml echo -n "root" | base64 kubectl apply -f secret.yml kubectl get secret kubectl describe secret user
kubectl explain pod.spec.containers.env.valueFrom
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: ngx name: ngx spec: containers: - image: nginx:alpine name: ngx resources: {} env: - name: NAME valueFrom: configMapKeyRef: name: info key: name - name: SNAME valueFrom: secretKeyRef: name: user key: name dnsPolicy: ClusterFirst restartPolicy: Always status: {}
kubectl apply -f env-pod.yml kubectl exec -it ngx -- sh echo $NAME $SNAME
apiVersion: v1 kind: Pod metadata: name: vol-pod spec: volumes: - name: cm-vol configMap: name: info - name: sec-vol secret: secretName: user containers: - volumeMounts: - mountPath: /tmp/cm-items name: cm-vol - mountPath: /tmp/sec-items name: sec-vol image: nginx:alpine name: ngx resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
vim vol-pod.yml kubectl apply -f vol-pod.yml kubectl get pod kubectl exec -it vol-pod -- sh cat /tmp/cm-items/namecat /tmp/sec-items/name
15 玩转 Kubernetes 搭建 WordPress 环境:
apiVersion: v1 kind: ConfigMap metadata: name: maria-cm data: DATABASE: "db" USER: "wp" PASSWORD: "123" ROOT_PASSWORD: "123" --- apiVersion: v1 kind: Pod metadata: name: maria-pod labels: app: wordpress role: database spec: containers: - image: mariadb:10 name: maria imagePullPolicy: IfNotPresent ports: - containerPort: 3306 envFrom: - prefix: "MARIADB_" configMapRef: name: maria-cm
kubectl apply -f mariadb-pod.yml kubectl get pod -o wide
apiVersion: v1 kind: ConfigMap metadata: name: wp-cm data: HOST: "172.17.0.5" USER: "wp" PASSWORD: "123" NAME: "db" --- apiVersion: v1 kind: Pod metadata: name: wp-pod labels: app: wordpress role: website spec: containers: - image: wordpress:5 name: wp-pod imagePullPolicy: IfNotPresent ports: - containerPort: 80 envFrom: - prefix: "WORDPRESS_DB_" configMapRef: name: wp-cm
kubectl apply -f wp-pod.yml kubectl get pod -o wide
kubectl port-forward wp-pod 8080:80 &
16 初级篇总结 minikube version minikube status minikube start --kubernetes-version=v1.23.3 minikube node list kubectl version kubectl run ngx --image=nginx:alpine kubectl get pod -n kube-system kubectl api-resources kubectl explain pod.metadata export out="--dry-run=client -o yaml" kubectl run ngx --image=nginx:alpine $out > pod.yml kubectl apply -f ngx-pod.yml kubectl get pod kubectl logs ngx-pod kubectl exec -it ngx-pod -- sh kubectl delete -f ngx-pod kubectl create job echo-job --image=busybox $out kubectl apply -f job.yml kubectl get job kubectl get pod kubectl logs echo-job-l52l7 kubectl create cj echo-cj --image=busybox --schedule="* * * * *" $out kubectl apply -f cronjob.yml kubectl get cj kubectl get pod kubectl create cm info --from-literal=k=v $out kubectl get cm kubectl describe cm info kubectl create secret generic user --from-literal=name=root $out kubectl get secret kubectl describe secret user echo cm9vdA== | base64 -d
References – EOF –