kind - Configuring Extra Port Mappings & IPv6 Networking - Part 4
Tags: opensource, docker, kubernetes, devops
Published: 2023-05-03 07:32:21

> 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

```bash
$ 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
```

Create the cluster using the above configuration file

```bash
$ 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
```

```bash
$ 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
```

Generate a manifest file for creating an Nginx pod

```bash
$ kubectl run nginx --image=nginx --port=80 --dry-run=client -o yaml > nginx.yml
```

```bash
$ 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: {}
```

Apply the nginx.yml manifest file

```bash
$ kubectl apply -f nginx.yml 
pod/nginx created
```

```bash
$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          19s
```

Generate a manifest file for exposing the Nginx service as NodePort

```bash
$ kubectl expose pod nginx --name=nginx-nodeport --type=NodePort --port=80 --target-port=80 --dry-run=client -o yaml > nginx-nodeport.yml
```

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

```bash
$ 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: {}
```

Apply the nginx-nodeport.yml manifest file

```bash
$ kubectl apply -f nginx-nodeport.yml 
service/nginx-nodeport created
```

```bash
$ 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
```

Verify the Nginx default webpage from your local machine

```bash
$ 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>
```

## 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

```bash
$ cat kind.yml 
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: dev
networking:
  ipFamily: ipv6
nodes:
- role: control-plane
- role: worker
- role: worker
```

```bash
$ 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
```

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

```bash
$ 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
```

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

```bash
$ kubectl run nginx --image=nginx --port=80 --expose
service/nginx created
pod/nginx created
```

```bash
$ 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>
```

```bash
$ kubectl get svc nginx
NAME    TYPE        CLUSTER-IP         EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   fd00:10:96::ca5a   <none>        80/TCP    3m23s
```

## Cleanup

Delete the cluster after use

```bash
$ kind delete cluster --name dev
Deleting cluster "dev" ...
Deleted nodes: ["dev-worker2" "dev-control-plane" "dev-worker"]
```

## Reference

[https://kind.sigs.k8s.io/](https://kind.sigs.k8s.io/)