因為公司的容器越來越多,好幾台的Docker desktop ,實在不好調度及橫向擴展,因此花了些時間研究,如何在 Docker Desktop for windows 裡安裝jenkins 整合 K8s。
公司沒Linux Server ,多數成員不擅長用 Linux
docker run -d -p 5000:5000 -e REGISTRY_STORAGE_DELETE_ENABLED=true --name registry registry:2 docker update --restart always registry
"insecure-registries": [ "192.168.50.49:5000" ]
P.S. 192.168.50.49 為我的實驗機IP,可透過訪問 http://192.168.50.49:5000/v2/_catalog 檢查目前私有的docker hub 有那些映像檔
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml kubectl get pod -n kubernetes-dashboard kubectl proxy 訪問此目錄 http://127.0.0.1:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/login
kubectl describe secrets -n kube-system (kubectl get secrets -n kube-system | Select-String "dashboard" | ForEach-Object { $_ -split " " | Select-Object -First 1 })
$TOKEN=((kubectl -n kube-system describe secret default | Select-String "token:") -split " +")[1] kubectl config set-credentials docker-for-desktop --token="${TOKEN}" echo $TOKEN
也可以透過config 查詢 token
kubectl config view // 同等於訪問 C:\Users\Mark\.kube\config
參考先前撰寫的文章
docker exec -it -uroot jenkins bash // 進入jenkins 容器中 curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.29.1/bin/linux/amd64/kubectl // 下載 kubectl chmod +x ./kubectl // 給予權限 mv ./kubectl /usr/local/bin/kubectl // 複製到系統環境資料夾 kubectl version --client // 查詢版本
P.S. Docker Desktop 和 Jenkins 容器的 kubectl 版本最好一致
docker exec -it -uroot jenkins /bin/bash mkdir -p /.kube exit docker cp C:/Users/Mark/.kube jenkins:/root/.kube // 複製檔案到 Jenkins 容器中
properties([pipelineTriggers([githubPush()])]) pipeline { agent any environment { tag = ':latest' imageShortName = 'de-next-ap' imageName = "${imageShortName}${tag}" containerName = "${imageShortName}-1" containerUrl = "192.168.50.49:2376" dockerfile = "./Dockerfile" registry = "192.168.50.49:5000/next-ec" } stages { stage("GitHub Pull") { steps { git branch: 'main', credentialsId: '946a703f-dff7-4138-84b2-0aba700dedca', url: '[email protected]:markku636/ec.git/' } } stage("Building Docker Image") { steps { script { dockerImage = docker.build "$registry:latest" } } } stage("Deploying to Registry Server") { steps { script { docker.withRegistry("http://192.168.50.49:5000", "") { dockerImage.push() } } } } stage("Cleaning Up") { steps { sleep(time: 3, unit: "SECONDS") sh "docker rmi --force $registry:latest" } } stage("Deply") { steps { withKubeConfig([credentialsId: 'k8s-secret', serverUrl: 'https://host.docker.internal:6443']) { sh 'kubectl apply -f ./next-js-deployment.yaml' } } } } }
apiVersion: v1 kind: Deployment metadata: name: nextjs-deployment labels: app: nextjs spec: replicas: 1 selector: matchLabels: app: nextjs template: metadata: labels: app: nextjs spec: containers: - name: nextjs image: 192.168.50.49:5000/next-ec:latest ports: - containerPort: 80 resources: limits: memory: "128Mi" cpu: "500m" --- apiVersion: v1 kind: Service metadata: name: nextjs-service spec: type: NodePort selector: app: nextjs ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30099
此時執行 Jenkins 執行建置,應該己經可以執行成功。
K8S 功能及配置還蠻複雜的,光配置就花蠻多時間,蠻多細節還需要花些時間實驗及測試。
yaml 檔改了幾次版本,請依據官方文件撰寫最新的 YAML 佈署檔