在Kubernetes中部署应用

在Kubernetes中部署应用,第1张

创建Deploymet

创建nginx-deployment.yaml文件:

apiVersion: apps/v1 # 指定api版本,此值必须在kubectl api-versions
kind: Deployment # 指定创建资源的角色或类型
metadata: # 资源的元数据或属性
  name: nginx-deployment # 资源的名字,在一个namespace下必须唯一
  labels: # 设定资源的标签
    app: nginx
spec: # 资源规范字段
  selector: # 选择器
    matchLabels: # 匹配标签
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec: # 资源规范字段
      containers: # 配置容器
        - name: nginx # 容器的名字
          image: nginx # 容器使用的镜像地址
          imagePullPolicy: IfNotPresent # 每次pod启动拉取镜像的策略,三个选择:Always、Never、IfNotPresent
                                        # Always:每次都检查;
                                        # Never:(不管本地是否存在)从不检查;
                                        # IfNotPresent:如果本地有就不检查,没有就拉取
  replicas: 2 # 声明副本数

应用nginx-deployment.yaml文件

➜   kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment configured
查看运行状态

查看Pod的运行状态

➜   kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx                               1/1     Running   0          82m
nginx-deployment-7cf7d6dbc8-fbm57   1/1     Running   0          7m9s
nginx-deployment-7cf7d6dbc8-fdtgt   1/1     Running   0          7m11s

也可以通过describe命令进行查看

➜   kubectl describe pod nginx-deployment-7cf7d6dbc8-fbm57
Name:         nginx-deployment-7cf7d6dbc8-fbm57
Namespace:    default
Priority:     0
Node:         docker-desktop/192.168.65.4
Start Time:   Fri, 01 Apr 2022 15:52:34 +0800
Labels:       app=nginx
              pod-template-hash=7cf7d6dbc8
Annotations:  <none>
Status:       Running
IP:           10.1.0.44
IPs:
  IP:           10.1.0.44
Controlled By:  ReplicaSet/nginx-deployment-7cf7d6dbc8
Containers:
  nginx:
    Container ID:   docker://d51b2a1ed6b4350e1b47c216a3174ccfa6a2ca4e0cedb16956908a80bf5cbf84
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 01 Apr 2022 15:52:34 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-58sw7 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-58sw7:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  9m31s  default-scheduler  Successfully assigned default/nginx-deployment-7cf7d6dbc8-fbm57 to docker-desktop
  Normal  Pulled     9m31s  kubelet            Container image "nginx" already present on machine
  Normal  Created    9m31s  kubelet            Created container nginx
  Normal  Started    9m31s  kubelet            Started container nginx
部署Nginx

创建nginx-service.yaml

apiVersion: v1 # 指定api版本,此值必须在kubectl
kind: Service # 指定资源类型
metadata: # 资源的元数据或属性
  name: nginx-service # 资源的名称,在同一个namespace中name必须唯一
  labels: # 设定资源的标签
    app: nginx
spec:
  selector:
    app: nginx
  ports:
    - port: 80
      name: nginx-port # 端口名称
      protocol: TCP # 协议
      nodePort: 30000
      targetPort: 80 # 容器暴露的端口
  type: NodePort

应用nginx-service.yaml

➜   kubectl apply -f nginx-service.yaml                                                                      
service/nginx-service created

访问 http://localhost:30000

curl http://localhost:30000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
部署 Go 程序
  1. 创建 go 应用
package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}
  1. 创建 Dockerfile
FROM golang:latest

WORKDIR /test
COPY . /test
ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn
RUN go build .

EXPOSE 8080
ENTRYPOINT ["./gindig"]
  1. 构建好镜像后将镜像推到 docker hub
docker login
docker push
  1. 创建deployment.yaml并应用kubectl apply -f deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-project
  labels:
    app: go-project
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-project
  template:
    metadata:
      labels:
        app: go-project
    spec:
      containers:
        - name: go-project
          image: wxl19950124/official_account:v0.0.1
---

apiVersion: v1
kind: Service
metadata:
  name: go-project-svc
  labels:
    app: go-project
spec:
  ports:
    - port: 8080
      targetPort: 8080
  type: ClusterIP
  selector:
    app: go-project

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-deployment-ingress 
spec:
  ingressClassName: nginx    # ingressClassName一定要配置否则
  rules:
    - host: nginx-ingress.local # 这种方式需要修改 /etc/hosts
                                # 127.0.0.1  nginx-ingress.local
                                # 也可以使用ngork http 80 将本地80端口映射到公网  
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: go-project-svc    # 这里要对应服务的名称
                port:
                  number: 8080
  1. 查看运行情况:
➜   kubectl get ingresses.
NAME                       CLASS   HOSTS                         ADDRESS     PORTS   AGE
official-account-ingress   nginx   d1f9-121-19-242-51.ngrok.io   localhost   80      13
  1. 验证是否可正常访问:
curl https://d1f9-121-19-242-51.ngrok.io/ping
pong

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/634341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-16
下一篇 2022-04-16

发表评论

登录后才能评论

评论列表(0条)

保存