DEV Community

Cover image for kind - Configuring Extra Port Mappings & IPv6 Networking - Part 4
Unni P
Unni P

Posted on • Originally published at iamunnip.hashnode.dev

kind - Configuring Extra Port Mappings & IPv6 Networking - Part 4

In this article we will look how we can configure extra port mappings and IPv6 networking

Extra Port Mappings

  • Can be used to port forward to the kind nodes, an option to get traffic into the cluster

  • Useful if we are using NodePort services or DaemonSets exposing host ports

  • To use port mappings with NodePort, the kind node containerPort and the service nodePort needs to be equal

In the below configuration file containerPort is set as 30080, so we need to set nodePort in Kubernetes service as 30080

$ cat kind.yml
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: dev
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30080
    hostPort: 30080
    listenAddress: "0.0.0.0"
    protocol: TCP
- role: worker
- role: worker
Enter fullscreen mode Exit fullscreen mode

Create the cluster using the above configuration file

$ kind create cluster --config kind.yml 
Creating cluster "dev" ...
 βœ“ Ensuring node image (kindest/node:v1.26.3) πŸ–Ό
 βœ“ Preparing nodes πŸ“¦ πŸ“¦ πŸ“¦  
 βœ“ Writing configuration πŸ“œ 
 βœ“ Starting control-plane πŸ•ΉοΈ 
 βœ“ Installing CNI πŸ”Œ 
 βœ“ Installing StorageClass πŸ’Ύ 
 βœ“ Joining worker nodes 🚜 
Set kubectl context to "kind-dev"
You can now use your cluster with:

kubectl cluster-info --context kind-dev
Enter fullscreen mode Exit fullscreen mode
$ kubectl get nodes
NAME                STATUS   ROLES           AGE   VERSION
dev-control-plane   Ready    control-plane   93s   v1.26.3
dev-worker          Ready    <none>          62s   v1.26.3
dev-worker2         Ready    <none>          62s   v1.26.3
Enter fullscreen mode Exit fullscreen mode

Generate a manifest file for creating an Nginx pod

$ kubectl run nginx --image=nginx --port=80 --dry-run=client -o yaml > nginx.yml
Enter fullscreen mode Exit fullscreen mode
$ cat nginx.yml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
Enter fullscreen mode Exit fullscreen mode

Apply the nginx.yml manifest file

$ kubectl apply -f nginx.yml 
pod/nginx created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          19s
Enter fullscreen mode Exit fullscreen mode

Generate a manifest file for exposing the Nginx service as NodePort

$ kubectl expose pod nginx --name=nginx-nodeport --type=NodePort --port=80 --target-port=80 --dry-run=client -o yaml > nginx-nodeport.yml
Enter fullscreen mode Exit fullscreen mode

Update the manifest file with nodePort value as 30080, which is equal to the kind node containerPort

$ cat nginx-nodeport.yml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx-nodeport
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080
  selector:
    run: nginx
  type: NodePort
status:
  loadBalancer: {}
Enter fullscreen mode Exit fullscreen mode

Apply the nginx-nodeport.yml manifest file

$ kubectl apply -f nginx-nodeport.yml 
service/nginx-nodeport created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get svc nginx-nodeport 
NAME             TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-nodeport   NodePort   10.96.152.109   <none>        80:30080/TCP   43s
Enter fullscreen mode Exit fullscreen mode

Verify the Nginx default webpage from your local machine

$ curl http://localhost:30080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

IPv6 Networking

  • kind has support for IPv4, IPv6 and dual-stack networking

  • By default it uses IPv4

  • Most OS has IPv6 enabled by default but we can check it using the below command

    sudo sysctl net.ipv6.conf.all.disable_ipv6

Create a cluster using the below configuration file which uses IPv6 networking

$ cat kind.yml 
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: dev
networking:
  ipFamily: ipv6
nodes:
- role: control-plane
- role: worker
- role: worker
Enter fullscreen mode Exit fullscreen mode
$ kind create cluster --config kind.yml 
Creating cluster "dev" ...
 βœ“ Ensuring node image (kindest/node:v1.26.3) πŸ–Ό
 βœ“ Preparing nodes πŸ“¦ πŸ“¦ πŸ“¦  
 βœ“ Writing configuration πŸ“œ 
 βœ“ Starting control-plane πŸ•ΉοΈ 
 βœ“ Installing CNI πŸ”Œ 
 βœ“ Installing StorageClass πŸ’Ύ 
 βœ“ Joining worker nodes 🚜 
Set kubectl context to "kind-dev"
You can now use your cluster with:

kubectl cluster-info --context kind-dev
Enter fullscreen mode Exit fullscreen mode

Once the cluster is up and running, we can see that the nodes have an IPv6 address

$ kubectl get nodes -o wide
NAME                STATUS   ROLES           AGE     VERSION   INTERNAL-IP             EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
dev-control-plane   Ready    control-plane   10m     v1.26.3   fc00:f853:ccd:e793::4   <none>        Ubuntu 22.04.2 LTS   5.15.0-69-generic   containerd://1.6.19-46-g941215f49
dev-worker          Ready    <none>          10m     v1.26.3   fc00:f853:ccd:e793::2   <none>        Ubuntu 22.04.2 LTS   5.15.0-69-generic   containerd://1.6.19-46-g941215f49
dev-worker2         Ready    <none>          9m58s   v1.26.3   fc00:f853:ccd:e793::3   <none>        Ubuntu 22.04.2 LTS   5.15.0-69-generic   containerd://1.6.19-46-g941215f49
Enter fullscreen mode Exit fullscreen mode

Now let’s check it by creating an Nginx pod and exposing it as ClusterIP

$ kubectl run nginx --image=nginx --port=80 --expose
service/nginx created
pod/nginx created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP                 NODE         NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          18s   fd00:10:244:1::2   dev-worker   <none>           <none>
Enter fullscreen mode Exit fullscreen mode
$ kubectl get svc nginx
NAME    TYPE        CLUSTER-IP         EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   fd00:10:96::ca5a   <none>        80/TCP    3m23s
Enter fullscreen mode Exit fullscreen mode

Cleanup

Delete the cluster after use

$ kind delete cluster --name dev
Deleting cluster "dev" ...
Deleted nodes: ["dev-worker2" "dev-control-plane" "dev-worker"]
Enter fullscreen mode Exit fullscreen mode

Reference

https://kind.sigs.k8s.io/

Top comments (0)