スポンサーリンク

client-goのexampleであるin-cluster-client-configurationを動かしてみる

client-go
この記事は約17分で読めます。

はじめに

以前kubernetesクラスタの外側からclient-goを使うexampleを動かしてみたので今度はkubernetesクラスタ内部からclient-goを動かしてみる。

 

client-goのexampleであるout-of-cluster-client-configurationを動かしてみる
初めに client-goを使ってみたいと思っていたら素晴らしいexampleが揃っていたので動かしたときのメモを残しておく。 こちらはkubernetesクラスタの外側から動かす方法 kindの準備 動...

 

動かしてみるexampleはこちら。

 

client-go/examples/in-cluster-client-configuration at release-1.21 · kubernetes/client-go
Go client for Kubernetes. Contribute to kubernetes/client-go development by creating an account on GitHub.

 

準備

kindを使う。

各versionはこちら参照。

client-goのexampleであるout-of-cluster-client-configurationを動かしてみる
初めに client-goを使ってみたいと思っていたら素晴らしいexampleが揃っていたので動かしたときのメモを残しておく。 こちらはkubernetesクラスタの外側から動かす方法 kindの準備 動...

 

out-of-cluster-client-configurationと違うのはskaffoldを使うところ

 

こちらで使ったgetting-startedから一式持ってくる。README.mdは不要

https://hakengineer.xyz/2021/10/29/skaffoldを使ってみる/

 

$ tree skaffold
skaffold
├── Dockerfile
├── k8s-pod.yaml
├── main.go
└── skaffold.yaml

0 directories, 4 files

 

 

main.go

in-cluster-client-configurationのものに書き換え

examplexxxxx“部分を今あるpod名に変更。この時はnginx-6799fc88d8-w7d5kだったのでそれに置換。

sed -i 's/example-xxxxx/nginx-6799fc88d8-w7d5k/g' main.go/* Your code... */

 

 

Dockerfile

色々いじってこんな感じ

FROM golang:1.16 as builder

ENV GO111MODULE=on
ENV GOPATH=

COPY ./main.go ./
RUN go mod init test
RUN go mod download
RUN go mod tidy
RUN go build -o /app ./main.go


FROM golang:1.16

COPY --from=builder /app .
ENTRYPOINT [ "./app" ]

 

起動

kind起動させたらとりあえずnginxをdeploy

kubectl create deployment nginx --image=nginx

clusterrolebindingを作成しておく

kubectl create clusterrolebinding default-view --clusterrole=view --serviceaccount=default:default

 

skaffoldの起動

$ skaffold dev
Listing files to watch...
 - skaffold-example
Generating tags...
 - skaffold-example -> skaffold-example:latest
Some taggers failed. Rerun with -vdebug for errors.
Checking cache...
 - skaffold-example: Not found. Building
Starting build...
Found [kind-kind] context, using local docker daemon.
Building [skaffold-example]...
Sending build context to Docker daemon   5.12kB
Step 1/11 : FROM golang:1.16 as builder
1.16: Pulling from library/golang
bb7d5a84853b: Pulling fs layer
f02b617c6a8c: Pulling fs layer
d32e17419b7e: Pulling fs layer
c9d2d81226a4: Pulling fs layer
7bd370e02e50: Pulling fs layer
5cdf2d652a4d: Pulling fs layer
4573d57ef426: Pulling fs layer
7bd370e02e50: Waiting
5cdf2d652a4d: Waiting
4573d57ef426: Waiting
c9d2d81226a4: Waiting
f02b617c6a8c: Verifying Checksum
f02b617c6a8c: Download complete
d32e17419b7e: Verifying Checksum
d32e17419b7e: Download complete
bb7d5a84853b: Verifying Checksum
bb7d5a84853b: Download complete
c9d2d81226a4: Verifying Checksum
c9d2d81226a4: Download complete
7bd370e02e50: Verifying Checksum
7bd370e02e50: Download complete
4573d57ef426: Verifying Checksum
4573d57ef426: Download complete
5cdf2d652a4d: Verifying Checksum
5cdf2d652a4d: Download complete
bb7d5a84853b: Pull complete
f02b617c6a8c: Pull complete
d32e17419b7e: Pull complete
c9d2d81226a4: Pull complete
7bd370e02e50: Pull complete
5cdf2d652a4d: Pull complete
4573d57ef426: Pull complete
Digest: sha256:c8b7bf9166093208456120876e51e9cb387b0523bddb9d8c0b33ff621347e26f
Status: Downloaded newer image for golang:1.16
 ---> 135e37b786c8
