2
votes

this my first deployment yaml file, i'm testing k8s with minikube like an external cluster, i would expose port 80 of minikube cluster to port 8080 of container (webservice). This my yaml:

apiVersion: v1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
items:

############ Services ###############
- apiVersion: v1
  kind: Service
  metadata:
    name: kuard-80
    labels:
      component: webserver
      app: k8s-test
  spec:
    ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
    loadBalancerIP: 192.168.99.100 # Minikube IP from "minikube ip"
    selector:
      component: webserver
    sessionAffinity: None
    type: LoadBalancer

############ Deployments ############
- apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: kuard
    labels:
      component: webserver
      app: k8s-test
  spec:
    replicas: 1 # tells deployment to run 1 pod matching the template
    selector:
      matchLabels:
        component: webserver
    strategy:
      rollingUpdate:
        maxSurge: 25%
        maxUnavailable: 25%
      type: RollingUpdate
    template:
      metadata:
        labels:
          component: webserver
      spec:
        volumes:
          - name: "kuard-data"
            hostPath:
              path: "/var/lib/kuard"
        containers:
          - image: gcr.io/kuar-demo/kuard-amd64:1
            name: kuard
            volumeMounts:
              - mountPath: "/data"
                name: "kuard-data"
            livenessProbe:
              httpGet:
                path: /healthy
                port: 8080
              initialDelaySeconds: 5
              timeoutSeconds: 1
              periodSeconds: 10
              failureThreshold: 3
            ports:
              - containerPort: 8080
                protocol: TCP

    restartPolicy: Always

I expect the port 80 to answer me on http://192.168.99.100 , where the error? here are the results of some commands, services and endpoints

$ kubectl get service

NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kuard-80     LoadBalancer   10.107.163.175   <pending>     80:30058/TCP   3m
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        34d

$ kubectl get endpoints

NAME         ENDPOINTS             AGE
kuard-80     172.17.0.7:8080       10m
kubernetes   192.168.99.100:8443   34d

Thanks for any help you can give me and excuse me if the question is stupid...

2

2 Answers

4
votes

Your service is of type LoadBalancer which is supported only for cloud, hence your external ip is in pending state.

NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kuard-80     LoadBalancer   10.107.163.175   <pending>     80:30058/TCP   3m

You can expose your service using NodePort in minikube. Following will be the yaml file for that:

 apiVersion: v1
   kind: Service
   metadata:
     name: kuard-80
     labels:
       component: webserver
       app: k8s-test
   spec:
     ports:
     - port: 80
       targetPort: 8080
       protocol: TCP
     selector:
       component: webserver
     type: NodePort

Now, when you do kubectl describe service kuard-80 you will be able to see a port of type NodePort whose value will be in between 30000-32767. You will be able to access your application using:

http://<vm_ip>:<node_port>

Hope this helps

0
votes

On minikube or KinD based clusters you can run MetalLB. It is a bare metal LB offering. To install MetalLB on your local cluster created using minikube or KinD, you need to run below yamls like.

You need a yaml file called metal-lb.yaml containing below :

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.1.31-192.168.1.40

Feel free to change the ip range.

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/metallb.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
kubectl apply -f ./metal-lb.yaml

You can then create services of LoadBalancer types too.