Closed10

skaffold using k3d

ぺこぺこ

Install

  • skaffold
    • already installed by another step
  • kubectl
    • already installed by Docker Desktop
  • k3d
asdf plugin add k3d
asdf install k3d latest
k3d -v

Setup version

asdf current k3d 5.5.1
ぺこぺこ

Create cluster

k3d cluster list
k3d cluster create my-cluster
output
INFO[0000] Prep: Network                                
INFO[0000] Created network 'k3d-my-cluster'             
INFO[0000] Created image volume k3d-my-cluster-images   
INFO[0000] Starting new tools node...                   
INFO[0001] Pulling image 'ghcr.io/k3d-io/k3d-tools:5.5.1' 
INFO[0001] Creating node 'k3d-my-cluster-server-0'      
INFO[0002] Starting Node 'k3d-my-cluster-tools'         
INFO[0003] Pulling image 'docker.io/rancher/k3s:v1.26.4-k3s1' 
INFO[0018] Creating LoadBalancer 'k3d-my-cluster-serverlb' 
INFO[0019] Pulling image 'ghcr.io/k3d-io/k3d-proxy:5.5.1' 
INFO[0023] Using the k3d-tools node to gather environment information 
INFO[0024] Starting new tools node...                   
INFO[0024] Starting Node 'k3d-my-cluster-tools'         
INFO[0026] Starting cluster 'my-cluster'                
INFO[0026] Starting servers...                          
INFO[0026] Starting Node 'k3d-my-cluster-server-0'      
INFO[0036] All agents already running.                  
INFO[0036] Starting helpers...                          
INFO[0036] Starting Node 'k3d-my-cluster-serverlb'      
INFO[0043] Injecting records for hostAliases (incl. host.k3d.internal) and for 3 network members into CoreDNS configmap... 
INFO[0045] Cluster 'my-cluster' created successfully!   
INFO[0045] You can now use it like this:                
kubectl cluster-info
k3d cluster list             
output
NAME         SERVERS   AGENTS   LOADBALANCER
my-cluster   1/1       0/0      true
k3d cluster stop my-cluster
output
INFO[0000] Stopping cluster 'my-cluster'                
INFO[0010] Stopped cluster 'my-cluster'                 
k3d cluster list           
output
NAME         SERVERS   AGENTS   LOADBALANCER
my-cluster   0/1       0/0      true
k3d cluster start my-cluster
output
INFO[0000] Using the k3d-tools node to gather environment information 
INFO[0000] Starting existing tools node k3d-my-cluster-tools... 
INFO[0000] Starting Node 'k3d-my-cluster-tools'         
INFO[0000] Starting new tools node...                   
INFO[0000] Starting Node 'k3d-my-cluster-tools'         
INFO[0001] Starting cluster 'my-cluster'                
INFO[0001] Starting servers...                          
INFO[0001] Starting Node 'k3d-my-cluster-server-0'      
INFO[0005] All agents already running.                  
INFO[0005] Starting helpers...                          
INFO[0005] Starting Node 'k3d-my-cluster-serverlb'      
INFO[0005] Starting Node 'k3d-my-cluster-tools'         
INFO[0011] Injecting records for hostAliases (incl. host.k3d.internal) and for 3 network members into CoreDNS configmap... 
INFO[0013] Started cluster 'my-cluster'
k3d cluster list            
output
NAME         SERVERS   AGENTS   LOADBALANCER
my-cluster   1/1       0/0      true
ぺこぺこ

skaffold config

skaffold.yaml
apiVersion: skaffold/v4beta6
kind: Config
metadata:
  name: service_name
build:
  artifacts:
    - image: hello
      context: app
  tagPolicy:
    sha256: {}
  local:
    useBuildkit: true
deploy:
  kubectl: {}
ぺこぺこ

k8s manifests

k8s/hello.yaml
apiVersion: v1
kind: Service
metadata:
  name: hello
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: hello
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello
  name: hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - image: hello
          imagePullPolicy: "IfNotPresent"
          name: hello
          command:
            - "./server"
          ports:
            - containerPort: 8080
              protocol: TCP
ぺこぺこ

Hello, World Service

Install required

asdf plugin add golang
asdf install golang latest
go version

Setup version

asdf current golang 1.20.5

Write app

app/main.go
package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World")
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

Dockerfile

app/Dockerfile
FROM golang:1.20 as builder
WORKDIR /tmp
ENV GO111MODULE="on"
ENV CGO_ENABLED=0
COPY . .
RUN go build -o server main.go

FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=builder /tmp/server /go/bin/server
WORKDIR /go/bin/

Build Image

docker build . -t hello:latest

Confirm application

docker run -p 8080:8080 hello:latest ./server
curl http://localhost:8080
ぺこぺこ

Launch by skaffold

