Kubernetes Port Forward pod access mechanism

 

Demand: pod need to debug deployment of whether to provide a normal visit, but not to be exposed to the outside pod cluster.

Achieved: by mapping mechanism Kubernetes Port Forward local port to the port to achieve pod

 

1, the installation kubectl

Official website download page: https://kubernetes.io/docs/tasks/tools/install-kubectl/

Just download the same version Kubernetes of kubectl:

https://storage.googleapis.com/kubernetes-release/release/v1.14.1/bin/windows/amd64/kubectl.exe

 Kubectl.exe the downloaded files are stored in C: \ k8s \ bin directory and add the directory to the PATH (step omitted)

  

2, the configuration kubectl

Need to get the configuration file:

1) Kubernetes general deployment, will generate $ HOME / .kube / config, the config files in that directory, stored in the corresponding Windows local home directory C: \ Users \ admin \ .kube under

 

 2) If the rancher deployment, configuration files can be viewed in a cluster home, copy down also stored in the home directory of the account

 

 

3) Then cmd view cluster

kubectl get node

Instructions for configuring kubectl success.

 

3, create a test pod

Create a nginx, designated port 80

cat > my-nginx.yaml <<EOF

apiVersion: apps/v1

kind: Deployment

metadata:

  name: my-nginx

spec:

  replicas: 1

  selector:

    matchLabels:

      k8s-app: my-nginx

  template:

    metadata:

      labels:

        k8s-app: my-nginx

    spec:

      containers:

      - name: my-nginx

        image: nginx:1.9

        ports:

        - containerPort: 80

EOF

# Create a pod

kubectl create -f my-nginx.yaml

 

# View pod name information

kubectl get svc,pod -o wide

 

4, set up port forwarding

1) 3080 forwards the local port to the port 80 of the container

kubectl port-forward my-nginx-6d7ddd766f-4fxxc 3080:80

Forwarding from 127.0.0.1:3080 -> 80
Forwarding from [::1]:3080 -> 80

As output, indicating successfully forward. If it is deployed on other namespace, specify the namespace with -n.

 

2) local browser access

127.0.0.1:3080

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/weavepub/p/10945189.html