kubernetes学习笔记(二)——部署服务

前面一篇文章部署好了minikube环境,这次学习下怎么部署app到kubernetes环境当中去。参考的是官网的教程,稍微做了一点改动,为了进一步熟悉go和docker,这里我把教程中的js换成了go,其他学习的朋友可以改成Python,java等其他语言,进行学习。

(1)创建app

这里写了一个简单的hello kubernetes的简单server.go。

package main

import (
"net/http"
"log"
"io"
) func hello(w http.ResponseWriter,r *http.Request) {
io.WriteString(w,"hello, kubetnetes!\n")
} func main() {
http.HandleFunc("/hello",hello)
err := http.ListenAndServe(":8080",nil)
if err != nil {
log.Panic(err)
}
}

编译之后可以运行起来。

[root@localhost src]# go build -o server server.go
[root@localhost src]# ./server

新开一个窗口进行curl,可以看到输出了hello, kubetnetes!

[root@localhost ~]# curl 192.168.42.131:8080/hello
hello, kubetnetes!

(2)创建app的docker镜像

1、编写Dockerfile

比较简单,基于golang的官方镜像开始制作,将编译出来的server二进制复制到bin目录,然后执行二进制启动server。(此处记得将docker源换成国内的,要不然会非常慢。参考:教程

[root@localhost src]# cat Dockerfile
FROM golang:1.10
EXPOSE 8080
COPY server /usr/local/bin/
CMD server
[root@localhost src]#

2、打包镜像

注意要将Dockerfile和编译出来的server二进制放在同一个目录

[root@localhost src]# ls
Dockerfile server server.go
[root@localhost src]# docker build -t server:v1 .
Sending build context to Docker daemon 6.153 MB
Step 1/4 : FROM golang:1.10
Trying to pull repository docker.io/library/golang ...
1.10: Pulling from docker.io/library/golang
05d1a5232b46: Pull complete
5cee356eda6b: Pull complete
89d3385f0fd3: Pull complete
80ae6b477848: Pull complete
94ebfeaaddf3: Pull complete
e132030a369d: Pull complete
c67c5750c788: Pull complete
Digest: sha256:a325153f1f1e2edb76a76ad789aff172b89dd6178e8f74d39ef87c04d87d1961
Status: Downloaded newer image for docker.io/golang:1.10
---> a4afc24299ee
Step 2/4 : EXPOSE 8080
---> Running in ac21ed38b066
---> a28f84e7be93
Removing intermediate container ac21ed38b066
Step 3/4 : COPY server /usr/local/bin/
---> e6050a624a06
Removing intermediate container 248ded7f9ac9
Step 4/4 : CMD server
---> Running in a465911d277d
---> e205f52f0ec8
Removing intermediate container a465911d277d
Successfully built e205f52f0ec8

使用 docker image ls 可以看刚刚做好的镜像。

(3)部署app到k8s

1、利用kubectl run 创建一个deployment 或job 来管理容器

[root@localhost src]# kubectl run hello-k8s --image=server:v1 --port=8080
deployment.apps "hello-k8s" created

2、查看创建的deployment

[root@localhost src]# kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-k8s 1 1 1 0 30s

3、查看pod

[root@localhost src]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-k8s-69c8f4fb76-jx4kg 1/1 Running 0 1m

4、将资源暴露为新的Kubernetes Service。

[root@localhost src]# kubectl expose deployment hello-k8s --type=LoadBalancer
service "hello-k8s" exposed

通过–type=LoadBalancer flag来在群集外暴露Service,在支持负载均衡的云提供商上,将配置外部IP地址来访问Service。在Minikube上,该LoadBalancer type使服务可以通过minikube Service 命令访问。

5、查看暴露后的Kubernetes Service

[root@localhost src]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-k8s LoadBalancer 10.100.59.64 <pending> 8080:31007/TCP 45s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1h

6、打开服务

[root@localhost src]# minikube service hello-k8s
Opening kubernetes service default/hello-k8s in default browser...
xdg-open: no method available for opening 'http://192.168.42.131:31007'

在浏览器当中打开http://192.168.42.131:31007/hello就可以看到输出了。

kubernetes学习笔记(二)——部署服务

(4)参考资料

https://kubernetes.io/docs/tutorials/hello-minikube/#before-you-begin

上一篇:angular2 学习笔记 ( server-side rendering, angular universal, 服务端渲染 )


下一篇:javascript 之 面向对象【创建对象】