#20230601(

eeapbh·2023년 6월 1일
0

MZC

목록 보기
52/61
kubectl config get-contexts
kubectl config set-context kubernetes-admin@kubernetes --namespace deafult
kubectl config get-contexts

kubectl apply -f nginx-pod.yaml

vi nodeport-pod.yaml

apiVersion: v1
kind: Service
metadata:
  name: nodeport-service-pod
spec:
  type: NodePort
  selector:
    app: nginx-pod
  ports:
  - name: http port
    protocol: TCP
    port: 6789
    targetPort: 80 # 컨테이너 포트가 맞다.
    nodePort: 30080
k get all
k get svc -o wide
k describe pod nginx-pod
k get pod -o wide --show-labels
cat /etc/hosts

# vi loadbalancer-pod.yaml
apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-service-pod
  labels:
    svc: nginx-pod-slb
spec:
  type: LoadBalancer
  externalIPs:
  - 192.168.2.121
  - 192.168.2.122
  - 192.168.2.123
  selector:
    app: nginx-pod
  ports:
  - protocol: TCP
    port: 7890
    targetPort: 80
    
# 도커에서 마치 hostport containerport 7890:80
k apply -f loadbalancer-pod.yaml
k get svc -o wide
k get svc -o wide --show-labels

ReplicaSet

# vi replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset
  labels: 
    app: replicaset  ###레플리카셋 자체의 라벨
spec:
  replicas: 3 # desired state (kube-controller-manager : 이 컴포넌트의 레플리카 컨트롤러가 replica관리)
  selector:
    matchLabels:
      app: nginx-replicaset  ##레플리카셋이 생성하는 pod의 라벨

  template:
    metadata:
      name: nginx-replicaset
      labels:
        app: nginx-replicaset
    spec:
      containers:
      - name: nginx-replicaset-container
        image: nginx
        ports:
        - containerPort: 80 ##안넣어도 됨
          name: http-web-svc ##컨테이너 포트에 이름을 부여(태깅)
      
      
      
 k apply -f replicaset.yaml
 k get rs -o wide
 k get rs -o wide --show-labels
 k describe rs nginx-replicaset

# vi clusterip-replicaset.yaml

apiVersion: v1
kind: Service
metadata:
  name: clusterip-service-replicaset
spec:
  type: ClusterIP
  selector:
    app: nginx-replicaset
  ports:
  - protocol: TCP
    port: 20080
    targetPort: http-web-svc # 위에서 정의한 레플리카셋 컨테이너포트의 이름
    

# vi nodeport-replicaset.yaml # 노드포트 야믈

apiVersion: v1
kind: Service
metadata:
  name: nodeport-service-replicaset
spec:
  type: NodePort
  externalIPs:
  - 192.168.2.121
  - 192.168.2.122
  - 192.168.2.123
  selector:
    app: nginx-replicaset
  ports:
  - protocol: TCP
    port: 10080
    targetPort: 80
    nodePort: 32080
    
vi clusterip-pod.yaml

k apply -f clusterip-pod.yaml

k get svc
kubectl edit svc clusterip-service-replicaset
# 추가
  externalIPs:
  - 192.168.2.121
  - 192.168.2.122
  - 192.168.2.123  

# vi loadbalancer-replicaset.yaml # 로드밸런서 야믈

apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-service-replicaset
spec:
  type: LoadBalancer
  externalIPs:
  - 192.168.2.121
  - 192.168.2.122
  - 192.168.2.123 
  selector:
    app: nginx-replicaset
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
    
# port 가 80이 아니라 바껴서 반드시 targetport설정을 해줘야되고 위에서 설정한 targetport name으로 해줌
k apply -f loadbalancer-replicaset.yaml
k get svc

Deployment

# vi deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-deployment

  template: 
    metadata:
      name: nginx-deployment
      labels:  # 템플릿에 있는 라벨은 4개의 파드에 각각 라벨들이 붙는다.
        app: nginx-deployment
    spec:
      containers:
      - name: nginx-deployment-container
        image: halilinux/web-site:aws
        ports:
        - containerPort: 80
k get apply -f deployment.yaml
k get deploy -o wide
k get deploy -o wide --show-labels
k get po -o wide --show-labels
# vi clusterip-deployment.yaml # 클러스터아이피 야믈
apiVersion: v1
kind: Service
metadata:
  name: clusterip-service-deployment
spec:
  type: ClusterIP
  externalIPs:
  - 192.168.2.121 
  selector:
    app: nginx-deployment
  ports:
  - protocol: TCP
    port: 81 # 앞에 설정하는거랑 충돌피하기 위해 바꿔줌
    targetPort: 80  # port 바꿨으니까 targetport 80으로


# kubectl apply -f clusterip-deployment.yaml
# kubectl get svc -o wide
# kubectl describe svc clusterip-service-deployment

k get pod --show-labels
k edit svc nodeport-service-pod

k get node
k get pod -o wide

k delete pod --grace-period 0 --force nginx-pod

# 현재 이만큼있고 얼마까지 늘릴것이다
kubectl scale --current-replicas=4 --replicas=5 deployment/nginx-deployment
k get pod -o wide

# 그냥 몇개까지 늘릴것이다
k scale --replicas=6 deployment/nginx-deployment
k set image deployment/nginx-deployment nginx-deployment-container=halilinux/web-site:food

k get pod
k get svc -o wide
  • 바뀐거 확인가능
k set image deployment/nginx-deployment nginx-deployment-container=nginx