Step 2/11 : ENV GO111MODULE=on
 ---> Running in 76951f168899
 ---> 1d63d640c05a
Step 3/11 : ENV GOPATH=
 ---> Running in ff98a5104983
 ---> 4a0b4d363bc6
Step 4/11 : COPY ./main.go ./
 ---> 4f5bfb7d6930
Step 5/11 : RUN go mod init test
 ---> Running in 23d42381197d
go: creating new go.mod: module test
go: to add module requirements and sums:
        go mod tidy
 ---> 3bbf9c2a7008
Step 6/11 : RUN go mod download
 ---> Running in 1d14478558b8
 ---> a56e4dc08941
Step 7/11 : RUN go mod tidy
 ---> Running in 72be72496ba1
go: finding module for package k8s.io/client-go/rest
go: finding module for package k8s.io/apimachinery/pkg/apis/meta/v1
go: finding module for package k8s.io/client-go/kubernetes
go: finding module for package k8s.io/apimachinery/pkg/api/errors
go: downloading k8s.io/client-go v1.5.2
go: downloading k8s.io/apimachinery v0.22.3
go: downloading k8s.io/client-go v0.22.3
go: found k8s.io/apimachinery/pkg/api/errors in k8s.io/apimachinery v0.22.3
go: found k8s.io/apimachinery/pkg/apis/meta/v1 in k8s.io/apimachinery v0.22.3
go: found k8s.io/client-go/kubernetes in k8s.io/client-go v0.22.3
go: found k8s.io/client-go/rest in k8s.io/client-go v0.22.3
go: downloading github.com/gogo/protobuf v1.3.2
go: downloading github.com/google/gofuzz v1.1.0
go: downloading sigs.k8s.io/yaml v1.2.0
go: downloading golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
go: downloading k8s.io/klog/v2 v2.9.0
go: downloading github.com/google/go-cmp v0.5.5
go: downloading github.com/stretchr/testify v1.7.0
go: downloading k8s.io/api v0.22.3
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/googleapis/gnostic v0.5.5
go: downloading google.golang.org/protobuf v1.26.0
go: downloading golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
go: downloading k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a
go: downloading sigs.k8s.io/structured-merge-diff/v4 v4.1.2
go: downloading gopkg.in/inf.v0 v0.9.1
go: downloading github.com/json-iterator/go v1.1.11
go: downloading github.com/modern-go/reflect2 v1.0.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
go: downloading golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
go: downloading github.com/go-logr/logr v0.4.0
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
go: downloading golang.org/x/sys v0.0.0-20210616094352-59db8d763f22
go: downloading golang.org/x/text v0.3.6
go: downloading github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e
go: downloading google.golang.org/appengine v1.6.5
go: downloading github.com/kr/text v0.2.0
 ---> 3f53169b40e2
Step 8/11 : RUN go build -o /app ./main.go
 ---> Running in aacb14bc2f9a
 ---> 1db62b52dd92
Step 9/11 : FROM golang:1.16
 ---> 135e37b786c8
Step 10/11 : COPY --from=builder /app .
 ---> 14f56687b159
Step 11/11 : ENTRYPOINT [ "./app" ]
 ---> Running in 13d3fa741c9c
 ---> 4c01516cfe79
Successfully built 4c01516cfe79
Successfully tagged skaffold-example:latest
Tags used in deployment:
 - skaffold-example -> skaffold-example:4c01516cfe7999b2147cf40b609fb3b959b2d520cb31492d9c1506367f80c6ef
Starting deploy...
Loading images into kind cluster nodes...
 - skaffold-example:4c01516cfe7999b2147cf40b609fb3b959b2d520cb31492d9c1506367f80c6ef -> Loaded
Images loaded in 43.816 seconds
 - pod/getting-started created
Waiting for deployments to stabilize...
 - pods is ready.
Deployments stabilized in 3.129 seconds
Press Ctrl+C to exit
Watching for changes...
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
[getting-started] There are 11 pods in the cluster
[getting-started] Found nginx-6799fc88d8-w7d5k pod in default namespace
^CCleaning up...
 - pod "getting-started" deleted

 

別窓建てて見てみる

skaffold dev中に確認

$ kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
getting-started          1/1     Running   0          74s
nginx-6799fc88d8-w7d5k   1/1     Running   0          12m


$ kubectl logs getting-started
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace
There are 11 pods in the cluster
Found nginx-6799fc88d8-w7d5k pod in default namespace

 

タイトルとURLをコピーしました