Prepare the environment
Reference: https://blog.csdn.net/qq_52397471/article/details/133979727?spm=1001.2014.3001.5501
Writing Golang applications
Code
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)) }
Compile
go mod init
go mod tidy
GOOS=linux GOARCH=386 go build -ldflags '-s -w' -o webserver
Deployment testing
1. Package Docker image
# docker build -t leo/webserver . # In order to reduce the size, use scratch, actually use the golang official image FROM scratch COPY ./webserver /webserver CMD ["/webserver"]
2. Build image
# 1. Create go image locally docker build -t yuluo/webserver . (name must be Dockerfile) docker image save yuluo/webserver > webserver.tar # 2. Upload to the docker image library in the minikube virtual machine minikube image load webserver.tar
3. Deploy Pod
1. Write Pod yaml resource file
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
This field is set to imagePullPolicy: Never
to use the local image, otherwise it will pull the latest image from the image warehouse and cause failure Error: ErrImagePull
At the same time, because the hostPort is set, minikubeIp:8080 can be accessed on the minikube node
2. Deployment
kubectl apply -f webserver-pod.yaml # The following appears to indicate successful deployment 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. Check status
# Check pod status kubectl get pods webserver kubectl describe pods webserver # Test pod access 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 # Node 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. Access verification
# Use minikube ssh to access the pod on this node for verification `minikube ssh --node minikube` `curl 10.244.0.10` #The final result is as follows ```shell docker@minikube:~$ curl 10.244.0.10 Hello World!
4. Create service exposure service
1. Write yaml file
apiVersion: v1 Kind: Service metadata: name: webserver-svc spec: selector: name: webserver ports: - port: 80 targetPort: 80 protocol: TCP
The above example defines a ClusterIP Service. Traffic to port 80 on ClusterIP will be forwarded to port 8080 on your Pod (targetPort configuration item), and the Pod carrying the name: webserver label will be added to the Service as an available endpoint for the service.
2. View svc
# kubectl describe service webserver-svc Use this command to view the relationship between service and 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. Access test
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 # Displayed as follows docker@minikube:~$ curl 10.244.0.10 Hello World!
5. Create Ingress exposed resources
1. Environment preparation
In order to use nginx-ingress in minikube, you must execute the following command to enable it
minikube addons enable ingress
Note: The creation may fail because the image pull fails, and the proxy needs to be set at this time.
Reference: https://blog.csdn.net/qq_52397471/article/details/133979528?spm=1001.2014.3001.5501
After the setup is complete, restart MiniKube and try again.
kubectl get pods -A
Check whether ingress-nginx starts successfully. If not, use the following command to try again.
kubectl get pod podName -n nameSpace -o yaml | kubectl replace --force - f -
The success status is as follows:
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
Use the following naming to view the created pod information
kubectl describe pods podName -n nameSpace
1. Write yaml file
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 is actually a completely different resource from Service. It is a layer of proxy above Service. Ingress is usually used before Service to provide HTTP routing configuration. It allows us to set up external URLs, domain-based virtual hosting, SSL and load balancing. Here nginx-ingress is used as the controller, which uses the NGINX server as a reverse proxy to route traffic to the subsequent Service.
2. View status information
# View the relationship between service and ingress through kubectl describe ingress webserver-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. Access test
# Set hosts file to create mapping relationship vim /etc/hosts <minikube ip> webserver.com # test curl webserver.com:8080 root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# curl webserver.com:8080 Hello World!