目录

go程序调用k8s-pod副本的名称IP手动赋值给configmap的参数

go程序调用k8s pod副本的名称IP手动赋值给configmap的参数

1、创建configmap

apiVersion: v1 data: config.yaml: >-

config.yaml

Etcd: Endpoints:

  • “etcd-server:2379” Username: "" Password: "" Exchanges: #- Name: “Binance”

Symbol: “BTCUSDT”

WSUrl: “wss://fstream.binance.com/ws”

  • Name: “OKX” Symbol: “BTCUSDT” WSUrl: “wss://ws.okx.com:8443/ws/v5/public” #- Name: “Bybit”

Symbol: “ETHUSDT”

WSUrl: “wss://stream.bybit.com/v5/public/linear”

#- Name: “Bitget”

Symbol: “DOGEUSDT”

WSUrl: “wss://ws.bitget.com/mix/v1/stream”

ContractType: “mc”

#- Name: “Pyth”

Symbol: “BTCUSDT”

WSUrl: “wss://hermes.pyth.network/ws”

PriceId: “e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43”

DatabaseDSN: “abc:defg@tcp(rm-sefdref.mysql.rds.aliyuncs.com:3306)/miniapp” RedisAddr: “0.0.0.0:6379” RedisPassword: “S4t#d%dgrdfer3” RedisDB: 1 KafkaBrokers:

  • “0.0.0.0:3195” BOT_TOKEN: “sefefedsfa:AAELY1TwInw46D3Lhpl0LvpQz2v-DoIq5ww” KafkaPriceUpdateTopic: “price_updates” SupportedCoins:
  • “BTC”
  • “ETH”

#Chains:

- Name: “ETH”

ChainName: “ETH”

NodeUrl:

ConfirmationNum: 16

ExcludedAddresses: “11”,“22”

ScanInterval: 60

BaseSlippage: 0.005 #Port: “0.0.0.0:8901” LogLevel: “info” Services:

  • Name: “api_gateway” ServiceID: “api-gateway-1” ServiceHost: “api_gateway” ServicePort: 8080
  • Name: “spider_service” ServiceID: “spider-service-1” ServiceHost: “spider_service” ServicePort: 50051
  • Name: “${SERVICE_NAME}” ServiceID: “${SERVICE_ID}” ServiceHost: “${POD_IP}” ServicePort: ${SERVICE_PORT}
  • Name: “trading_engine_service” ServiceID: “trading-engine-service-1” ServiceHost: “trading_engine_service” ServicePort: 50053
  • Name: “auth_service” ServiceID: “auth-service-1” ServiceHost: “auth_service” ServicePort: 50054
  • Name: “user_service” ServiceID: “user-service-1” ServiceHost: “user_service” ServicePort: 50055
  • Name: “account_service” ServiceID: “account-service-1” ServiceHost: “account_service” ServicePort: 50056
  • Name: “wallet_service” ServiceID: “wallet-service-1” ServiceHost: “wallet_service” ServicePort: 50057

HttpPort : 8091

  • Name: “chain_scanner_service” ServiceID: “chain-scanner-service-1” ServiceHost: “chain_scanner_service” ServicePort: 50058
  • Name: “out_service” ServiceID: “out-service-1” ServiceHost: “out_service” ServicePort: 50059 SlippageTiers: tier1:
  • Threshold: 1000 Factor: 0.001
  • Threshold: 5000 Factor: 0.0008 tier2:
  • Threshold: 10000 Factor: 0.0005 GameOptions: TenantId: 90026 ProductCode: UD_OPTION ExitUrl: AccessCode: 15224562686655 IvValue: DJIOHFDsaifiajfiwefjqwi63 GetUrl: kind: ConfigMap metadata: name: config.yaml namespace: test resourceVersion: ‘150039730’ 2、创建deployment,其中包含initContainers和containers两部分

apiVersion: apps/v1 kind: Deployment metadata: annotations: k8s.kuboard.cn/displayName: bot-service labels: k8s.kuboard.cn/name: bot-service name: bot-service namespace: test resourceVersion: ‘150036998’ spec: progressDeadlineSeconds: 600 replicas: 2 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/name: bot-service strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: k8s.kuboard.cn/name: bot-service spec: containers:

  • env:
  • name: SERVICE_ID valueFrom: fieldRef: apiVersion: v1 fieldPath: metadata.name image: ‘harbor.cuiwjrpcvi.com/bktest/bot_service:test’ imagePullPolicy: IfNotPresent name: bot-service ports:
  • containerPort: 50052 name: adwqdw protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts:
  • mountPath: /telegram/shared/config name: config-output dnsPolicy: ClusterFirst hostAliases:
  • hostnames:
  • etcd-server ip: 172.16.1.193 imagePullSecrets:
  • name: harbor-secret initContainers:
  • args:

apk add –no-cache gettext && envsubst < /config-template/config.yaml > /telegram/shared/config/config.yaml; command:

  • /bin/sh
  • ‘-c’ env:
  • name: SERVICE_ID valueFrom: fieldRef: apiVersion: v1 fieldPath: metadata.name
  • name: POD_IP valueFrom: fieldRef: apiVersion: v1 fieldPath: status.podIP
  • name: SERVICE_NAME value: bot-service
  • name: SERVICE_PORT value: ‘50052’ image: ‘alpine:3.17’ imagePullPolicy: IfNotPresent name: bot-service-init resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts:
  • mountPath: /config-template name: config-template
  • mountPath: /telegram/shared/config/ name: config-output restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 volumes:
  • configMap: defaultMode: 420 items:
  • key: config.yaml path: config.yaml name: config.yaml name: config-template
  • emptyDir: {} name: config-output 3、启动deployment并登录容器验证结果 查询configmap文本内容 节点一 cat shared/config/config.yaml 。。。。。。
  • Name: “bot-service” ServiceID: “bot-service-5c7745fff4-c8w6b” ServiceHost: “172.20.250.31” ServicePort: 50052 。。。。。。 节点二 cat shared/config/config.yaml 。。。。。。
  • Name: “bot-service” ServiceID: “bot-service-5c7745fff4-8p72g” ServiceHost: “172.20.171.3” ServicePort: 50052 。。。。。。 4、登录etcd查看注册情况 root@iZj6ch896i3lsjbs6cqk2iZ:/data/docker-compose/bot_service# docker exec -it etcd-server /bin/bash I have no name!@etcd-server:/opt/bitnami/etcd$ etcdctl get –prefix /service /services/bot/bot-service-5c7745fff4-8p72g 172.20.171.3:50052 /services/bot/bot-service-5c7745fff4-c8w6b 172.20.250.31:50052 至此go程序通过自己提交ip和服务信息注册到etcd已成功完成