1。概要:
1.1環境
バージョン情報は次のとおりです。a
。オペレーティングシステム:centos 7.6、amd64
b、kubernetesバージョン:v1.18.16
c、dockerバージョン:v1.18.06
d、rancherバージョン:v2.5.6
e、golangバージョン:v1.14
f、 golandバージョン:2020.3
2go環境とdockerをインストールします。
省略。
3イメージをコンパイルしてビルドします。
3.1ベアメタル環境でコンパイルコマンドとビルドコマンドを実行する
mkdir -p /opt/ && cd /opt/
# 下载rancher源码
git clone -b v2.5.6 https://github.com/rancher/rancher.git
cd rancher
# 安装开发工具
yum groupinstall "Development Tools" -y
yum install glibc-devel -y
make ci
3.2カスタム開発コンテナでコンパイルコマンドとビルドコマンドを実行する
mkdir -p /opt/ && cd /opt/
# 下载rancher源码
git clone -b v2.5.6 https://github.com/rancher/rancher.git
# rancher-build:v2.5.6镜像包含了构建rancher所需要的文件(开发工具、docker等)
# 通过挂载的方式将宿主机的sock文件和rancher源码映射进容器内部
docker run -it --rm -v /opt/rancher:/go/src/github.com/rancher/rancher -v /var/run/docker.sock:/var/run/docker.sock registry.cn-shenzhen.aliyuncs.com/gzlj/rancher-build:v2.5.6 bash
# 已经进入容器内部,正好坐落在/go/src/github.com/rancher/rancher,执行一下命令
export GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
cd script
./ci
# 执行成功后,能在宿主机看见新生成的rancher镜像,agent镜像。
kubernetesの4つのデバッグランチャー:
4.1牧場主のソースコードをダウンロードする
hostPathのローカルボリュームを使用してソースコードをコンテナーにマウントするため、k8sクラスターのすべてのノードでrancherのソースコードをダウンロードします。
mkdir -p /opt/ && cd /opt/
git clone -b v2.5.6 https://github.com/rancher/rancher.git
4.2k8sクラスターに複数のオブジェクトをデプロイする
デプロイが成功すると、サービスオブジェクトのNodePortはgolandIDEリモート接続のポートになります。4.3操作では、delveプロセスがサービスポートに対応するコンテナネットワークカードのポート2345でリッスンしていることに注意してください。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: rancher-debug
name: rancher-debug
namespace: default
spec:
selector:
matchLabels:
app: rancher-debug
template:
metadata:
labels:
app: rancher-debug
spec:
serviceAccountName: rancher-debug
nodeSelector:
kubernetes.io/hostname: lj
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
containers:
- command:
- sleep
- "36000"
image: registry.cn-shenzhen.aliyuncs.com/gzlj/rancher-build:v2.5.6
imagePullPolicy: IfNotPresent
name: rancher-debug
env:
- name: GOPROXY
value: "https://goproxy.cn,https://goproxy.io,direct"
volumeMounts:
- name: code
mountPath: /go/src/github.com/rancher/rancher
- name: docker
mountPath: /var/run/docker.sock
volumes:
- name: code
hostPath:
path: /opt/rancher
type: ""
- name: docker
hostPath:
path: /var/run/docker.sock
type: ""
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: rancher-debug
labels:
app: rancher-debug
subjects:
- kind: ServiceAccount
name: rancher-debug
namespace: default
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: rancher-debug
labels:
app: rancher-debug
---
apiVersion: v1
kind: Service
metadata:
name: rancher-debug
namespace: default
spec:
ports:
- name: dlv
port: 2345
protocol: TCP
targetPort: 2345
selector:
app: rancher-debug
sessionAffinity: None
type: NodePort
4.3delveソフトウェアをインストールする
kubectl execコマンドを使用してランチャーコンテナを入力し、delveソフトウェアをインストールします。
go get -u -v github.com/go-delve/delve/cmd/dlv
4.4ランチャーをデバッグモードで起動します
# 在容器中的/go/src/github.com/rancher/rancher/目录下执行
# 此时会打印2345端口,并完全卡住,需要等待delve客户端来连接。
dlv debug --listen=:2345 --headless=true --api-version=2 --accept-multiclient main.go -- --add-local=true -- --k8s-mod=auto
4.5 golandIDEの設定
4.5.1リモート接続情報の設定
メニューバーで、[実行] —> [構成の編集] —> [追加] —> [リモートに移動] —> [OK]をクリックします。セットされたIPはk8sクラスター内のノードの1つであり、ポートはサービスNodePortです。
4.5.2ブレークポイントを設定する
ウィンドウのrancherプロジェクトでmain.goファイルを開き、main.goのmain()の最初の行または他の行にブレークポイントを設定します。
4.5.3リモート接続
4.5.1で設定したリモート接続をクリックして、デバッグを開始します。
4.5.4効果画像
goland IDEの[続行]ボタンをクリックすると、コンテナのコンソールでランチャーログの印刷が開始されます。
5まとめ
牧場主をコンパイルしてコンテナイメージを構築するプロセスは複雑ではありません。k8sクラスターにあるランチャーサービスはリモートでデバッグできます。このように、ランチャーのソースコードを変更するときに、一部の変数を出力するためにprintステートメントを使用する必要はありません。