简单介绍
今天使用Docker+Kubernetes搭建一个简单的Hello World Node.js应用程序。
实际操作
1、按照教程安装Docker+Kubernetes for Mac

docker version

k8s verison

Kubernetes版本
由此Docker+Kubernetes安装成功,属于mac的单机master node。
2、编写node.js应用程序
将此代码保存在以文件名命名hellonode 的文件夹中server.js:
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);
这可以先试用node server.js来运行一下,应该能够在http:// localhost:8080 /上看到“Hello World!”消息。按Ctrl-C停止正在运行的Node.js服务器。
3、将应用程序打包到Docker容器中
这里需要使用Dockerfile文件来创建容器,在server.js文件夹内创建Dockerfile文件,如下:
FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js
Docker镜像的这个配置从Docker注册表中的官方Node.js LTS镜像开始,公开端口8080,将server.js文件复制到镜像并启动Node.js服务器。
4、使用build指令根据Dockerfile文件创建镜像
使用Docker守护进程构建Docker镜像(注意最后的相对目录点 .):
docker build -t hello-node:v1 .
5、创建部署
Kubernetes Pod是一个由一个或多个容器组成的组合,用于管理和联网。本教程中的Pod只有一个Container。Kubernetes 部署会检查Pod的运行状况,并在Pod终止时重新启动Pod的容器。部署是管理Pod的创建和扩展的推荐方法。
使用该kubectl run命令创建管理Pod的Deployment。Pod根据hello-node:v1Docker镜像运行Container 。将 --image-pull-policy标志设置Never为始终使用本地映像,而不是从Docker注册表中提取它(因为您还没有将它推到那里):
kubectl run hello-node --image=hello-node:v1 --port=8080 --image-pull-policy=Never
查看部署:
kubectl get deployments
输出:

查看Pod:
kubectl get pods
输出:

6、创建服务
默认情况下,Pod只能通过Kubernetes集群中的内部IP地址访问。要从hello-node Kubernetes虚拟网络外部访问Container,须将Pod公开为Kubernetes 服务。可以使用以下kubectl expose命令将Pod公开到公共Internet :
kubectl expose deployment hello-node --type=LoadBalancer
查看刚刚创建的服务:
kubectl get services
输出:

--type=LoadBalancer标志表示在群集外部公开的服务。
7、使用本地IP地址自动打开浏览器窗口,并显示“Hello World”消息。

8、更新应用
编辑server.js文件以返回新消息,修改hello word那行:
response.end('Hello World Again!');
9、重新构建版本的镜像
docker build -t hello-node:v2 .
10、更新部署的镜像:
kubectl set image deployment/hello-node hello-node=hello-node:v2

11、再次运行您的应用以查看新消息:

12、Kubernetes仪表盘

参考:https://www.cnblogs.com/April-Chou-HelloWorld/p/9425151.html
------------------ 跑一个nginx demo---------
Nginx 示例
跑一个 Nginx container 来看看整个过程:
$ kubectl run hello-nginx --image=nginx --port=80
deployment “hello-nginx” created
这里会报一个kubectl run 命令即将废弃的错误,忽略,命令已经执行了
这行命令创建了一个 deployment,该 deployment 会创建一个 pod,pod 负责运行 container:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-nginx-556b7bf96-2xw8f 0/1 ContainerCreating 0 12s
等几秒钟,再看 Dashboard 的 Deployments:

当 Pods 由 0/1 变成 1/1 的时候, 刚才的命令结果也变成 Running 了
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-nginx-556b7bf96-2xw8f 1/1 Running 0 4m
点侧边栏的 Pod,再点我们刚创建的 Pod “hello-nginx-556b7bf96-2xw8f”,能看到这个 pod 的细节:

这里能看到给定的默认 labels,和分配的 IP (来此 docker-for-desktop Node)。
右上角的这几个按钮也是我们经常用的功能,EXEC 打开一个浏览器中的 shell ssh 进入 pod,LOGS 查看日志。

同样的,我们也可以通过 kubectl describe node/pod
命令来查看信息,如下:
$ kubectl describe pod hello-nginx-556b7bf96-2xw8f
Name: hello-nginx-556b7bf96-2xw8f
Namespace: default
Node: docker-for-desktop/192.168.65.3
Start Time: Tue, 20 Feb 2018 22:34:19 -0800
Labels: pod-template-hash=112636952
run=hello-nginx
Annotations: <none>
Status: Running
IP: 10.1.0.54
Controlled By: ReplicaSet/hello-nginx-556b7bf96
Containers:
hello-nginx:
Container ID: docker://5c5f68348ed8a204dcce089a21d71e36cbfdb6c7955fd28a7f148d5f44662861
Image: nginx
Image ID: docker-pullable://nginx@sha256:65f0ed5362b39c16cad51f27217c12ac9dd0db4dc47b0197969885f05198d4d8
Port: 80/TCP
State: Running
Started: Tue, 20 Feb 2018 22:34:37 -0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-m99ws (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-m99ws:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-m99ws
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 15m default-scheduler Successfully assigned hello-nginx-556b7bf96-2xw8f to docker-for-desktop
Normal SuccessfulMountVolume 15m kubelet, docker-for-desktop MountVolume.SetUp succeeded for volume "default-token-m99ws"
Normal Pulling 15m kubelet, docker-for-desktop pulling image "nginx"
Normal Pulled 15m kubelet, docker-for-desktop Successfully pulled image "nginx"
Normal Created 15m kubelet, docker-for-desktop Created container
Normal Started 15m kubelet, docker-for-desktop Started container
通过外部访问
可以用 port-forward pod 来实现外部访问:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-nginx-556b7bf96-2xw8f 1/1 Running 0 46m
$ kubectl port-forward hello-nginx-556b7bf96-2xw8f 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
但这只暴露特定 pod 的端口,在实际中并不实用。
所以我们换一个方法,把 deployment 暴露成一个服务,供外部访问:
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-nginx 1 1 1 1 18m
$ kubectl expose deployment hello-nginx --type=NodePort --name=hello-nginx-service
service "hello-nginx-service" exposed
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-nginx-service NodePort 10.109.0.203 <none> 80:30351/TCP 30s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10d
现在,浏览器里就能访问 localhost:30351 了。
网友评论