카타코더 k8s - Networking Introduction

1 개요[ | ]

카타코더 Kubernetes - Networking Introduction
카타코더 Kubernetes
# 🔗 제목
카타코더 Kubernetes/1 e
🡵 카타코더 k8s - Launch A Single Node Cluster
🡵 카타코더 k8s - Launch a multi-node cluster using Kubeadm
🡵 카타코더 k8s - Deploy Containers Using Kubectl
🡵 카타코더 k8s - Deploy Containers Using YAML
🡵 카타코더 k8s - Deploy Guestbook Web App Example
🡵 카타코더 k8s - Networking Introduction
🡵 카타코더 k8s - Create Ingress Routing
🡵 카타코더 k8s - Liveness and Readiness Healthchecks
🡵 카타코더 k8s - Getting Started With CRI-O and Kubeadm
🡵 카타코더 k8s - Running Stateful Services on Kubernetes

2 Cluster IP[ | ]

master:~$ cat clusterip.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp1-clusterip-svc
  labels:
    app: webapp1-clusterip
spec:
  ports:
  - port: 80
  selector:
    app: webapp1-clusterip
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1-clusterip-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: webapp1-clusterip
    spec:
      containers:
      - name: webapp1-clusterip-pod
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80
---
master:~$ kubectl apply -f clusterip.yaml
service/webapp1-clusterip-svc created
deployment.extensions/webapp1-clusterip-deployment created
master:~$ kubectl get pods
NAME                                            READY     STATUS    RESTARTS   AGE
webapp1-clusterip-deployment-7fd8b9674b-b9dns   1/1       Running   0       22s
webapp1-clusterip-deployment-7fd8b9674b-zdqbq   1/1       Running   0       22s
master:~$ kubectl get svc
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes              ClusterIP   10.96.0.1      <none>        443/TCP   1m
webapp1-clusterip-svc   ClusterIP   10.98.44.183   <none>        80/TCP   31s
master:~$ kubectl describe svc/webapp1-clusterip-svc
Name:              webapp1-clusterip-svc
Namespace:         default
Labels:            app=webapp1-clusterip
Annotations:       kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"webapp1-clusterip"},"name":"webapp1-clusterip-svc","namespace":"defau...
Selector:          app=webapp1-clusterip
Type:              ClusterIP
IP:                10.98.44.183
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.32.0.2:80,10.32.0.3:80
Session Affinity:  None
Events:            <none>
master:~$ export CLUSTER_IP=$(kubectl get services/webapp1-clusterip-svc-o go-template='{{(index .spec.clusterIP)}}')
master:~$ echo CLUSTER_IP=$CLUSTER_IP
CLUSTER_IP=10.98.44.183
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-b9dns</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-b9dns</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-b9dns</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-zdqbq</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-zdqbq</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-zdqbq</h1>
master:~$ curl $CLUSTER_IP:80
<h1>This request was processed by host: webapp1-clusterip-deployment-7fd8b9674b-b9dns</h1>

3 Target Port[ | ]

master:~$ cat clusterip-target.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp1-clusterip-targetport-svc
  labels:
    app: webapp1-clusterip-targetport
