DevOpsifying Aplikasi Web Go: Panduan Hujung-ke-Hujung

WBOY
Lepaskan: 2024-09-06 06:39:02
asal
685 orang telah melayarinya

pengenalan

Dalam siaran ini, saya akan membimbing anda melalui proses DevOpsifying aplikasi web berasaskan Go. Kami akan merangkumi segala-galanya daripada membekalkan aplikasi dengan Docker hingga menggunakan aplikasi pada kelompok Kubernetes (AWS EKS) menggunakan Helm, menyediakan penyepaduan berterusan dengan GitHub Actions dan mengautomasikan penggunaan dengan ArgoCD. Pada penghujung tutorial ini, anda akan mempunyai aplikasi web Go berdaya CI/CD yang beroperasi sepenuhnya.

Prasyarat

Sebelum memulakan projek ini, pastikan anda memenuhi prasyarat berikut:

Akaun AWS: Anda memerlukan akaun AWS yang aktif untuk mencipta dan mengurus kelompok EKS anda untuk menggunakan aplikasi berasaskan Go.

Akaun DockerHub: Anda sepatutnya mempunyai akaun DockerHub untuk menolak imej Docker anda.

Pengetahuan Asas DevOps: Kebiasaan dengan konsep dan amalan DevOps adalah penting, termasuk memahami saluran paip CI/CD, kontena, orkestrasi dan penggunaan awan.

Helm: Pengetahuan asas Helm, pengurus pakej Kubernetes, akan diperlukan untuk membungkus dan menggunakan aplikasi anda.

Dengan memenuhi prasyarat ini, anda akan bersedia untuk mengikuti langkah-langkah dalam panduan ini dan berjaya DevOpsify aplikasi berasaskan Go anda!

Langkah 1: Mendapatkan Kod Sumber

Untuk memulakan projek, anda perlu mengklon kod sumber daripada repositori GitHub. Gunakan arahan berikut untuk mengklon projek:

git clone https://github.com/iam-veeramalla/go-web-app-devops.git
Salin selepas log masuk

Repositori ini mengandungi semua fail dan konfigurasi yang diperlukan untuk menyediakan dan menggunakan aplikasi berasaskan Go menggunakan amalan DevOps yang diterangkan dalam panduan ini. Setelah diklon, anda boleh menavigasi melalui langkah-langkah di bawah dan mengikuti langkah-langkah tersebut untuk mengisi kontena, menggunakan dan mengurus aplikasi.

Langkah 2: Menyimpan Aplikasi Go Web

Langkah pertama adalah untuk menyimpan aplikasi Go kami. Kami akan menggunakan fail Docker berbilang peringkat untuk membina aplikasi Go dan mencipta imej sedia pengeluaran yang ringan.

FROM golang:1.22.5 as build

WORKDIR /app

COPY go.mod .

RUN go mod download

COPY . .

RUN go build -o main .

FROM gcr.io/distroless/base

WORKDIR /app

COPY --from=build /app/main .

COPY --from=build /app/static ./static

EXPOSE 8080

CMD ["./main"]
Salin selepas log masuk

Arahan untuk Membina dan Menolak Imej Docker:

docker login
docker build . -t go-web-app
docker push go-web-app:latest
Salin selepas log masuk

Dalam Fail Docker ini, peringkat pertama menggunakan imej Golang untuk membina aplikasi. Peringkat kedua menggunakan imej asas tanpa distro, yang jauh lebih kecil dan lebih selamat, mengandungi hanya fail yang diperlukan untuk menjalankan aplikasi Go kami.

Langkah 3: Menggunakan Kubernetes dengan AWS EKS

Seterusnya, kami akan menggunakan aplikasi kontena kami ke gugusan Kubernetes. Begini cara anda boleh menyediakan kluster anda dan menggunakan apl anda.

Buat Kluster EKS:

eksctl create cluster --name demo-cluster --region us-east-1
Salin selepas log masuk

