“在Kubernetes上运行应用程序”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
 
第1行: 第1行:
 +
=golang=
 +
<pre>
 +
tmp# cat Dockerfile
 +
FROM golang:alpine
 +
RUN mkdir /app
 +
COPY . /app
 +
WORKDIR /app
 +
RUN go mod init  example.com/m/v2
 +
RUN go build -o main .
 +
CMD ["/app/main"]
 +
root@myxps:/home/evan/data/devops/go/gotmp# cat 1.go
 +
package main
  
 +
import (
 +
    "fmt"
 +
    "net/http"
 +
)
 +
 +
func index(w http.ResponseWriter, r *http.Request) {
 +
    fmt.Fprintf(w, "<h1>Hello World</h1>")
 +
}
 +
 +
func check(w http.ResponseWriter, r *http.Request) {
 +
    fmt.Fprintf(w, "<h1>Health check</h1>")
 +
}
 +
 +
func main() {
 +
    http.HandleFunc("/", index)
 +
    http.HandleFunc("/health_check", check)
 +
    fmt.Println("Server starting...")
 +
    http.ListenAndServe(":3000", nil)
 +
}
 +
 +
docker build  --tag evan886/go-app:v1 .
 +
 +
docker push  evan886/go-app:v1
 +
 +
cat go.yml
 +
apiVersion: apps/v1
 +
kind: Deployment
 +
metadata:
 +
  name: my-go-app
 +
spec:
 +
  replicas: 1
 +
  selector:
 +
    matchLabels:
 +
      app: go-app
 +
  template:
 +
    metadata:
 +
      labels:
 +
        app: go-app
 +
    spec:
 +
      containers:
 +
        - name: go-app-container
 +
          image: evan886/go-app:v1
 +
          resources:
 +
            limits:
 +
              memory: "128Mi"
 +
              cpu: "500m"
 +
          ports:
 +
            - containerPort: 3000
 +
 +
 +
 +
kubectl apply -f go.yml
 +
deployment.apps/my-go-app created
 +
root@ubuntu-200430-1:~/go# kubectl get deployments
 +
NAME              READY  UP-TO-DATE  AVAILABLE  AGE
 +
my-go-app          0/1    1            0          8s
 +
mysql              1/1    1            1          10h
 +
nginx-deployment  1/1    1            1          3d23h
 +
nginx-server      1/1    1            1          3d22h
 +
root@ubuntu-200430-1:~/go# kubectl get pod
 +
NAME                                READY  STATUS              RESTARTS      AGE
 +
debug-pod                          2/2    Running            10 (11h ago)  3d23h
 +
debug-podd                          2/2    Running            11 (11h ago)  3d23h
 +
kafka-0                            0/1    Pending            0              2d10h
 +
kafka-1                            0/1    Pending            0              2d10h
 +
kafka-2                            0/1    Pending            0              2d10h
 +
my-go-app-697b68fd46-q9nrv          0/1    ContainerCreating  0              17s
 +
 +
root@ubuntu-200430-1:~/go# kubectl expose deployment my-go-app --type=NodePort --name=go-app-svc --target-port=3000
 +
service/go-app-svc exposed
 +
 +
root@ubuntu-200430-1:~/go# kubectl  get svc
 +
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP  PORT(S)            AGE
 +
go-app-svc      NodePort    10.233.151.125  <none>        3000:31645/TCP      27s
 +
 +
 +
http://192.168.10.31:31645/
 +
 +
</pre>
 +
 +
 +
 +
 +
 +
=renferences=
 
[https://linux.cn/article-9845-1.html 在 Kubernetes 上运行一个 Python 应用程序]
 
[https://linux.cn/article-9845-1.html 在 Kubernetes 上运行一个 Python 应用程序]
  

2023年11月2日 (四) 13:35的最新版本

golang

tmp# cat Dockerfile 
FROM golang:alpine
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go mod init  example.com/m/v2
RUN go build -o main . 
CMD ["/app/main"]
root@myxps:/home/evan/data/devops/go/gotmp# cat 1.go 
package main

import (
    "fmt"
    "net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>Hello World</h1>")
}

func check(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>Health check</h1>")
}

func main() {
    http.HandleFunc("/", index)
    http.HandleFunc("/health_check", check)
    fmt.Println("Server starting...")
    http.ListenAndServe(":3000", nil)
}

docker build  --tag evan886/go-app:v1 .

docker push   evan886/go-app:v1

 cat go.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-go-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-app
  template:
    metadata:
      labels:
        app: go-app
    spec:
      containers:
        - name: go-app-container
          image: evan886/go-app:v1
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
            - containerPort: 3000



 kubectl apply -f go.yml 
deployment.apps/my-go-app created
root@ubuntu-200430-1:~/go# kubectl get deployments 
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
my-go-app          0/1     1            0           8s
mysql              1/1     1            1           10h
nginx-deployment   1/1     1            1           3d23h
nginx-server       1/1     1            1           3d22h
root@ubuntu-200430-1:~/go# kubectl get pod 
NAME                                READY   STATUS              RESTARTS       AGE
debug-pod                           2/2     Running             10 (11h ago)   3d23h
debug-podd                          2/2     Running             11 (11h ago)   3d23h
kafka-0                             0/1     Pending             0              2d10h
kafka-1                             0/1     Pending             0              2d10h
kafka-2                             0/1     Pending             0              2d10h
my-go-app-697b68fd46-q9nrv          0/1     ContainerCreating   0              17s

root@ubuntu-200430-1:~/go# kubectl expose deployment my-go-app --type=NodePort --name=go-app-svc --target-port=3000 
service/go-app-svc exposed

root@ubuntu-200430-1:~/go# kubectl  get svc 
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
go-app-svc       NodePort    10.233.151.125   <none>        3000:31645/TCP      27s


http://192.168.10.31:31645/



renferences

在 Kubernetes 上运行一个 Python 应用程序

Kubernetes集群部署tomcat容器/Java web应用流程-docker cp方式

Kubernetes集群部署tomcat容器/Java web应用流程-docker cp方式

使用Docker搭建 Java Web运行环境