相同ns不同的pod之间互相访问
文章目录前言前几天和同事讨论k8s写了代码尝试一、服务1二、服务2三、尝试前言前几天和同事讨论k8s写了代码尝试一、服务1package mainimport ("go.opencensus.io/plugin/ochttp""io/ioutil""log""net/http")func main() {mux := http.NewServeMux()mux.HandleFunc("/", li
·
前言
前几天和同事讨论k8s写了代码尝试
一、服务1
package main
import (
"go.opencensus.io/plugin/ochttp"
"io/ioutil"
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", list)
h := &ochttp.Handler{Handler: mux}
log.Printf("Server listening! ...")
log.Fatal(http.ListenAndServe(":9999", h))
}
func list(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving request: %s", r.URL.Path)
response, err := http.DefaultClient.Get("http://demo-dep:8222/user")
defer response.Body.Close()
if err != nil {
return
}
data, err := ioutil.ReadAll(response.Body)
log.Printf(string(data))
w.Write(data)
}
打包成镜像名字叫maingo上传到本地docker
二、服务2
用springboot写的get请求,发送/user返回字符串"hello"
打包成镜像名字叫10.0.24.3/library/javademo-01:v1上传到本地docker
三、尝试
然后 helm install 这两个yaml
deploymen
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: myapp
creationTimestamp: null
labels:
app: demo-dep
name: demo-dep
spec:
replicas: 1
selector:
matchLabels:
app: demo-dep
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: demo-dep
spec:
containers:
- image: 10.0.24.3/library/javademo-01:v1
name: javademo-01
resources: {}
status: {}
svc
apiVersion: v1
kind: Service
metadata:
namespace: myapp
creationTimestamp: null
labels:
app: demo-dep
name: demo-dep
spec:
ports:
- port: 8222
protocol: TCP
targetPort: 8222
# nodePort: 30811
selector:
app: demo-dep
# type: NodePort
status:
loadBalancer: {}
和
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: myapp
creationTimestamp: null
labels:
app: demo-dep1
name: demo-go
spec:
replicas: 1
selector:
matchLabels:
app: demo-dep1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: demo-dep1
spec:
containers:
- image: maingo:latest
name: maingo
imagePullPolicy: Never
resources: {}
status: {}
svc
apiVersion: v1
kind: Service
metadata:
namespace: myapp
creationTimestamp: null
labels:
app: demo-dep1
name: demo-go
spec:
ports:
- port: 9999
protocol: TCP
targetPort: 9999
# nodePort: 30811
selector:
app: demo-dep1
# type: NodePort
status:
loadBalancer: {}
安装这两个chart
注意是不要把这两个chart内的labels设置成相同的,我一开始也是设置成相同的了,在pod内wget -O - -q http://demo-dep:8222/user时候可能会访问失败,查看ep会有两个地址,具体原因不清楚…
然后进入到服务2容器里面去
kubectl -n myapp exec -it demo-go – /bin/sh
访问
wget -O - -q http://demo-go:9999
会打印返回的字符串
“hello”
更多推荐
已为社区贡献1条内容
所有评论(0)