先在宿主机的docker下载一下golang的最新镜像
docker pull golang:test
写个简单的服务器,监听7878端口,请求都返回hello, world
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
fmt.Println("start main")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello, world")
})
err := http.ListenAndServe(":7878", nil)
if err != nil {
panic(err)
}
os.Exit(0)
}
搞到docker里面制作镜像
# 先把容器起起来,-i保证不退出,-d后台启动
docker run -d -i --name test-con golang:latest
# 把文件拷贝进去
docker cp test.go test-con:/root/
# 提交镜像
docker commit test-con test-con:latest
这个时候查看docker的镜像里面就存在了我们制作的要发布的镜像
=> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test-con latest f6ee44e250ad 32 minutes ago 911MB
golang latest 0457bb691895 12 days ago 838MB
mirrors.sangfor.com/google_containers/k8s-minikube/kicbase v0.0.45 aeed0e1d4642 2 months ago 1.28GB
mirrors.sangfor.com/google_containers/kicbase v0.0.45 aeed0e1d4642 2 months ago 1.28GB
mirrors.sangfor.com/google_containers/coredns/coredns v1.11.1 cbb01a7bd410 15 months ago 59.8MB
mirrors.sangfor.com/google_containers/dashboard v2.7.0 07655ddf2eeb 2 years ago 246MB
mirrors.sangfor.com/google_containers/kubernetesui/dashboard v2.7.0 07655ddf2eeb 2 years ago 246MB
mirrors.sangfor.com/google_containers/kubernetesui/metrics-scraper v1.0.8 115053965e86 2 years ago 43.8MB
mirrors.sangfor.com/google_containers/metrics-scraper v1.0.8 115053965e86 2 years ago 43.8MB
mirrors.sangfor.com/google_containers/k8s-minikube/storage-provisioner v5 6e38f40d628d 3 years ago 31.5MB
mirrors.sangfor.com/google_containers/storage-provisioner v5 6e38f40d628d 3 years ago 31.5MB
注意,这里的镜像是宿主机的docker的,不是minikube里面的,minikube是个类似虚拟机的环境,里面还有一个docker。我们把这个镜像加载进去
minikube image load test-con:latest
然后就能看到这个这个镜像在minikube里面
=> minikube image ls
mirrors.sangfor.com/google_containers/pause:3.10
mirrors.sangfor.com/google_containers/kubernetesui/metrics-scraper:v1.0.8
mirrors.sangfor.com/google_containers/kubernetesui/dashboard:v2.7.0
mirrors.sangfor.com/google_containers/kube-scheduler:v1.31.0
mirrors.sangfor.com/google_containers/kube-proxy:v1.31.0
mirrors.sangfor.com/google_containers/kube-controller-manager:v1.31.0
mirrors.sangfor.com/google_containers/kube-apiserver:v1.31.0
mirrors.sangfor.com/google_containers/k8s-minikube/storage-provisioner:v5
mirrors.sangfor.com/google_containers/etcd:3.5.15-0
mirrors.sangfor.com/google_containers/coredns/coredns:v1.11.1
docker.io/library/test-con:latest
1) 使用pod启动镜像给外部访问
写一个yaml文件
apiVersion: v1 # 版本信息
kind: Pod # 类型是pod
metadata:
name: test-pod # pod取名为test-pod
labels: # 添加标签,主要为了service暴露端口使用
app: test
spec: # 定义spec期望
containers:
- name: test # 一个test镜像
image: test-con:latest # 使用test-con:latest镜像
imagePullPolicy: Never # 不允许从官方搜索,只找本地
command: ["go"] # 镜像启动后执行的命令
args: ["run", "/root/test.go"]
启动一下看看效果
kubectl apply -f test.yaml
看一下pod情况
=> kubectl get pods
NAME READY STATUS RESTARTS AGE
test-pod 1/1 Running 0 38m
STATUS在Running状态就对了,但是现在还是访问不了,因为只是在pod里面监听了7878端口,但是外部还没有监听。前面讲了service是用来将pod里面的端口对外暴露使用的,那么定义一个service
apiVersion: v1
kind: Service # 类型是service
metadata:
name: test-service # 起个名字
spec:
type: NodePort # 具体看后面解释
selector: # 选择器,此服务对应哪些pod
app: test # app为test的pod
ports:
- protocol: TCP
port: 80 # 集群内部监听端口
targetPort: 7878 # pod里面的端口
# nodePort: 30001 # 外部监听的端口,不设置就是随机一个,设置就要取30000以上的
启动看看效果
kubectl apply -f test-service.yaml
查看service情况
=> kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37d
test-service NodePort 10.99.198.195 <none> 80:30449/TCP 57m
可以看到集群节点内部监听了80,外部暴露了一个随机端口30449,想要访问这个端口需要使用minikube对外暴露的ip。
=> minikube ip
192.168.49.2
=> curl 192.168.49.2:30449
hello, world%
到这里,自己启动的pod给外部访问成功了
标签:google,mirrors,golang,minikube,test,sangfor,com,containers,搭建 From: https://blog.csdn.net/qq_26124425/article/details/143891010