brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Aug 18. 2021

162. GCP 쿠버네티스와 젠킨스 설치


<1> GCP 환경 설정

<2> 클러스터 만들기

<3> Helm 설정

<4> 젠킨스 설치  (10분)

<5>  애플리케이션 배포

<6>  최소 4개의 복제본 실행

<7>  저장소 만들기

<20> 같이 보면 좋을 자료




<1> GCP 환경 설정


1

gcloud auth list

gcloud config list project


2

gcloud config set compute/zone us-east1-d


인증 설정 참고하기

https://brunch.co.kr/@topasvga/1132



<2> 클러스터 만들기


1

gcloud container clusters create jenkins-cd \

--num-nodes 2 \

--machine-type n1-standard-2 \

--scopes "https://www.googleapis.com/auth/source.read_write,cloud-platform"


생성 상태 

NAME        LOCATION    MASTER_VERSION  MASTER_IP      MACHINE_TYPE   NODE_VERSION    NUM_NODES  STATUS

jenkins-cd  us-east1-d  1.20.8-gke.900  35.231.237.66  n1-standard-2  1.20.8-gke.900  2          RUNNING

student_02_5112c2b77e78@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-1eea15366652)$


3

클러스터 상태 확인

gcloud container clusters list


$ gcloud container clusters list

NAME        LOCATION    MASTER_VERSION  MASTER_IP      MACHINE_TYPE   NODE_VERSION    NUM_NODES  STATUS

jenkins-cd  us-east1-d  1.20.8-gke.900  35.231.237.66  n1-standard-2  1.20.8-gke.900  2          RUNNING



4

사용자 인증 정보 가져오기

gcloud container clusters get-credentials jenkins-cd


$ gcloud container clusters get-credentials jenkins-cd

Fetching cluster endpoint and auth data.

kubeconfig entry generated for jenkins-cd.



5

사용자 인증 정보 확인

kubectl cluster-info


$ kubectl cluster-info

Kubernetes control plane is running at https://35.231.237.66

GLBCDefaultBackend is running at https://35.231.237.66/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy

KubeDNS is running at https://35.231.237.66/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Metrics-server is running at https://35.231.237.66/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy


To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.





<3> Helm 설정


1

Hml 레포에 젠킨스 추가

helm repo add jenkins https://charts.jenkins.io


2

helm 차트 업데이트

helm repo update




<4> 젠킨스 설치  (10분)



1

helm install cd jenkins/jenkins -f jenkins/values.yaml --version 1.2.2 --wait


$ helm install cd jenkins/jenkins -f jenkins/values.yaml --version 1.2.2 --wait

NAME: cd

LAST DEPLOYED: Tue Aug 17 10:30:03 2021

NAMESPACE: default

STATUS: deployed

REVISION: 1

NOTES:

