minikube是一个单节点的kubernetes集群,通过它我们能够快速搭建kubernetes学习环境。
curl -Lo minikube https://aliyun.hellozjf.com:7004/uploads/2019/9/2/minikube
chmod +x minikube
install minikube /usr/local/bin
curl -Lo images.tar.gz https://aliyun.hellozjf.com:7004/uploads/2019/9/2/images.tar.gz
tar xvf images.tar.gz
cd images
docker load -i coredns
docker load -i echoserver
docker load -i etcd
docker load -i kube-addon-manager
docker load -i kube-apiserver
docker load -i kube-controller-manager
docker load -i kube-proxy
docker load -i kubernetes-dashboard-amd64
docker load -i kube-scheduler
docker load -i pause
docker load -i storage-provisioner
minikube start --vm-driver=none
kubectl是kubernetes终端工具,通过它可以操作kubernetes集群
curl -Lo kubectl https://aliyun.hellozjf.com:7004/uploads/2019/9/2/kubectl
chmod +x kubectl
mv ./kubectl /usr/local/bin/kubectl
kubectl version
显示下面内容就是正常的
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
server.js
var http = require('http');
var os = require('os');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!\nhostname:' + os.hostname());
};
var www = http.createServer(handleRequest);
www.listen(8080);
Dockerfile
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
生成docker镜像
docker build -t hellozjf/hello-world:1.0.0 .
修改server.js,将Hello World
改为Hello World2
再次生成docker镜像
docker build -t hellozjf/hello-world:2.0.0 .
上传镜像
docker push hellozjf/hello-world:1.0.0
docker push hellozjf/hello-world:2.0.0
术语 | 解释 |
---|---|
Node | 一台安装了docker的机器 |
Pod | 一个docker容器(其实也可以是容器的集合,不过我现在学的比较浅,先这样理解) |
Deployment | 一个部署规则,比方说指定A镜像要开三个实例,调度程序会根据这个规则创建容器 |
Service | 公开的容器,它通常使用NodePort,即公开的服务使用宿主机的端口 |
kubectl create deployment hello-world --image=hellozjf/hello-world:1.0.0
kubectl get deployments
结果为
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 1/1 1 1 5m17s
如何卡住了,可以通过kubectl get pods
查看当前pod的状态
kubectl expose deployment hello-world --type=NodePort --port=8080
kubectl get services
结果为
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world LoadBalancer 10.103.247.11 8080:32088/TCP 8s
kubernetes ClusterIP 10.96.0.1 443/TCP 32s
访问32088端口
kubectl scale deployments/hello-world --replicas=3
kubectl get deployment
结果为
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 3/3 3 3 3m56s
通过一下命令查看每个pod的情况
kubectl get pods
结果为
NAME READY STATUS RESTARTS AGE
hello-world-5d5c98456d-766qk 1/1 Running 0 6m37s
hello-world-5d5c98456d-sqllk 1/1 Running 0 14s
hello-world-5d5c98456d-wnhcf 1/1 Running 0 14s
负载均衡在Node内部是正常的,但是别的机器访问却是不正常的(总是访问同一个Pod),原因不明,我的知识量还不够大,搞不定这个东西。
kubectl set image deployments/hello-world hello-world=hellozjf/hello-world:2.0.0
查看结果
kubectl get rs
可以看到之前1.0.0的三个横向扩展的应用已经下线,2.0.0的三个横向扩展应用上线了
NAME DESIRED CURRENT READY AGE
hello-world-5d5c98456d 0 0 0 104m
hello-world-7969497f97 3 3 3 2m14s
kubectl rollout undo deployments/hello-world
查看结果
kubectl get rs
可以看到之前1.0.0的三个横向扩展应用恢复了,2.0.0的三个横向扩展应用下线了
NAME DESIRED CURRENT READY AGE
hello-world-5d5c98456d 3 3 3 106m
hello-world-7969497f97 0 0 0 4m32s
https://kubernetes.io/docs/tutorials/hello-minikube/