Konfigurasi Penerapan (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-web-app
  labels:
    app: go-web-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-web-app
  template:
    metadata:
      labels:
        app: go-web-app
    spec:
      containers:
      - name: go-web-app
        image: iamamash/go-web-app:latest
        ports:
        - containerPort: 8080
Salin selepas log masuk

Tatarajah Perkhidmatan (service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: go-web-app
  labels:
    app: go-web-app
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: go-web-app
  type: ClusterIP
Salin selepas log masuk

Konfigurasi Ingress (ingress.yaml):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-web-app
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: go-web-app.local
    http:
      paths: 
      - path: /
        pathType: Prefix
        backend:
          service:
            name: go-web-app
            port:
              number: 80
Salin selepas log masuk

Gunakan konfigurasi menggunakan kubectl:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

Salin selepas log masuk

Menyediakan Pengawal Ingress Nginx:

Pengawal Ingress dalam Kubernetes mengurus akses luaran kepada perkhidmatan dalam kelompok, biasanya mengendalikan trafik HTTP dan HTTPS. Ia menyediakan penghalaan berpusat, membolehkan anda menentukan peraturan tentang cara trafik harus mencapai perkhidmatan anda. Dalam projek ini, kami menggunakan pengawal Nginx Ingress untuk mengurus dan menghalakan trafik dengan cekap ke aplikasi berasaskan Go kami yang digunakan dalam kelompok Kubernetes.

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.1/deploy/static/provider/aws/deploy.yaml
Salin selepas log masuk

Langkah 4: Pembungkusan dengan Helm

Untuk mengurus sumber Kubernetes kami dengan lebih berkesan, kami membungkus aplikasi kami menggunakan Helm, pengurus pakej untuk Kubernetes.

Buat Carta Helm:

helm create go-web-app-chart
Salin selepas log masuk

Selepas mencipta carta, gantikan segala-galanya di dalam direktori templat dengan fail deployment.yaml, service.yaml dan ingress.yaml anda.

Kemas kini values.yaml: Fail values.yaml akan mengandungi nilai dinamik, seperti teg imej Docker. Teg ini akan dikemas kini secara automatik berdasarkan ID larian Tindakan GitHub, memastikan setiap penggunaan adalah unik.

# Default values for go-web-app-chart.
replicaCount: 1

image:
  repository: iamamash/Go-Web-App
  pullPolicy: IfNotPresent
  tag: "10620920515" # Will be updated by CI/CD pipeline

ingress:
  enabled: false
  className: ""
  annotations: {}
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
Salin selepas log masuk

Penggunaan Helm:

kubectl delete -f k8s/.
helm install go-web-app helm/go-web-app-chart
kubectl get all
Salin selepas log masuk

Langkah 5: Penyepaduan Berterusan dengan Tindakan GitHub

Untuk mengautomasikan binaan dan penggunaan aplikasi kami, kami menyediakan saluran paip CI/CD menggunakan GitHub Actions.

Aliran Kerja Tindakan GitHub (.github/workflows/cicd.yaml):

name: CI/CD

on:
  push:
    branches:
      - main
    paths-ignore:
      - 'helm/**'
      - 'README.md'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up Go 1.22
      uses: actions/setup-go@v2
      with:
        go-version: 1.22

    - name: Build
      run: go build -o go-web-app

    - name: Test
      run: go test ./...

  push:
    runs-on: ubuntu-latest
    needs: build
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to DockerHub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}

    - name: Build and Push action
      uses: docker/build-push-action@v6
      with:
        context: .
        file: ./Dockerfile
        push: true
        tags: ${{ secrets.DOCKERHUB_USERNAME }}/go-web-app:${{github.run_id}}

  update-newtag-in-helm-chart:
    runs-on: ubuntu-latest
    needs: push
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.TOKEN }}

    - name: Update tag in Helm chart
      run: |
        sed -i 's/tag: .*/tag: "${{github.run_id}}"/' helm/go-web-app-chart/values.yaml

    - name: Commit and push changes
      run: |
        git config --global user.email "ansari2002ksp@gmail.com"
        git config --global user.name "Amash Ansari"
        git add helm/go-web-app-chart/values.yaml
        git commit -m "Updated tag in Helm chart"
        git push