spec:
  ports:
  - port: 8080
    targetPort: 80
  selector:
    app: webapp1-clusterip-targetport
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1-clusterip-targetport-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: webapp1-clusterip-targetport
    spec:
      containers:
      - name: webapp1-clusterip-targetport-pod
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80
---
master:~$ kubectl apply -f clusterip-target.yaml
service/webapp1-clusterip-targetport-svc created
deployment.extensions/webapp1-clusterip-targetport-deployment created
master:~$ kubectl get svc
NAME                               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes                         ClusterIP   10.96.0.1        <none>      443/TCP    3m
webapp1-clusterip-svc              ClusterIP   10.98.44.183     <none>      80/TCP     3m
webapp1-clusterip-targetport-svc   ClusterIP   10.105.237.185   <none>      8080/TCP   16s
master:~$ kubectl describe svc/webapp1-clusterip-targetport-svc
Name:              webapp1-clusterip-targetport-svc
Namespace:         default
Labels:            app=webapp1-clusterip-targetport
Annotations:       kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"webapp1-clusterip-targetport"},"name":"webapp1-clusterip-targetport-s...
Selector:          app=webapp1-clusterip-targetport
Type:              ClusterIP
IP:                10.105.237.185
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         10.32.0.6:80,10.32.0.7:80
Session Affinity:  None
Events:            <none>
master:~$ export CLUSTER_IP=$(kubectl get services/webapp1-clusterip-targetport-svc -o go-template='{{(index .spec.clusterIP)}}')
master:~$ echo CLUSTER_IP=$CLUSTER_IP
CLUSTER_IP=10.105.237.185
master:~$ curl $CLUSTER_IP:8080
<h1>This request was processed by host: webapp1-clusterip-targetport-deployment-ffdbb7558-pdc9j</h1>
master:~$ curl $CLUSTER_IP:8080
<h1>This request was processed by host: webapp1-clusterip-targetport-deployment-ffdbb7558-pdc9j</h1>
master:~$ curl $CLUSTER_IP:8080
<h1>This request was processed by host: webapp1-clusterip-targetport-deployment-ffdbb7558-wtvs7</h1>
master:~$ curl $CLUSTER_IP:8080
<h1>This request was processed by host: webapp1-clusterip-targetport-deployment-ffdbb7558-wtvs7</h1>
master:~$ curl $CLUSTER_IP:8080
<h1>This request was processed by host: webapp1-clusterip-targetport-deployment-ffdbb7558-pdc9j</h1>

4 NodePort[ | ]

master:~$ cat nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp1-nodeport-svc
  labels:
    app: webapp1-nodeport
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080
  selector:
    app: webapp1-nodeport
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1-nodeport-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: webapp1-nodeport
    spec:
      containers:
      - name: webapp1-nodeport-pod
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80
---
master:~$ kubectl apply -f nodeport.yaml
service/webapp1-nodeport-svc created
deployment.extensions/webapp1-nodeport-deployment created
master:~$ kubectl get svc
NAME                               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                         ClusterIP   10.96.0.1        <none>      443/TCP        6m
webapp1-clusterip-svc              ClusterIP   10.98.44.183     <none>      80/TCP         6m
webapp1-clusterip-targetport-svc   ClusterIP   10.105.237.185   <none>      8080/TCP       3m
webapp1-nodeport-svc               NodePort    10.97.69.120     <none>      80:30080/TCP   20s
master:~$ kubectl describe svc/webapp1-nodeport-svc
Name:                     webapp1-nodeport-svc
Namespace:                default
Labels:                   app=webapp1-nodeport
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"webapp1-nodeport"},"name":"webapp1-nodeport-svc","namespace":"default...
Selector:                 app=webapp1-nodeport
Type:                     NodePort
IP:                       10.97.69.120
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.32.0.8:80,10.32.0.9:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
master:~$ curl 172.17.0.19:30080
<h1>This request was processed by host: webapp1-nodeport-deployment-785989576b-j5fc6</h1>
master:~$ curl 172.17.0.19:30080
<h1>This request was processed by host: webapp1-nodeport-deployment-785989576b-j5fc6</h1>
master:~$ curl 172.17.0.19:30080
<h1>This request was processed by host: webapp1-nodeport-deployment-785989576b-gcwgb</h1>
master:~$ curl 172.17.0.19:30080
<h1>This request was processed by host: webapp1-nodeport-deployment-785989576b-j5fc6</h1>

5 External IPs[ | ]

master:~$ cat externalip.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp1-externalip-svc
  labels:
    app: webapp1-externalip
