参考:https://blog.csdn.net/qq_52397471/article/details/133979727?spm=1001.2014.3001.5501
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World!")
})
log.Fatalln(http.ListenAndServe(":80", nil))
}
go mod init
go mod tidy
GOOS=linux GOARCH=386 go build -ldflags '-s -w' -o webserver
# docker build -t leo/webserver .
# 为了减小体积,使用scratch,实际使用golang官方镜像
FROM scratch
COPY ./webserver /webserver
CMD ["/webserver"]
# 1.本机制作go镜像
docker build -t yuluo/webserver . (名称必须是 Dockerfile)
docker image save yuluo/webserver > webserver.tar
# 2.上传到minikube虚拟机中docker镜像库
minikube image load webserver.tar
apiVersion: v1
kind: Pod
metadata:
name: webserver
labels:
name: webserver
spec:
containers:
- name: webserver
image: yuluo/webserver
imagePullPolicy: Never
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
hostPort: 8080
该字段设置imagePullPolicy: Never
使用本地的镜像,否则会从镜像仓库拉取最新导致失败Error: ErrImagePull
同时 因为设置 hostPort,可以在 minikube node 上访问 minikubeIp:8080
kubectl apply -f webserver-pod.yaml
# 出现如下表明部署成功
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl get pods -A | grep webserver
NAMESPACE NAME READY STATUS RESTARTS AGE
default webserver 1/1 Running 0 7s
# 查看 pod 状态
kubectl get pods webserver
kubectl describe pods webserver
# 测试 pod 访问
kubectl get pods webserver
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe pod webserver
Name: webserver
Namespace: default
Priority: 0
Service Account: default
Node: minikube/192.168.49.2 # 节点 ip
Start Time: Sat, 21 Oct 2023 04:22:54 +0000
Labels: name=webserver
Annotations: <none>
Status: Running
IP: 10.244.0.10 # pod ip
# 使用 minikube ssh 到此 节点 上访问 pod 验证
`minikube ssh --node minikube`
`curl 10.244.0.10`
# 最终结果如下
```shell
docker@minikube:~$ curl 10.244.0.10
Hello World!
apiVersion: v1
kind: Service
metadata:
name: webserver-svc
spec:
selector:
name: webserver
ports:
- port: 80
targetPort: 80
protocol: TCP
上面的示例定义了一个ClusterIP Service。到 ClusterIP 上端口 80 的流量将转发到你的Pod 上的端口 8080 (targetPort配置项),携带 name: webserver 标签的 Pod 将被添加到 Service中作为作为服务的可用端点。
# kubectl describe service webserver-svc 通过此命令查看 service 和 pod 的关系
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe service webserver-svc
Name: webserver-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: name=webserver
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.103.70.226
IPs: 10.103.70.226
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.0.10:80
Session Affinity: None
Events: <none>
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27h
webserver-svc ClusterIP 10.103.70.226 <none> 80/TCP 76s
minikube ssh --node minikube
# 显示如下
docker@minikube:~$ curl 10.244.0.10
Hello World!
为了在 minikube 中使用 nginx-ingress ,必须执行以下命令启用
minikube addons enable ingress
Note: 可能创建失败,因为 镜像 拉取失败,此时需要设置代理。
参考:https://blog.csdn.net/qq_52397471/article/details/133979528?spm=1001.2014.3001.5501
设置完成之后,重启 MiniKube 重试。
kubectl get pods -A
查看 ingress-nginx 是否启动成功,如没有 使用以下命令重试
kubectl get pod podName -n nameSpace -o yaml | kubectl replace --force - f -
如下所示即为成功状态:
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl get pods -n ingress-nginx | grep ingress-nginx-controller
ingress-nginx-controller-7799c6795f-29dnh 1/1 Running 0 21h
使用如下命名查看创建 pod 信息
kubectl describe pods podName -n nameSpace
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: webserver-ingress
spec:
ingressClassName: nginx-ingress
rules:
- host: "webserver.com"
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: webserver-svc
port:
number: 80
Ingress 实际上是与Service完全不同的资源,算是Service上面的一层代理,通常在 Service前使用Ingress来提供HTTP路由配置。它让我们可以设置外部 URL、基于域名的虚拟主机、SSL 和负载均衡。此处使用nginx-ingress作为控制器,它使用NGINX服务器作为反向代理来把流量路由给后面的Service。
# 通过 kubectl describe ingress webserver-ingress 查看 service 和 ingress 的关系
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe ingress webserver-ingress
Name: webserver-ingress
Labels: <none>
Namespace: default
Address:
Ingress Class: nginx-ingress
Default backend: <default>
Rules:
Host Path Backends
---- ---- --------
webserver.com
/ webserver-svc:80 (10.244.0.10:80)
Annotations: <none>
Events: <none>
# 设置 hosts 文件创建映射关系
vim /etc/hosts
<minikube ip> webserver.com
# 测试
curl webserver.com:8080
root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# curl webserver.com:8080
Hello World!