Kubernetes practice
December 15, 2023
Service examples
Примеры использования service.
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: cam-nginx namespace: default labels: app: nginx environment: prod annotations: author: cameda spec: containers: - name: nginx image: nginx:latest imagePullPolicy: IfNotPresent ports: - containerPort: 80 - containerPort: 443 resources: requests: cpu: 300m memory: 300Mi limits: memory: 400Mi restartPolicy: Always hostname: nginx subdomain: web EOF
cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: cameda-nginx namespace: default labels: app: nginx environment: prod annotations: author: cameda spec: replicas: 2 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 1 maxUnavailable: 1 type: RollingUpdate template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 resources: requests: cpu: 300m memory: 300Mi limits: memory: 400Mi restartPolicy: Always hostname: nginx subdomain: web dnsPolicy: ClusterFirst terminationGracePeriodSeconds: 90 EOF
Service NodePort
#Минимальный вариант. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service1 labels: environment: prod annotations: author: cameda spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 80 EOF
#Указываем targetPort/nodePort. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service2 labels: environment: prod annotations: author: cameda spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 nodePort: 30010 EOF
#Открываем два порта. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service3 labels: environment: prod annotations: author: cameda spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 80 - name: https protocol: TCP port: 443 EOF
#На подах видны адреса источников обращения. Также идёт привязка сессий. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service4 labels: environment: prod annotations: author: cameda spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 externalTrafficPolicy: Local sessionAffinity: ClientIP EOF
Service ClusterIP
#Минимальный вариант. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service5 labels: environment: prod annotations: author: cameda spec: type: ClusterIP selector: app: nginx ports: - name: http protocol: TCP port: 80 EOF
#Указываем targetPort. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service6 labels: environment: prod annotations: author: cameda spec: type: ClusterIP selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 EOF
#Открываем два порта. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service7 labels: environment: prod annotations: author: cameda spec: type: ClusterIP selector: app: nginx ports: - name: http protocol: TCP port: 80 - name: https protocol: TCP port: 443 EOF
Service LoadBalancer
#Простой сервис типа балансер. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service8 labels: environment: prod annotations: author: cameda spec: type: LoadBalancer selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 EOF
#Внутренний балансировщик cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service9 annotations: # Тип балансировщика: внутренний. yandex.cloud/load-balancer-type: internal labels: environment: prod annotations: author: cameda spec: type: LoadBalancer selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 EOF
#Балансировщик с зарезервированным адресом, проброской адреса источника в под. cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service10 annotations: yandex.cloud/subnet-id: <subnet-id> labels: environment: prod annotations: author: cameda spec: type: LoadBalancer selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 externalTrafficPolicy: Local loadBalancerIP: <заранее зарезервированный IP-адрес> EOF
#Балансировщик с зарезервированным адресом, проброской адреса источника в под и привязкой сессий cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: namespace: default name: nginx-service11 annotations: yandex.cloud/subnet-id: <subnet-id> labels: environment: prod annotations: author: cameda spec: type: LoadBalancer selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: https protocol: TCP port: 443 targetPort: 443 externalTrafficPolicy: Local loadBalancerIP: <заранее зарезервированный IP-адрес> sessionAffinity: ClientIP EOF