如今的互联网行情,作为Java开发人员,对于Docker + K8S 这两个技术点是必须掌握的,本专题就让我带领大家揭开Docker+K8S的神秘面纱,如果您还未曾了解过,那么也不要紧,只需跟着操作流程动手实验,用心思考,不论是面试,还是工作,想必定不会让各位失望~
在 Docker 篇 我们给到五个小节的内容加以阐述,分别是:
K8S Docs Concepts:https://kubernetes.io/docs/concepts/
(1)先以container为起点,k8s既然是容器编排工具,那么一定会有container
(2)那k8s如何操作这些container呢?从感性的角度来讲,得要有点逼格,k8s不想直接操作container,因为操作container的事情是docker来做的,k8s中要有自己的最小操作单位,称之为 Pod
说白了,Pod就是一个或多个Container的组合
看看官网怎么描述的 :https://kubernetes.io/docs/concepts/workloads/pods/pod/
A Pod (as in a pod of whales or pea pod) is a group of one or more containers (such as Docker containers),
with shared storage/network, and a specification for how to run the containers.
(3)那Pod的维护谁来做呢?那就是ReplicaSet,通过selector来进行管理
看看官网怎么描述的 :https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
A ReplicaSet is defined with fields, including a selector that specifies how to identify Pods it can acquire, a number of replicas indicating how many Pods it should be maintaining, and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria.
(4)Pod和ReplicaSet的状态如何维护和监测呢?Deployment
官网是如何描述的 :https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
A Deployment controller provides declarative updates for Pods and ReplicaSets.
You describe a desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.
(5)不妨把相同或者有关联的Pod分门别类一下,那怎么分门别类呢?Label
官网是如何描述的 :https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
Labels are key/value pairs that are attached to objects, such as pods.
(6)具有相同label的service要是能够有个名称就好了,Service
看官网上怎么说 :https://kubernetes.io/docs/concepts/services-networking/service/
An abstract way to expose an application running on a set of Pods as a network service.
With Kubernetes you don’t need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.
(7)上述说了这么多,Pod运行在哪里呢?当然是机器咯,比如一台centos机器,我们把这个机器称作为Node
看看官网怎么说 :https://kubernetes.io/docs/concepts/architecture/nodes/
A node is a worker machine in Kubernetes, previously known as a minion. A node may be a VM or physical machine, depending on the cluster. Each node contains the services necessary to run pods and is managed by the master components.
(8)难道只有一个Node吗?显然不太合适,多台Node共同组成集群才行嘛
画个图表示一下咯,最好能把之前的Label,Service也一起画上去,整体感受一下
(9)此时,我们把目光转移到由3个Node节点组成的Master-Node集群
(10)这个集群要配合完成一些工作,总要有一些组件的支持吧?接下来我们来想想有哪些组件,然后画一个相对完整的架构图
kubectl
Master Node
,然后再分配给Worker Node
创建Pod
之类的关键是命令通过kubectl
过来之后,是不是要认证授权一下?
Master Node
中谁来接收?APIServer
Worker Node
创建Pod
,Container
之类的,得要有调度策略Scheduler
https://kubernetes.io/docs/concepts/scheduling/kube-scheduler/
Scheduler
通过不同的策略,真正要分发请求到不同的Worker Node
上创建内容,具体谁负责?Controller Manager
Worker Node
接收到创建请求之后,具体谁来负责Kubelet
服务,最终Kubelet
会调用Docker Engine
,创建对应的容器[这边是不是也反应出一点,在Node
上需要有Docker Engine
,不然怎么创建维护容器?]
DNS
Dashboard
ETCD
Docker
中的内容(11)不妨把这个图翻转一下方便查看
(12)官网K8S架构图
https://kubernetes.io/docs/concepts/architecture/cloud-controller/
小结:有些小伙伴可能会想,有些组件知道它的作用了,有些有些模糊。没关系,目前我们只是停留在理论层面的推断和分析,毕竟一点实际操作都没有,现在能够有个感性的认知就已经很不错了。
Kelsey Hightower :https://github.com/kelseyhightower
网址 :https://labs.play-with-k8s.com/
This is a sandbox environment. Using personal credentials is HIGHLY! discouraged. Any consequences of doing so, are completely the user’s responsibilites.
You can bootstrap a cluster as follows:
kubeadm init --apiserver-advertise-address $(hostname -i)
Initialize cluster networking: kubectl apply -n kube-system -f
“https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64|tr -d ‘\n’)”
(Optional) Create an nginx deployment: kubectl apply -f
https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/ application/nginx-app.yaml
GitHub :https://github.com/kubernetes/kops
coreos :https://coreos.com/tectonic/
K8S单节点,适合在本地学习使用
官网 :https://kubernetes.io/docs/setup/learning-environment/minikube/
GitHub :https://github.com/kubernetes/minikube
本地多节点
GitHub :https://github.com/kubernetes/kubeadm
kubectl官网 :https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-win dows
minikube官网 :https://kubernetes.io/docs/tasks/tools/install-minikube/
选择任意一种虚拟化的方式
Hyper-V
VirtualBox[课上选择的]
安装kubectl
(1)根据官网步骤 [或] 直接下载: https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/windows/amd64/kubectl.exe
(2)配置kubectl.exe
所在路径的环境变量,使得cmd
窗口可以直接使用kubectl
命令
(4)kubectl version
检查是否配置成功
安装minikube
(1)根据官网步骤 [或] 直接下载:
https://github.com/kubernetes/minikube/releases/download/v1.5.2/minikube-windows-amd64.exe
(2)修改minikube-windows-amd64.exe
名称为minikube.exe
(3)配置minikube
所在路径的环境变量,使得cmd
窗口可以直接使用minikube
命令
(4)minikube version
检查是否配置成功
使用minikube
创建单节点的k8s
minikube start --vm-driver=virtualbox --image-repository=gcr.azk8s.cn/google-containers
小结
其实就是通过minikube创建一个虚拟机,这个虚拟机中安装好了单节点的K8S环境然后通过kubectl进行交互
#创建K8S
minikube start
#删除K8S
minikube delete
#进入到K8S的机器中
minikube ssh
#查看状态
minikube status
#进入
dashboard minikube dashboard
kubectl官网 :https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-lin ux
minikube官网 :https://kubernetes.io/docs/tasks/tools/install-minikube/
安装docker
安装kubectl
#01 下载
#02 授权
chmod +x ./kubectl
#03 添加到环境变量
sudo mv ./kubectl /usr/local/bin/kubectl
#04 检查
kubectl version
安装minikube
#01 下载
wget https://github.com/kubernetes/minikube/releases/download/v1.5.2/minikube-linux-amd64
#02 配置环境变量
sudo mv minikube-linux-amd64 minikube && chmod +x minikube && mv minikube /usr/local/bin/
#03 检查
minikube version
使用minikube创建单节点的k8s
minikube start --vm-driver=none --image-repository=gcr.azk8s.cn/google-containers
也是下载安装kubectl
和minikube
,选择virtualbox
,然后minikube start
,就可以通过kubectl
操作咯
既然已经通过Minikube
搭建了单节点的Kubernetes
,不妨先感受一些组件的存在以及操作咯
kubectl config view
kubectl config get-contexts
kubectl cluster-info
(1)创建pod_nginx.yaml
resources/basic/pod_nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
-name: nginx
image: nginx ports:
- containerPort: 80
(2)根据pod_nginx.yaml文件创建pod
kubectl apply -f pod_nginx.yaml
(3)查看pod
kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx
(4)进入nginx容器
kubectl exec -it nginx bash
#通过docker进入 minikube ssh docker ps
docker exec -it containerid bash
(5)访问nginx,端口转发
#若在minikube中,直接访问
#若在物理主机上,要做端口转发
kubectl port-forward nginx 8080:80
(6)删除pod
kubectl delete -f pod_nginx.yaml
小结 :通过Minikube,我们使用kubectl操作单节点的K8S,而且也能感受到pod的创建和删除,包括pod中对应的容器,一切才刚刚开始,具体细节咱们先不聊,后面慢慢说。
更多架构知识,欢迎关注本套Java系列文章:Java架构师成长之路