Salin selepas log masuk

To securely store sensitive information like DockerHub credentials and Personal Access Tokens (PAT) in GitHub, you can use GitHub Secrets. To create a secret, navigate to your repository on GitHub, go to Settings > Secrets and variables > Actions > New repository secret. Here, you can add secrets like DOCKERHUB_USERNAME, DOCKERHUB_TOKEN, and TOKEN. Once added, these secrets can be accessed in your GitHub Actions workflows using ${{ secrets.SECRET_NAME }} syntax, ensuring that your sensitive data is securely managed during the CI/CD process.

Step 6: Continuous Deployment with ArgoCD

Finally, we implement continuous deployment using ArgoCD to automatically deploy the application whenever changes are pushed.

Install ArgoCD:

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
kubectl get svc argocd-server -n argocd
Salin selepas log masuk

Setup ArgoCD Project: To access the ArgoCD UI after setting it up, you first need to determine the external IP of the node where ArgoCD is running. You can obtain this by running the command:

kubectl get nodes -o wide
Salin selepas log masuk

Next, get the port number at which the ArgoCD server is running using:

kubectl get svc argocd-server -n argocd
Salin selepas log masuk

Once you have the external IP and port number, you can access the ArgoCD UI by navigating to http://:. For example, if the external IP is 54.161.25.151 and the port number is 30498, the URL to access ArgoCD UI would be http://54.161.25.151:30498.

To log in to the ArgoCD UI for the first time, use the default username admin. The password can be retrieved from the ArgoCD secrets using:

kubectl edit secret argocd-initial-admin-secret -n argocd
Salin selepas log masuk

Copy the encoded password from the data.password field and decode it using base64:

echo <encoded-password> | base64 --decode
Salin selepas log masuk

For example, if the encoded password is kjasdfbSNLnlkaW==, decoding it with:

echo kjasdfbSNLnlkaW== | base64 --decode
Salin selepas log masuk

will provide the actual password. Be sure to exclude any trailing % symbol from the decoded output when using the password to log in.

Now, after accessing the ArgoCD UI, since both ArgoCD and the application are in the same cluster, you can create a project. To do this, click on the "New App" button and fill in the required fields, such as:

  • App Name: Provide a name for your application.
  • Sync Policy: Choose between manual or automatic synchronization.
  • Self-Heal: Enable this option if you want ArgoCD to automatically fix any drift.
  • Source Path: Enter the GitHub repository URL where your application code resides.
  • Helm Chart Path: Specify the path to the Helm chart within your repository.
  • Destination: Set the Cluster URL and namespace where you want the application deployed.
  • Helm Values: Select the appropriate values.yaml file for your Helm chart.

After filling in these details, click on "Create" and wait for ArgoCD to create the project. ArgoCD will pick up the Helm chart and deploy the application to the Kubernetes cluster for you. You can verify the deployment using:

kubectl get all
Salin selepas log masuk

That's all you need to do!

Conclusion

Congratulations! You have successfully DevOpsified your Go web application. This end-to-end guide covered containerizing your application with Docker, deploying it with Kubernetes and Helm, automating builds with GitHub Actions, and setting up continuous deployments with ArgoCD. You are now ready to manage your Go application with full CI/CD capabilities.

DevOpsifying a Go Web Application: An End-to-End Guide

Feel free to leave your comments and feedback below! Happy DevOpsifying!

Reference

For a detailed video guide on deploying Go applications on AWS EKS, check out this video.

Atas ialah kandungan terperinci DevOpsifying Aplikasi Web Go: Panduan Hujung-ke-Hujung. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!