iT邦幫忙

2022 iThome 鐵人賽

DAY 4
0

pod

pod-demo.yaml

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-pod
spec:
  containers:
  - name: ubuntu
    image: ubuntu:20.04
    args: [bash, -c, 'for ((i = 0; ; i++)); do echo "$i: $(date)"; sleep 100; done']

部屬pod

root@master:/# kubectl apply -f pod-demo.yaml 
pod/ubuntu-pod created

查看pod

root@master:/# kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
ubuntu-pod   0/1     Running   0          4s

進入pod(kubectl exec -ti pod-name -- bash}

root@master:/# kubectl exec -ti ubuntu-pod -- bash
root@ubuntu-pod:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

刪除pod

root@master:/# kubectl delete pod ubuntu-pod
pod "ubuntu-pod" deleted

service

1.ClusterIP:提供cluster內部存取。
hello.yaml

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  namespace: default
  labels:
    app: node
spec:
  containers:
  - name: hello-world
    image: zxcvbn5512345/hello-node:latest

部屬pod

root@master:/# kubectl apply -f hello.yaml 
pod/hello-world created

hello-clusterip.yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-world-clusterip
spec:
  selector:
    app: node
  ports:
  - name: http
    protocol: TCP
    port: 3000
    targetPort: 8080

部屬clusterip

root@master:/# kubectl apply -f hello-clusterip.yaml 
service/hello-world-clusterip created

確認clusterip

root@master:/# kubectl get svc
NAME                    TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
hello-world-clusterip   ClusterIP   10.99.16.39   <none>        3000/TCP   15m
kubernetes              ClusterIP   10.96.0.1     <none>        443/TCP    26h

測試clusterip

root@master:/# curl 10.99.16.39:3000
Hello World!

2.NodePort:供外部存取。
hello-world-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-world-nodeport
spec:
  type: NodePort
  selector:
    app: node
  ports:
  - name: http
    protocol: TCP
    port: 8080

部屬nodeport

root@master:/# kubectl apply -f hello-world-nodeport.yaml 
service/hello-world-nodeport created

確認nodeport

root@master:/# kubectl get svc
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-world-clusterip   ClusterIP   10.99.16.39      <none>        3000/TCP         21m
hello-world-nodeport    NodePort    10.104.194.204   <none>        8080:31317/TCP   5s
kubernetes              ClusterIP   10.96.0.1        <none>        443/TCP          26h

測試nodeport(your ip:your nodeport)
https://ithelp.ithome.com.tw/upload/images/20220830/201519035M1OmgRURp.png


上一篇
Day3-安裝k8s
下一篇
Day5-Deployment
系列文
技能亂點的無名打工仔技能記錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言