spec:
  ports:
  - port: 80
  externalIPs:
  - HOSTIP
  selector:
    app: webapp1-externalip
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1-externalip-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: webapp1-externalip
    spec:
      containers:
      - name: webapp1-externalip-pod
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80
---
master:~$ sed -i 's/HOSTIP/172.17.0.19/g' externalip.yaml
master:~$ kubectl apply -f externalip.yaml
service/webapp1-externalip-svc created
deployment.extensions/webapp1-externalip-deployment created
master:~$ kubectl get svc
NAME                               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                         ClusterIP   10.96.0.1        <none>      443/TCP        9m
webapp1-clusterip-svc              ClusterIP   10.98.44.183     <none>      80/TCP         8m
webapp1-clusterip-targetport-svc   ClusterIP   10.105.237.185   <none>      8080/TCP       6m
webapp1-externalip-svc             ClusterIP   10.109.41.233    172.17.0.19   80/TCP         21s
webapp1-nodeport-svc               NodePort    10.97.69.120     <none>      80:30080/TCP   2m
master:~$ kubectl describe svc/webapp1-externalip-svc
Name:              webapp1-externalip-svc
Namespace:         default
Labels:            app=webapp1-externalip
Annotations:       kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"webapp1-externalip"},"name":"webapp1-externalip-svc","namespace":"def...
Selector:          app=webapp1-externalip
Type:              ClusterIP
IP:                10.109.41.233
External IPs:      172.17.0.19
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.32.0.10:80,10.32.0.11:80
Session Affinity:  None
Events:            <none>
master:~$ curl 172.17.0.19
<h1>This request was processed by host: webapp1-externalip-deployment-bdf9688c4-chw4b</h1>
master:~$ curl 172.17.0.19
<h1>This request was processed by host: webapp1-externalip-deployment-bdf9688c4-8p4fg</h1>
master:~$ curl 172.17.0.19
<h1>This request was processed by host: webapp1-externalip-deployment-bdf9688c4-chw4b</h1>

6 Load Balancer[ | ]

master:~$ cat cloudprovider.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: kube-keepalived-vip
  namespace: kube-system
spec:
  template:
    metadata:
      labels:
        name: kube-keepalived-vip
    spec:
      hostNetwork: true
      containers:
        - image: gcr.io/google_containers/kube-keepalived-vip:0.9
          name: kube-keepalived-vip
          imagePullPolicy: Always
          securityContext:
            privileged: true
          volumeMounts:
            - mountPath: /lib/modules
              name: modules
              readOnly: true
            - mountPath: /dev
              name: dev
          # use downward API
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          # to use unicast
          args:
          - --services-configmap=kube-system/vip-configmap
          # unicast uses the ip of the nodes instead of multicast
          # this is useful if running in cloud providers (like AWS)
          #- --use-unicast=true
      volumes:
        - name: modules
          hostPath:
            path: /lib/modules
        - name: dev
          hostPath:
            path: /dev
      nodeSelector:
        # type: worker # adjust this to match your worker nodes
---
## We also create an empty ConfigMap to hold our config
apiVersion: v1
kind: ConfigMap
metadata:
  name: vip-configmap
  namespace: kube-system
data:
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: keepalived-cloud-provider
  name: keepalived-cloud-provider
  namespace: kube-system
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: keepalived-cloud-provider
  strategy:
    type: RollingUpdate
  template:
    metadata:
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ""
        scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
      labels:
        app: keepalived-cloud-provider
    spec:
      containers:
      - name: keepalived-cloud-provider
        image: quay.io/munnerz/keepalived-cloud-provider:0.0.1
        imagePullPolicy: IfNotPresent
        env:
        - name: KEEPALIVED_NAMESPACE
          value: kube-system
        - name: KEEPALIVED_CONFIG_MAP
          value: vip-configmap
        - name: KEEPALIVED_SERVICE_CIDR
          value: 10.10.0.0/26 # pick a CIDR that is explicitly reservedfor keepalived
        volumeMounts:
        - name: certs
          mountPath: /etc/ssl/certs
        resources:
          requests:
            cpu: 200m
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10252
            host: 127.0.0.1
          initialDelaySeconds: 15
          timeoutSeconds: 15
          failureThreshold: 8
      volumes:
      - name: certs
        hostPath:
          path: /etc/ssl/certs