skaffold dev --port-forward
output
WARN[0001] failed to detect active kubernetes cluster node platform. Specify the correct build platform in the `skaffold.yaml` file or using the `--platform` flag  subtask=-1 task=DevLoop
Generating tags...
 - hello -> hello:latest
Checking cache...
 - hello: Found Locally
Tags used in deployment:
 - hello -> hello:b6e3c1b7137ed3cbb2b6e79e512d10a1d1184d8828af545d51ef0ccf41bd09a3
Starting deploy...
Cleaning up...
 - E0709 18:05:21.777532   18025 memcache.go:265] couldn't get current server API group list: Get "https://host.docker.internal:49785/api?timeout=32s": dial tcp 172.30.63.254:49785: connect: connection refused
 - E0709 18:05:21.784669   18025 memcache.go:265] couldn't get current server API group list: Get "https://host.docker.internal:49785/api?timeout=32s": dial tcp 172.30.63.254:49785: connect: connection refused
 - unable to recognize "STDIN": Get "https://host.docker.internal:49785/api?timeout=32s": dial tcp 172.30.63.254:49785: connect: connection refused
 - unable to recognize "STDIN": Get "https://host.docker.internal:49785/api?timeout=32s": dial tcp 172.30.63.254:49785: connect: connection refused
Cleaning up resources encountered an error, will continue to clean up other resources.
Deploy Failed. Could not connect to cluster due to "https://host.docker.internal:49785/version": dial tcp 172.30.63.254:49785: connect: connection refused. Check your connection for the cluster.

Modify ~/.kube/config

  • change clusters/cluster/server value
  • diff from the original
diff ~/.kube/config ~/.kube/config.org
output
<     server: https://kubernetes.docker.internal:51617
---
>     server: https://host.docker.internal:51617

Restart k3d cluster

k3d cluster stop my-cluster
k3d cluster start my-cluster
kubectl cluster-info
output
Kubernetes control plane is running at https://kubernetes.docker.internal:60694
CoreDNS is running at https://kubernetes.docker.internal:60694/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://kubernetes.docker.internal:60694/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

何回かkubectl cluster-infoを試したら上記のように正常になった

Re-try

skaffold dev --port-forward
output
Generating tags...
 - hello -> hello:latest
Checking cache...
 - hello: Found Locally
Tags used in deployment:
 - hello -> hello:4debb742bcadf738f2f8e46346f7d5dbbe79411e6e3649e7572878a103f2eca4
Starting deploy...
Loading images into k3d cluster nodes...
 - hello:4debb742bcadf738f2f8e46346f7d5dbbe79411e6e3649e7572878a103f2eca4 -> Loaded
Images loaded in 3.815 seconds
 - service/hello created
 - deployment.apps/hello created
Waiting for deployments to stabilize...
 - deployment/hello is ready.
Deployments stabilized in 1.216 second
Port forwarding service/hello in namespace default, remote port 8080 -> http://127.0.0.1:8080
Listing files to watch...
 - hello
Press Ctrl+C to exit
Watching for changes...

Confirm application

curl http://localhost:8080

=> OK

ぺこぺこ

Appendix - skaffold devの際にCannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?が出た

背景

  • 作業の途中でDocker Desktopの更新をしていた

事象とやったこと

  • skaffold devを実行したら以下のようなエラーが出た
skaffold dev --port-forward 
output
Generating tags...
 - hello -> hello:latest
Checking cache...
 - hello: Error checking cache.
Cleaning up...
 - No resources found
getting imageID for hello:latest: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  • Dockerのコンテキスト一覧を確認
docker context ls
output
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                       KUBERNETES ENDPOINT   ORCHESTRATOR
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                                 
desktop-linux *     moby                Docker Desktop                            unix:///Users/${username}/.docker/run/docker.sock                         
  • NAME=desktop-linux, DESCRIPTION=Docker Desktopが設定されている

  • Docker Desktopの更新前でコンテキスト一覧がどうなっていたか不明

  • /var/run/docker.sockの実体を確認

ls -l /var/run/docker.sock
output
lrwxr-xr-x  1 root  daemon  74  7 11 11:44 /var/run/docker.sock -> /Users/${username}/Library/Containers/com.docker.docker/Data/docker.sock
  • 設定されているDockerコンテキストと異なっていた

  • /var/run/docker.sockの実体を設定されているDockerコンテキストのDOCKER ENDPOINTにする

sudo ln -sf ~/.docker/run/docker.sock /var/run/docker.sock
  • シンボリックリンクが反映されたことを確認
ls -l /var/run/docker.sock
output
lrwxr-xr-x  1 root  daemon  44  7 11 11:48 /var/run/docker.sock -> /Users/${username}/.docker/run/docker.sock
  • 再度、skaffold devを実行してみる
skaffold dev --port-forward
  • skaffold devが正常に実行することを確認できた
ぺこぺこ

なんかエラー起きたタイミングを記憶違いしてる気がする。後で確かめる

このスクラップは2023/07/11にクローズされました