# kubectl rollout history deployment nginx-deployment
# kubectl rollout history deployment nginx-deployment --revision=2 # 리비전2 상세보기
# kubectl rollout undo deployment nginx-deployment # 롤백(전 단계로 복원)
# kubectl get all
# kubectl rollout history deployment nginx-deployment
# kubectl rollout history deployment nginx-deployment --revision=3 # 리비전3 상세보기
# kubectl rollout undo deployment nginx-deployment --to-revision=2 # 리비전2로 복원

롤링업데이트의 참의미는 복원 되돌리기가 가능하다.

docker, worker1, worker2 2048mb, cpu 1개

docker

docker run -d -p 5000:5000 --restart=always --name private-docker-registry registry

docker, worker1, worker2

# vi /etc/docker/daemon.json

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "insecure-registries":["192.168.1.156:5000"]
}

# systemctl restart docker
    

worker 2

docker tag halilinux/web-site:aws 192.168.1.156:5000/web-site:aws
docker push 192.168.1.156:5000/web-site:aws

docker tag halilinux/web-site:food 192.168.1.156:5000/web-site:food
docker push 192.168.1.156:5000/web-site:food
curl -X GET http://192.168.1.156:5000/v2/_catalog
curl -X GET http://192.168.1.156:5000/v2/nginx/tags/list

master1, worker1, worker2

docker login

master1

ls

cd workspace/
vi multipod.yaml


apiVersion: v1
kind: Pod
metadata:
  name: multipod
spec:
  containers:
  - name: nginx-container        #1번째 컨테이너
    image: nginx:1.14
    ports:
    - containerPort: 80
  - name: centos-container       #2번째 컨테이너
    image: centos:7
    command:
    - sleep
    - "10000"
kubectl apply -f multipod.yaml
kubectl get pod

docker tag centos:7 192.168.1.156:5000/centos:7
docker push 192.168.1.156:5000/centos:7

vi multipod.yaml


apiVersion: v1
kind: Pod
metadata:
  name: multipod
spec:
  containers:
  - name: nginx-container        #1번째 컨테이너
    image: 192.168.1.156:5000/nginx:latest
    ports:
    - containerPort: 80
  - name: centos-container       #2번째 컨테이너
    image: 192.168.1.156:5000/centos:7
    command:
    - sleep
    - "10000"

kubectl apply -f multipod.yaml
kubectl get pod

kubectl exec -it multipod -- bash # pod에들어가서 그 pod에 있는 컨테이너에 들어감 위에서 만든 2개의 컨테이너 중에 어디일까?
cat /etc/os-release # 현재 nginx-container 들어간거 확인가능

exit
kubectl exec -it multipod -c centos-container --bash
cat /etc/redhat-release
docker pull mysql:5.7
docker tag mysql:5.7 192.168.1.156:5000/mysql:5.7
docker push 192.168.1.156:5000/mysql:5.7

docker pull wordpress:latest
docker tag wordpress:latest 192.168.1.156:5000/wordpress:latest
docker push 192.168.1.156:5000/wordpress:latest

kubectl exec -it multipod -c centos-container -- bash
ping multipod # multipod 라는 pod의 이름이 일종의 도메인이다.
curl multipod 

exit
kubectl get po

vi wp-multi-container.yaml

apiVersion: v1
kind: Pod
metadata:
  name: wordpress-pod
  labels:
    app: wordpress-pod
spec:
  containers:
- name: mysql-container
    image: 192.168.1.156:5000/mysql:5.7
    env:
    - name: MYSQL_ROOT_HOST
      value: '%' # wpuser@%
    - name: MYSQL_ROOT_PASSWORD
      value: mode1752
    - name: MYSQL_DATABASE
      value: wordpress
    - name: MYSQL_USER
      value: wpuser
    - name: MYSQL_PASSWORD
      value: wppass
    ports:
    - containerPort: 3306
  - name: wordpress-container
    image: 192.168.1.156:5000/wordpress:latest
    env:
    - name: WORDPRESS_DB_HOST
      value: wordpress-pod:3306
    - name: WORDPRESS_DB_USER
      value: wpuser
    - name: WORDPRESS_DB_PASSWORD
      value: wppass 
    - name: WORDPRESS_DB_NAME
      value: wordpress
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-service-deployment-wordpress
spec:
  type: LoadBalancer
  externalIPs:
  - 192.168.2.121
  selector:
    app: wordpress-pod
  ports:
  - name: web-port
    protocol: TCP
    port: 80
    targetPort: 80
  - name: db-port
    protocol: TCP
    port: 3306
    targetPort: 3306


  • clusterip-pod 에 들어가서 external ip날리자
kubectl edit svc clusterip-pod

spec에 external ip 3개 지우기

kubectl get svc
  • 이렇게 뜨면 됨
kubectl apply -f wp-multi-container.yaml
->  pod 하나 만들어지고 서비스도 만들어진다.

kubectl get svc
  • 이 ip로 접속 ㄱㄱ

  • 한국어 -> 계속

설정하고 로그인

  • 댓글 달기
kubectl apply -f wp-multi-container.yaml


  • 댓글달고 새로고침하면 잘보이는 거 확인 가능
kubectl get pod --show-labels

kubectl edit svc clusterip-pod 

그림위에 externalIPS ?..?

kubectl get svc -o wide

  • 새롭게 clusterip 파서 port 집어넣어서 다른 ip로 들어가도록 한것이다.

0개의 댓글