master:~$ kubectl apply -f cloudprovider.yaml
daemonset.extensions/kube-keepalived-vip created
configmap/vip-configmap created
deployment.apps/keepalived-cloud-provider created
master:~$ kubectl get pods -n kube-system
NAME                                        READY     STATUS    RESTARTS   AGE
coredns-78fcdf6894-d4cwq                    1/1       Running   0   11m
coredns-78fcdf6894-j2wc5                    1/1       Running   0   11m
etcd-master                                 1/1       Running   0   10m
keepalived-cloud-provider-fb7465d76-jxckr   1/1       Running   0   9s
kube-apiserver-master                       1/1       Running   0   10m
kube-controller-manager-master              1/1       Running   0   9m
kube-keepalived-vip-2rdt2                   1/1       Running   0   9s
kube-proxy-rpmm5                            1/1       Running   0   11m
kube-scheduler-master                       1/1       Running   0   10m
weave-net-6ppzr                             2/2       Running   1   11m
master:~$ cat loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp1-loadbalancer-svc
  labels:
    app: webapp1-loadbalancer
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: webapp1-loadbalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1-loadbalancer-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: webapp1-loadbalancer
    spec:
      containers:
      - name: webapp1-loadbalancer-pod
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80
---
master:~$ kubectl apply -f loadbalancer.yaml
service/webapp1-loadbalancer-svc created
deployment.extensions/webapp1-loadbalancer-deployment created
master:~$ kubectl get svc
NAME                               TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                         ClusterIP      10.96.0.1        <none>        443/TCP        12m
webapp1-clusterip-svc              ClusterIP      10.98.44.183     <none>        80/TCP         11m
webapp1-clusterip-targetport-svc   ClusterIP      10.105.237.185   <none>        8080/TCP       8m
webapp1-externalip-svc             ClusterIP      10.109.41.233    172.17.0.19   80/TCP         2m
webapp1-loadbalancer-svc           LoadBalancer   10.110.83.174    10.10.0.1     80:31235/TCP   8s
webapp1-nodeport-svc               NodePort       10.97.69.120     <none>        80:30080/TCP   5m
master:~$ kubectl describe svc/webapp1-loadbalancer-svc
Name:                     webapp1-loadbalancer-svc
Namespace:                default
Labels:                   app=webapp1-loadbalancer
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"webapp1-loadbalancer"},"name":"webapp1-loadbalancer-svc","namespace":...
Selector:                 app=webapp1-loadbalancer
Type:                     LoadBalancer
IP:                       10.110.83.174
LoadBalancer Ingress:     10.10.0.1
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31235/TCP
Endpoints:                10.32.0.13:80,10.32.0.14:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  CreatingLoadBalancer  19s   service-controller  Creating loadbalancer
  Normal  CreatedLoadBalancer   19s   service-controller  Created load balancer
master:~$ export LoadBalancerIP=$(kubectl get services/webapp1-loadbalancer-svc -o go-template='{{(index .status.loadBalancer.ingress 0).ip}}')
master:~$ echo LoadBalancerIP=$LoadBalancerIP
LoadBalancerIP=10.10.0.1
master:~$ curl $LoadBalancerIP
<h1>This request was processed by host: webapp1-loadbalancer-deployment-69b9f76fd6-zwgvz</h1>
master:~$ curl $LoadBalancerIP
<h1>This request was processed by host: webapp1-loadbalancer-deployment-69b9f76fd6-wr847</h1>
master:~$ curl $LoadBalancerIP
<h1>This request was processed by host: webapp1-loadbalancer-deployment-69b9f76fd6-zwgvz</h1>
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}