1. Get your 'admin' user password by running:

  printf $(kubectl get secret --namespace default cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo

2. Get the Jenkins URL to visit by running these commands in the same shell:

  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=cd" -o jsonpath="{.items[0].metadata.name}")

  echo http://127.0.0.1:8080

  kubectl --namespace default port-forward $POD_NAME 8080:8080


3. Login with the password from step 1 and the username: admin

For more information on running Jenkins on Kubernetes, visit:

https://cloud.google.com/solutions/jenkins-on-container-engine



2

alias k=kubectl

complete -F __start_kubectl k

k get ns,no,po,svc,deploy,rs,ing,ep  


$ k get ns,no,po,svc,deploy,rs,ing,ep


NAME                        STATUS   AGE

namespace/default           Active   6m38s

namespace/kube-node-lease   Active   6m39s

namespace/kube-public       Active   6m39s

namespace/kube-system       Active   6m40s


NAME                                             STATUS   ROLES    AGE     VERSION

node/gke-jenkins-cd-default-pool-46403413-5v24   Ready    <none>   5m56s   v1.20.8-gke.900

node/gke-jenkins-cd-default-pool-46403413-mgmq   Ready    <none>   5m56s   v1.20.8-gke.900


NAME                              READY   STATUS    RESTARTS   AGE

pod/cd-jenkins-7b6c86874d-gl99z   1/1     Running   0          2m31s


NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE

service/cd-jenkins         ClusterIP   10.3.246.188   <none>        8080/TCP    2m31s

service/cd-jenkins-agent   ClusterIP   10.3.252.229   <none>        50000/TCP   2m31s

service/kubernetes         ClusterIP   10.3.240.1     <none>        443/TCP     6m38s


NAME                         READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/cd-jenkins   1/1     1            1           2m31s


NAME                                    DESIRED   CURRENT   READY   AGE

replicaset.apps/cd-jenkins-7b6c86874d   1         1         1       2m31s


NAME                         ENDPOINTS           AGE

endpoints/cd-jenkins         10.0.0.6:8080       2m31s

endpoints/cd-jenkins-agent   10.0.0.6:50000      2m31s

endpoints/kubernetes         35.231.237.66:443   6m38s



3

kubectl get pods


4

서비스 계정 만들기

$ kubectl create clusterrolebinding jenkins-deploy --clusterrole=cluster-admin --serviceaccount=default:cd-jenkins

clusterrolebinding.rbac.authorization.k8s.io/jenkins-deploy created


5

export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=cd" -o jsonpath="{.items[0].metadata.name}")


kubectl port-forward $POD_NAME 8080:8080 >> /dev/null &


8

etes (qwiklabs-gcp-02-1eea15366652)$ kubectl get svc

NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE

cd-jenkins         ClusterIP   10.3.246.188   <none>        8080/TCP    4m33s

cd-jenkins-agent   ClusterIP   10.3.252.229   <none>        50000/TCP   4m33s

kubernetes         ClusterIP   10.3.240.1     <none>        443/TCP     8m40s



9

젠킨스 암호 만들기


443/TCP     8m40s

student_02_5112c2b77e78@cloudshell:~/continuous-deployment-on-kubernetes (qwiklabs-gcp-02-1eea15366652)$ printf $(kubectl get secret cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo

jaCLj8eCXB

student_02_5112c2b77e78@clo




<5>  애플리케이션 배포


1

별도의 네임 스페이스는 만든다.


$ kubectl create ns production

namespace/production created


2

prd와 카나리 배포하고

$ kubectl apply -f k8s/production -n production

deployment.apps/gceme-backend-production created

deployment.apps/gceme-frontend-production created


3

$ kubectl apply -f k8s/canary -n production

deployment.apps/gceme-backend-canary created

deployment.apps/gceme-frontend-canary created


4

서비스 생성하기

$ kubectl apply -f k8s/services -n production

service/gceme-backend created


5

확인


$ k get ns,no,po,svc,deploy,rs,ing,ep


NAME                        STATUS   AGE

namespace/default           Active   13m

namespace/kube-node-lease   Active   13m

namespace/kube-public       Active   13m

namespace/kube-system       Active   13m

namespace/production        Active   56s


NAME                                             STATUS   ROLES    AGE   VERSION

node/gke-jenkins-cd-default-pool-46403413-5v24   Ready    <none>   12m   v1.20.8-gke.900

node/gke-jenkins-cd-default-pool-46403413-mgmq   Ready    <none>   12m   v1.20.8-gke.900


NAME                              READY   STATUS    RESTARTS   AGE

pod/cd-jenkins-7b6c86874d-gl99z   1/1     Running   0          9m5s


NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE

service/cd-jenkins         ClusterIP   10.3.246.188   <none>        8080/TCP    9m5s

service/cd-jenkins-agent   ClusterIP   10.3.252.229   <none>        50000/TCP   9m5s

service/kubernetes         ClusterIP   10.3.240.1     <none>        443/TCP     13m


NAME                         READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/cd-jenkins   1/1     1            1           9m6s


NAME                                    DESIRED   CURRENT   READY   AGE

replicaset.apps/cd-jenkins-7b6c86874d   1         1         1       9m6s


NAME                         ENDPOINTS           AGE

endpoints/cd-jenkins         10.0.0.6:8080       9m6s

endpoints/cd-jenkins-agent   10.0.0.6:50000      9m6s

endpoints/kubernetes         35.231.237.66:443   13m




<6>  최소 4개의 복제본 실행


kubectl scale 명령어를 사용


1

확장

kubectl scale deployment gceme-frontend-production -n production --replicas 4


2

kubectl get pods -n production -l app=gceme -l role=frontend


3

kubectl get pods -n production -l app=gceme -l role=backend


4

kubectl get service gceme-frontend -n production


$ kubectl get service gceme-frontend -n production


NAME             TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE

gceme-frontend   LoadBalancer   10.3.252.247   34.139.143.17   80:32588/TCP   7m49s



5

$ kubectl get pods -n production -l app=gceme -l role=frontend


NAME                                        READY   STATUS    RESTARTS   AGE

gceme-frontend-canary-767dc49f5-46gnz       1/1     Running   0          3m50s

gceme-frontend-production-fd8947698-58xkv   1/1     Running   0          3m58s

gceme-frontend-production-fd8947698-7lm5h   1/1     Running   0          94s

gceme-frontend-production-fd8947698-ndrsq   1/1     Running   0          94s

gceme-frontend-production-fd8947698-tgrnm   1/1     Running   0          94s


$ kubectl get pods -n production -l app=gceme -l role=backend


NAME                                        READY   STATUS    RESTARTS   AGE

gceme-backend-canary-6db485dfd5-bksmn       1/1     Running   0          4m28s

gceme-backend-production-59c75dcb54-mdrpt   1/1     Running   0          4m36s


6

변수 등록

export FRONTEND_SERVICE_IP=$(kubectl get -o jsonpath="{.status.loadBalancer.ingress[0].ip}" --namespace=production services gceme-frontend)



7

curl 

http://$FRONTEND_SERVICE_IP/version


curl 

http://

34.139.143.17

/version


$ curl http://34.139.143.17/version

1.0.0




<7>  저장소 만들기


1

gcloud source repos create default


2

git config --global user.email "topasvga@naver.com"

git config  --global user.name "taeho"


참고

git config --global user.email "you@example.com"

git config --global user.name "Your Name"


3

git add .


git commit -m "Initial commit"


git push origin master


4

git 참고 자료

https://brunch.co.kr/@topasvga/764






<20> 같이 보면 좋을 자료


https://brunch.co.kr/@topasvga/1800



감사합니다.


브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari