実行手順
1. テスト用ページの作成minikubeのホストにテスト用ページを作成します。
minikube ssh
sudo mkdir -p /var/mytomcat/logs
sudo mkdir -p /var/mytomcat/webapps
cd /var/mytomcat/webapps
sudo curl --output sample.war https://tomcat.apache.org/tomcat-9.0-doc/appdev/sample/sample.war
exit
2.local永続ボリュームの作成
以下のコマンドを実行してlocal永続ボリュームを作成します
※logsフォルダ用
cat << EOF > local-tomcat-logs-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-tomcat-logs-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
storageClassName: local-storage
local:
path: /var/mytomcat/logs
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
EOF
kubectl apply -f ./local-tomcat-logs-pv.yml
※webappsフォルダ用
cat << EOF > local-tomcat-webapps-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-tomcat-webapps-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
storageClassName: local-storage
local:
path: /var/mytomcat/webapps
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
EOF
kubectl apply -f ./local-tomcat-webapps-pv.yml
3. 永続ボリューム要求の作成
以下のコマンドを実行してlocal永続ボリューム要求を作成します
※logs用
cat << EOF > local-tomcat-logs-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-tomcat-logs-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 5Gi
volumeName: local-tomcat-logs-pv
EOF
kubectl apply -f ./local-tomcat-logs-pvc.yml
※webapps用
cat << EOF > local-tomcat-webapps-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-tomcat-webapps-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 5Gi
volumeName: local-tomcat-webapps-pv
EOF
kubectl apply -f ./local-tomcat-webapps-pvc.yml
4. Deploymentの作成
以下のコマンドでtomcat:9.0-jre17-temurin-focalのイメージと作成したlocal永続ボリュームを使用するDeploymentを作成します。
cat << EOF > tomcat-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
labels:
app: mytomcat
spec:
replicas: 1
selector:
matchLabels:
app: mytomcat
template:
metadata:
labels:
app: mytomcat
spec:
containers:
- name: mytomcat
image: tomcat:9.0-jre17-temurin-focal
ports:
- containerPort: 8080
volumeMounts:
- name: logs
mountPath: /usr/local/tomcat/logs
- name: webapps
mountPath: /usr/local/tomcat/webapps
volumes:
- name: logs
persistentVolumeClaim:
claimName: local-tomcat-logs-pvc
- name: webapps
persistentVolumeClaim:
claimName: local-tomcat-webapps-pvc
EOF
kubectl apply -f ./tomcat-deployment.yml
5. サービスの作成
以下のコマンドでサービスを作成します。
cat << EOF > tomcat-service.yml
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
selector:
app: mytomcat
ports:
- protocol: TCP
port: 8080
targetPort: 8080
EOF
kubectl apply -f ./tomcat-service.yml
6. Port-Fowardingで外部からアクセスできるようにする
kubectl port-forward --address 0.0.0.0 service/tomcat-service 8080:8080
ブラウザからhttp://<ホスト名:8080/sample/ にアクセスします
0 件のコメント:
コメントを投稿