ubuntu 22.04 minikube 部署 应用测试

准备环境

参考:https://blog.csdn.net/qq_52397471/article/details/133979727?spm=1001.2014.3001.5501

编写 Golang 应用

代码

golang 复制代码
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

部署测试

1. 打包 Docker 镜像

Dockerfile 复制代码
# docker build -t leo/webserver .
# 为了减小体积,使用scratch,实际使用golang官方镜像
FROM scratch

COPY ./webserver /webserver

CMD ["/webserver"]

2. 构建镜像

shell 复制代码
# 1.本机制作go镜像
docker build -t yuluo/webserver .    (名称必须是 Dockerfile)
docker image save yuluo/webserver > webserver.tar
# 2.上传到minikube虚拟机中docker镜像库
minikube image load webserver.tar

3. 部署 Pod

1. 编写 Pod yaml 资源文件
yaml 复制代码
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

2. 部署
shell 复制代码
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
3. 查看状态
shell 复制代码
# 查看 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
4. 访问验证
shell 复制代码
# 使用 minikube ssh 到此 节点 上访问 pod 验证
`minikube ssh --node minikube`

`curl 10.244.0.10`

# 最终结果如下
```shell
docker@minikube:~$ curl 10.244.0.10
Hello World!

4. 创建 service 暴露服务

1. 编写 yaml 文件
yaml 复制代码
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中作为作为服务的可用端点。

2. 查看 svc
shell 复制代码
# 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>
3. 访问测试
shell 复制代码
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!

5. 创建 Ingress 暴露资源

1. 环境准备

为了在 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 -

如下所示即为成功状态:

shell 复制代码
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

1. 编写 yaml 文件
yaml 复制代码
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。

2. 查看状态信息
shell 复制代码
# 通过 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>
3. 访问测试
shell 复制代码
# 设置 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!
相关推荐
wdfk_prog2 小时前
[Linux]学习笔记系列 -- hashtable
linux·笔记·学习
每日出拳老爷子2 小时前
【远程协助】内网 IT 运维远程协助系统的最小可用架构
运维·服务器·远程工作·流媒体·视音频
weixin_462446232 小时前
使用 Puppeteer 设置 Cookies 并实现自动化分页操作:前端实战教程
运维·前端·自动化
oMcLin2 小时前
如何在 Ubuntu 22.10 上通过 Kubernetes 和 Helm 管理微服务应用,简化跨平台电商平台的自动化部署?
ubuntu·微服务·kubernetes
CheungChunChiu2 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
oMcLin3 小时前
如何在Ubuntu 20.04上配置并优化容器化的SaaS应用平台,实现弹性伸缩与跨区域分布?
ubuntu·sass
BlueBirdssh4 小时前
linux 内核通过 dts 设备树 配置pcie 控制器 各种参数和中断等, 那freeRTOS 是通过直接设置PCIe寄存器吗
linux
小目标一个亿4 小时前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx
实战项目4 小时前
软件测试自动化框架的设计与实现
运维·自动化
Aotman_4 小时前
Element-UI Message Box弹窗 使用$confirm方法自定义模版内容,修改默认样式
linux·运维·前端