先前有研究 Windows Desktop 上的 Kubernetes 整合 jenkins,這次來試著用Jenkins 整合 Linux 的 Kubernetes。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
sudo vim dashboard-adminuser.yaml
apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: admin-user roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: admin-user namespace: kubernetes-dashboard
kubectl apply -f dashboard-adminuser.yaml
kubectl -n kubernetes-dashboard create token admin-user --duration=876000h
kubectl proxy
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/login
參考先前撰寫的文章
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. Unbuntu 和 Jenkins 容器的 kubectl 版本最好一致
docker exec -it -uroot jenkins /bin/bash mkdir -p /.kube exit docker cp ~/.kube/config jenkins:/root/.kube
4. Kubernetes URL => Ubuntu 透過可以 kubectl cluster-info 查出 Url ![disable http certificate check](kubectl cluster-info.png.png)
https://192.168.50.50:6443 // 192.168.50.50是我家的Ubuntu 內網的主機IP
properties([pipelineTriggers([githubPush()])]) pipeline { agent any environment { tag = ':latest' imageShortName = 'k8s-next-ec' imageName = "${imageShortName}${tag}" containerName = "${imageShortName}-1" dockerfile = "./Dockerfile" registryUrl = "192.168.50.50:5000" registry = "${registryUrl}/${imageShortName}" } stages { stage("GitHub Pull") { steps { git branch: 'main', credentialsId: 'e85233ad-a3c5-448b-a6ea-9f53e4f9b3f1', url: '[email protected]:markku636/ec.git/' } } stage("Building Docker Image") { steps { script { dockerImage = docker.build "$registry${tag}" } } } stage("Deploying to Registry Server") { steps { script { docker.withRegistry("","") { dockerImage.push("latest") } } } } stage("Cleaning Up") { steps { sleep(time: 3, unit: "SECONDS") sh "docker rmi --force $registry:latest" } } stage("Deply") { steps { withKubeConfig([credentialsId: 'k8s-secret', serverUrl: 'https://192.168.50.50:6443']) { sh 'kubectl apply -f ./next-js-deployment.yaml' sh 'kubectl rollout restart deployment/k8s-next-ec' } } } } }
apiVersion: apps/v1 kind: Deployment metadata: name: k8s-next-ec labels: app: k8s-next-ec spec: selector: matchLabels: app: k8s-next-ec tier: web template: metadata: labels: app: k8s-next-ec tier: web spec: containers: - name: k8s-next-ec-app image: 192.168.50.50:5000/k8s-next-ec:latest ports: - containerPort: 3000 --- apiVersion: v1 kind: Service metadata: name: k8s-next-ec labels: app: k8s-next-ec spec: selector: app: k8s-next-ec type: NodePort ports: - name: http protocol: TCP port: 3000 targetPort: 3000 nodePort: 30066
此時執行 Jenkins 執行建置,應該己經可以看到成功。
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Head “http://%2Fvar%2Frun%2Fdocker.sock/_ping”: dial unix /var/run/docker.sock: connect: permission denied
sudo chmod 777 /var/run/docker.sock